summaryrefslogtreecommitdiff
path: root/ishtar_common/forms.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2026-03-31 18:45:10 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2026-03-31 18:45:10 +0200
commit09264888638ba506b33f6fc08350950b4629df0a (patch)
tree26aadf9f3582344e271826f437d5c6201a85b81f /ishtar_common/forms.py
parented72a1cbbdd35f0997bec4d2cf25d4a012ea79db (diff)
downloadIshtar-develop-5.0-BPC.tar.bz2
Ishtar-develop-5.0-BPC.zip
✨ sites - datings: manage datings (forms, sheet)develop-5.0-BPC
Diffstat (limited to 'ishtar_common/forms.py')
-rw-r--r--ishtar_common/forms.py75
1 files changed, 74 insertions, 1 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py
index 9558186bb..4db6a138b 100644
--- a/ishtar_common/forms.py
+++ b/ishtar_common/forms.py
@@ -21,6 +21,7 @@
Forms definition
"""
from collections import OrderedDict
+from copy import copy
import datetime
from markdown import markdown
import re
@@ -534,6 +535,8 @@ class LockForm(object):
if pk_key not in cleaned_data or not cleaned_data[pk_key]:
raise forms.ValidationError(_("You should select an item."))
model = self.associated_models[pk_key]
+ if isinstance(model, tuple):
+ model = apps.get_model(*model)
pks = self.cleaned_data[pk_key]
pks = [pks] if isinstance(pks, int) else pks.split(",")
for pk in pks:
@@ -564,7 +567,10 @@ class MultiSearchForm(CustomFormSearch):
@classmethod
def get_current_model(cls):
- return cls.associated_models[cls.pk_key]
+ model = cls.associated_models[cls.pk_key]
+ if isinstance(model, tuple):
+ model = apps.get_model(*model)
+ return model
@classmethod
def get_formated_datas(cls, cleaned_datas):
@@ -691,6 +697,8 @@ class FieldType:
def __init__(self, key, model, is_multiple=False, extra_args=None,
empty_first=True, help_text=True):
self.key = key
+ if isinstance(model, tuple):
+ model = apps.get_model(*model)
self.model = model
self.is_multiple = is_multiple
self.extra_args = extra_args
@@ -1560,3 +1568,68 @@ class GeoItemSelect(DocumentItemSelect): # all geo item can have documents
FieldType("geodata__origin", models.GeoOriginType),
FieldType("geodata__provider", models.GeoProviderType),
] + DocumentItemSelect.TYPES
+
+
+class QADating(ManageOldType, forms.Form):
+ form_label = _("Dating")
+ associated_models = {
+ "dating_type_id": ("archaeological_context_records", "DatingType"),
+ "quality_id": ("archaeological_context_records", "DatingQuality"),
+ "period_id": ("archaeological_operations", "Period"),
+ }
+ pk = forms.IntegerField(required=False, widget=forms.HiddenInput)
+ reference = forms.CharField(
+ label=_("Reference"), validators=[validators.MaxLengthValidator(400)],
+ required=False
+ )
+ period_id = widgets.Select2SimpleField(
+ label=_("Period"), required=False, modal="modal-dynamic-form"
+ )
+ start_date = forms.IntegerField(label=_("Start date"), required=False)
+ end_date = forms.IntegerField(label=_("End date"), required=False)
+ quality_id = forms.ChoiceField(label=_("Quality"), required=False, choices=[])
+ dating_type_id = forms.ChoiceField(label=_("Dating type"), required=False, choices=[])
+ precise_dating = forms.CharField(label=_("Precise on this dating"), required=False,
+ widget=forms.Textarea)
+
+ TYPES = [
+ FieldType("dating_type_id", ("archaeological_context_records", "DatingType")),
+ FieldType("quality_id", ("archaeological_context_records", "DatingQuality")),
+ FieldType("period_id", ("archaeological_operations", "Period")),
+ ]
+
+ def __init__(self, *args, **kwargs):
+ self.dating_model = kwargs.pop("dating_model")
+ self.current_item = kwargs.pop("current_item")
+ return super().__init__(*args, **kwargs)
+
+ def clean(self):
+ data = self.cleaned_data
+ reference = data['reference']
+ if reference:
+ q_attr = {
+ "reference": reference,
+ self.dating_model.CURRENT_MODEL_ATTR + "_id": self.current_item.pk
+ }
+ q = self.dating_model.objects.filter(**q_attr)
+ if data.get("pk", None):
+ q = q.exclude(pk=data["pk"])
+ if q.count():
+ raise forms.ValidationError(
+ _("This reference already exists for this item.")
+ )
+ if any(1 for k in self.cleaned_data if self.cleaned_data[k]):
+ return data
+ raise forms.ValidationError(_("No data provided."))
+
+ def save(self):
+ data = copy(self.cleaned_data)
+ data[self.dating_model.CURRENT_MODEL_ATTR + "_id"] = self.current_item.pk
+ for attr in ['period_id', 'quality_id', 'dating_type_id']:
+ if not data.get(attr, None):
+ data[attr] = None
+ if data.get("pk", None):
+ pk = data.pop("pk")
+ self.dating_model.objects.filter(pk=pk).update(**data)
+ return
+ self.dating_model.objects.create(**data)