diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2014-12-12 23:21:45 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2014-12-12 23:21:45 +0100 |
commit | 00b2c7e6d225fa97567bab088b9c8deef76dda62 (patch) | |
tree | 9203d4e291c65caae20c72870e5eb0e5f9702c40 /archaeological_operations/forms.py | |
parent | 982bface0b1d11c614f457a40bffb7c84d987cc7 (diff) | |
download | Ishtar-00b2c7e6d225fa97567bab088b9c8deef76dda62.tar.bz2 Ishtar-00b2c7e6d225fa97567bab088b9c8deef76dda62.zip |
Better management of document generation form (refs #2132)
* check if the document template is compatible with the selected object
* remove dead code (old document generation form)
Diffstat (limited to 'archaeological_operations/forms.py')
-rw-r--r-- | archaeological_operations/forms.py | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index bb743372d..aa5f789a2 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -809,14 +809,34 @@ class DocumentGenerationAdminActForm(forms.Form): document_template = forms.ChoiceField(label=_("Template"), choices=[]) def __init__(self, *args, **kwargs): - document_type = 'O' + self.document_type = 'O' if 'operation' in kwargs: - document_type = 'O' if kwargs.pop('operation') else 'F' + self.document_type = 'O' if kwargs.pop('operation') else 'F' + self.obj = None + if 'obj' in kwargs: + self.obj = kwargs.pop('obj') super(DocumentGenerationAdminActForm, self).__init__(*args, **kwargs) self.fields['document_template'].choices = DocumentTemplate.get_tuples( dct={'associated_object_name': 'archaeological_operations.models.AdministrativeAct', - 'acttypes__intented_to':document_type}) + 'acttypes__intented_to':self.document_type}) + + def clean(self): + if not self.obj: + raise forms.ValidationError(_(u"You should select an administrative" + " act.")) + cleaned_data = self.cleaned_data + try: + dt = DocumentTemplate.objects.get( + pk=self.cleaned_data['document_template']) + except DocumentTemplate.DoesNotExist: + raise forms.ValidationError(_(u"This document is not intended for " + u"this type of act.")) + if self.obj.act_type.pk not in [act_type.pk + for act_type in dt.acttypes.all()]: + raise forms.ValidationError(_(u"This document is not intended for " + u"this type of act.")) + return cleaned_data def save(self, object_pk): try: |