#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2014 Étienne Loks # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # See the file COPYING for details. import datetime from django import forms from django.core import validators from django.utils.translation import ugettext_lazy as _ from ishtar_common.models import Person, valid_id from archaeological_files import models 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 class FileFormGeneral(forms.Form): form_label = _("General") associated_models = {'file_type':models.FileType} file_type = forms.ChoiceField(label=_("File type"), choices=[]) year = forms.IntegerField(label=_("Year"), initial=lambda:datetime.datetime.now().year, validators=[validators.MinValueValidator(1900), validators.MaxValueValidator(2100)]) creation_date = forms.DateField(label=_(u"Creation date"), initial=get_now, widget=widgets.JQueryDate) reception_date = forms.DateField(label=_(u"Reception date"), initial=get_now, widget=widgets.JQueryDate) def __init__(self, *args, **kwargs): super(FileFormGeneral, self).__init__(*args, **kwargs) self.fields['file_type'].choices = models.FileType.get_types() self.fields['file_type'].help_text = models.FileType.get_help() class FileFormPreventiveType(forms.Form): form_label = u"Saisine" associated_models = {'saisine_type':models.SaisineType, 'permit_type':models.PermitType} permit_type = forms.ChoiceField(label=_(u"Permit type"), required=False, choices=[]) saisine_type = forms.ChoiceField(label=_(u"Saisine type"), choices=[]) def __init__(self, *args, **kwargs): super(FileFormPreventiveType, self).__init__(*args, **kwargs) self.fields['saisine_type'].choices = models.SaisineType.get_types() self.fields['saisine_type'].help_text = models.SaisineType.get_help() self.fields['permit_type'].choices = models.PermitType.get_types( 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} name = forms.CharField(label=_(u"Planning name"), required=False, max_length=100) main_town = get_town_field(required=False) locality = forms.CharField(label=_(u"Locality"), max_length=100, required=False) address = forms.CharField(label=_(u"Address (number/street)"), widget=forms.Textarea, required=False) postal_code = forms.CharField(label=_(u"Postal code"), max_length=10, required=False) total_surface = forms.IntegerField(required=False, widget=widgets.AreaWidget, label=_(u"Total surface (m²)"), validators=[validators.MinValueValidator(0), validators.MaxValueValidator(999999999)]) total_developed_surface = forms.IntegerField(widget=widgets.AreaWidget, 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)])