konova/konova/autocompletes.py
mipel 77b290216b Konova Codelist enhancements
* adds proper boolean mapping to update_codelist
* differs between id and atom_id for KonovaCode since atomIds are not unique (could change in the future)
* adds is_selectable and is_archived to KonovaCode
* scales width of DAL form fields to 100% width
* adds table-responsive wrapping container for table forms to prevent unwanted rendering artifacts in case of table resizing due to long content
* adds autocomplete routes for law, registration offices and conservation offices
2021-08-26 12:45:48 +02:00

157 lines
4.6 KiB
Python

"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 07.12.20
"""
from dal_select2.views import Select2QuerySetView
from django.db.models import Q
from codelist.models import KonovaCode
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID, CODELIST_LAW_ID, \
CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID
from compensation.models import EcoAccount
from intervention.models import Intervention
from organisation.models import Organisation
class OrganisationAutocomplete(Select2QuerySetView):
def get_queryset(self):
if self.request.user.is_anonymous:
return Organisation.objects.none()
qs = Organisation.objects.all()
if self.q:
qs = qs.filter(name__icontains=self.q)
qs = qs.order_by(
"name"
)
return qs
class NonOfficialOrganisationAutocomplete(Select2QuerySetView):
def get_queryset(self):
if self.request.user.is_anonymous:
return Organisation.objects.none()
qs = Organisation.objects.all()
if self.q:
qs = qs.filter(
name__icontains=self.q,
)
qs = qs.order_by(
"name"
)
return qs
class EcoAccountAutocomplete(Select2QuerySetView):
def get_queryset(self):
if self.request.user.is_anonymous:
return EcoAccount.objects.none()
qs = EcoAccount.objects.filter(
deleted=None,
recorded__isnull=False,
)
if self.q:
qs = qs.filter(
identifier__icontains=self.q
)
qs = qs.order_by(
"identifier"
)
return qs
class InterventionAutocomplete(Select2QuerySetView):
def get_queryset(self):
if self.request.user.is_anonymous:
return Intervention.objects.none()
qs = Intervention.objects.filter(
deleted=None,
users__in=[self.request.user],
)
if self.q:
qs = qs.filter(
identifier__icontains=self.q
)
qs = qs.order_by(
"identifier"
)
return qs
class KonovaCodeAutocomplete(Select2QuerySetView):
"""
Provides simple autocomplete functionality for codes
Parameter support:
* q: Search for a word inside long_name of a code
* c: Search inside a special codelist
"""
def get_queryset(self):
if self.request.user.is_anonymous:
return KonovaCode.objects.none()
qs = KonovaCode.objects.filter(
is_archived=False,
is_selectable=True,
is_leaf=True,
).order_by(
"long_name"
)
if self.c:
qs = qs.filter(
code_lists__in=[self.c]
)
if self.q:
q_or = Q()
q_or |= Q(long_name__icontains=self.q)
q_or |= Q(short_name__icontains=self.q)
qs = qs.filter(q_or)
return qs
class CompensationActionCodeAutocomplete(KonovaCodeAutocomplete):
"""
Due to limitations of the django dal package, we need to subclass for each code list
"""
def __init__(self, *args, **kwargs):
self.c = CODELIST_COMPENSATION_ACTION_ID
super().__init__(*args, **kwargs)
class BiotopeCodeAutocomplete(KonovaCodeAutocomplete):
"""
Due to limitations of the django dal package, we need to subclass for each code list
"""
def __init__(self, *args, **kwargs):
self.c = CODELIST_BIOTOPES_ID
super().__init__(*args, **kwargs)
class LawCodeAutocomplete(KonovaCodeAutocomplete):
"""
Due to limitations of the django dal package, we need to subclass for each code list
"""
def __init__(self, *args, **kwargs):
self.c = CODELIST_LAW_ID
super().__init__(*args, **kwargs)
class RegistrationOfficeCodeAutocomplete(KonovaCodeAutocomplete):
"""
Due to limitations of the django dal package, we need to subclass for each code list
"""
def __init__(self, *args, **kwargs):
self.c = CODELIST_REGISTRATION_OFFICE_ID
super().__init__(*args, **kwargs)
class ConservationOfficeCodeAutocomplete(KonovaCodeAutocomplete):
"""
Due to limitations of the django dal package, we need to subclass for each code list
"""
def __init__(self, *args, **kwargs):
self.c = CODELIST_CONSERVATION_OFFICE_ID
super().__init__(*args, **kwargs)