#54 Grouped autocomplete
* refactors default autocomplete into grouped autocompletes, if parents exist for grouping * updates requirement for django-autocomplete-light due to an issue with an attribute in pip's default version 3.8.2. More info here: https://github.com/yourlabs/django-autocomplete-light/issues/1278
This commit is contained in:
@@ -5,7 +5,7 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 07.12.20
|
||||
|
||||
"""
|
||||
from dal_select2.views import Select2QuerySetView
|
||||
from dal_select2.views import Select2QuerySetView, Select2GroupQuerySetView
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models import Q
|
||||
|
||||
@@ -85,7 +85,7 @@ class ShareUserAutocomplete(Select2QuerySetView):
|
||||
return qs
|
||||
|
||||
|
||||
class KonovaCodeAutocomplete(Select2QuerySetView):
|
||||
class KonovaCodeAutocomplete(Select2GroupQuerySetView):
|
||||
"""
|
||||
Provides simple autocomplete functionality for codes
|
||||
|
||||
@@ -94,6 +94,22 @@ class KonovaCodeAutocomplete(Select2QuerySetView):
|
||||
* c: Search inside a special codelist
|
||||
|
||||
"""
|
||||
|
||||
def order_by(self, qs):
|
||||
""" Orders by a predefined value
|
||||
|
||||
Wrapped in a function to provide inheritance-based different orders
|
||||
|
||||
Args:
|
||||
qs (QuerySet): The queryset to be ordered
|
||||
|
||||
Returns:
|
||||
qs (QuerySet): The ordered queryset
|
||||
"""
|
||||
return qs.order_by(
|
||||
"long_name"
|
||||
)
|
||||
|
||||
def get_queryset(self):
|
||||
if self.request.user.is_anonymous:
|
||||
return KonovaCode.objects.none()
|
||||
@@ -101,9 +117,8 @@ class KonovaCodeAutocomplete(Select2QuerySetView):
|
||||
is_archived=False,
|
||||
is_selectable=True,
|
||||
is_leaf=True,
|
||||
).order_by(
|
||||
"long_name"
|
||||
)
|
||||
qs = self.order_by(qs)
|
||||
if self.c:
|
||||
qs = qs.filter(
|
||||
code_lists__in=[self.c]
|
||||
@@ -123,38 +138,82 @@ class KonovaCodeAutocomplete(Select2QuerySetView):
|
||||
qs = qs.filter(_filter).distinct()
|
||||
return qs
|
||||
|
||||
def get_result_label(self, result):
|
||||
return f"{result.long_name}"
|
||||
|
||||
def get_selected_result_label(self, result):
|
||||
return f"{result.__str__()}"
|
||||
|
||||
|
||||
class CompensationActionCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
"""
|
||||
Due to limitations of the django dal package, we need to subclass for each code list
|
||||
"""
|
||||
group_by_related = "parent"
|
||||
related_field_name = "long_name"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.c = CODELIST_COMPENSATION_ACTION_ID
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def order_by(self, qs):
|
||||
return qs.order_by(
|
||||
"parent__long_name"
|
||||
)
|
||||
|
||||
|
||||
class BiotopeCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
"""
|
||||
Due to limitations of the django dal package, we need to subclass for each code list
|
||||
"""
|
||||
group_by_related = "parent"
|
||||
related_field_name = "long_name"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.c = CODELIST_BIOTOPES_ID
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def order_by(self, qs):
|
||||
""" Orders by a predefined value
|
||||
|
||||
Wrapped in a function to provide inheritance-based different orders
|
||||
|
||||
Args:
|
||||
qs (QuerySet): The queryset to be ordered
|
||||
|
||||
Returns:
|
||||
qs (QuerySet): The ordered queryset
|
||||
"""
|
||||
return qs.order_by(
|
||||
"short_name",
|
||||
)
|
||||
|
||||
def get_result_label(self, result):
|
||||
return f"{result.long_name} ({result.short_name})"
|
||||
|
||||
|
||||
class LawCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
"""
|
||||
Due to limitations of the django dal package, we need to subclass for each code list
|
||||
"""
|
||||
group_by_related = "parent"
|
||||
related_field_name = "long_name"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.c = CODELIST_LAW_ID
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def get_result_label(self, result):
|
||||
return f"{result.long_name} ({result.short_name})"
|
||||
|
||||
|
||||
class ProcessTypeCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
"""
|
||||
Due to limitations of the django dal package, we need to subclass for each code list
|
||||
"""
|
||||
group_by_related = "parent"
|
||||
related_field_name = "long_name"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.c = CODELIST_PROCESS_TYPE_ID
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -164,6 +223,9 @@ class RegistrationOfficeCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
"""
|
||||
Due to limitations of the django dal package, we need to subclass for each code list
|
||||
"""
|
||||
group_by_related = "parent"
|
||||
related_field_name = "long_name"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.c = CODELIST_REGISTRATION_OFFICE_ID
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -173,6 +235,12 @@ class ConservationOfficeCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
"""
|
||||
Due to limitations of the django dal package, we need to subclass for each code list
|
||||
"""
|
||||
group_by_related = "parent"
|
||||
related_field_name = "long_name"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.c = CODELIST_CONSERVATION_OFFICE_ID
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def get_result_label(self, result):
|
||||
return f"{result.long_name} ({result.short_name})"
|
||||
Reference in New Issue
Block a user