Skip to content

Commit

Permalink
feat: Ability to filter items based on current user
Browse files Browse the repository at this point in the history
Added request to get_items method.
  • Loading branch information
lucbelliveau committed Jun 27, 2023
1 parent af2c5ed commit be8bcc7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
20 changes: 15 additions & 5 deletions autocomplete/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def get_component_id(cls, override_id=None):
return override_id
return cls.component_id if cls.component_id else cls.get_route_name()

def get_items(self, search=None, values=None):
def get_items(self, search=None, values=None, request=None):
"""Get available items based on search or values.
If search is specified, only items who's label contain the search
Expand All @@ -395,9 +395,15 @@ class is not specified at all, the overridden method is expected to
return an array of dictionaries where each item has the `label` and
value` keys defined.
IMPORTANT: When used as a widget in a form, django will call the get_items
method in order to validate the selected items are truly part of the list - in
this context the request object is not available so it is important to be aware
of this when customizing the get_items method.
Parameters:
search (str): The search term
values (str[]): Array of values
request (HttpRequest): Django request object
Returns:
array of dictionaries
Expand Down Expand Up @@ -490,7 +496,8 @@ def put(self, request, method):
return HttpResponseBadRequest()

items = self.map_items(
self.get_items(values=items_selected + [item]), items_selected
self.get_items(values=items_selected + [item], request=request),
items_selected,
)

def sort_items(item):
Expand Down Expand Up @@ -518,7 +525,6 @@ def sort_items(item):
if item != target_item:
item["selected"] = False


template = loader.get_template("autocomplete/item.html")
return HttpResponse(
template.render(
Expand Down Expand Up @@ -597,7 +603,9 @@ def get(self, request, method):

if method == "component":
template = loader.get_template("autocomplete/component.html")
selected_options = self.map_items(self.get_items(values=items_selected))
selected_options = self.map_items(
self.get_items(values=items_selected, request=request)
)

return HttpResponse(
template.render(
Expand All @@ -624,7 +632,9 @@ def get(self, request, method):
search = request.GET.get("search", "")
show = len(search) >= self.minimum_search_length
items = (
self.map_items(self.get_items(search), items_selected) if show else []
self.map_items(self.get_items(search=search, request=request), items_selected)
if show
else []
)
total_results = len(items)
if self.max_results is not None and len(items) > self.max_results:
Expand Down
4 changes: 2 additions & 2 deletions autocomplete/test_autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def test_classes_generate_routes(self):
class Test(HTMXAutoComplete): # pylint: disable=unused-variable
"""test case 1"""
name = "test1"
def get_items(self, search=None, values=None):
def get_items(self, search=None, values=None, request=None):
return []
class Test2(HTMXAutoComplete): # pylint: disable=unused-variable
"""test case 2"""
name = "test"
def get_items(self, search=None, values=None):
def get_items(self, search=None, values=None, request=None):
return []

urls = HTMXAutoComplete.url_dispatcher('test')
Expand Down
4 changes: 2 additions & 2 deletions tests/app/ac_test/ac_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class GetItemsAutoComplete(HTMXAutoComplete):
name = "getitems"
minimum_search_length = 0

def get_items(self, search=None, values=None):
def get_items(self, search=None, values=None, request=None):
if values:
return list(filter(lambda x: x.get("value") in values, data))

Expand All @@ -86,7 +86,7 @@ class GetItemsMultiAutoComplete(HTMXAutoComplete):
multiselect = True
minimum_search_length = 0

def get_items(self, search=None, values=None):
def get_items(self, search=None, values=None, request=None):
if values:
return list(filter(lambda x: x.get("value") in values, data))

Expand Down
4 changes: 2 additions & 2 deletions tests/app/ac_test/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SingleFormGetItem(forms.Form):
"""Form used for single select using get_items"""

@staticmethod
def get_items(search=None, values=None):
def get_items(search=None, values=None, request=None):
"""Example function used to provide list of options to widget
Args:
Expand Down Expand Up @@ -59,7 +59,7 @@ class MultipleFormGetItem(forms.Form):
"""Form used for multiple select using get_items"""

@staticmethod
def get_items(search=None, values=None):
def get_items(search=None, values=None, request=None):
"""Example function used to provide list of options to widget
Args:
Expand Down

0 comments on commit be8bcc7

Please sign in to comment.