summaryrefslogtreecommitdiff
path: root/archaeological_warehouse
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_warehouse')
-rw-r--r--archaeological_warehouse/forms.py55
-rw-r--r--archaeological_warehouse/tests.py70
2 files changed, 108 insertions, 17 deletions
diff --git a/archaeological_warehouse/forms.py b/archaeological_warehouse/forms.py
index 07c10ba91..3acc24282 100644
--- a/archaeological_warehouse/forms.py
+++ b/archaeological_warehouse/forms.py
@@ -46,6 +46,7 @@ from archaeological_finds.models import (
ConservatoryState,
Find,
FindBasket,
+ FindTreatment,
IntegrityType,
MaterialType,
ObjectType,
@@ -859,21 +860,33 @@ class QAContainerMoveForm(QABasePackagingForm):
def save(self, items, user):
location_id = self.cleaned_data.get("qalocation", None)
parent_id = self.cleaned_data.get("qaparent", None)
+
+ changed = []
for container in self.items:
- changed = False
+ # move the container
if parent_id and parent_id != container.parent_id:
container.parent_id = parent_id
- changed = True
+ models.Container.objects.filter(id=container.id).update(
+ parent_id=parent_id
+ )
+ changed.append(container)
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()
+ models.Container.objects.filter(id=container.id).update(
+ location_id=location_id
+ )
+ if container not in changed:
+ # remove parent if do not share the same location
+ if container.parent \
+ and container.parent.location != container.location:
+ container.parent = None
+ models.Container.objects.filter(id=container.id).update(
+ parent=None
+ )
+ changed.append(container)
if not self.cleaned_data.get("create_treatment", False):
+ for container in changed:
+ container.save()
return
treat_type = TreatmentType.objects.get(pk=self.cleaned_data['treatment_type'])
treat_input_status = TreatmentInputStatus.get_validated_state()
@@ -902,8 +915,30 @@ class QAContainerMoveForm(QABasePackagingForm):
q = Find.objects.filter(Q(container=container) | Q(container_ref=container))
if not q.count():
continue
+ loca = container.generate_full_location()
for find in q.all():
- t.finds.add(find)
+ if find.update_current_full_location():
+ Find.objects.filter(pk=find.id).update(
+ container_fisrt_full_location=find.container_fisrt_full_location
+ )
+ if find.update_ref_full_location():
+ Find.objects.filter(pk=find.id).update(
+ container_ref_fisrt_full_location=find.container_ref_fisrt_full_location
+ )
+ location_type = "C"
+ if find.container_ref == container:
+ if find.container == container:
+ location_type = "B"
+ else:
+ location_type = "R"
+ FindTreatment.objects.create(
+ find=find, treatment=t,
+ location_type=location_type,
+ full_location=loca
+ )
t.save() # force find container history
+ for container in changed: # container post treatment after save
+ container.save()
+
diff --git a/archaeological_warehouse/tests.py b/archaeological_warehouse/tests.py
index 0b677475d..20037909d 100644
--- a/archaeological_warehouse/tests.py
+++ b/archaeological_warehouse/tests.py
@@ -631,9 +631,12 @@ class ContainerQATest(FindInit, TestCase):
def test_move(self):
c = Client()
- find_0 = self.finds[0]
+ find_0 = Find.objects.get(pk=self.finds[0].pk)
find_0.container = self.container_3
+ find_0.container_ref = self.container_3
find_0.save()
+ find = Find.objects.get(pk=find_0.pk)
+ self.assertTrue(find.container_fisrt_full_location)
pks_1 = f"{self.container_3.pk}-{self.container_4.pk}"
pks_2 = str(self.container_1.pk)
@@ -656,18 +659,33 @@ class ContainerQATest(FindInit, TestCase):
packaging = TreatmentType.objects.get(txt_idx="packaging")
- def check_location_of_children():
+ def check_location_of_children(idx):
# check change location of children
container3 = models.Container.objects.get(pk=self.container_3.pk)
self.assertEqual(container3.location, self.alt_warehouse)
container4 = models.Container.objects.get(pk=self.container_4.pk)
self.assertEqual(container4.location, self.alt_warehouse)
- def check_reinit_parent():
+ def check_reinit_parent(idx):
# reinit parent when not provided and location changed
container = models.Container.objects.get(pk=self.container_3.pk)
self.assertEqual(container.parent, None)
+ def init_treatment(find_0):
+ # create a first packaging treatment
+ treat_state = TreatmentState.get_completed_state()
+ t = Treatment.objects.create(
+ container=self.container_3,
+ year=2024,
+ start_date=datetime.date(2024, 2, 29),
+ location=self.main_warehouse,
+ treatment_state=treat_state
+ )
+ treat_type = TreatmentType.objects.get(txt_idx="packaging")
+ t.treatment_types.add(treat_type)
+ t.save()
+ t.finds.add(find_0)
+ t.save() # force find container history
data_check_lst = [
(
@@ -677,6 +695,7 @@ class ContainerQATest(FindInit, TestCase):
{"parent": self.container_2},
0,
pks_1,
+ None,
None
),
(
@@ -686,6 +705,7 @@ class ContainerQATest(FindInit, TestCase):
{"location": self.alt_warehouse},
0,
pks_1,
+ None,
None
),
(
@@ -698,10 +718,24 @@ class ContainerQATest(FindInit, TestCase):
{"parent": self.container_2},
2,
pks_1,
+ None,
None
),
(
{
+ "qa-move-qaparent": self.container_2.pk,
+ "qa-move-create_treatment": True,
+ "qa-move-year": 2019,
+ "qa-move-treatment_type": packaging.pk,
+ },
+ {"parent": self.container_2},
+ 2,
+ pks_1,
+ None,
+ init_treatment
+ ),
+ (
+ {
"qa-move-qalocation": self.alt_warehouse.pk,
"qa-move-create_treatment": True,
"qa-move-year": 2019,
@@ -710,6 +744,7 @@ class ContainerQATest(FindInit, TestCase):
{"location": self.alt_warehouse},
2,
pks_1,
+ None,
None
),
(
@@ -719,7 +754,8 @@ class ContainerQATest(FindInit, TestCase):
{"location": self.alt_warehouse},
0,
pks_2,
- check_location_of_children
+ check_location_of_children,
+ None
),
(
{
@@ -728,12 +764,13 @@ class ContainerQATest(FindInit, TestCase):
{"location": self.alt_warehouse},
0,
pks_1,
- check_reinit_parent
+ check_reinit_parent,
+ None
),
]
for idx, lst in enumerate(data_check_lst):
- data, check, nb_treat, pks, extra_check = lst
+ data, check, nb_treat, pks, extra_check, pre_init = lst
# reinit
self.container_1.location = self.main_warehouse
self.container_1.parent = None
@@ -744,6 +781,12 @@ class ContainerQATest(FindInit, TestCase):
self.container_4.location = self.main_warehouse
self.container_4.parent = self.container_1
self.container_4.save()
+ find_0.save()
+ FindTreatment.objects.exclude(id__isnull=True).delete()
+ Treatment.objects.exclude(id__isnull=True).delete()
+
+ if pre_init:
+ pre_init(find_0)
init_nb_treat = Treatment.objects.count()
@@ -758,7 +801,7 @@ class ContainerQATest(FindInit, TestCase):
self.assertEqual(init_nb_treat + nb_treat, final_nb_treat)
if extra_check:
- extra_check()
+ extra_check(idx)
if not final_nb_treat:
self.assertEqual(FindTreatment.objects.filter(find=find_0).count(), 0)
@@ -774,6 +817,19 @@ class ContainerQATest(FindInit, TestCase):
self.assertTrue(ft.full_location)
self.assertTrue(ft.location_type)
+ find = Find.objects.get(pk=find_0.pk)
+ if pre_init:
+ # pre init add a packaging -> first full location is not changed
+ self.assertNotEqual(
+ find.container_fisrt_full_location,
+ find.container.generate_full_location()
+ )
+ else:
+ self.assertEqual(
+ find.container_fisrt_full_location,
+ find.container.generate_full_location()
+ )
+
class ContainerTest(FindInit, TestCase):
fixtures = WAREHOUSE_FIXTURES