diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-03-21 18:12:27 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-03-21 18:12:27 +0100 |
commit | 8a5a127af79fd2f6ebd1ea1f106c06e675d66f8b (patch) | |
tree | 3faed4493575229e097764fa20fb26b227f78b3e | |
parent | 30e374e52f7bdf336d1f9076e4e343d596aa9ebc (diff) | |
download | Ishtar-8a5a127af79fd2f6ebd1ea1f106c06e675d66f8b.tar.bz2 Ishtar-8a5a127af79fd2f6ebd1ea1f106c06e675d66f8b.zip |
⚡️ custom form: better filter for JSON fields - 🐛 fix administrative act custom forms (refs #6238, refs #6239)
-rw-r--r-- | archaeological_files/forms.py | 6 | ||||
-rw-r--r-- | archaeological_finds/forms.py | 6 | ||||
-rw-r--r-- | archaeological_finds/forms_treatments.py | 14 | ||||
-rw-r--r-- | archaeological_operations/forms.py | 4 | ||||
-rw-r--r-- | ishtar_common/admin.py | 10 | ||||
-rw-r--r-- | ishtar_common/migrations/0262_migrate_custom_form_slug.py | 33 | ||||
-rw-r--r-- | ishtar_common/models.py | 54 |
7 files changed, 96 insertions, 31 deletions
diff --git a/archaeological_files/forms.py b/archaeological_files/forms.py index a61b500df..b1c3d691b 100644 --- a/archaeological_files/forms.py +++ b/archaeological_files/forms.py @@ -709,7 +709,7 @@ class FinalFileDeleteForm(FinalForm): class FileFormPreventiveDetail(forms.ModelForm, CustomForm, ManageOldType): form_label = _("Preventive file") form_admin_name = _("Preventive file - 020 - Edition form") - form_slug = "preventive-020-edition-form" + form_slug = "file-020-preventiveedition-form" associated_models = {} @@ -1256,8 +1256,8 @@ class AdministrativeActFileModifyFormSelection(AdministrativeActOpeFormSelection class AdministrativeActFileForm(AdministrativeActForm): - form_admin_name = _("Archaeological file - Administrative act - General") - form_slug = "file-adminact-general" + form_admin_name = _("Administrative act - Archaeological file - General") + form_slug = "adminact-file-general" act_type = forms.ChoiceField(label=_("Act type"), choices=[]) TYPES = [ diff --git a/archaeological_finds/forms.py b/archaeological_finds/forms.py index 04e85a2b0..2796bb844 100644 --- a/archaeological_finds/forms.py +++ b/archaeological_finds/forms.py @@ -210,7 +210,7 @@ class BasicFindForm(CustomForm, ManageOldType): file_upload = True form_label = _("Find") form_admin_name = _("Simple find - 020 - General") - form_slug = "simplefind-020-general" + form_slug = "find-020-simplegeneral" base_models = [ "object_type", "material_type", @@ -737,7 +737,7 @@ class ResultingFindForm(CustomForm, ManageOldType): file_upload = True form_label = _("Resulting find") form_admin_name = _("Treatment n-1 - 030 - Resulting find") - form_slug = "treatmentn1-030-resulting-find" + form_slug = "treatment-030-n1-resulting-find" associated_models = { "resulting_material_type": models.MaterialType, @@ -845,7 +845,7 @@ class ResultingFindForm(CustomForm, ManageOldType): class ResultingFindsForm(CustomForm, ManageOldType): form_label = _("Resulting finds") form_admin_name = _("Treatment 1-n - 030 - Resulting finds") - form_slug = "treatment1n-030-resulting-finds" + form_slug = "treatment-030-1n-resulting-finds" associated_models = {} resultings_number = forms.IntegerField( diff --git a/archaeological_finds/forms_treatments.py b/archaeological_finds/forms_treatments.py index 3131f4e1d..3b71b388d 100644 --- a/archaeological_finds/forms_treatments.py +++ b/archaeological_finds/forms_treatments.py @@ -374,7 +374,7 @@ class BaseTreatmentForm(CustomForm, ManageOldType): class N1TreatmentForm(BaseTreatmentForm): UPSTREAM_IS_MANY = True form_admin_name = _("Treatment n-1 - 020 - General") - form_slug = "treatmentn1-020-general" + form_slug = "treatment-020-n1-general" TYPES = [ FieldType('treatment_status', models.TreatmentStatus, True), @@ -390,7 +390,7 @@ class N1TreatmentForm(BaseTreatmentForm): class OneNTreatmentForm(BaseTreatmentForm): DOWNSTREAM_IS_MANY = True form_admin_name = _("Treatment 1-n - 020 - General") - form_slug = "treatment1n-020-general" + form_slug = "treatment-020-1n-general" TYPES = [ FieldType('treatment_status', models.TreatmentStatus, True), @@ -667,8 +667,8 @@ class AdministrativeActTreatmentFormSelection( class AdministrativeActTreatmentForm(AdministrativeActForm): - form_admin_name = _("Treatment - Administrative act - General") - form_slug = "treatment-adminact-general" + form_admin_name = _("Administrative act - Treatment - General") + form_slug = "adminact-treatment-general" act_type = forms.ChoiceField(label=_("Act type"), choices=[]) TYPES = [ @@ -756,6 +756,8 @@ class TreatmentFileFormSelectionMultiple(MultiSearchForm): class TreatmentFileForm(CustomForm, ManageOldType): + form_admin_name = _("Treatment file - 010 - General") + form_slug = "treatmentfile-010-general" form_label = _("Treatment request") base_models = ['treatment_type_type'] associated_models = { @@ -929,8 +931,8 @@ class AdministrativeActTreatmentFileFormSelection( class AdministrativeActTreatmentFileForm(AdministrativeActForm): - form_admin_name = _("Treatment request - Administrative act - General") - form_slug = "treatmentfile-adminact-general" + form_admin_name = _("Administrative act - Treatment request - General") + form_slug = "adminact-treatmentfile-general" act_type = forms.ChoiceField(label=_("Act type"), choices=[]) TYPES = [ diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index 6a76c4197..28d8825f4 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -1549,8 +1549,8 @@ class AdministrativeActForm(CustomForm, ManageOldType): class AdministrativeActOpeForm(AdministrativeActForm): - form_admin_name = _("Operation - Administrative act - General") - form_slug = "operation-adminact-general" + form_admin_name = _("Administrative act - Operation - General") + form_slug = "adminact-operation-general" class AdministrativeActModifForm(object): diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py index 0d2cf3a0d..50dbbe25a 100644 --- a/ishtar_common/admin.py +++ b/ishtar_common/admin.py @@ -262,13 +262,13 @@ def export_as_geojson_action( fields=field_names, ).encode("utf-8") in_memory = BytesIO() - zip = zipfile.ZipFile(in_memory, "a") - zip.writestr(basename + ".geojson", geojson) + czip = zipfile.ZipFile(in_memory, "a") + czip.writestr(basename + ".geojson", geojson) # fix for Linux zip files read in Windows - for file in zip.filelist: - file.create_system = 0 - zip.close() + for cfile in czip.filelist: + cfile.create_system = 0 + czip.close() response = HttpResponse(content_type="application/zip") response["Content-Disposition"] = "attachment; filename={}.zip".format(basename) in_memory.seek(0) diff --git a/ishtar_common/migrations/0262_migrate_custom_form_slug.py b/ishtar_common/migrations/0262_migrate_custom_form_slug.py new file mode 100644 index 000000000..f35d78e1a --- /dev/null +++ b/ishtar_common/migrations/0262_migrate_custom_form_slug.py @@ -0,0 +1,33 @@ +# Generated by Django 2.2.28 on 2025-03-21 17:52 + +from django.db import migrations + +FORM_SLUG = [ + ("preventive-020-edition-form", "file-020-preventiveedition-form"), + ("file-adminact-general", "adminact-file-general"), + ("simplefind-020-general", "find-020-simplegeneral"), + ("treatmentn1-030-resulting-find", "treatment-030-n1-resulting-find"), + ("treatment1n-030-resulting-finds", "treatment-030-1n-resulting-finds"), + ("treatmentn1-020-general", "treatment-020-n1-general"), + ("treatment1n-020-general", "treatment-020-1n-general"), + ("treatment-adminact-general", "adminact-treatment-general"), + ("treatmentfile-adminact-general", "adminact-treatmentfile-general"), + ("operation-adminact-general", "adminact-operation-general"), +] + + +def migrate_slug(apps, __): + CustomForm = apps.get_model("ishtar_common", "customform") + for old, new in FORM_SLUG: + CustomForm.objects.filter(form=old).update(form=new) + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0261_geo_reference_acquisition_date'), + ] + + operations = [ + migrations.RunPython(migrate_slug) + ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 50abded4c..9197d4839 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1728,6 +1728,26 @@ class CustomForm(models.Model): unique_together = (("name", "form"),) ADMIN_SECTION = _("Custom data / custom forms") + MODELS = { + "contextrecord": ("archaeological_context_records", "contextrecord"), + "file": ("archaeological_files", "contextrecord"), + "adminact": ("archaeological_operations", "administrativeact"), + "operation": ("archaeological_operations", "operation"), + "archaeological_site": ("archaeological_operations", "archaeologicalsite"), + "find": ("archaeological_finds", "find"), + "findbasket": ("archaeological_finds", "findbasket"), + "treatment": ("archaeological_finds", "treatment"), + "treatmentfile": ("archaeological_finds", "treatmentfile"), + "exhibition": ("archaeological_finds", "exhibition"), + "warehouse": ("archaeological_warehouse", "warehouse"), + "container": ("archaeological_warehouse", "container"), + "person": ("ishtar_common", "person"), + "organization": ("ishtar_common", "organization"), + "biographicalnote": ("ishtar_common", "biographicalnote"), + "document": ("ishtar_common", "document"), + "geoitem": ("ishtar_common", "geovectordata"), + } + def natural_key(self): return (self.name, self.form) @@ -1771,6 +1791,7 @@ class CustomForm(models.Model): if "Form" not in form and "Select" not in form: # not very clean... but do not treat inappropriate items continue + form = getattr(app_form, form) if ( not inspect.isclass(form) @@ -1794,19 +1815,28 @@ class CustomForm(models.Model): return register[self.form] def get_available_json_fields(self): - register, register_fields = self.register() - if self.form not in self._register: - return [] - current_form = register[self.form] - app_name = current_form.__module__.split(".")[0] - if app_name not in register_fields: - return [] - res = [] - for model_name in register_fields[app_name]: + model_name = self.form.split("-")[0] + content_types = [] + if model_name in self.MODELS: + app_name, model_name = self.MODELS[model_name] q = ContentType.objects.filter(app_label=app_name, model=model_name) - if not q.count(): - continue - ct = q.all()[0] + if q.count(): + content_types.append(q.all()[0]) + if not content_types: + register, register_fields = self.register() + if self.form not in self._register: + return [] + current_form = register[self.form] + app_name = current_form.__module__.split(".")[0] + if app_name not in register_fields: + return [] + for model_name in register_fields[app_name]: + q = ContentType.objects.filter(app_label=app_name, model=model_name) + if not q.count(): + continue + content_types.append(q.all()[0]) + res = [] + for ct in content_types: for json_field in JsonDataField.objects.filter(content_type=ct).all(): res.append( ( |