diff options
Diffstat (limited to 'archaeological_context_records/models.py')
| -rw-r--r-- | archaeological_context_records/models.py | 85 | 
1 files changed, 81 insertions, 4 deletions
| diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 377cea087..1ec32ea83 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -20,6 +20,7 @@  from django.conf import settings  from django.contrib.gis.db import models  from django.core.urlresolvers import reverse +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 @@ -76,8 +77,9 @@ class Dating(models.Model):  class Unit(GeneralType):      order = models.IntegerField(_(u"Order")) -    parent = models.ForeignKey("Unit", verbose_name=_(u"Parent context record type"), -                               blank=True, null=True) +    parent = models.ForeignKey( +        "Unit", verbose_name=_(u"Parent context record type"), +        blank=True, null=True)      class Meta:          verbose_name = _(u"Context record Type") @@ -118,6 +120,26 @@ post_save.connect(post_save_cache, sender=IdentificationType)  post_delete.connect(post_save_cache, sender=IdentificationType) +class CRBulkView(object): +    CREATE_SQL = """ +        CREATE VIEW context_records_cached_label_bulk_update +        AS ( +            SELECT cr.id AS id, ope.code_patriarche AS main_code, +                ope.year AS year, +                ope.operation_code AS ope_code, +                parcel.section AS section, +                parcel.parcel_number AS number, cr.label AS label +            FROM archaeological_context_records_contextrecord AS cr +            INNER JOIN archaeological_operations_operation ope +                ON ope.id = cr.operation_id +            INNER JOIN archaeological_operations_parcel parcel +                ON cr.parcel_id = parcel.id +        );""" +    DELETE_SQL = """ +        DROP VIEW context_records_cached_label_bulk_update; +    """ + +  class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms,                        ValueGetter, ShortMenuItem):      SHOW_URL = 'show-contextrecord' @@ -193,8 +215,8 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms,      datings = models.ManyToManyField(Dating)      datings_comment = models.TextField(_(u"Comment on datings"), blank=True,                                         null=True) -    unit = models.ForeignKey(Unit, verbose_name=_(u"Context record type"), related_name='+', -                             blank=True, null=True) +    unit = models.ForeignKey(Unit, verbose_name=_(u"Context record type"), +                             related_name='+', blank=True, null=True)      has_furniture = models.NullBooleanField(_(u"Has furniture?"), blank=True,                                              null=True)      filling = models.TextField(_(u"Filling"), blank=True, null=True) @@ -253,6 +275,61 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms,      def __unicode__(self):          return self.short_label +    @classmethod +    def cached_label_bulk_update(cls, operation_id=None, parcel_id=None): +        if operation_id: +            where = "operation_id = %s" +            args = [int(operation_id)] +            kwargs = {'operation_id': operation_id} +        elif parcel_id: +            where = "parcel_id = %s" +            args = [int(parcel_id)] +            kwargs = {'parcel_id': parcel_id} +        else: +            return +        sql = """ +        UPDATE "archaeological_context_records_contextrecord" AS cr +            SET cached_label = +                CASE +                WHEN context_records_cached_label_bulk_update.main_code +                  IS NULL +                THEN +                    CASE +                      WHEN context_records_cached_label_bulk_update.year +                            IS NOT NULL +                      AND context_records_cached_label_bulk_update.ope_code +                            IS NOT NULL +                    THEN +                       '{ope_prefix}' || +                       context_records_cached_label_bulk_update.year || +                       '-' || +                       context_records_cached_label_bulk_update.ope_code +                    ELSE '' +                    END +                ELSE +                    '{main_ope_prefix}' || +                    context_records_cached_label_bulk_update.main_code +                END +                || '{join}' || +                context_records_cached_label_bulk_update.section || '{join}' || +                context_records_cached_label_bulk_update.number || '{join}' || +                context_records_cached_label_bulk_update.label +            FROM context_records_cached_label_bulk_update +            WHERE cr.id = context_records_cached_label_bulk_update.id +                  AND cr.id IN ( +                SELECT id FROM archaeological_context_records_contextrecord +                WHERE {where} +            ); +        """.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) +      @property      def short_label(self):          return settings.JOINT.join([unicode(item) for item in [ | 
