diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-08-19 12:04:58 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-08-19 12:04:58 +0200 |
commit | 0e39bad8d61b8c1cb90f072beb58c8b5320a5a11 (patch) | |
tree | de69c2af87cef1e2c7f89feae3c53aaa860c96d8 /archaeological_operations | |
parent | d1fb3d446ccfe8b7eec27a85726cd8dd0cf36f6e (diff) | |
download | Ishtar-0e39bad8d61b8c1cb90f072beb58c8b5320a5a11.tar.bz2 Ishtar-0e39bad8d61b8c1cb90f072beb58c8b5320a5a11.zip |
Operations: check PATRIARCHE unicity inside forms (refs #2930)
Diffstat (limited to 'archaeological_operations')
-rw-r--r-- | archaeological_operations/forms.py | 25 | ||||
-rw-r--r-- | archaeological_operations/models.py | 4 | ||||
-rw-r--r-- | archaeological_operations/templates/ishtar/blocks/OAWidget.html | 1 | ||||
-rw-r--r-- | archaeological_operations/widgets.py | 18 |
4 files changed, 40 insertions, 8 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index 644114619..eaba1bca3 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -41,7 +41,7 @@ from ishtar_common.wizards import MultiValueDict from archaeological_files.models import File import models -from widgets import ParcelWidget, SelectParcelWidget +from widgets import ParcelWidget, SelectParcelWidget, OAWidget from ishtar_common import widgets from ishtar_common.forms import FinalForm, FormSet, get_now, \ @@ -458,7 +458,8 @@ class OperationSelect(TableSelect): operation_code = forms.IntegerField(label=_(u"Numeric reference")) if settings.COUNTRY == 'fr': code_patriarche = forms.IntegerField( - label="Numéro d'opération (OA Patriarche)") + widget=OAWidget, + label="Code PATRIARCHE") towns = get_town_field() parcel = ParcelField(label=_("Parcel (section/number/public domain)")) if settings.ISHTAR_DPTS: @@ -721,6 +722,7 @@ class OperationFormGeneral(forms.Form): pk = forms.IntegerField(required=False, widget=forms.HiddenInput) if settings.COUNTRY == 'fr': code_patriarche = forms.IntegerField(label=u"Code PATRIARCHE", + widget=OAWidget, required=False) common_name = forms.CharField(label=_(u"Generic name"), required=False, max_length=120, widget=forms.Textarea) @@ -831,13 +833,24 @@ class OperationFormGeneral(forms.Form): raise forms.ValidationError( _(u"The excavation end date cannot be before the start " u"date.")) + # verify patriarche + code_p = self.cleaned_data.get('code_patriarche', None) + + if code_p: + ops = models.Operation.objects.filter(code_patriarche=code_p) + if 'pk' in cleaned_data and cleaned_data['pk']: + ops = ops.exclude(pk=cleaned_data['pk']) + if ops.count(): + msg = _(u"Ce code Patriarche a déjà été affecté à une " + u"autre opération") + raise forms.ValidationError(msg) + # manage unique operation ID year = self.cleaned_data.get("year") operation_code = cleaned_data.get("operation_code", None) if not operation_code: return self.cleaned_data ops = models.Operation.objects.filter(year=year, operation_code=operation_code) - # manage unique operation ID if 'pk' in cleaned_data and cleaned_data['pk']: ops = ops.exclude(pk=cleaned_data['pk']) if ops.count(): @@ -1150,7 +1163,8 @@ class OperationSourceSelect(SourceSelect): label=_(u"Numeric reference")) if settings.COUNTRY == 'fr': operation__code_patriarche = forms.IntegerField( - label="Numéro d'opération (OA Patriarche)") + widget=OAWidget, + label="Code PATRIARCHE") operation__towns = get_town_field(label=_(u"Operation's town")) operation__operation_type = forms.ChoiceField(label=_(u"Operation type"), choices=[]) @@ -1181,7 +1195,8 @@ class AdministrativeActOpeSelect(TableSelect): ref_sra = forms.CharField(label=u"Autre référence", max_length=15) operation__code_patriarche = forms.IntegerField( - label="Numéro d'opération (OA Patriarche)") + widget=OAWidget, + label="Code PATRIARCHE") act_type = forms.ChoiceField(label=_("Act type"), choices=[]) indexed = forms.NullBooleanField(label=_(u"Indexed?")) operation__towns = get_town_field() diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index ec2a29f51..de44e6d97 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -846,8 +846,8 @@ class Parcel(LightHistorizedItem): nb += unicode(_(u"Public domain")) grouped[-1].parcel_numbers.append(nb) grouped[-1].parcel_numbers.sort() - grouped[-1].parcel_numbers = [strip_zero(nb) - for nb in grouped[-1].parcel_numbers] + grouped[-1].parcel_numbers = [strip_zero(n) + for n in grouped[-1].parcel_numbers] return grouped @classmethod diff --git a/archaeological_operations/templates/ishtar/blocks/OAWidget.html b/archaeological_operations/templates/ishtar/blocks/OAWidget.html new file mode 100644 index 000000000..a1df5a00c --- /dev/null +++ b/archaeological_operations/templates/ishtar/blocks/OAWidget.html @@ -0,0 +1 @@ +OA <input class="widget-oa" type="text"{{final_attrs|safe}}> diff --git a/archaeological_operations/widgets.py b/archaeological_operations/widgets.py index 3a4c458f6..bb219ab76 100644 --- a/archaeological_operations/widgets.py +++ b/archaeological_operations/widgets.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2013 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2013-2016 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -17,7 +17,9 @@ # See the file COPYING for details. +from django import forms from django.forms import widgets +from django.template import Context, loader from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ @@ -52,3 +54,17 @@ class SelectParcelWidget(widgets.TextInput): render += u" <button name='formset_add' value='add'>%s</button>" \ % _(u"Add") return mark_safe(render) + + +class OAWidget(forms.TextInput): + def render(self, name, value, attrs=None): + if not value: + value = u"" + final_attrs = widgets.flatatt( + self.build_attrs(attrs, name=name, value=value)) + dct = {'final_attrs': final_attrs, + 'id': attrs['id'], + "safe_id": attrs['id'].replace('-', '_')} + t = loader.get_template('ishtar/blocks/OAWidget.html') + rendered = t.render(Context(dct)) + return mark_safe(rendered) |