summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2017-01-23 13:30:02 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2017-01-23 13:30:02 +0100
commitba337429e0f062195c23cf12950826ddf8eaf18a (patch)
treed9670b2cfda751566edb6cde81aedf81eb9fa6e8
parentf02eaa8d0ab5a381b64b7108dc9f4a7c3ffdbcef (diff)
downloadIshtar-ba337429e0f062195c23cf12950826ddf8eaf18a.tar.bz2
Ishtar-ba337429e0f062195c23cf12950826ddf8eaf18a.zip
Parcels: on associated operation or file detachment remove the parcel
-rw-r--r--archaeological_files/tests.py31
-rw-r--r--archaeological_operations/models.py29
-rw-r--r--archaeological_operations/tests.py26
-rw-r--r--ishtar_common/wizards.py13
4 files changed, 81 insertions, 18 deletions
diff --git a/archaeological_files/tests.py b/archaeological_files/tests.py
index f3419013a..8a4c105c6 100644
--- a/archaeological_files/tests.py
+++ b/archaeological_files/tests.py
@@ -44,9 +44,9 @@ class FileInit(object):
self.user.set_password('tralala')
self.user.save()
self.o_user, created = User.objects.get_or_create(username='ousername')
- person_type = PersonType(label=u'Test person type',
- txt_idx='test_person', available=True)
- person_type.save()
+ person_type, created = PersonType.objects.get_or_create(
+ label=u'Test ' u'person type', txt_idx='test_person',
+ available=True)
self.extra_models['person_type'] = person_type
self.model_list.append(person_type)
@@ -56,9 +56,8 @@ class FileInit(object):
self.extra_models['person'] = person
self.model_list.append(person)
- file_type = models.FileType(label=u'Test file type',
- txt_idx='test_file', available=True)
- file_type.save()
+ file_type, created = models.FileType.objects.get_or_create(
+ label=u'Test file type', txt_idx='test_file', available=True)
self.extra_models['file_type'] = file_type
self.model_list.append(file_type)
@@ -205,6 +204,26 @@ class FileTest(TestCase, FileInit):
self.assertTrue(data['records'] == 1)
self.assertEqual(data['rows'][0]['internal_reference'], initial_ref)
+ def testPostDeleteParcels(self):
+ fle = self.item
+ town = Town.objects.create(name='plouf', numero_insee='20000')
+ parcel = Parcel.objects.create(town=town)
+ parcel_nb = Parcel.objects.count()
+ fle.parcels.add(parcel)
+ fle.delete()
+ # our parcel has no operation attached and should be deleted
+ self.assertEqual(parcel_nb - 1, Parcel.objects.count())
+
+ self.create_file()
+ fle = self.item
+ parcel = Parcel.objects.create(town=town)
+ parcel_nb = Parcel.objects.count()
+ fle.parcels.add(parcel)
+ fle.parcels.clear() # no signal raised... should resave
+ Parcel.objects.filter(pk=parcel.pk).all()[0].save()
+ # our parcel has no operation attached and should be deleted
+ self.assertEqual(parcel_nb - 1, Parcel.objects.count())
+
# class ImporterTest(TestCase):
# def testFormaters(self):
# from archaeological_files import data_importer
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index c8c291752..4a900c276 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -557,14 +557,6 @@ class Operation(ClosedItem, BaseHistorizedItem, ImageModel, OwnPerms,
def is_active(self):
return not bool(self.end_date)
- def save(self, *args, **kwargs):
- # put a default year if start_date is defined
- if self.start_date and not self.year:
- self.year = self.start_date.year
- if self.operation_code is None:
- self.operation_code = self.get_available_operation_code(self.year)
- return super(Operation, self).save(*args, **kwargs)
-
@property
def nb_parcels(self):
_(u"Number of parcels")
@@ -767,6 +759,14 @@ class Operation(ClosedItem, BaseHistorizedItem, ImageModel, OwnPerms,
res['mode'] = u" ; ".join([str(m) for m in mode(finds)])
return res
+ def save(self, *args, **kwargs):
+ # put a default year if start_date is defined
+ if self.start_date and not self.year:
+ self.year = self.start_date.year
+ if self.operation_code is None:
+ self.operation_code = self.get_available_operation_code(self.year)
+ return super(Operation, self).save(*args, **kwargs)
+
m2m_changed.connect(cached_label_changed, sender=Operation.towns.through)
@@ -1193,10 +1193,10 @@ class Parcel(LightHistorizedItem):
associated_file = models.ForeignKey(
'archaeological_files.File',
related_name='parcels', verbose_name=_(u"File"),
- blank=True, null=True)
+ blank=True, null=True, on_delete=models.CASCADE)
operation = models.ForeignKey(
Operation, related_name='parcels', blank=True, null=True,
- verbose_name=_(u"Operation"))
+ verbose_name=_(u"Operation"), on_delete=models.CASCADE)
year = models.IntegerField(_(u"Year"), blank=True, null=True)
town = models.ForeignKey(Town, related_name='parcels',
verbose_name=_(u"Town"))
@@ -1356,6 +1356,13 @@ def parcel_post_save(sender, **kwargs):
if not kwargs['instance']:
return
parcel = kwargs['instance']
+ created = kwargs.get('created', None)
+
+ # remove when the parcel is linked to nothing
+ if not getattr(parcel, '_updated_id', None) and not created and not \
+ parcel.operation and not parcel.associated_file:
+ parcel.delete()
+ return
updated = False
if not parcel.external_id or parcel.auto_external_id:
@@ -1365,6 +1372,7 @@ def parcel_post_save(sender, **kwargs):
parcel.auto_external_id = True
parcel.external_id = external_id
if updated:
+ parcel._updated_id = True
parcel.save()
return
@@ -1378,6 +1386,7 @@ def parcel_post_save(sender, **kwargs):
if parcel.operation and parcel.associated_file:
# parcels are copied between files and operations
parcel.copy_to_operation()
+
post_save.connect(parcel_post_save, sender=Parcel)
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py
index 86eb2dfe7..33199dd04 100644
--- a/archaeological_operations/tests.py
+++ b/archaeological_operations/tests.py
@@ -35,7 +35,7 @@ import models
from archaeological_operations import views
from ishtar_common.models import OrganizationType, Organization, \
- ImporterType, IshtarUser, TargetKey, IshtarSiteProfile
+ ImporterType, IshtarUser, TargetKey, IshtarSiteProfile, Town
from ishtar_common import forms_common
from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \
@@ -603,10 +603,28 @@ class OperationTest(TestCase, OperationInitTest):
response = c.get(reverse('get-operation'), search)
self.assertTrue(json.loads(response.content)['total'] == 2)
- def testRelations(self):
+ def testPostDeleteRelations(self):
self.create_relations()
self.operations[0].delete()
+ def testPostDeleteParcels(self):
+ ope = self.operations[0]
+ town = Town.objects.create(name='plouf', numero_insee='20000')
+ parcel = models.Parcel.objects.create(town=town)
+ parcel_nb = models.Parcel.objects.count()
+ ope.parcels.add(parcel)
+ ope.delete()
+ # our parcel has no operation attached and should be deleted
+ self.assertEqual(parcel_nb - 1, models.Parcel.objects.count())
+ ope = self.operations[1]
+ parcel = models.Parcel.objects.create(town=town)
+ parcel_nb = models.Parcel.objects.count()
+ 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())
+
def testOwnSearch(self):
c = Client()
response = c.get(reverse('get-operation'), {'year': '2010'})
@@ -726,6 +744,8 @@ class OperationWizardDeleteTest(OperationWizardCreationTest):
def pre_wizard(self):
self.ope = self.get_default_operation(force=True)
+ self.ope.parcels.add(self.create_parcel()[0])
+ self.parcel_nb = models.Parcel.objects.count()
self.form_datas[0].form_datas['selec-operation_deletion']['pk'] = \
self.ope.pk
self.operation_number = models.Operation.objects.count()
@@ -734,6 +754,8 @@ class OperationWizardDeleteTest(OperationWizardCreationTest):
def post_wizard(self):
self.assertEqual(self.operation_number - 1,
models.Operation.objects.count())
+ # associated parcel removed
+ self.assertEqual(self.parcel_nb - 1, models.Parcel.objects.count())
class OperationWizardClosingTest(OperationWizardCreationTest):
diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py
index 18336cff5..735ad62fd 100644
--- a/ishtar_common/wizards.py
+++ b/ishtar_common/wizards.py
@@ -31,6 +31,7 @@ from django.core.files.storage import default_storage
from django.core.mail import send_mail
from django.db.models.fields.files import FileField
from django.db.models.fields.related import ManyToManyField
+
from django.http import HttpResponseRedirect
from django.forms import ValidationError
from django.shortcuts import render_to_response, redirect
@@ -679,6 +680,18 @@ class Wizard(NamedUrlWizardView):
# material_index management for baseitems
obj._cached_label_checked = False
obj.save()
+
+ # force post_save for old related m2ms (which can have been detached
+ # from the current object)
+ for model in old_m2ms:
+ for item in old_m2ms[model]:
+ # verify it hasn't been deleted
+ q = item.__class__.objects.filter(pk=item.pk)
+ if q.count():
+ item = q.all()[0]
+ item.skip_history_when_saving = True
+ item.save()
+
# make the new object a default
if self.current_obj_slug:
self.request.session[self.current_obj_slug] = unicode(obj.pk)