master #116

Merged
mpeltriaux merged 13 commits from master into 110_Biotope_codelists 2022-02-16 08:50:41 +01:00
10 changed files with 126 additions and 8 deletions
Showing only changes of commit 62e452c625 - Show all commits

View File

@ -11,12 +11,14 @@ from django import forms
from django.contrib import messages from django.contrib import messages
from django.http import HttpRequest, HttpResponseRedirect from django.http import HttpRequest, HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse
from django.utils.translation import pgettext_lazy as _con, gettext_lazy as _ from django.utils.translation import pgettext_lazy as _con, gettext_lazy as _
from codelist.models import KonovaCode from codelist.models import KonovaCode
from codelist.settings import CODELIST_BIOTOPES_ID, CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_EXTRA_CODES_ID, \ from codelist.settings import CODELIST_BIOTOPES_ID, CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_EXTRA_CODES_ID, \
CODELIST_COMPENSATION_ACTION_DETAIL_ID CODELIST_COMPENSATION_ACTION_DETAIL_ID
from compensation.models import CompensationDocument, EcoAccountDocument from compensation.models import CompensationDocument, EcoAccountDocument
from intervention.inputs import TreeSelectMultiple
from konova.contexts import BaseContext from konova.contexts import BaseContext
from konova.forms import BaseModalForm, NewDocumentModalForm, RemoveModalForm from konova.forms import BaseModalForm, NewDocumentModalForm, RemoveModalForm
from konova.models import DeadlineType from konova.models import DeadlineType
@ -411,15 +413,11 @@ class NewActionModalForm(BaseModalForm):
required=True, required=True,
help_text=_("Select the action type"), help_text=_("Select the action type"),
queryset=KonovaCode.objects.filter( queryset=KonovaCode.objects.filter(
is_archived=False,
is_leaf=True,
code_lists__in=[CODELIST_COMPENSATION_ACTION_ID], code_lists__in=[CODELIST_COMPENSATION_ACTION_ID],
is_archived=False,
), ),
widget=autocomplete.ModelSelect2Multiple( widget=TreeSelectMultiple(
url="codes-compensation-action-autocomplete", url=None,
attrs={
"data-placeholder": _("Action"),
}
), ),
) )
action_type_details = forms.ModelMultipleChoiceField( action_type_details = forms.ModelMultipleChoiceField(
@ -482,6 +480,14 @@ class NewActionModalForm(BaseModalForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.form_title = _("New action") self.form_title = _("New action")
self.form_caption = _("Insert data for the new action") self.form_caption = _("Insert data for the new action")
url = reverse("codes-action-children")
self.fields["action_type"].widget.attrs = {
"url": url,
}
def is_valid(self):
super_valid = super().is_valid()
return super_valid
def save(self): def save(self):
action = self.instance.add_action(self) action = self.instance.add_action(self)

View File

@ -2,6 +2,11 @@
{% load i18n l10n static fontawesome_5 humanize ksp_filters %} {% load i18n l10n static fontawesome_5 humanize ksp_filters %}
{% block head %} {% block head %}
{% comment %}
Needed for custom Checkbox Tree Select Widget
{% endcomment %}
{% include 'form/scripts/jstree-scripts.html' %}
{% comment %} {% comment %}
dal documentation (django-autocomplete-light) states using form.media for adding needed scripts. dal documentation (django-autocomplete-light) states using form.media for adding needed scripts.
This does not work properly with modal forms, as the scripts are not loaded properly inside the modal. This does not work properly with modal forms, as the scripts are not loaded properly inside the modal.

View File

@ -2,6 +2,11 @@
{% load i18n l10n static fontawesome_5 humanize %} {% load i18n l10n static fontawesome_5 humanize %}
{% block head %} {% block head %}
{% comment %}
Needed for custom Checkbox Tree Select Widget
{% endcomment %}
{% include 'form/scripts/jstree-scripts.html' %}
{% comment %} {% comment %}
dal documentation (django-autocomplete-light) states using form.media for adding needed scripts. dal documentation (django-autocomplete-light) states using form.media for adding needed scripts.
This does not work properly with modal forms, as the scripts are not loaded properly inside the modal. This does not work properly with modal forms, as the scripts are not loaded properly inside the modal.

View File

@ -2,6 +2,11 @@
{% load i18n l10n static fontawesome_5 humanize %} {% load i18n l10n static fontawesome_5 humanize %}
{% block head %} {% block head %}
{% comment %}
Needed for custom Checkbox Tree Select Widget
{% endcomment %}
{% include 'form/scripts/jstree-scripts.html' %}
{% comment %} {% comment %}
dal documentation (django-autocomplete-light) states using form.media for adding needed scripts. dal documentation (django-autocomplete-light) states using form.media for adding needed scripts.
This does not work properly with modal forms, as the scripts are not loaded properly inside the modal. This does not work properly with modal forms, as the scripts are not loaded properly inside the modal.

View File

@ -1,4 +1,5 @@
from django import forms from django import forms
from django.urls import reverse
class DummyFilterInput(forms.HiddenInput): class DummyFilterInput(forms.HiddenInput):
@ -30,3 +31,17 @@ class GenerateInput(forms.TextInput):
""" """
template_name = "konova/widgets/generate-content-input.html" template_name = "konova/widgets/generate-content-input.html"
class TreeSelectMultiple(forms.SelectMultiple):
""" Provides multiple selection of parent-child data
"""
template_name = "konova/widgets/checkbox-tree-select-base.html"
url = None
def __init__(self, *args, **kwargs):
self.url = kwargs.pop("url", None)
if self.url:
self.url = reverse(self.url)
super().__init__(*args, **kwargs)

View File

@ -0,0 +1,45 @@
<div id="jstree">
</div>
<input id="jstree-input" name="{{ widget.name }}[]" hidden="hidden"/>
<script>
$(function () {
$('#jstree').jstree({
'plugins': [
'checkbox',
],
'checkbox': {
'whole_node': false,
},
'core' : {
'data' : {
'url' : '{{ widget.attrs.url }}',
'data' : function (node) {
return { 'id' : node.id };
}
}
}
});
$('#jstree')
.on('deselect_node.jstree', function (e, data) {
$(data.selected).each(function (val){
console.log(val)
console.log(this)
$(this).after(
"<input name='{{widget.name}}[]' value="+val+"/>"
)
});
//$('#jstree-input').val(data.selected);
})
.on('select_node.jstree', function (e, data) {
$(data.selected).each(function (val){
$(this).after(
"<input name='{{widget.name}}[]' value="+val+"/>"
)
});
//$('#jstree-input').val(data.selected);
})
});
</script>

View File

@ -0,0 +1,9 @@
{% load l10n %}
<ul>
{% for code in codes %}
<li id="{{code.pk|unlocalize}}" class="{% if code.is_leaf%}jstree-leaf{% else %}jstree-closed{% endif %}">
{{code.long_name}}
</li>
{% endfor %}
</ul>

View File

@ -23,7 +23,7 @@ from konova.autocompletes import EcoAccountAutocomplete, \
ShareUserAutocomplete, BiotopeExtraCodeAutocomplete, CompensationActionDetailCodeAutocomplete ShareUserAutocomplete, BiotopeExtraCodeAutocomplete, CompensationActionDetailCodeAutocomplete
from konova.settings import SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY, DEBUG from konova.settings import SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY, DEBUG
from konova.sso.sso import KonovaSSOClient from konova.sso.sso import KonovaSSOClient
from konova.views import logout_view, home_view from konova.views import logout_view, home_view, get_konova_code_action_children
sso_client = KonovaSSOClient(SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY) sso_client = KonovaSSOClient(SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY)
urlpatterns = [ urlpatterns = [
@ -40,6 +40,8 @@ urlpatterns = [
path('analysis/', include("analysis.urls")), path('analysis/', include("analysis.urls")),
path('api/', include("api.urls")), path('api/', include("api.urls")),
path("codes/comp/action/children", get_konova_code_action_children, name="codes-action-children"),
# Autocomplete paths for all apps # Autocomplete paths for all apps
path("atcmplt/eco-accounts", EcoAccountAutocomplete.as_view(), name="accounts-autocomplete"), path("atcmplt/eco-accounts", EcoAccountAutocomplete.as_view(), name="accounts-autocomplete"),
path("atcmplt/interventions", InterventionAutocomplete.as_view(), name="interventions-autocomplete"), path("atcmplt/interventions", InterventionAutocomplete.as_view(), name="interventions-autocomplete"),

View File

@ -9,9 +9,12 @@ from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import HttpRequest, FileResponse from django.http import HttpRequest, FileResponse
from django.shortcuts import redirect, render, get_object_or_404 from django.shortcuts import redirect, render, get_object_or_404
from django.template.loader import render_to_string
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from codelist.models import KonovaCode
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID
from compensation.models import Compensation, EcoAccount from compensation.models import Compensation, EcoAccount
from intervention.models import Intervention from intervention.models import Intervention
from konova.contexts import BaseContext from konova.contexts import BaseContext
@ -124,3 +127,24 @@ def get_500_view(request: HttpRequest):
""" """
context = BaseContext.context context = BaseContext.context
return render(request, "500.html", context, status=500) return render(request, "500.html", context, status=500)
@login_required
def get_konova_code_action_children(request: HttpRequest):
template = "konova/widgets/checkbox-tree-select-content.html"
_id = request.GET.get("id", None)
if _id == "#":
# Return all!
codes = KonovaCode.objects.filter(
code_lists__in=[CODELIST_COMPENSATION_ACTION_ID],
parent=None,
)
else:
codes = KonovaCode.objects.filter(
code_lists__in=[CODELIST_COMPENSATION_ACTION_ID],
parent__id=_id,
)
context = {
"codes": codes
}
return render(request, template, context)

View File

@ -0,0 +1,2 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>