#112 Autocomplete enhancements
* enhances filtering for Autocomplete -> parent_parent will be searched for match as well * adds rendering of parent_parent group for BiotopeAutocomplete * enhances ordering of registration office autocomplete
This commit is contained in:
@@ -5,7 +5,12 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 07.12.20
|
||||
|
||||
"""
|
||||
import collections
|
||||
|
||||
from dal_select2.views import Select2QuerySetView, Select2GroupQuerySetView
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
from konova.utils.message_templates import UNGROUPED
|
||||
from user.models import User
|
||||
from django.db.models import Q
|
||||
|
||||
@@ -139,6 +144,8 @@ class KonovaCodeAutocomplete(Select2GroupQuerySetView):
|
||||
q_or |= Q(short_name__icontains=keyword)
|
||||
q_or |= Q(parent__long_name__icontains=keyword)
|
||||
q_or |= Q(parent__short_name__icontains=keyword)
|
||||
q_or |= Q(parent__parent__long_name__icontains=keyword)
|
||||
q_or |= Q(parent__parent__short_name__icontains=keyword)
|
||||
_filter.add(q_or, Q.AND)
|
||||
qs = qs.filter(_filter).distinct()
|
||||
return qs
|
||||
@@ -214,6 +221,41 @@ class BiotopeCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
def get_result_label(self, result):
|
||||
return f"{result.long_name} ({result.short_name})"
|
||||
|
||||
def get_results(self, context):
|
||||
"""Return the options grouped by a common related model.
|
||||
|
||||
Raises ImproperlyConfigured if self.group_by_name is not configured
|
||||
"""
|
||||
if not self.group_by_related:
|
||||
raise ImproperlyConfigured("Missing group_by_related.")
|
||||
|
||||
super_groups = collections.OrderedDict()
|
||||
|
||||
object_list = context['object_list']
|
||||
|
||||
for result in object_list:
|
||||
group = result.parent if result.parent else None
|
||||
group_name = f"{group.long_name} ({group.short_name})" if group else UNGROUPED
|
||||
super_group = result.parent.parent if result.parent else None
|
||||
super_group_name = f"{super_group.long_name} ({super_group.short_name})" if super_group else UNGROUPED
|
||||
super_groups.setdefault(super_group_name, {})
|
||||
super_groups[super_group_name].setdefault(group_name, [])
|
||||
super_groups[super_group_name][group_name].append(result)
|
||||
|
||||
return [{
|
||||
'id': None,
|
||||
'text': super_group,
|
||||
'children': [{
|
||||
"id": None,
|
||||
"text": group,
|
||||
"children": [{
|
||||
'id': self.get_result_value(result),
|
||||
'text': self.get_result_label(result),
|
||||
'selected_text': self.get_selected_result_label(result),
|
||||
} for result in results]
|
||||
} for group, results in groups.items()]
|
||||
} for super_group, groups in super_groups.items()]
|
||||
|
||||
|
||||
class BiotopeExtraCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
"""
|
||||
@@ -284,6 +326,11 @@ class RegistrationOfficeCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
self.c = CODELIST_REGISTRATION_OFFICE_ID
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def order_by(self, qs):
|
||||
return qs.order_by(
|
||||
"parent__long_name"
|
||||
)
|
||||
|
||||
|
||||
class ConservationOfficeCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
"""
|
||||
@@ -297,4 +344,4 @@ class ConservationOfficeCodeAutocomplete(KonovaCodeAutocomplete):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def get_result_label(self, result):
|
||||
return f"{result.long_name} ({result.short_name})"
|
||||
return f"{result.long_name} ({result.short_name})"
|
||||
|
||||
@@ -242,15 +242,24 @@ Similar to bootstraps 'shadow-lg'
|
||||
.select2-results__option--highlighted{
|
||||
background-color: var(--rlp-red) !important;
|
||||
}
|
||||
/*
|
||||
.select2-container--default .select2-results__group{
|
||||
background-color: var(--rlp-gray-light);
|
||||
}
|
||||
.select2-container--default .select2-results__option .select2-results__option{
|
||||
padding-left: 2em !important;
|
||||
padding-left: 1em !important;
|
||||
}
|
||||
|
||||
*/
|
||||
.select2-results__options--nested{
|
||||
padding-left: 1em !important;
|
||||
}
|
||||
.select2-container--default .select2-results > .select2-results__options{
|
||||
max-height: 500px !important;
|
||||
}
|
||||
/*
|
||||
.select2-container--default .select2-results__option .select2-results__option{
|
||||
padding-left: 2em;
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
@@ -7,7 +7,7 @@ Created on: 02.08.21
|
||||
"""
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
UNGROUPED = _("Ungrouped")
|
||||
FORM_INVALID = _("There was an error on this form.")
|
||||
PARAMS_INVALID = _("Invalid parameters")
|
||||
INTERVENTION_INVALID = _("There are errors in this intervention.")
|
||||
|
||||
Reference in New Issue
Block a user