diff options
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 |
commit | 0cd398940a316afe8fc7ec2b0102c793c3f4342b (patch) | |
tree | fe78f8109bbae1e761e79bdd7998f639d895f640 /archaeological_operations/models.py | |
parent | f46de1b6d4cbf832ce6f22fe82a5377b5e0ed6a4 (diff) | |
download | Ishtar-0cd398940a316afe8fc7ec2b0102c793c3f4342b.tar.bz2 Ishtar-0cd398940a316afe8fc7ec2b0102c793c3f4342b.zip |
Generic manner of managing external id
Diffstat (limited to 'archaeological_operations/models.py')
-rw-r--r-- | archaeological_operations/models.py | 29 |
1 files changed, 20 insertions, 9 deletions
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() |