konova/konova/autocompletes.py
mipel 49a9d03471 #7 New Form
* extends KonovaCode filtering for parent objects matching given input
2021-09-29 11:44:07 +02:00

168 lines
5.0 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, CODELIST_PROCESS_TYPE_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)
q_or |= Q(parent__long_name__icontains=self.q)
q_or |= Q(parent__short_name__icontains=self.q)
qs = qs.filter(q_or).distinct()
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 ProcessTypeCodeAutocomplete(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_PROCESS_TYPE_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)