summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-08-01 17:18:33 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2018-08-13 18:26:03 +0200
commitc6b17aa8d058fe581539bf722b6c5cc2c6711fb0 (patch)
treecf9e060d38c62d422684f0de85239822eafed2df
parentb6afe4668dcaa05df44614d8d362943eb660967c (diff)
downloadIshtar-c6b17aa8d058fe581539bf722b6c5cc2c6711fb0.tar.bz2
Ishtar-c6b17aa8d058fe581539bf722b6c5cc2c6711fb0.zip
Fix clean parcel script
-rw-r--r--archaeological_operations/models.py31
-rw-r--r--archaeological_operations/tests.py6
-rw-r--r--ishtar_common/management/commands/clean_ishtar.py3
3 files changed, 18 insertions, 22 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index 1a534af7a..c9244ca48 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -1566,6 +1566,20 @@ class Parcel(LightHistorizedItem):
self.operation = None
self.save()
+ def clean_orphan(self):
+ """
+ Remove when the parcel is linked to nothing
+ """
+ if self.operation or self.associated_file:
+ return
+ if self.context_record.count():
+ # trying to restore a lost parcel
+ self.operation = self.context_record.all()[0].operation
+ self.skip_history_when_saving = True
+ self.save()
+ elif self.id:
+ self.delete()
+
def parcel_post_save(sender, **kwargs):
if not kwargs['instance']:
@@ -1576,23 +1590,6 @@ def parcel_post_save(sender, **kwargs):
updated = False
- """
- # remove when the parcel is linked to nothing
- # problematic in wizards
- # TODO: add admin action
- if not getattr(parcel, '_updated_id', None) and not created \
- and not parcel.operation and not parcel.associated_file:
- if parcel.context_record.count():
- # trying to restore a lost parcel
- parcel.operation = parcel.context_record.all()[0].operation
- updated = True
- elif parcel.id:
- parcel.delete()
- return
- else:
- return
- """
-
if updated:
parcel.save()
return
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py
index 38f832cf8..b4c7bc617 100644
--- a/archaeological_operations/tests.py
+++ b/archaeological_operations/tests.py
@@ -1014,8 +1014,8 @@ class OperationTest(TestCase, OperationInitTest):
ope.parcels.add(parcel)
ope.parcels.clear() # no signal raised... should resave
models.Parcel.objects.filter(pk=parcel.pk).all()[0].save()
- # our parcel has no operation attached and should be deleted
- self.assertEqual(parcel_nb - 1, models.Parcel.objects.count())
+ # parcel is no longer automatically delete on detach
+ self.assertEqual(parcel_nb, models.Parcel.objects.count())
def testIndex(self):
ope = create_operation(self.user, values={'year': 2042})
@@ -2228,7 +2228,7 @@ class OperationWizardModifTest(WizardTest, OperationInitTest, TestCase):
# the init parcel is not detached from the operation
test_object.assertEqual(operation.parcels.count(),
test_object.parcel_number + 1)
- # update teh external id on update
+ # update the external id on update
cr = ContextRecord.objects.get(pk=self.cr.pk)
test_object.assertEqual(cr.external_id,
"codeope42-12345-A1-Context record")
diff --git a/ishtar_common/management/commands/clean_ishtar.py b/ishtar_common/management/commands/clean_ishtar.py
index d3da40fd8..3117022ee 100644
--- a/ishtar_common/management/commands/clean_ishtar.py
+++ b/ishtar_common/management/commands/clean_ishtar.py
@@ -30,6 +30,5 @@ class Command(BaseCommand):
def handle(self, *args, **options):
for parcel in Parcel.objects.all():
- parcel.skip_history_when_saving = True
- parcel.save()
+ parcel.clean_orphan()
self.stdout.write('Parcel cleaned.\n')