From e60666c795e1531fda90013a001ac6dbc58b5667 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 20 Mar 2020 19:40:24 +0100 Subject: Refactoring and many fixes on file module --- archaeological_files_pdl/forms.py | 199 +++++++++------------ .../ishtar/wizard/wizard_generalcontractor.html | 39 ---- .../ishtar/wizard/wizard_instruction.html | 116 +++++------- .../ishtar/wizard/wizard_planningservice.html | 53 ------ archaeological_files_pdl/wizards.py | 14 +- 5 files changed, 138 insertions(+), 283 deletions(-) delete mode 100644 archaeological_files_pdl/templates/ishtar/wizard/wizard_generalcontractor.html delete mode 100644 archaeological_files_pdl/templates/ishtar/wizard/wizard_planningservice.html (limited to 'archaeological_files_pdl') diff --git a/archaeological_files_pdl/forms.py b/archaeological_files_pdl/forms.py index 7ade0b3f0..7ada27be8 100644 --- a/archaeological_files_pdl/forms.py +++ b/archaeological_files_pdl/forms.py @@ -26,11 +26,14 @@ from ishtar_common.utils import ugettext_lazy as _ from ishtar_common.models import Person, Town, Department, valid_id, \ person_type_pk_lazy, person_type_pks_lazy, organization_type_pks_lazy, \ - organization_type_pk_lazy + organization_type_pk_lazy, get_sra_agent_label, \ + get_orga_general_contractor_label, get_general_contractor_label, \ + get_orga_planning_service_label, get_responsible_planning_service_label + from archaeological_files import models from ishtar_common.forms import get_now, reverse_lazy, ManageOldType, \ - CustomForm, FieldType, IshtarForm + CustomForm, FieldType, IshtarForm, FormHeader from ishtar_common import widgets from bootstrap_datepicker.widgets import DatePicker @@ -84,18 +87,22 @@ class FileFormPreventiveType(CustomForm, ManageOldType, forms.Form): self.fields['permit_type'].help_text = models.PermitType.get_help() -class FileFormPlanning(CustomForm, forms.Form): +class FileFormPlanning(CustomForm, ManageOldType): form_label = _(u"Planning") form_admin_name = _(u"Archaeological file - 017 - Preventive - Planning") form_slug = "file-017-preventiveplanning" base_models = ['town', 'department'] associated_models = {'town': Town, 'department': Department} + HEADERS = {} + HEADERS['town'] = FormHeader(_("Localisation")) name = forms.CharField(label=_(u"Planning name"), required=False, max_length=100) town = widgets.Select2MultipleField( model=Town, label=_("Towns"), required=False, remote=True) department = widgets.Select2MultipleField( - model=Department, label=_("Departments"), required=False) + model=Department, label=_("Departments"), required=False, + help_text=_("Only relevant when no town is provided.") + ) locality = forms.CharField(label=_(u"Locality"), max_length=100, required=False) address = forms.CharField( @@ -104,6 +111,7 @@ class FileFormPlanning(CustomForm, forms.Form): required=False) postal_code = forms.CharField(label=_(u"Postal code"), max_length=10, required=False) + HEADERS['total_surface'] = FormHeader(_("Surfaces")) total_surface = forms.FloatField( required=False, widget=widgets.AreaWidget, @@ -236,33 +244,36 @@ class PersonOrgaForm(forms.Form): validators=[valid_id(models.Organization)]) -class FileFormGeneralContractor(CustomForm, PersonOrgaForm): +class FileFormGeneralContractor(CustomForm, ManageOldType): form_label = _(u"General contractor") form_admin_name = _("Archaeological file - 030 - General contractor") form_slug = "file-030-generalcontractor" + extra_form_modals = ["person", "organization"] associated_models = {'general_contractor': models.Person, 'corporation_general_contractor': models.Organization} + corporation_general_contractor = forms.IntegerField( label=_("General contractor"), - required=False, - widget=widgets.JQueryPersonOrganization( - reverse_lazy('autocomplete-organization', - args=[ - organization_type_pks_lazy(['general_contractor'])] - ), - reverse_lazy('organization_create'), - model=models.Organization, + widget=widgets.JQueryAutoComplete( + reverse_lazy( + 'autocomplete-organization', + args=[ + organization_type_pks_lazy(['general_contractor']), + ]), limit={ 'organization_type': [ organization_type_pk_lazy('general_contractor') - ]}, - js_template='ishtar/blocks/JQueryCorporationPerson.js', - new=True), - validators=[valid_id(models.Organization)] - ) + ] + }, + tips=get_orga_general_contractor_label, + associated_model=models.Organization, new=True, + detail=True, + modify=True + ), + validators=[valid_id(models.Organization)]) general_contractor = forms.IntegerField( - label=_(u"In charge"), + label=_("In charge"), required=False, widget=widgets.JQueryAutoComplete( reverse_lazy('autocomplete-person', @@ -271,118 +282,56 @@ class FileFormGeneralContractor(CustomForm, PersonOrgaForm): ]), associated_model=Person, limit={'person_types': [ - person_type_pk_lazy(['general_contractor']) + person_type_pk_lazy('general_contractor') ]}, - dynamic_limit=['general_contractor'], - url_new='new-person-noorga', + tips=get_general_contractor_label, + detail=True, + modify=True, new=True), validators=[valid_id(Person)] ) - PERSON_FIELD = 'general_contractor' - PERSON_TYPE_PK = person_type_pk_lazy('general_contractor') - PERSON_LABEL = _(u"General contractor") - ORGA_FIELD = 'corporation_general_contractor' - ORGA_TYPE_PK = organization_type_pk_lazy('general_contractor') - ORGA_LABEL = _(u"General contractor") - - def __init__(self, *args, **kwargs): - # get the status: natural person or corporation - DEFAULT_STATUS = 'natural' - current_status = '' - if 'data' in kwargs: - # the order is important: PERSON can have an ORGA - for field in [self.PERSON_FIELD, self.ORGA_FIELD]: - current_item_key = ( - (kwargs['prefix'] + '-') - if kwargs.get('prefix') else '') + field - if kwargs['data'] and kwargs['data'].get(current_item_key): - model = self.associated_models[field] - try: - model.objects.get( - pk=kwargs['data'][current_item_key]) - current_status = 'natural' \ - if field == self.PERSON_FIELD else 'corporation' - except (model.DoesNotExist, ValueError): - pass - initial = kwargs.get("initial", {}) - if not current_status: - # the order is important: PERSON can have an ORGA - for field in [self.ORGA_FIELD, self.PERSON_FIELD]: - value = initial.get(field) - model = self.associated_models[field] - try: - model.objects.get(pk=value) - current_status = 'natural' if field == self.PERSON_FIELD \ - 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 - - self.status = status - - if status not in ('natural', 'corporation'): - status = DEFAULT_STATUS - - user = None - if 'user' in kwargs: - user = kwargs.pop('user') - - super(PersonOrgaForm, self).__init__(*args, **kwargs) - - # distinct widget for natural and corporation - if status == 'natural': - self.fields[self.PERSON_FIELD] = forms.IntegerField( - label=self.PERSON_LABEL, - required=False, - initial=initial.get(self.PERSON_FIELD, None), - widget=widgets.JQueryPersonOrganization( - reverse_lazy('autocomplete-person', - args=[self.PERSON_TYPE_PK]), - reverse_lazy('person_create'), - model=Person, - limit={'person_types': [self.PERSON_TYPE_PK], - 'attached_to__isnull': True}, - js_template='ishtar/blocks/JQueryNaturalPerson.js', - new=True), - validators=[valid_id(Person)]) - self.fields.pop(self.ORGA_FIELD) - - -class FileFormPlanningService(CustomForm, forms.Form): - form_label = _(u"Planning service") + def clean(self): + general_contractor = self.cleaned_data["general_contractor"] + corporation_general_contractor = self.cleaned_data[ + "corporation_general_contractor"] + if general_contractor: + try: + person = models.Person.objects.get(pk=general_contractor) + except models.Person.DoesNotExist: + raise forms.ValidationError(_("Non existing person.")) + if person.attached_to.pk != corporation_general_contractor: + raise forms.ValidationError(_( + "The organization of the person in charge differs from the " + "general contractor.")) + return self.cleaned_data + + +class FileFormPlanningService(CustomForm, IshtarForm): + form_label = _("Planning service") form_admin_name = _("Archaeological file - 040 - Planning service") form_slug = "file-040-planningservice" + extra_form_modals = ["person", "organization"] associated_models = {'responsible_town_planning_service': models.Person, 'planning_service': models.Organization} - permit_reference = forms.CharField(label=_(u"File reference"), - required=False, max_length=200) planning_service = forms.IntegerField( label=_("Planning service"), required=False, - widget=widgets.JQueryPersonOrganization( + widget=widgets.JQueryAutoComplete( reverse_lazy( 'autocomplete-organization', args=[organization_type_pks_lazy(['planning_service'])]), - reverse_lazy('organization_create'), - model=models.Organization, + associated_model=models.Organization, limit={ 'organization_type': [organization_type_pk_lazy(['planning_service'])], }, - js_template='ishtar/blocks/JQueryCorporationPerson.js', - new=True), + tips=get_orga_planning_service_label, + new=True, + detail=True, + modify=True, + ), validators=[valid_id(models.Organization)] ) responsible_town_planning_service = forms.IntegerField( @@ -398,21 +347,39 @@ class FileFormPlanningService(CustomForm, forms.Form): person_type_pk_lazy('responsible_planning_service') ]}, dynamic_limit=['planning_service'], - url_new='new-person-noorga', + tips=get_responsible_planning_service_label, + detail=True, + modify=True, new=True), validators=[valid_id(Person)] ) + permit_reference = forms.CharField(label=_(u"File reference"), + required=False, max_length=200) + + def clean(self): + responsible = self.cleaned_data["responsible_town_planning_service"] + orga = self.cleaned_data["planning_service"] + if responsible: + try: + person = models.Person.objects.get(pk=responsible) + except models.Person.DoesNotExist: + raise forms.ValidationError(_("Non existing person.")) + if person.attached_to.pk != orga: + raise forms.ValidationError(_( + "The organization of the person in charge differs from the " + "planning service.")) + return self.cleaned_data class FileFormInstruction(CustomForm, IshtarForm): - form_label = u"Instruction SRA" + form_label = _("Instruction") form_admin_name = _("Archaeological file - 050 - Instruction") form_slug = "file-050-instruction" extra_form_modals = ["person", "organization"] associated_models = {'in_charge': models.Person, 'related_file': models.File} in_charge = forms.IntegerField( - label=_("Person in charge"), + label=_("File managed by"), widget=widgets.JQueryAutoComplete( reverse_lazy( 'autocomplete-person', @@ -421,7 +388,9 @@ class FileFormInstruction(CustomForm, IshtarForm): 'person_types': [ person_type_pk_lazy('sra_agent')] }, - associated_model=Person, new=True), + tips=get_sra_agent_label, + associated_model=Person, new=True, + ), validators=[valid_id(Person)]) related_file = forms.IntegerField( label=_("Related file"), required=False, diff --git a/archaeological_files_pdl/templates/ishtar/wizard/wizard_generalcontractor.html b/archaeological_files_pdl/templates/ishtar/wizard/wizard_generalcontractor.html deleted file mode 100644 index 169b2757e..000000000 --- a/archaeological_files_pdl/templates/ishtar/wizard/wizard_generalcontractor.html +++ /dev/null @@ -1,39 +0,0 @@ -{% extends "ishtar/wizard/wizard_person_orga.html" %} -{% load replace_underscore %} - -{% block corporation %} - - {{ wizard.form.corporation_general_contractor.label_tag }} - {{ wizard.form.corporation_general_contractor.errors }}{{wizard.form.corporation_general_contractor|safe}} - -{% endblock %} - -{% block natural %} - - {{ wizard.form.general_contractor.label_tag }} - {{ wizard.form.general_contractor.errors }}{{wizard.form.general_contractor|safe}} - -{% endblock %} -{% block extra_head_po %} - -{% endblock %} diff --git a/archaeological_files_pdl/templates/ishtar/wizard/wizard_instruction.html b/archaeological_files_pdl/templates/ishtar/wizard/wizard_instruction.html index 72dc1d35d..34db944aa 100644 --- a/archaeological_files_pdl/templates/ishtar/wizard/wizard_instruction.html +++ b/archaeological_files_pdl/templates/ishtar/wizard/wizard_instruction.html @@ -1,76 +1,48 @@ {% extends "ishtar/wizard/default_wizard.html" %} {% load i18n range table_form %} -{% block wizard_form %} -
{% csrf_token %} -
-{{ wizard.form.media }} -{{ wizard.management_form }} - +{% block form_detail %} +
+ {% with wizard.form.in_charge as field %} + {% include "blocks/bs_field_snippet.html" %} + {% endwith %} + {% with wizard.form.related_file as field %} + {% include "blocks/bs_field_snippet.html" %} + {% endwith %} + {% with wizard.form.comment as field %} + {% include "blocks/bs_field_snippet.html" %} + {% endwith %} -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -{% if saisine_type %} - -{% endif %} - - - - - - - - - - -
{{wizard.form.in_charge.errors}}{{wizard.form.in_charge|safe}}
{{wizard.form.related_file|safe}}
{{wizard.form.comment|safe}}
{{wizard.form.end_date.errors}} : {{wizard.form.end_date|safe}}
{{ saisine_type }}{% if saisine_type_delay %} : délai de {{saisine_type_delay}} jours{% endif %}
{{wizard.form.instruction_deadline.errors}}{{wizard.form.instruction_deadline|safe}}
{{wizard.form.numeric_reference.errors}}SRA {{wizard.form.year|safe}} - {{wizard.form.numeric_reference|safe}}
- - -{{ previous_fields|safe }} - {% block "footer" %} - - {% endblock %} -
-
- -{% endblock %} + {% endblock %} diff --git a/archaeological_files_pdl/templates/ishtar/wizard/wizard_planningservice.html b/archaeological_files_pdl/templates/ishtar/wizard/wizard_planningservice.html deleted file mode 100644 index 5eb7c014b..000000000 --- a/archaeological_files_pdl/templates/ishtar/wizard/wizard_planningservice.html +++ /dev/null @@ -1,53 +0,0 @@ -{% extends "ishtar/wizard/default_wizard.html" %} -{% load i18n range table_form %} -{% block wizard_form %} -
{% csrf_token %} -
-{% if wizard.form.media %}{{ wizard.form.media }}{% endif %} -{{ wizard.management_form }} - - - {% if wizard.form.non_field_errors %} - - {%endif%} - - - - - - - - - -
{{wizard.form.non_field_errors}}
{{ wizard.form.planning_service.label_tag }} {{ wizard.form.planning_service.errors }}{{wizard.form.planning_service|safe}}
{{ wizard.form.responsible_town_planning_service.label_tag }} {{ wizard.form.responsible_town_planning_service.errors }}{{wizard.form.responsible_town_planning_service|safe}}
- - -
- - {% if permit_type %} - - - {% endif %} - - - - -
{{permit_type}}
{{ wizard.form.permit_reference.label_tag }}{% if permit_type_code %} [{{permit_type_code}}]{% endif %} {{ wizard.form.permit_reference.errors }}{{wizard.form.permit_reference|safe}}
-
- - -{{ previous_fields|safe }} - {% block "footer" %} - - {% endblock %} -
-
-{% endblock %} - - - diff --git a/archaeological_files_pdl/wizards.py b/archaeological_files_pdl/wizards.py index d13811eea..988303937 100644 --- a/archaeological_files_pdl/wizards.py +++ b/archaeological_files_pdl/wizards.py @@ -17,9 +17,13 @@ # See the file COPYING for details. +from django.conf import settings + from archaeological_files.wizards import FileWizard as BaseFileWizard from archaeological_files import models +from ishtar_common.utils import ugettext_lazy as _ + class FileWizard(BaseFileWizard): parcel_step_key = 'parcelspdl-' @@ -28,8 +32,8 @@ class FileWizard(BaseFileWizard): towns_formset = False multi_towns = True wizard_templates = { - 'generalcontractor-%(url_name)s': - 'ishtar/wizard/wizard_generalcontractor.html', + #'generalcontractor-%(url_name)s': + # 'ishtar/wizard/wizard_generalcontractor.html', 'planningservice-%(url_name)s': 'ishtar/wizard/wizard_planningservice.html', 'instruction-%(url_name)s': @@ -82,9 +86,13 @@ class FileWizard(BaseFileWizard): pass elif self.steps.current == forminstruction: saisine_type = self.get_saisine_type() + context['FILE_PREFIX'] = settings.ISHTAR_FILE_PREFIX if saisine_type: context['saisine_type'] = str(saisine_type) - context['saisine_type_delay'] = saisine_type.delay or 0 + context['saisine_type_message'] = str(saisine_type) + if saisine_type.delay: + context['saisine_type_message'] += str(_( + ": delay of {} days")).format(saisine_type.delay) elif self.steps.current == formfinal: if self.steps.current.endswith('creation'): # creation only parcels = [] -- cgit v1.2.3