diff options
Diffstat (limited to 'archaeological_finds/forms_treatments.py')
-rw-r--r-- | archaeological_finds/forms_treatments.py | 83 |
1 files changed, 53 insertions, 30 deletions
diff --git a/archaeological_finds/forms_treatments.py b/archaeological_finds/forms_treatments.py index 5f3e49887..8ae0dbd36 100644 --- a/archaeological_finds/forms_treatments.py +++ b/archaeological_finds/forms_treatments.py @@ -131,7 +131,8 @@ class BaseTreatmentForm(CustomForm, ManageOldType): end_date = forms.DateField(label=_(u"Closing date"), required=False, widget=DatePicker) container = forms.IntegerField( - label=_(u"Destination container (relevant for packaging and loan)"), + label=_(u"Destination container (relevant for treatment that change " + u"location)"), widget=widgets.JQueryAutoComplete( reverse_lazy('autocomplete-container'), associated_model=Container, new=True), @@ -193,41 +194,62 @@ 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.")) + treatment_types = data.get('treatment_type', []) - if str(packaging.pk) in treatment_types \ - and str(loan.pk) in treatment_types: + change_current_location = [ + unicode(tp) for tp in treatment_types if tp.change_current_location] + restore_reference_location = [ + unicode(tp) for tp in treatment_types + if tp.restore_reference_location + ] + change_ref_location = [ + unicode(tp) for tp in treatment_types + if tp.change_reference_location + ] + + if (change_ref_location or change_current_location + ) and restore_reference_location: + if change_ref_location: + raise forms.ValidationError( + unicode( + _(u"{} is not compatible with {} " + u"treatments.")).format( + u' ; '.join(restore_reference_location), + u' ; '.join(change_ref_location), + ) + ) + else: + raise forms.ValidationError( + unicode( + _(u"{} is not compatible with {} " + u"treatments.")).format( + u' ; '.join(restore_reference_location), + u' ; '.join(change_current_location) + ) + ) + + if change_ref_location and change_current_location: raise forms.ValidationError( - _(u"Packaging (concerning the reference location) and loan " - u"(concerning a temporary location) cannot be in the same " - u"treatment.")) + unicode( + _(u"{} is not compatible with {} " + u"treatments.")).format( + u' ; '.join(change_ref_location), + u' ; '.join(change_current_location) + ) + ) if data.get('container', None) \ - and str(packaging.pk) not in treatment_types\ - and str(loan.pk) not in treatment_types: + and not change_ref_location\ + and not change_current_location: raise forms.ValidationError( - _(u"The container field is attached to the treatment. If " - u"no packaging or no loan treatment is done it is not " - u"relevant.")) + _(u"The container field is attached to the treatment but " + u"no treatment with container change is defined.")) if not data.get('container', None) and ( - str(packaging.pk) in treatment_types or - str(loan.pk) in treatment_types): + change_ref_location or change_current_location): raise forms.ValidationError( - _(u"If a packaging/loan treatment is done, the container field " - u"must be filled.")) + _(u"A treatment with location change is defined, the container " + u"field 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.")) @@ -381,8 +403,9 @@ class QAFindTreatmentForm(IshtarForm): container = Container.objects.get(pk=self.cleaned_data['container']) if self.cleaned_data['create_treatment']: packaging, created = models.TreatmentType.objects.get_or_create( - label=u"Conditionnement", - txt_idx='packaging' + txt_idx='packaging', + defaults={"label": _(u"Packaging"), + "change_reference_location": True} ) t = models.Treatment.objects.create( container=container, |