Skip to content

Commit

Permalink
fix: upgrade autocomplete (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexCLeduc authored Dec 9, 2024
1 parent ad231ec commit 3ba507d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 70 deletions.
68 changes: 12 additions & 56 deletions server/cpho/forms.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,22 @@
from django.db.models import Q
from autocomplete import ModelAutocomplete, register

from autocomplete import HTMXAutoComplete
from autocomplete import widgets as ac_widgets
from cpho import constants
from cpho.models import Indicator, User

from cpho.models import Indicator, IndicatorDirectory, User


class MultiIndicatorAutocomplete(HTMXAutoComplete):
name = "indicators"
multiselect = True
@register
class IndicatorAutocomplete(ModelAutocomplete):
minimum_search_length = 0
model = Indicator

def get_items(self, search=None, values=None):
data = Indicator.objects.all()

if search is not None:
data = data.filter(
Q(name__icontains=search)
| Q(detailed_indicator__icontains=search)
| Q(sub_indicator_measurement__icontains=search)
)
items = [{"label": str(x), "value": str(x.id)} for x in data]
return items

if values is not None:
items = [
{"label": str(x), "value": str(x.id)}
for x in data
if str(x.id) in values
]
return items

return []
search_attrs = ["name", "detailed_indicator", "sub_indicator_measurement"]


class MultiUserAutocomplete(HTMXAutoComplete):
name = "users"
multiselect = True
@register
class UserAutocomplete(ModelAutocomplete):
minimum_search_length = 0
model = User
search_attrs = ["username", "email", "name"]

def get_items(self, search=None, values=None):
data = User.objects.all()

if search is not None:
filt = (
Q(username__icontains=search)
| Q(email__icontains=search)
| Q(name__icontains=search)
)
data = data.filter(filt)

items = [{"label": str(x), "value": str(x.id)} for x in data]
return items

if values is not None:
items = [
{"label": str(x), "value": str(x.id)}
for x in data
if str(x.id) in values
]
return items

return []
@classmethod
def get_queryset(cls):
return super().get_queryset().exclude(is_staff=True)
21 changes: 10 additions & 11 deletions server/cpho/views/user_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
View,
)

from autocomplete import HTMXAutoComplete
from autocomplete import widgets as ac_widgets
from autocomplete import AutocompleteWidget
from phac_aspc.rules import test_rule

from server.form_util import StandardFormMixin

from cpho.constants import ADMIN_GROUP_NAME, HSO_GROUP_NAME
from cpho.forms import MultiIndicatorAutocomplete, MultiUserAutocomplete
from cpho.forms import IndicatorAutocomplete, UserAutocomplete
from cpho.models import (
DimensionType,
Indicator,
Expand Down Expand Up @@ -213,10 +212,10 @@ class Meta:
indicators = forms.ModelMultipleChoiceField(
queryset=Indicator.objects.all(),
required=False,
widget=ac_widgets.Autocomplete(
use_ac=MultiIndicatorAutocomplete,
attrs={
"id": "indicators__textinput",
widget=AutocompleteWidget(
ac_class=IndicatorAutocomplete,
options={
"multiselect": True,
},
),
label=tdt("indicators"),
Expand All @@ -225,10 +224,10 @@ class Meta:
users = forms.ModelMultipleChoiceField(
queryset=User.objects.all(),
required=False,
widget=ac_widgets.Autocomplete(
use_ac=MultiUserAutocomplete,
attrs={
"id": "users__textinput",
widget=AutocompleteWidget(
ac_class=UserAutocomplete,
options={
"multiselect": True,
},
),
label=tdt("users"),
Expand Down
2 changes: 1 addition & 1 deletion server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ django-debug-toolbar==4.4.2
django-data-fetcher==2.2
django-graphiql-debug-toolbar==0.2.0
django-extensions==3.2.3
django-htmx-autocomplete==0.8.2
django-htmx-autocomplete==1.0.5
django-phac_aspc-helpers==3.1.1
Django==5.0.9
django-versionator==2.0.1
Expand Down
2 changes: 2 additions & 0 deletions server/server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,5 @@
# CORS
# for infobase and potentially anyone else who may want to use public data,
CORS_ALLOW_ALL_ORIGINS = True

AUTOCOMPLETE_BLOCK_UNAUTHENTICATED = True
4 changes: 2 additions & 2 deletions server/server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.urls import include, path, re_path
from django.views.decorators.csrf import csrf_exempt

from autocomplete import HTMXAutoComplete
from autocomplete import urls as autocomplete_urls

from server.middleware import allow_unauthenticated

Expand All @@ -28,7 +28,7 @@
path("phac_admin/", admin.site.urls),
path("login/", LoginView.as_view(), name="login"),
path("logout/", LogoutView.as_view(), name="logout"),
*HTMXAutoComplete.url_dispatcher("ac"),
path("ac/", autocomplete_urls),
path("", include(cpho_urls)),
path(
"graphiql/",
Expand Down

0 comments on commit 3ba507d

Please sign in to comment.