diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-02-26 18:34:09 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-02-26 18:34:09 +0100 |
commit | c8aeaadac1156a679a4ec021f7b36784b9ab69c0 (patch) | |
tree | 04c4f2769840880c3311f34a57597372509bdcd6 /archaeological_context_records/models.py | |
parent | 33fe24b1e8bb81b8e4e2f9851d967ce86616c62b (diff) | |
download | Ishtar-c8aeaadac1156a679a4ec021f7b36784b9ab69c0.tar.bz2 Ishtar-c8aeaadac1156a679a4ec021f7b36784b9ab69c0.zip |
Operations: bulk update of base find and finds (refs #3472)
Diffstat (limited to 'archaeological_context_records/models.py')
-rw-r--r-- | archaeological_context_records/models.py | 74 |
1 files changed, 22 insertions, 52 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index b36c56a85..c45cb1fd4 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -77,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") @@ -214,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) @@ -275,7 +276,17 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, return self.short_label @classmethod - def cached_label_bulk_update(cls, operation_id): + 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 = @@ -304,58 +315,17 @@ class ContextRecord(BaseHistorizedItem, ImageModel, OwnPerms, WHERE cr.id = context_records_cached_label_bulk_update.id AND cr.id IN ( SELECT id FROM archaeological_context_records_contextrecord - WHERE operation_id = %s + WHERE {where} ); """.format(main_ope_prefix=settings.ISHTAR_OPE_PREFIX, ope_prefix=settings.ISHTAR_DEF_OPE_PREFIX, - join=settings.JOINT) + join=settings.JOINT, where=where) + # with connection.cursor() as c: # django 1.8 c = connection.cursor() - # with connection.cursor() as c: - c.execute(sql, [int(operation_id)]) + c.execute(sql, args) transaction.commit_unless_managed() - ''' - sql = """ - WITH t 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 - ) - - UPDATE archaeological_context_records_contextrecord AS cr - SET cached_label = - CASE t.main_code - WHEN NULL THEN - CASE t.year IS NOT NULL AND t.ope_code IS NOT NULL - WHEN TRUE THEN - '{ope_prefix}' || t.year || '-' || t.ope_code - ELSE '' - END - ELSE - '{main_ope_prefix}' || t.main_code - END - || - '{join}' || t.section || '{join}' || - t.number || '{join}' || t.label - FROM t - WHERE cr.id = t.id AND cr.id IN ( - SELECT id FROM archaeological_context_records_contextrecord - WHERE operation_id = {ope_id} - ); - """.format(main_ope_prefix=settings.ISHTAR_OPE_PREFIX, - ope_prefix=settings.ISHTAR_DEF_OPE_PREFIX, - join=settings.JOINT, ope_id=operation_id) - - cursor = connection.cursor() - cursor.execute(sql) - - ''' + cls._meta.get_field_by_name( + 'base_finds')[0].model.cached_label_bulk_update(**kwargs) @property def short_label(self): |