summaryrefslogtreecommitdiff
path: root/archaeological_context_records/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_context_records/models.py')
-rw-r--r--archaeological_context_records/models.py68
1 files changed, 37 insertions, 31 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py
index 940330d86..483d77883 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
@@ -30,7 +32,7 @@ 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
+ ImageModel, post_save_cache, ValueGetter, BulkUpdatedItem
from archaeological_operations.models import Operation, Period, Parcel
@@ -158,7 +160,7 @@ class CRBulkView(object):
"""
-class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms,
+class ContextRecord(BulkUpdatedItem, BaseHistorizedItem, ImageModel, OwnPerms,
ValueGetter, ShortMenuItem):
SHOW_URL = 'show-contextrecord'
SLUG = 'contextrecord'
@@ -235,8 +237,7 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms,
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)
+ 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"),
@@ -267,7 +268,7 @@ 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)
+ 'ContextRecord', through='RecordRelations', blank=True)
point = models.PointField(_(u"Point"), 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)
@@ -277,15 +278,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',)
@@ -301,7 +298,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)]
@@ -312,6 +315,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 =
@@ -348,12 +353,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()
+ with connection.cursor() as c:
+ c.execute(sql, args)
cls._meta.get_field_by_name(
- 'base_finds')[0].model.cached_label_bulk_update(**kwargs)
+ 'base_finds')[0].related_model.cached_label_bulk_update(**kwargs)
@property
def short_label(self):
@@ -365,11 +368,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
@@ -547,7 +550,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,
@@ -559,6 +562,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",
@@ -628,15 +634,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"),