From de925c8cc6dfb8c9e1f32c2fc43df72edd321781 Mon Sep 17 00:00:00 2001 From: mipel Date: Tue, 24 Aug 2021 09:31:12 +0200 Subject: [PATCH] Compensation action code list * integrates action code list to NewActionForm --- codelist/models.py | 2 +- compensation/forms.py | 14 +++++++++++++- konova/autocompletes.py | 28 +++++++++++++++++++++------- konova/urls.py | 4 ++-- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/codelist/models.py b/codelist/models.py index 7411ab32..a4fc2906 100644 --- a/codelist/models.py +++ b/codelist/models.py @@ -41,7 +41,7 @@ class KonovaCode(models.Model): def __str__(self): if self.is_leaf and self.parent: - return "{} | {}".format(self.parent.long_name, self.long_name) + return "{} > {}".format(self.parent.long_name, self.long_name) return self.long_name diff --git a/compensation/forms.py b/compensation/forms.py index ef972d8a..79a933dd 100644 --- a/compensation/forms.py +++ b/compensation/forms.py @@ -6,6 +6,7 @@ Created on: 04.12.20 """ from bootstrap_modal_forms.utils import is_ajax +from dal import autocomplete from django import forms from django.contrib import messages from django.db import transaction @@ -13,6 +14,7 @@ from django.http import HttpRequest, HttpResponseRedirect from django.shortcuts import render from django.utils.translation import gettext_lazy as _ +from codelist.models import KonovaCode from compensation.models import Payment, CompensationState, CompensationAction, UnitChoices from konova.contexts import BaseContext from konova.forms import BaseForm, BaseModalForm @@ -256,11 +258,21 @@ class NewDeadlineModalForm(BaseModalForm): class NewActionModalForm(BaseModalForm): - action_type = forms.CharField( + action_type = forms.ModelChoiceField( label=_("Action Type"), label_suffix="", required=True, help_text=_("Select the action type"), + queryset=KonovaCode.objects.filter( + is_active=True, + ), + widget=autocomplete.ModelSelect2( + url="codes-compensation-action-autocomplete", + attrs={ + "data-placeholder": _("Action"), + "data-minimum-input-length": 3, + } + ), ) unit = forms.ChoiceField( label=_("Unit"), diff --git a/konova/autocompletes.py b/konova/autocompletes.py index c10284ec..dd7082cf 100644 --- a/konova/autocompletes.py +++ b/konova/autocompletes.py @@ -8,6 +8,7 @@ Created on: 07.12.20 from dal_select2.views import Select2QuerySetView from codelist.models import KonovaCode +from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID from compensation.models import EcoAccount from intervention.models import Intervention from organisation.models import Organisation @@ -86,17 +87,12 @@ class KonovaCodeAutocomplete(Select2QuerySetView): * c: Search inside a special codelist """ - def dispatch(self, request, *args, **kwargs): - # Retrieve 'c' for 'code' from request - self.c = request.GET.get("c", "") - return super().dispatch(request, *args, **kwargs) - def get_queryset(self): if self.request.user.is_anonymous: return KonovaCode.objects.none() qs = KonovaCode.objects.filter( is_active=True, - is_leaf=True, + parent__isnull=False, ) if self.c: qs = qs.filter( @@ -109,4 +105,22 @@ class KonovaCodeAutocomplete(Select2QuerySetView): qs.order_by( "long_name" ) - return qs \ No newline at end of file + 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) diff --git a/konova/urls.py b/konova/urls.py index ac94fd7a..18bde91b 100644 --- a/konova/urls.py +++ b/konova/urls.py @@ -18,7 +18,7 @@ from django.contrib import admin from django.urls import path, include from konova.autocompletes import OrganisationAutocomplete, NonOfficialOrganisationAutocomplete, EcoAccountAutocomplete, \ - InterventionAutocomplete, KonovaCodeAutocomplete + InterventionAutocomplete, CompensationActionCodeAutocomplete from konova.settings import SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY, DEBUG from konova.sso.sso import KonovaSSOClient from konova.views import logout_view, home_view, get_document_view, remove_document_view, remove_deadline_view @@ -49,7 +49,7 @@ urlpatterns = [ path("atcmplt/orgs/other", NonOfficialOrganisationAutocomplete.as_view(), name="other-orgs-autocomplete"), path("atcmplt/eco-accounts", EcoAccountAutocomplete.as_view(), name="accounts-autocomplete"), path("atcmplt/interventions", InterventionAutocomplete.as_view(), name="interventions-autocomplete"), - path("atcmplt/codes", KonovaCodeAutocomplete.as_view(), name="codes-autocomplete"), + path("atcmplt/codes/compensation-action", CompensationActionCodeAutocomplete.as_view(), name="codes-compensation-action-autocomplete"), ] if DEBUG: