diff options
-rw-r--r-- | archaeological_operations/forms.py | 8 | ||||
-rw-r--r-- | ishtar_common/forms_common.py | 18 |
2 files changed, 22 insertions, 4 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index c780fdcd7..b6304d14d 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -52,7 +52,7 @@ from ishtar_common.forms import FinalForm, FormSet, get_now, \ reverse_lazy, get_form_selection, TableSelect, get_data_from_formset, \ ManageOldType, IshtarForm, CustomForm, FieldType, FormSetWithDeleteSwitches from ishtar_common.forms_common import TownFormSet, SourceForm, SourceSelect, \ - get_town_field, TownForm, get_image_help, BaseImageForm + get_town_field, TownForm, get_image_help, BaseImageForm, BaseImageFormset from archaeological_operations.utils import parse_parcels @@ -911,8 +911,8 @@ class OperationFormGeneral(CustomForm, ManageOldType): # verify the logic between start date and excavation end date if self.are_available(['excavation_end_date', 'start_date']) \ - and cleaned_data.get('excavation_end_date'): - if not self.cleaned_data['start_date']: + and cleaned_data.get('excavation_end_date', None): + if not cleaned_data.get('start_date', None): raise forms.ValidationError( _(u"If you want to set an excavation end date you " u"have to provide a start date.")) @@ -1026,7 +1026,7 @@ class CollaboratorForm(CustomForm, IshtarForm): ImagesFormset = formset_factory(BaseImageForm, can_delete=True, - formset=FormSet) + formset=BaseImageFormset) ImagesFormset.file_upload = True ImagesFormset.form_label = _(u"Images") ImagesFormset.form_admin_name = _(u"Operation - 025 - Images") diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 49054ebac..9e082bccf 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -1121,3 +1121,21 @@ class BaseImageForm(ManageOldType): if not data: return 0 return len(get_data_from_formset(data)) + + +class BaseImageFormset(FormSet): + def clean(self): + """ + Verify that no two images are main image + """ + if any(self.errors): + return + have_main = False + for form in self.forms: + is_main = form.cleaned_data.get('is_main', False) + if not is_main: + continue + if is_main and have_main: + raise forms.ValidationError(_(u"Only one image can be a main " + u"image")) + have_main = True
\ No newline at end of file |