From 086cb36acde4343f5e4dc8dc18a70cdbd8512c33 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Sun, 28 Dec 2014 01:15:07 +0100 Subject: Work on dynamic (too much of ajax and JS...) person/organization widget --- archaeological_files_pdl/forms.py | 89 +++++++++++++++++++++- .../ishtar/blocks/JQueryCorporationPerson.js | 2 + .../templates/ishtar/blocks/JQueryNaturalPerson.js | 2 + .../templates/ishtar/blocks/JQueryPersonOrga.js | 65 ++++++++++++++++ .../ishtar/wizard/wizard_generalcontractor.html | 32 ++++++++ archaeological_files_pdl/views.py | 8 +- archaeological_files_pdl/wizards.py | 9 +++ 7 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 archaeological_files_pdl/templates/ishtar/blocks/JQueryCorporationPerson.js create mode 100644 archaeological_files_pdl/templates/ishtar/blocks/JQueryNaturalPerson.js create mode 100644 archaeological_files_pdl/templates/ishtar/blocks/JQueryPersonOrga.js create mode 100644 archaeological_files_pdl/templates/ishtar/wizard/wizard_generalcontractor.html (limited to 'archaeological_files_pdl') diff --git a/archaeological_files_pdl/forms.py b/archaeological_files_pdl/forms.py index aedf60ba1..fc4efff73 100644 --- a/archaeological_files_pdl/forms.py +++ b/archaeological_files_pdl/forms.py @@ -23,11 +23,13 @@ from django import forms from django.core import validators from django.utils.translation import ugettext_lazy as _ -from ishtar_common.models import Person +from ishtar_common.models import Person, valid_id from archaeological_files import models -from ishtar_common.forms import get_now +from ishtar_common.forms import get_now, reverse_lazy from ishtar_common.forms_common import get_town_field +from archaeological_files.forms import GENERAL_CONTRACTOR, \ + RESPONSIBLE_PLANNING_SERVICE from ishtar_common import widgets @@ -65,7 +67,6 @@ class FileFormPreventiveType(forms.Form): default='NP') self.fields['permit_type'].help_text = models.PermitType.get_help() - class FileFormPlanning(forms.Form): form_label = _(u"Planning") associated_models = {'town':models.Town} @@ -87,3 +88,85 @@ class FileFormPlanning(forms.Form): label=_(u"Total developed surface (m²)"), required=False, validators=[validators.MinValueValidator(0), validators.MaxValueValidator(999999999)]) + +class FileFormGeneralContractor(forms.Form): + form_label = _(u"General contractor") + associated_models = {'general_contractor':models.Person} + + def __init__(self, *args, **kwargs): + + # get the status: natural person or corporation + DEFAULT_STATUS = 'natural' + current_status = '' + if 'data' in kwargs: + current_item_key = ((kwargs['prefix'] + '-') if kwargs.get('prefix')\ + else '') + 'general_contractor' + if kwargs['data'] and kwargs['data'].get(current_item_key): + model = self.associated_models['general_contractor'] + try: + item = model.objects.get(pk=kwargs['data'][current_item_key]) + current_status = 'natural' if item.is_natural() \ + else 'corporation' + except (model.DoesNotExist, ValueError): + pass + + status = '' + if 'status' in kwargs: + status = kwargs.pop('status') + if current_status != status: + if kwargs.get('data'): + # status is different from the existing - clear fields + kwargs.pop('data') + elif current_status: + status = current_status + else: + status = DEFAULT_STATUS + + if status not in ('natural', 'corporation'): + status = DEFAULT_STATUS + super(FileFormGeneralContractor, self).__init__(*args, **kwargs) + + # distinct widget for natural and corporation + if status == 'natural': + self.fields['general_contractor'] = forms.IntegerField( + label=_(u"General contractor"), + widget=widgets.JQueryPersonOrganization( + reverse_lazy('autocomplete-person', + args=[GENERAL_CONTRACTOR.pk]), + reverse_lazy('person_create'), + model=Person, + limit={'person_types':[GENERAL_CONTRACTOR.pk], + 'attached_to__isnull':True}, + js_template='ishtar/blocks/JQueryNaturalPerson.js', + new=True), + validators=[valid_id(Person)]) + else: + self.fields['general_contractor'] = forms.IntegerField( + label=_(u"General contractor"), + widget=widgets.JQueryPersonOrganization( + reverse_lazy('autocomplete-person', + args=[GENERAL_CONTRACTOR.pk]), + reverse_lazy('organization_person_create'), + model=Person, + limit={'person_types':[GENERAL_CONTRACTOR.pk]}, + js_template='ishtar/blocks/JQueryCorporationPerson.js', + new=True), + validators=[valid_id(Person)]) + +class FileFormPlanningService(forms.Form): + form_label = _(u"Town planning service") + associated_models = {'responsible_planning_service':models.Person} + + def __init__(self, *args, **kwargs): + super(FileFormPlanningService, self).__init__(*args, **kwargs) + self.fields['responsible_planning_service'] = forms.IntegerField( + label=_(u"Responsible town planning service"), + widget=widgets.JQueryPersonOrganization( + reverse_lazy('autocomplete-person', + args=[RESPONSIBLE_PLANNING_SERVICE.pk]), + reverse_lazy('person_create'), + model=Person, + limit={'person_types':[RESPONSIBLE_PLANNING_SERVICE.pk]}, + new=True), + validators=[valid_id(Person)]) + diff --git a/archaeological_files_pdl/templates/ishtar/blocks/JQueryCorporationPerson.js b/archaeological_files_pdl/templates/ishtar/blocks/JQueryCorporationPerson.js new file mode 100644 index 000000000..3eb375167 --- /dev/null +++ b/archaeological_files_pdl/templates/ishtar/blocks/JQueryCorporationPerson.js @@ -0,0 +1,2 @@ +var current_status = 'corporation'; +{% include "ishtar/blocks/JQueryPersonOrga.js" %} diff --git a/archaeological_files_pdl/templates/ishtar/blocks/JQueryNaturalPerson.js b/archaeological_files_pdl/templates/ishtar/blocks/JQueryNaturalPerson.js new file mode 100644 index 000000000..fc4b9a90c --- /dev/null +++ b/archaeological_files_pdl/templates/ishtar/blocks/JQueryNaturalPerson.js @@ -0,0 +1,2 @@ +var current_status = 'natural'; +{% include "ishtar/blocks/JQueryPersonOrga.js" %} diff --git a/archaeological_files_pdl/templates/ishtar/blocks/JQueryPersonOrga.js b/archaeological_files_pdl/templates/ishtar/blocks/JQueryPersonOrga.js new file mode 100644 index 000000000..c151a5e4d --- /dev/null +++ b/archaeological_files_pdl/templates/ishtar/blocks/JQueryPersonOrga.js @@ -0,0 +1,65 @@ +person_save_callback = function(item_id, lbl){ + var url = {{edit_source}}; + $('#id_{{field_id}}').val(null); + $('#id_select_{{field_id}}').val(lbl); + if (item_id){ + url = {{edit_source}}+item_id; + $('#id_{{field_id}}').val(item_id); + } + $("#id_select_{{field_id}}").trigger('autocompletechange'); + $.get(url , function( data ) { + $( "#div-{{field_id}}" ).html( data ); + }); +}; + +edit_url = {{edit_source}}; +parent_id = "{{field_id}}"; + +person_new_callback = function(){ + var url = {{edit_source}}; + $('#id_{{field_id}}').val(null); + $('#id_select_{{field_id}}').val(null); +} + +$(function() { + var $radios = $('input:radio[name=person_type]'); + if($radios.is(':checked') === false) { + $radios.filter('[value='+ current_status +']').prop('checked', true); + } + + $radios.change(function(){ + var loc = window.location; + window.location = loc.protocol + '//' + loc.host + loc.pathname + "?status=" + $('input:radio[name=person_type]:checked').val(); + }); + + $("#id_select_{{field_id}}").autocomplete({ + source: {{source}}, + select: function( event, ui ) { + var url = {{edit_source}}; + if(ui.item){ + url = {{edit_source}}+ui.item.id; + $('#id_{{field_id}}').val(ui.item.id); + } else { + $('#id_{{field_id}}').val(null); + } + $.get(url , function( data ) { + $( "#div-{{field_id}}" ).html( data ); + }); + }, + minLength: 2{% if options %}, + {{options}} + {% endif %} + }); + + $.get( {{edit_source}}{% if selected %}+'{{selected}}'{% endif %}, function( data ) { + $( "#div-{{field_id}}" ).html( data ); + }); + + $('#id_select_{{field_id}}').live('click', function(){ + $('#id_{{field_id}}').val(null); + $('#id_select_{{field_id}}').val(null); + $.get( {{edit_source}}, function( data ) { + $( "#div-{{field_id}}" ).html( data ); + }); + }); +}); diff --git a/archaeological_files_pdl/templates/ishtar/wizard/wizard_generalcontractor.html b/archaeological_files_pdl/templates/ishtar/wizard/wizard_generalcontractor.html new file mode 100644 index 000000000..4788a41fc --- /dev/null +++ b/archaeological_files_pdl/templates/ishtar/wizard/wizard_generalcontractor.html @@ -0,0 +1,32 @@ +{% extends "ishtar/wizard/default_wizard.html" %} +{% load i18n range table_form %} +{% block wizard_form %} +
{% csrf_token %} +
+{{ wizard.form.media }} +{{ wizard.management_form }} + + + + + + + + + + + +
Statut
+ + +{% table_form wizard.form %} +
+ +{{ previous_fields|safe }} +
+ + {% if next_steps %}{% endif %} +
+
+
+{% endblock %} diff --git a/archaeological_files_pdl/views.py b/archaeological_files_pdl/views.py index 1dd69b50c..139da223d 100644 --- a/archaeological_files_pdl/views.py +++ b/archaeological_files_pdl/views.py @@ -33,7 +33,10 @@ file_creation_wizard = FileWizard.as_view([ ('preventivetype-file_creation', forms.FileFormPreventiveType), ('preventiveplanning-file_creation', forms.FileFormPlanning), ('parcelspdl-file_creation', ParcelFormSet), - ('preventive-file_creation', ref_forms.FileFormPreventive), + ('generalcontractor-file_creation', + forms.FileFormGeneralContractor), + ('planningservice-file_creation', + forms.FileFormPlanningService), ('research-file_creation', ref_forms.FileFormResearch), ('final-file_creation', ref_forms.FinalForm)], label=_(u"New file"), @@ -41,9 +44,6 @@ file_creation_wizard = FileWizard.as_view([ 'preventivetype-file_creation':\ is_preventive('general-file_creation', models.FileType, type_key='file_type'), - 'preventive-file_creation':\ - is_preventive('general-file_creation', models.FileType, - type_key='file_type'), 'research-file_creation':\ is_not_preventive('general-file_creation', models.FileType, type_key='file_type'), diff --git a/archaeological_files_pdl/wizards.py b/archaeological_files_pdl/wizards.py index 679b0823f..4abe5f0d5 100644 --- a/archaeological_files_pdl/wizards.py +++ b/archaeological_files_pdl/wizards.py @@ -24,4 +24,13 @@ class FileWizard(BaseFileWizard): town_step_key = 'preventiveplanning-' town_input_id = 'main_town' multi_towns = False + wizard_templates = { + 'generalcontractor-file_creation':\ + 'ishtar/wizard/wizard_generalcontractor.html',} + def get_form_kwargs(self, *args, **kwargs): + returned = super(FileWizard, self).get_form_kwargs(*args, **kwargs) + if args and args[0].startswith('generalcontractor-file_creation'): + if 'status' in self.request.GET: + returned['status'] = self.request.GET['status'] + return returned -- cgit v1.2.3