diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-09-03 18:40:27 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-09-05 16:09:40 +0200 |
commit | 69dd532066585cad66e202e650f2c174808dc11f (patch) | |
tree | db6ac956ef1044af25d6a9b49ba1f08a2eb0c41e /archaeological_warehouse/forms.py | |
parent | 1720a9a24b4d53cd4d5981f1b847bd3642fa6fae (diff) | |
download | Ishtar-69dd532066585cad66e202e650f2c174808dc11f.tar.bz2 Ishtar-69dd532066585cad66e202e650f2c174808dc11f.zip |
✨ QA container form: move form
Diffstat (limited to 'archaeological_warehouse/forms.py')
-rw-r--r-- | archaeological_warehouse/forms.py | 123 |
1 files changed, 84 insertions, 39 deletions
diff --git a/archaeological_warehouse/forms.py b/archaeological_warehouse/forms.py index 963042346..f2605ba12 100644 --- a/archaeological_warehouse/forms.py +++ b/archaeological_warehouse/forms.py @@ -22,10 +22,11 @@ import datetime from bootstrap_datepicker.widgets import DateField from django import forms -from django.db.models import Max +from django.db.models import Max, Q from django.conf import settings from django.core.validators import validate_slug from django.forms.formsets import formset_factory + from ishtar_common.utils import ugettext_lazy as _ from ishtar_common.models import ( @@ -35,21 +36,23 @@ from ishtar_common.models import ( SpatialReferenceSystem, Organization, valid_ids, - person_type_pks_lazy, ) from archaeological_operations.models import ArchaeologicalSite from archaeological_context_records.models import ContextRecord from archaeological_finds.models import ( - TreatmentType, + AlterationType, + AlterationCauseType, + ConservatoryState, + Find, FindBasket, + IntegrityType, MaterialType, ObjectType, - IntegrityType, RemarkabilityType, - ConservatoryState, - AlterationType, - AlterationCauseType, TreatmentEmergencyType, + Treatment, + TreatmentState, + TreatmentType, ) from . import models @@ -80,6 +83,7 @@ from ishtar_common.forms_common import ( MergeIntoForm, ) from archaeological_finds.forms import FindMultipleFormSelection, SelectFindBasketForm +from archaeological_finds.forms_treatments import QABasePackagingForm def get_warehouse_field(label=_("Warehouse"), required=True): @@ -719,35 +723,50 @@ class QAContainerFormMulti(QAForm): PREFIX = "qa" form_admin_name = _("Container - Quick action - Modify") form_slug = "container-quickaction-modify" - base_models = ["qaparent", "qacontainer_type", "qalocation", "qaresponsibility"] + base_models = ["qacontainer_type", "qaresponsibility"] associated_models = { - "qaparent": models.Container, "qacontainer_type": models.ContainerType, - "qalocation": models.Warehouse, "qaresponsibility": models.Warehouse, } MULTI = True - REPLACE_FIELDS = ["qaparent", "qacontainer_type", "qalocation", "qaresponsibility"] + REPLACE_FIELDS = ["qacontainer_type", "qaresponsibility"] HEADERS = { - "qalocation": FormHeader(_("Warehouse")), + "qaresponsibility": FormHeader(_("Warehouse")), } SINGLE_FIELDS = [] qacontainer_type = forms.ChoiceField( label=_("Container type"), required=False, choices=[] ) - qalocation = forms.IntegerField( - label=_("Location"), + qaresponsibility = forms.IntegerField( + label=_("Responsibility"), widget=widgets.JQueryAutoComplete( reverse_lazy("autocomplete-warehouse"), associated_model=models.Warehouse ), validators=[valid_id(models.Warehouse)], required=False, ) - qaresponsibility = forms.IntegerField( - label=_("Responsibility"), + + TYPES = [ + FieldType("qacontainer_type", models.ContainerType), + ] + + def __init__(self, *args, **kwargs): + self.items = kwargs["items"] + super().__init__(*args, **kwargs) + + def _get_qaresponsibility(self, value): + try: + return models.Warehouse.objects.get(pk=value).name + except models.Warehouse.DoesNotExist: + return "" + + +class QAContainerMoveForm(QABasePackagingForm): + qalocation = forms.IntegerField( + label=_("Location"), widget=widgets.JQueryAutoComplete( reverse_lazy("autocomplete-warehouse"), associated_model=models.Warehouse ), @@ -765,13 +784,9 @@ class QAContainerFormMulti(QAForm): required=False, ) - TYPES = [ - FieldType("qacontainer_type", models.ContainerType), - ] - def __init__(self, *args, **kwargs): self.items = kwargs["items"] - super(QAContainerFormMulti, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) locations = {item.location_id for item in self.items} if len(locations) == 1 and "qalocation" in self.fields: self.fields["qalocation"].initial = locations.pop() @@ -782,12 +797,6 @@ class QAContainerFormMulti(QAForm): except models.Warehouse.DoesNotExist: return "" - def _get_qaresponsibility(self, value): - try: - return models.Warehouse.objects.get(pk=value).name - except models.Warehouse.DoesNotExist: - return "" - def _get_qaparent(self, value): try: return models.Container.objects.get(pk=value).cached_label @@ -796,8 +805,6 @@ class QAContainerFormMulti(QAForm): def clean(self): new_values = {} - if self.cleaned_data.get("qacontainer_type", None): - new_values["container_type_id"] = self.cleaned_data["qacontainer_type"] if self.cleaned_data.get("qalocation", None): new_values["location_id"] = self.cleaned_data["qalocation"] if self.cleaned_data.get("qaparent", None): @@ -805,8 +812,8 @@ class QAContainerFormMulti(QAForm): new_tuples = [] for item in self.items: if ( - new_values.get("parent_id", None) - and int(new_values["parent_id"]) == item.pk + new_values.get("parent_id", None) + and int(new_values["parent_id"]) == item.pk ): raise forms.ValidationError( _("A container cannot be a parent of himself.") @@ -846,12 +853,50 @@ class QAContainerFormMulti(QAForm): return self.cleaned_data def save(self, items, user): - super(QAContainerFormMulti, self).save(items, user) - if self.cleaned_data.get("qaparent", None): + location_id = self.cleaned_data.get("qalocation", None) + parent_id = self.cleaned_data.get("qaparent", None) + for container in self.items: + changed = False + if parent_id and parent_id != container.parent_id: + container.parent_id = parent_id + changed = True + if location_id and location_id != container.location_id: + container.location_id = location_id + # remove parent if do not share the same location + if not changed and container.parent \ + and container.parent.location != container.location: + container.parent = None + changed = True + if changed: + container.save() + if not self.cleaned_data.get("create_treatment", False): return - for item in items: - item = models.Container.objects.get(pk=item.pk) - # remove parent if do not share the same location - if item.parent and item.parent.location != item.location: - item.parent = None - item.save() + treat_type = TreatmentType.objects.get(pk=self.cleaned_data['treatment_type']) + treat_state = TreatmentState.get_completed_state() + + try: + location = models.Warehouse.objects.get(pk=self.cleaned_data.get("qalocation", None)) + except models.Warehouse.DoesNotExist: + location = None + + for container in self.items: + t = Treatment.objects.create( + container=container, + year=self.cleaned_data['year'], + start_date=self.cleaned_data['start_date'], + location=location, + person_id=self.cleaned_data['person'], + organization_id=self.cleaned_data['organization'], + history_modifier=user, + treatment_state=treat_state + ) + t.treatment_types.add(treat_type) + t.save() + q = Find.objects.filter(Q(container=container) | Q(container_ref=container)) + if not q.count(): + continue + for find in q.all(): + t.finds.add(find) + t.save() # force find container history + + |