diff options
Diffstat (limited to 'archaeological_finds/forms_treatments.py')
-rw-r--r-- | archaeological_finds/forms_treatments.py | 59 |
1 files changed, 36 insertions, 23 deletions
diff --git a/archaeological_finds/forms_treatments.py b/archaeological_finds/forms_treatments.py index 4f120adbf..a0ac1a7c6 100644 --- a/archaeological_finds/forms_treatments.py +++ b/archaeological_finds/forms_treatments.py @@ -23,7 +23,6 @@ from collections import OrderedDict from django import forms from django.core import validators -from django.forms.formsets import formset_factory from django.utils.translation import ugettext_lazy as _ from archaeological_finds import models @@ -132,7 +131,7 @@ class BaseTreatmentForm(CustomForm, ManageOldType): end_date = forms.DateField(label=_(u"Closing date"), required=False, widget=DatePicker) container = forms.IntegerField( - label=_(u"Container (relevant for packaging)"), + label=_(u"Container (relevant for packaging and loan)"), widget=widgets.JQueryAutoComplete( reverse_lazy('autocomplete-container'), associated_model=Container, new=True), @@ -194,39 +193,45 @@ class BaseTreatmentForm(CustomForm, ManageOldType): def clean(self, *args, **kwargs): data = self.cleaned_data + loan = models.TreatmentType.get_cache('loan') + loan_return = models.TreatmentType.get_cache('loan-return') packaging = models.TreatmentType.get_cache('packaging') if not packaging: logger.warning("No 'packaging' treatment type defined") return + treatment_types = data.get('treatment_type', []) + + if (str(packaging.pk) in treatment_types + or str(loan.pk) in treatment_types) and \ + str(loan_return.pk) in treatment_types: + raise forms.ValidationError( + _(u"\"Loan return\" is not compatible with \"loan\" or " + u"\"packaging\" treatments.")) + + if str(packaging.pk) in treatment_types \ + and str(loan.pk) in treatment_types: + raise forms.ValidationError( + _(u"Packaging (concerning the reference location) and loan " + u"(concerning a temporary location) cannot be in the same " + u"treatment.")) if data.get('container', None) \ - and str(packaging.pk) not in data.get('treatment_type', []): + and str(packaging.pk) not in treatment_types\ + and str(loan.pk) not in treatment_types: raise forms.ValidationError( _(u"The container field is attached to the treatment. If " - u"no packaging treatment is done it is not relevant.")) - if not data.get('container', None) \ - and str(packaging.pk) in data.get('treatment_type', []): + u"no packaging or no loan treatment is done it is not " + u"relevant.")) + if not data.get('container', None) and ( + str(packaging.pk) in treatment_types or + str(loan.pk) in treatment_types): raise forms.ValidationError( - _(u"If a packaging treatment is done, the container field " + _(u"If a packaging/loan treatment is done, the container field " u"must be filled.")) if not data.get('person', None) and not data.get('organization', None): raise forms.ValidationError( _(u"A responsible or an organization must be defined.")) return data - # TODO - """ - for treatment_type in self.cleaned_data.get('treatment_type', []): - try: - treatment = models.TreatmentType.objects.get( - pk=treatment_type, available=True) - except models.TreatmentType.DoesNotExist: - raise forms.ValidationError(_(u"This treatment type is not " - u"available.")) - if treatment.upstream_is_many and \ - not self.cleaned_data.get('basket'): - raise forms.ValidationError(_(u"This treatment needs a " - u"basket.")) - """ class N1TreatmentForm(BaseTreatmentForm): @@ -314,6 +319,11 @@ class QAFindTreatmentForm(IshtarForm): reverse_lazy('autocomplete-container'), associated_model=Container, new=True), validators=[valid_id(Container)]) + reference_container = forms.BooleanField( + label=_(u"Change the reference container"), required=False, + widget=widgets.CheckboxInput, initial=True, + help_text=_(u"If unchecked the current container will be changed") + ) create_treatment = forms.BooleanField( label=_(u"Create a treatment"), required=False, widget=widgets.CheckboxInput @@ -386,10 +396,13 @@ class QAFindTreatmentForm(IshtarForm): t.treatment_types.add(packaging) t.save(items=items) return + container_attr = 'container' + if self.cleaned_data.get('reference_container', False): + container_attr = 'container_ref' for find in items: - if find.container == container: + if getattr(find, container_attr) == container: continue - find.container = container + setattr(find, container_attr, container) find.save() |