-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad231ec
commit 3ba507d
Showing
5 changed files
with
27 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters