diff options
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: | 
