diff options
Diffstat (limited to 'archaeological_context_records/models.py')
-rw-r--r-- | archaeological_context_records/models.py | 123 |
1 files changed, 68 insertions, 55 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index f5df4e5ec..2f02ed9df 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -17,6 +17,8 @@ # See the file COPYING for details. +import time + from django.conf import settings from django.contrib.gis.db import models from django.core.urlresolvers import reverse @@ -24,13 +26,14 @@ from django.db import connection, transaction from django.db.models import Q from django.db.models.signals import post_delete, post_save from django.utils.translation import ugettext_lazy as _, ugettext, pgettext +from django.utils.text import slugify from ishtar_common.utils import cached_label_changed from ishtar_common.models import GeneralType, BaseHistorizedItem, \ HistoricalRecords, OwnPerms, ShortMenuItem, Source, GeneralRelationType,\ - GeneralRecordRelations, post_delete_record_relation, get_external_id, \ - ImageModel, post_save_cache, ValueGetter + GeneralRecordRelations, post_delete_record_relation, \ + ImageModel, post_save_cache, ValueGetter, BulkUpdatedItem from archaeological_operations.models import Operation, Period, Parcel @@ -74,6 +77,20 @@ class Dating(models.Model): return unicode(self.period) return u"%s (%s-%s)" % (self.period, start_date, end_date) + def context_records_lbl(self): + return u" - ".join( + [cr.cached_label for cr in self.context_records.all()] + ) + context_records_lbl.short_description = _(u"Context record") + context_records_lbl.admin_order_field = "context_records__cached_label" + + def finds_lbl(self): + return u" - ".join( + [f.cached_label for f in self.find.all()] + ) + finds_lbl.short_description = _(u"Find") + finds_lbl.admin_order_field = "find__cached_label" + @classmethod def fix_dating_association(cls, obj): """ @@ -172,10 +189,12 @@ class CRBulkView(object): """ -class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, - ValueGetter, ShortMenuItem): +class ContextRecord(BulkUpdatedItem, BaseHistorizedItem, + ImageModel, OwnPerms, ValueGetter, ShortMenuItem): SHOW_URL = 'show-contextrecord' SLUG = 'contextrecord' + EXTERNAL_ID_KEY = 'context_record_external_id' + EXTERNAL_ID_DEPENDENCIES = ['base_finds'] TABLE_COLS = ['label', 'operation__common_name', 'parcel__town__name', 'parcel__label', 'unit'] if settings.COUNTRY == 'fr': @@ -199,7 +218,6 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, 'related_context_records': 'detailled_related_context_records' } } - IMAGE_PREFIX = 'context_records/' # search parameters EXTRA_REQUEST_KEYS = { @@ -248,9 +266,8 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, _(u"Location"), blank=True, null=True, help_text=_(u"A short description of the location of the context " u"record")) - datings = models.ManyToManyField(Dating) - documentations = models.ManyToManyField(DocumentationType, blank=True, - null=True) + datings = models.ManyToManyField(Dating, related_name='context_records') + documentations = models.ManyToManyField(DocumentationType, blank=True) datings_comment = models.TextField(_(u"Comment on datings"), blank=True, null=True) unit = models.ForeignKey(Unit, verbose_name=_(u"Context record type"), @@ -281,8 +298,9 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, ExcavationTechnicType, blank=True, null=True, verbose_name=_(u"Excavation technique")) related_context_records = models.ManyToManyField( - 'ContextRecord', through='RecordRelations', blank=True, null=True) - point = models.PointField(_(u"Point"), blank=True, null=True, dim=3) + 'ContextRecord', through='RecordRelations', blank=True) + point_2d = models.PointField(_(u"Point (2D)"), blank=True, null=True) + point = models.PointField(_(u"Point (3D)"), blank=True, null=True, dim=3) polygon = models.PolygonField(_(u"Polygon"), blank=True, null=True) cached_label = models.TextField(_(u"Cached name"), null=True, blank=True) history = HistoricalRecords() @@ -291,15 +309,11 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, verbose_name = _(u"Context Record") verbose_name_plural = _(u"Context Record") permissions = ( - ("view_contextrecord", ugettext(u"Can view all Context Records")), - ("view_own_contextrecord", - ugettext(u"Can view own Context Record")), - ("add_own_contextrecord", - ugettext(u"Can add own Context Record")), - ("change_own_contextrecord", - ugettext(u"Can change own Context Record")), - ("delete_own_contextrecord", - ugettext(u"Can delete own Context Record")), + ("view_contextrecord", u"Can view all Context Records"), + ("view_own_contextrecord", u"Can view own Context Record"), + ("add_own_contextrecord", u"Can add own Context Record"), + ("change_own_contextrecord", u"Can change own Context Record"), + ("delete_own_contextrecord", u"Can delete own Context Record"), ) ordering = ('cached_label',) @@ -315,7 +329,13 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, return self.short_label @classmethod - def cached_label_bulk_update(cls, operation_id=None, parcel_id=None): + def cached_label_bulk_update(cls, operation_id=None, parcel_id=None, + transaction_id=None): + transaction_id, is_recursion = cls.bulk_recursion( + transaction_id, [operation_id, parcel_id]) + if is_recursion: + return + if operation_id: where = "operation_id = %s" args = [int(operation_id)] @@ -326,6 +346,8 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, kwargs = {'parcel_id': parcel_id} else: return + kwargs['transaction_id'] = transaction_id + sql = """ UPDATE "archaeological_context_records_contextrecord" AS cr SET cached_label = @@ -362,12 +384,10 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, """.format(main_ope_prefix=settings.ISHTAR_OPE_PREFIX, ope_prefix=settings.ISHTAR_DEF_OPE_PREFIX, join=settings.JOINT, where=where) - # with connection.cursor() as c: # django 1.8 - c = connection.cursor() - c.execute(sql, args) - transaction.commit_unless_managed() - cls._meta.get_field_by_name( - 'base_finds')[0].model.cached_label_bulk_update(**kwargs) + with connection.cursor() as c: + c.execute(sql, args) + cls._meta.get_field( + 'base_finds').related_model.cached_label_bulk_update(**kwargs) @property def short_label(self): @@ -379,11 +399,11 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, return reverse('show-contextrecord', args=[self.pk, '']) @classmethod - def get_query_owns(cls, user): - return (Q(operation__scientist=user.ishtaruser.person) | - Q(operation__in_charge=user.ishtaruser.person) | - Q(operation__collaborators__pk=user.ishtaruser.person.pk) | - Q(history_creator=user)) \ + def get_query_owns(cls, ishtaruser): + return (Q(operation__scientist=ishtaruser.person) | + Q(operation__in_charge=ishtaruser.person) | + Q(operation__collaborators__pk=ishtaruser.person.pk) | + Q(history_creator=ishtaruser.user_ptr)) \ & Q(operation__end_date__isnull=True) @classmethod @@ -429,6 +449,13 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, context_record_id=self.pk) return True + def _get_base_image_path(self): + ope = self.operation + return u"operation/{}/{}/{}/{}".format( + ope.year, ope.reference, self.SLUG, + slugify(self.label or u"00") + ) + @property def reference(self): if not self.operation: @@ -493,20 +520,6 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, from archaeological_finds.models import FindSource return FindSource.objects.filter(find__base_finds__context_record=self) - def save(self, *args, **kwargs): - returned = super(ContextRecord, self).save(*args, **kwargs) - updated = False - if not self.external_id or self.auto_external_id: - external_id = get_external_id('context_record_external_id', self) - if external_id != self.external_id: - updated = True - self.auto_external_id = True - self.external_id = external_id - if updated: - self._cached_label_checked = False - self.save() - return returned - def fix(self): """ Fix redundant m2m dating association (usually after imports) @@ -518,10 +531,6 @@ post_save.connect(cached_label_changed, sender=ContextRecord) class RelationType(GeneralRelationType): - inverse_relation = models.ForeignKey( - 'RelationType', verbose_name=_(u"Inverse relation"), blank=True, - null=True) - class Meta: verbose_name = _(u"Relation type") verbose_name_plural = _(u"Relation types") @@ -567,7 +576,7 @@ post_delete.connect(post_delete_record_relation, sender=RecordRelations) class RecordRelationView(models.Model): - """ + CREATE_SQL = """ CREATE VIEW record_relations AS SELECT DISTINCT right_record_id as id, right_record_id, @@ -579,6 +588,9 @@ class RecordRelationView(models.Model): CREATE RULE record_relations_del AS ON DELETE TO record_relations DO INSTEAD DELETE FROM record_relations where id=NULL; """ + DELETE_SQL = """ + DROP VIEW record_relations; + """ TABLE_COLS = [ "relation_type", "right_record__label", "right_record__unit", "right_record__parcel", @@ -648,15 +660,15 @@ class ContextRecordSource(Source): verbose_name_plural = _(u"Context record documentations") permissions = ( ("view_contextrecordsource", - ugettext(u"Can view all Context record sources")), + u"Can view all Context record sources"), ("view_own_contextrecordsource", - ugettext(u"Can view own Context record source")), + u"Can view own Context record source"), ("add_own_contextrecordsource", - ugettext(u"Can add own Context record source")), + u"Can add own Context record source"), ("change_own_contextrecordsource", - ugettext(u"Can change own Context record source")), + u"Can change own Context record source"), ("delete_own_contextrecordsource", - ugettext(u"Can delete own Context record source")), + u"Can delete own Context record source"), ) context_record = models.ForeignKey( ContextRecord, verbose_name=_(u"Context record"), @@ -674,3 +686,4 @@ class ContextRecordSource(Source): Q(context_record__operation__collaborators__pk= user.ishtaruser.person.pk)) \ & Q(context_record__operation__end_date__isnull=True) + |