""" 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, Select2GroupQuerySetView from user.models import User 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, \ CODELIST_BIOTOPES_EXTRA_CODES_ID from compensation.models import EcoAccount from intervention.models import Intervention class EcoAccountAutocomplete(Select2QuerySetView): """ Autocomplete for ecoAccount entries Only returns entries that are accessible for the requesting user and already are recorded """ def get_queryset(self): if self.request.user.is_anonymous: return EcoAccount.objects.none() qs = EcoAccount.objects.filter( deleted=None, recorded__isnull=False, users__in=[self.request.user], ).order_by( "identifier" ) if self.q: qs = qs.filter( identifier__icontains=self.q ) return qs class InterventionAutocomplete(Select2QuerySetView): """ Autocomplete for intervention entries Only returns entries that are accessible for the requesting user """ 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], ).order_by( "identifier" ) if self.q: qs = qs.filter( identifier__icontains=self.q ) return qs class ShareUserAutocomplete(Select2QuerySetView): """ Autocomplete for intervention entries Only returns entries that are accessible for the requesting user """ def get_queryset(self): if self.request.user.is_anonymous: return User.objects.none() exclude_user_ids = self.forwarded.get("users", []) _exclude = {"id__in": exclude_user_ids} qs = User.objects.all().exclude( **_exclude ).order_by( "username" ) if self.q: # Due to privacy concerns only a full username match will return the proper user entry qs = qs.filter( username=self.q ) return qs class KonovaCodeAutocomplete(Select2GroupQuerySetView): """ 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 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() qs = KonovaCode.objects.filter( is_archived=False, is_selectable=True, is_leaf=True, ) qs = self.order_by(qs) if self.c: qs = qs.filter( code_lists__in=[self.c] ) if self.q: # Remove whitespaces from self.q and split input in all keywords (if multiple given) q = dict.fromkeys(self.q.strip().split(" ")) # Create one filter looking up for all keys where all keywords can be found in the same result _filter = Q() for keyword in q: q_or = Q() q_or |= Q(long_name__icontains=keyword) q_or |= Q(short_name__icontains=keyword) q_or |= Q(parent__long_name__icontains=keyword) q_or |= Q(parent__short_name__icontains=keyword) _filter.add(q_or, Q.AND) 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 BiotopeExtraCodeAutocomplete(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_EXTRA_CODES_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( "parent__long_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) 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) 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})"