diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-09-14 09:03:56 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-09-14 09:03:56 +0200 |
commit | 89e94b9e2e18ce0f4a9572258b85e401463eb0ea (patch) | |
tree | bf7480e89c9d0dea0932faf346762e3db7e5b7f9 | |
parent | 8627e7dc6cdd0ca96c42fb88c593e75302188fc4 (diff) | |
download | Ishtar-89e94b9e2e18ce0f4a9572258b85e401463eb0ea.tar.bz2 Ishtar-89e94b9e2e18ce0f4a9572258b85e401463eb0ea.zip |
Context records: multiple deletion - redirections after wizard changes
-rw-r--r-- | archaeological_context_records/forms.py | 63 | ||||
-rw-r--r-- | archaeological_context_records/views.py | 4 | ||||
-rw-r--r-- | archaeological_context_records/wizards.py | 6 | ||||
-rw-r--r-- | ishtar_common/forms.py | 2 | ||||
-rw-r--r-- | ishtar_common/wizards.py | 11 |
5 files changed, 61 insertions, 25 deletions
diff --git a/archaeological_context_records/forms.py b/archaeological_context_records/forms.py index 375943c9c..fef256b68 100644 --- a/archaeological_context_records/forms.py +++ b/archaeological_context_records/forms.py @@ -29,12 +29,13 @@ from django.forms.formsets import formset_factory from django.utils.translation import ugettext_lazy as _ from ishtar_common.models import valid_id, IshtarSiteProfile, Town, \ - SpatialReferenceSystem + SpatialReferenceSystem, valid_ids from archaeological_context_records import models from ishtar_common.forms import FinalForm, FormSet, \ - reverse_lazy, get_form_selection, TableSelect, ManageOldType, CustomForm, \ - FieldType, CustomFormSearch, IshtarForm, FormHeader, HistorySelect + reverse_lazy, get_form_selection, ManageOldType, CustomForm, \ + FieldType, CustomFormSearch, IshtarForm, FormHeader, HistorySelect, \ + MultiSearchForm from ishtar_common.forms_common import get_town_field from archaeological_operations.forms import OperationSelect, ParcelField, \ RecordRelationsForm as OpeRecordRelationsForm, RecordRelationsFormSetBase @@ -112,10 +113,35 @@ class RecordSelect(HistorySelect): ids.append('ope_relation_types_{}'.format(idx)) return ids +class BaseRecordFormSelection(object): + pk_key = None -class RecordFormSelection(CustomFormSearch): + def clean(self): + cleaned_data = self.cleaned_data + print(self.cleaned_data, self.pk_key) + if self.pk_key not in cleaned_data or not cleaned_data[self.pk_key]: + raise forms.ValidationError(_(u"You should at least select one " + u"context record.")) + pks = self.cleaned_data[self.pk_key] + if isinstance(pks, int): + pks = [pks] + else: + pks = pks.split(",") + for pk in pks: + try: + cr = models.ContextRecord.objects.get(pk=pk) + except models.ContextRecord.DoesNotExist: + raise forms.ValidationError(_("Invalid selection.")) + if cr.locked: + raise forms.ValidationError(_("This context record is locked " + "for edition.")) + return self.cleaned_data + + +class RecordFormSelection(BaseRecordFormSelection, CustomFormSearch): SEARCH_AND_SELECT = True form_label = _("Context record search") + pk_key = 'pk' associated_models = {'pk': models.ContextRecord} currents = {'pk': models.ContextRecord} @@ -128,20 +154,21 @@ class RecordFormSelection(CustomFormSearch): source_full=reverse_lazy('get-contextrecord-full')), validators=[valid_id(models.ContextRecord)]) - def clean(self): - cleaned_data = self.cleaned_data - if 'pk' not in cleaned_data or not cleaned_data['pk']: - raise forms.ValidationError(_(u"You should at least select one " - u"context record.")) - pk = self.cleaned_data["pk"] - try: - cr = models.ContextRecord.objects.get(pk=pk) - except models.ContextRecord.DoesNotExist: - raise forms.ValidationError(_("Invalid selection.")) - if cr.locked: - raise forms.ValidationError(_("This context record is locked for " - "edition.")) - return self.cleaned_data + +class RecordFormMultiSelection(BaseRecordFormSelection, MultiSearchForm): + form_label = _("Context record search") + associated_models = {'pks': models.ContextRecord} + pk_key = 'pks' + + pk = forms.CharField( + label="", required=False, + widget=widgets.DataTable( + reverse_lazy('get-contextrecord'), + RecordSelect, models.ContextRecord, + multiple_select=True, + gallery=True, map=True, + source_full=reverse_lazy('get-contextrecord-full')), + validators=[valid_ids(models.ContextRecord)]) class RecordFormGeneral(CustomForm, ManageOldType): diff --git a/archaeological_context_records/views.py b/archaeological_context_records/views.py index 20718b33f..2493c9ba7 100644 --- a/archaeological_context_records/views.py +++ b/archaeological_context_records/views.py @@ -145,7 +145,7 @@ def record_modify(request, pk): kwargs={'step': 'operation-record_modification'})) record_deletion_wizard = wizards.RecordDeletionWizard.as_view([ - ('selec-record_deletion', forms.RecordFormSelection), + ('selec-record_deletion', forms.RecordFormMultiSelection), ('final-record_deletion', forms.RecordDeletionForm)], label=_(u"Context record deletion"), url_name='record_deletion',) @@ -157,7 +157,7 @@ def record_delete(request, pk): return HttpResponseRedirect("/") wizards.RecordDeletionWizard.session_set_value( - request, 'selec-record_deletion', 'pk', pk, reset=True) + request, 'selec-record_deletion', 'pks', pk, reset=True) return redirect(reverse('record_deletion', kwargs={'step': 'final-record_deletion'})) diff --git a/archaeological_context_records/wizards.py b/archaeological_context_records/wizards.py index f0f11a1f3..c880f2a4f 100644 --- a/archaeological_context_records/wizards.py +++ b/archaeological_context_records/wizards.py @@ -22,7 +22,7 @@ from django.utils.translation import ugettext_lazy as _ from . import models from ishtar_common.forms import reverse_lazy -from ishtar_common.wizards import Wizard, DeletionWizard, SearchWizard +from ishtar_common.wizards import Wizard, MultipleDeletionWizard, SearchWizard class RecordSearch(SearchWizard): @@ -34,6 +34,7 @@ class RecordWizard(Wizard): edit = False wizard_done_window = reverse_lazy('show-contextrecord') relations_step_key = 'relations' + redirect_url = 'record_modification' def get_template_names(self): templates = super(RecordWizard, self).get_template_names() @@ -147,10 +148,11 @@ class RecordModifWizard(RecordWizard): return kwargs -class RecordDeletionWizard(DeletionWizard): +class RecordDeletionWizard(MultipleDeletionWizard): model = models.ContextRecord fields = ['label', 'parcel', 'description', 'length', 'width', 'thickness', 'depth', 'location', 'datings', 'units', 'documentations', 'filling', 'interpretation', 'taq', 'taq_estimated', 'tpq', 'tpq_estimated'] filter_owns = {'selec-record_deletion': ['pk']} + redirect_url = "record_deletion" diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 04cd5a327..5be5deafe 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -395,7 +395,7 @@ class MultiSearchForm(CustomFormSearch): raise NotImplementedError("A \"pk\" field must be defined") if self.pk_key not in self.associated_models: raise NotImplementedError("\"{}\" must be defined in " - "associated_models") + "associated_models".format(self.pk_key)) self.fields['pk'].required = True self.fields[self.pk_key] = self.fields.pop('pk') diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index 9c89fd04b..0c13139df 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -1527,7 +1527,7 @@ class DeletionWizard(Wizard): (hasattr(self, 'model') and hasattr(self.model, 'TABLE_COLS')): self.fields = self.model.TABLE_COLS assert self.model - return super(DeletionWizard, self).__init__(*args, **kwargs) + super(DeletionWizard, self).__init__(*args, **kwargs) def get_formated_datas(self, forms): datas = super(DeletionWizard, self).get_formated_datas(forms) @@ -1604,7 +1604,14 @@ class MultipleDeletionWizard(MultipleItemWizard): (hasattr(self, 'model') and hasattr(self.model, 'TABLE_COLS')): self.fields = self.model.TABLE_COLS assert self.model - return super(MultipleDeletionWizard, self).__init__(*args, **kwargs) + super(MultipleDeletionWizard, self).__init__(*args, **kwargs) + + def get_template_names(self): + current_step = self.steps.current + if current_step.startswith("final-") and \ + current_step not in self.wizard_templates: + return ["ishtar/wizard/delete_wizard.html"] + return super(MultipleDeletionWizard, self).get_template_names() def get_formated_datas(self, forms): datas = super(MultipleDeletionWizard, self).get_formated_datas(forms) |