diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-11-15 16:29:30 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:23:19 +0100 |
commit | cda77c979e232386ef24ea7a04600f16f3c32c98 (patch) | |
tree | 7d53e762723338913594ccdb42c6fa08c4bfb5ef /archaeological_files/wizards.py | |
parent | 4746cd2938df3cf87ae338d22eb4f67f35bac960 (diff) | |
download | Ishtar-cda77c979e232386ef24ea7a04600f16f3c32c98.tar.bz2 Ishtar-cda77c979e232386ef24ea7a04600f16f3c32c98.zip |
File module refactoring - more tests for files
Diffstat (limited to 'archaeological_files/wizards.py')
-rw-r--r-- | archaeological_files/wizards.py | 122 |
1 files changed, 86 insertions, 36 deletions
diff --git a/archaeological_files/wizards.py b/archaeological_files/wizards.py index 1538a984a..569b01a56 100644 --- a/archaeological_files/wizards.py +++ b/archaeological_files/wizards.py @@ -39,10 +39,23 @@ class FileSearch(SearchWizard): class FileWizard(OperationWizard): model = models.File object_parcel_type = "associated_file" - parcel_step_key = "parcels-" - town_step_keys = ["towns-"] + parcel_step_key = "parcelspdl-" + town_step_keys = ["preventiveplanning-", "researchaddress-"] wizard_done_window = reverse_lazy("show-file") redirect_url = "file_modification" + town_input_id = "town" + towns_formset = False + multi_towns = True + wizard_templates = { + "planningservice-%(url_name)s": "ishtar/wizard/wizard_planningservice.html", + "instruction-%(url_name)s": "ishtar/wizard/wizard_instruction.html", + "preventiveplanning-%(url_name)s": "ishtar/wizard/wizard_preventiveplanning.html", + } + wizard_confirm = "ishtar/wizard/file_confirm_wizard.html" + + def get_current_year(self): + general_form_key = "general-" + self.url_name + return self.session_get_value(general_form_key, "year") def get_extra_model(self, dct, m2m, form_list): dct = super(FileWizard, self).get_extra_model(dct, m2m, form_list) @@ -53,9 +66,79 @@ class FileWizard(OperationWizard): dct["numeric_reference"] = current_ref and current_ref + 1 or 1 return dct + def get_form_kwargs(self, *args, **kwargs): + returned = super(FileWizard, self).get_form_kwargs(*args, **kwargs) + if args and args[0].startswith("generalcontractor-"): + if "status" in self.request.GET: + returned["status"] = self.request.GET["status"] + if args and args[0].startswith("instruction-"): + returned["year"] = self.get_current_year() + returned["saisine_type"] = self.get_saisine_type() + returned["reception_date"] = self.session_get_value( + "general-" + self.url_name, "reception_date" + ) + return returned + + def get_saisine_type(self): + try: + idx = int( + self.session_get_value( + "preventivetype-" + self.url_name, "saisine_type" + ) + ) + return models.SaisineType.objects.get(pk=idx) + except (TypeError, ValueError, models.PermitType.DoesNotExist): + pass + + def get_context_data(self, form, **kwargs): + context = super(FileWizard, self).get_context_data(form) + formplanning = "planningservice-" + self.url_name + forminstruction = "instruction-" + self.url_name + formfinal = "final-" + self.url_name + if self.steps.current == formplanning: + try: + idx = int( + self.session_get_value( + "preventivetype-" + self.url_name, "permit_type" + ) + ) + permit_type = models.PermitType.objects.get(pk=idx) + context["permit_type"] = str(permit_type) + context["permit_type_code"] = str(permit_type.txt_idx) + except (TypeError, ValueError, models.PermitType.DoesNotExist): + pass + elif self.steps.current == forminstruction: + saisine_type = self.get_saisine_type() + context["FILE_PREFIX"] = settings.ISHTAR_FILE_PREFIX + if saisine_type: + context["saisine_type"] = str(saisine_type) + context["saisine_type_message"] = str(saisine_type) + if saisine_type.delay: + context["saisine_type_message"] += str( + _(": delay of {} days") + ).format(saisine_type.delay) + elif self.steps.current == formfinal: + if not self.steps.current.endswith("creation"): # modification only + try: + numeric_reference = int( + self.session_get_value( + "instruction-" + self.url_name, "numeric_reference" + ) + ) + + q = models.File.objects.filter( + numeric_reference=numeric_reference, + year=self.get_current_year(), + ).exclude(pk=self.get_current_object().pk) + context["numeric_reference_files"] = q.all() + except (ValueError, TypeError): + pass + + return context + def done(self, form_list, **kwargs): """ - Save parcels and make numeric_reference unique + Make numeric_reference unique """ r = super(FileWizard, self).done(form_list, return_object=True, **kwargs) if type(r) not in (list, tuple) or len(r) != 2: @@ -77,39 +160,6 @@ class FileWizard(OperationWizard): if changed: obj.numeric_reference = numeric_reference obj.save() - obj.parcels.clear() - for form in form_list: - if ( - not hasattr(form, "prefix") - or not form.prefix.startswith(self.parcel_step_key) - or not hasattr(form, "forms") - ): - continue - for frm in form.forms: - if not frm.is_valid(): - continue - dct = frm.cleaned_data.copy() - if "parcel" in dct: - try: - parcel = Parcel.objects.get(pk=dct["parcel"]) - setattr(parcel, self.object_parcel_type, obj) - parcel.save() - except (ValueError, ObjectDoesNotExist): - continue - continue - try: - dct["town"] = models.Town.objects.get(pk=int(dct["town"])) - except (ValueError, ObjectDoesNotExist, KeyError): - continue - dct["associated_file"], dct["operation"] = None, None - dct[self.object_parcel_type] = obj - if "DELETE" in dct: - dct.pop("DELETE") - parcel = Parcel.objects.filter(**dct).count() - if not parcel: - dct["history_modifier"] = self.request.user - parcel = Parcel(**dct) - parcel.save() return res |