summaryrefslogtreecommitdiff
path: root/archaeological_operations
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2017-09-21 00:26:03 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2017-09-21 00:26:03 +0200
commit5957ad8979558c67a357a554201a1ecb4c428606 (patch)
treefe78f8109bbae1e761e79bdd7998f639d895f640 /archaeological_operations
parent03cb63b9b6c5d0d2a63088528da24f187457f4a1 (diff)
downloadIshtar-5957ad8979558c67a357a554201a1ecb4c428606.tar.bz2
Ishtar-5957ad8979558c67a357a554201a1ecb4c428606.zip
Generic manner of managing external id
Diffstat (limited to 'archaeological_operations')
-rw-r--r--archaeological_operations/forms.py4
-rw-r--r--archaeological_operations/models.py29
2 files changed, 22 insertions, 11 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py
index 24b0a70be..651cd740f 100644
--- a/archaeological_operations/forms.py
+++ b/archaeological_operations/forms.py
@@ -260,8 +260,8 @@ class ParcelFormSet(FormSet):
if not value:
continue
try:
- parcel = models.Parcel.objects.get(pk=value)
- except models.Parcel.DoesNotExist:
+ parcel = models.Parcel.objects.get(pk=int(value))
+ except (models.Parcel.DoesNotExist, ValueError):
continue
ordering_keys[number] = [
parcel.public_domain, parcel.town, parcel.year,
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index 44f7a529e..54ed96cec 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -24,6 +24,7 @@ from django.conf import settings
from django.contrib.gis.db import models
from django.core.cache import cache
from django.core.urlresolvers import reverse
+from django.db import IntegrityError, transaction
from django.db.models import Q, Count, Sum, Max, Avg
from django.db.models.signals import post_save, m2m_changed, post_delete
from django.forms import ValidationError
@@ -37,7 +38,7 @@ from ishtar_common.models import GeneralType, BaseHistorizedItem, \
SourceType, Person, Organization, Town, Dashboard, IshtarUser, ValueGetter,\
DocumentTemplate, ShortMenuItem, DashboardFormItem, GeneralRelationType,\
GeneralRecordRelations, post_delete_record_relation, OperationType, \
- get_external_id, ImageModel, post_save_cache, ExternalIdManager
+ ImageModel, post_save_cache
class RemainType(GeneralType):
@@ -818,10 +819,10 @@ def operation_post_save(sender, **kwargs):
# external id update
for parcel in operation.parcels.all():
- parcel.update_external_id()
+ parcel.update_external_id(save=True)
for cr in operation.context_record.all():
- cr.update_external_id()
+ cr.update_external_id(save=True)
post_save.connect(operation_post_save, sender=Operation)
@@ -1255,7 +1256,7 @@ def strip_zero(value):
return value
-class Parcel(ExternalIdManager, LightHistorizedItem):
+class Parcel(LightHistorizedItem):
EXTERNAL_ID_KEY = 'parcel_external_id'
associated_file = models.ForeignKey(
@@ -1452,12 +1453,22 @@ def parcel_post_save(sender, **kwargs):
parcel_id=parcel.id)
if parcel.operation and parcel.operation.pk and \
- parcel.town not in list(parcel.operation.towns.all()):
- parcel.operation.towns.add(parcel.town)
+ parcel.town not in list(parcel.operation.towns.all()):
+ try:
+ # multiple save can cause multiple add
+ with transaction.atomic():
+ parcel.operation.towns.add(parcel.town)
+ except IntegrityError:
+ pass
if parcel.associated_file and \
- parcel.associated_file.pk and \
- parcel.town not in list(parcel.associated_file.towns.all()):
- parcel.associated_file.towns.add(parcel.town)
+ parcel.associated_file.pk and \
+ parcel.town not in list(parcel.associated_file.towns.all()):
+ try:
+ # multiple save can cause multiple add
+ with transaction.atomic():
+ parcel.associated_file.towns.add(parcel.town)
+ except IntegrityError:
+ pass
if parcel.operation and parcel.associated_file:
# parcels are copied between files and operations
parcel.copy_to_operation()