WIP: CompensationAction using jstree
This commit is contained in:
		
							parent
							
								
									afb78fa670
								
							
						
					
					
						commit
						62e452c625
					
				@ -11,12 +11,14 @@ from django import forms
 | 
			
		||||
from django.contrib import messages
 | 
			
		||||
from django.http import HttpRequest, HttpResponseRedirect
 | 
			
		||||
from django.shortcuts import render
 | 
			
		||||
from django.urls import reverse
 | 
			
		||||
from django.utils.translation import pgettext_lazy as _con, gettext_lazy as _
 | 
			
		||||
 | 
			
		||||
from codelist.models import KonovaCode
 | 
			
		||||
from codelist.settings import CODELIST_BIOTOPES_ID, CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_EXTRA_CODES_ID, \
 | 
			
		||||
    CODELIST_COMPENSATION_ACTION_DETAIL_ID
 | 
			
		||||
from compensation.models import CompensationDocument, EcoAccountDocument
 | 
			
		||||
from intervention.inputs import TreeSelectMultiple
 | 
			
		||||
from konova.contexts import BaseContext
 | 
			
		||||
from konova.forms import BaseModalForm, NewDocumentModalForm, RemoveModalForm
 | 
			
		||||
from konova.models import DeadlineType
 | 
			
		||||
@ -411,15 +413,11 @@ class NewActionModalForm(BaseModalForm):
 | 
			
		||||
        required=True,
 | 
			
		||||
        help_text=_("Select the action type"),
 | 
			
		||||
        queryset=KonovaCode.objects.filter(
 | 
			
		||||
            is_archived=False,
 | 
			
		||||
            is_leaf=True,
 | 
			
		||||
            code_lists__in=[CODELIST_COMPENSATION_ACTION_ID],
 | 
			
		||||
            is_archived=False,
 | 
			
		||||
        ),
 | 
			
		||||
        widget=autocomplete.ModelSelect2Multiple(
 | 
			
		||||
            url="codes-compensation-action-autocomplete",
 | 
			
		||||
            attrs={
 | 
			
		||||
                "data-placeholder": _("Action"),
 | 
			
		||||
            }
 | 
			
		||||
        widget=TreeSelectMultiple(
 | 
			
		||||
            url=None,
 | 
			
		||||
        ),
 | 
			
		||||
    )
 | 
			
		||||
    action_type_details = forms.ModelMultipleChoiceField(
 | 
			
		||||
@ -482,6 +480,14 @@ class NewActionModalForm(BaseModalForm):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        self.form_title = _("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):
 | 
			
		||||
        action = self.instance.add_action(self)
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,11 @@
 | 
			
		||||
{% load i18n l10n static fontawesome_5 humanize ksp_filters %}
 | 
			
		||||
 | 
			
		||||
{% block head %}
 | 
			
		||||
    {% comment %}
 | 
			
		||||
        Needed for custom Checkbox Tree Select Widget
 | 
			
		||||
    {% endcomment %}
 | 
			
		||||
    {% include 'form/scripts/jstree-scripts.html' %}
 | 
			
		||||
 | 
			
		||||
    {% comment %}
 | 
			
		||||
        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.
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,11 @@
 | 
			
		||||
{% load i18n l10n static fontawesome_5 humanize %}
 | 
			
		||||
 | 
			
		||||
{% block head %}
 | 
			
		||||
    {% comment %}
 | 
			
		||||
        Needed for custom Checkbox Tree Select Widget
 | 
			
		||||
    {% endcomment %}
 | 
			
		||||
    {% include 'form/scripts/jstree-scripts.html' %}
 | 
			
		||||
 | 
			
		||||
    {% comment %}
 | 
			
		||||
        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.
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,11 @@
 | 
			
		||||
{% load i18n l10n static fontawesome_5 humanize %}
 | 
			
		||||
 | 
			
		||||
{% block head %}
 | 
			
		||||
    {% comment %}
 | 
			
		||||
        Needed for custom Checkbox Tree Select Widget
 | 
			
		||||
    {% endcomment %}
 | 
			
		||||
    {% include 'form/scripts/jstree-scripts.html' %}
 | 
			
		||||
 | 
			
		||||
    {% comment %}
 | 
			
		||||
        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.
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
from django import forms
 | 
			
		||||
from django.urls import reverse
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DummyFilterInput(forms.HiddenInput):
 | 
			
		||||
@ -30,3 +31,17 @@ class GenerateInput(forms.TextInput):
 | 
			
		||||
 | 
			
		||||
    """
 | 
			
		||||
    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)
 | 
			
		||||
 | 
			
		||||
@ -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>
 | 
			
		||||
@ -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>
 | 
			
		||||
@ -23,7 +23,7 @@ from konova.autocompletes import EcoAccountAutocomplete, \
 | 
			
		||||
    ShareUserAutocomplete, BiotopeExtraCodeAutocomplete, CompensationActionDetailCodeAutocomplete
 | 
			
		||||
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
 | 
			
		||||
from konova.views import logout_view, home_view, get_konova_code_action_children
 | 
			
		||||
 | 
			
		||||
sso_client = KonovaSSOClient(SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY)
 | 
			
		||||
urlpatterns = [
 | 
			
		||||
@ -40,6 +40,8 @@ urlpatterns = [
 | 
			
		||||
    path('analysis/', include("analysis.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
 | 
			
		||||
    path("atcmplt/eco-accounts", EcoAccountAutocomplete.as_view(), name="accounts-autocomplete"),
 | 
			
		||||
    path("atcmplt/interventions", InterventionAutocomplete.as_view(), name="interventions-autocomplete"),
 | 
			
		||||
 | 
			
		||||
@ -9,9 +9,12 @@ from django.contrib.auth import logout
 | 
			
		||||
from django.contrib.auth.decorators import login_required
 | 
			
		||||
from django.http import HttpRequest, FileResponse
 | 
			
		||||
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.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 intervention.models import Intervention
 | 
			
		||||
from konova.contexts import BaseContext
 | 
			
		||||
@ -124,3 +127,24 @@ def get_500_view(request: HttpRequest):
 | 
			
		||||
    """
 | 
			
		||||
    context = BaseContext.context
 | 
			
		||||
    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)
 | 
			
		||||
							
								
								
									
										2
									
								
								templates/form/scripts/jstree-scripts.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								templates/form/scripts/jstree-scripts.html
									
									
									
									
									
										Normal 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>
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user