Merge pull request '#54 Grouped autocomplete' (#59) from 54_Improve_autocomplete_selection into master

Reviewed-on: SGD-Nord/konova#59
This commit is contained in:
Michel Peltriaux 2022-01-07 13:54:44 +01:00
commit 9fe054c681
3 changed files with 75 additions and 7 deletions

View File

@ -48,9 +48,9 @@ class KonovaCode(models.Model):
help_text="Whether this code is archived or not" help_text="Whether this code is archived or not"
) )
def __str__(self): def __str__(self, with_parent: bool = True):
ret_val = "" ret_val = ""
if self.parent: if self.parent and with_parent:
ret_val += self.parent.long_name + " > " ret_val += self.parent.long_name + " > "
ret_val += self.long_name ret_val += self.long_name
if self.short_name and self.short_name != self.long_name: if self.short_name and self.short_name != self.long_name:

View File

@ -5,7 +5,7 @@ Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 07.12.20 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.contrib.auth.models import User
from django.db.models import Q from django.db.models import Q
@ -85,7 +85,7 @@ class ShareUserAutocomplete(Select2QuerySetView):
return qs return qs
class KonovaCodeAutocomplete(Select2QuerySetView): class KonovaCodeAutocomplete(Select2GroupQuerySetView):
""" """
Provides simple autocomplete functionality for codes Provides simple autocomplete functionality for codes
@ -94,6 +94,22 @@ class KonovaCodeAutocomplete(Select2QuerySetView):
* c: Search inside a special codelist * 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): def get_queryset(self):
if self.request.user.is_anonymous: if self.request.user.is_anonymous:
return KonovaCode.objects.none() return KonovaCode.objects.none()
@ -101,9 +117,8 @@ class KonovaCodeAutocomplete(Select2QuerySetView):
is_archived=False, is_archived=False,
is_selectable=True, is_selectable=True,
is_leaf=True, is_leaf=True,
).order_by(
"long_name"
) )
qs = self.order_by(qs)
if self.c: if self.c:
qs = qs.filter( qs = qs.filter(
code_lists__in=[self.c] code_lists__in=[self.c]
@ -123,38 +138,82 @@ class KonovaCodeAutocomplete(Select2QuerySetView):
qs = qs.filter(_filter).distinct() qs = qs.filter(_filter).distinct()
return qs 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): class CompensationActionCodeAutocomplete(KonovaCodeAutocomplete):
""" """
Due to limitations of the django dal package, we need to subclass for each code list 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): def __init__(self, *args, **kwargs):
self.c = CODELIST_COMPENSATION_ACTION_ID self.c = CODELIST_COMPENSATION_ACTION_ID
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def order_by(self, qs):
return qs.order_by(
"parent__long_name"
)
class BiotopeCodeAutocomplete(KonovaCodeAutocomplete): class BiotopeCodeAutocomplete(KonovaCodeAutocomplete):
""" """
Due to limitations of the django dal package, we need to subclass for each code list 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): def __init__(self, *args, **kwargs):
self.c = CODELIST_BIOTOPES_ID self.c = CODELIST_BIOTOPES_ID
super().__init__(*args, **kwargs) 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): class LawCodeAutocomplete(KonovaCodeAutocomplete):
""" """
Due to limitations of the django dal package, we need to subclass for each code list 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): def __init__(self, *args, **kwargs):
self.c = CODELIST_LAW_ID self.c = CODELIST_LAW_ID
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def get_result_label(self, result):
return f"{result.long_name} ({result.short_name})"
class ProcessTypeCodeAutocomplete(KonovaCodeAutocomplete): class ProcessTypeCodeAutocomplete(KonovaCodeAutocomplete):
""" """
Due to limitations of the django dal package, we need to subclass for each code list 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): def __init__(self, *args, **kwargs):
self.c = CODELIST_PROCESS_TYPE_ID self.c = CODELIST_PROCESS_TYPE_ID
super().__init__(*args, **kwargs) 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 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): def __init__(self, *args, **kwargs):
self.c = CODELIST_REGISTRATION_OFFICE_ID self.c = CODELIST_REGISTRATION_OFFICE_ID
super().__init__(*args, **kwargs) 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 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): def __init__(self, *args, **kwargs):
self.c = CODELIST_CONSERVATION_OFFICE_ID self.c = CODELIST_CONSERVATION_OFFICE_ID
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def get_result_label(self, result):
return f"{result.long_name} ({result.short_name})"

View File

@ -12,7 +12,7 @@ click-plugins==1.1.1
click-repl==0.2.0 click-repl==0.2.0
Deprecated==1.2.13 Deprecated==1.2.13
Django==3.1.3 Django==3.1.3
django-autocomplete-light==3.8.2 django-autocomplete-light==3.9.0rc5
django-bootstrap-modal-forms==2.2.0 django-bootstrap-modal-forms==2.2.0
django-bootstrap4==3.0.1 django-bootstrap4==3.0.1
django-debug-toolbar==3.1.1 django-debug-toolbar==3.1.1