summaryrefslogtreecommitdiff
path: root/archaeological_context_records/models.py
blob: 128342b5739677441ff1eb254c3aad59ab51f474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2017 Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

from django.conf import settings
from django.contrib.gis.db import models
from django.core.urlresolvers import reverse
from django.db import connection
from django.db.models import Q
from django.db.models.signals import post_delete, post_save, m2m_changed
from django.utils.translation import ugettext_lazy as _, pgettext, \
    activate, pgettext_lazy, deactivate
from django.utils.text import slugify

from ishtar_common.utils import cached_label_changed, \
    m2m_historization_changed, post_save_geo

from ishtar_common.model_managers import ExternalIdManager
from ishtar_common.models import Document, GeneralType, \
    BaseHistorizedItem, HistoricalRecords, OwnPerms, ShortMenuItem, \
    GeneralRelationType, GeneralRecordRelations, post_delete_record_relation,\
    post_save_cache, ValueGetter, BulkUpdatedItem, \
    RelationItem, Town, get_current_profile, document_attached_changed, \
    HistoryModel, SearchAltName, GeoItem, QRCodeItem, SearchVectorConfig, \
    DocumentItem
from archaeological_operations.models import Operation, Period, Parcel, \
    ArchaeologicalSite


class DatingType(GeneralType):
    class Meta:
        verbose_name = _("Dating type")
        verbose_name_plural = _("Dating types")
        ordering = ('label',)


post_save.connect(post_save_cache, sender=DatingType)
post_delete.connect(post_save_cache, sender=DatingType)


class DatingQuality(GeneralType):
    class Meta:
        verbose_name = _("Dating quality type")
        verbose_name_plural = _("Dating quality types")
        ordering = ('label',)


post_save.connect(post_save_cache, sender=DatingQuality)
post_delete.connect(post_save_cache, sender=DatingQuality)


class Dating(models.Model):
    period = models.ForeignKey(Period, verbose_name=_("Period"))
    start_date = models.IntegerField(_("Start date"), blank=True, null=True)
    end_date = models.IntegerField(_("End date"), blank=True, null=True)
    dating_type = models.ForeignKey(DatingType, verbose_name=_("Dating type"),
                                    on_delete=models.SET_NULL,
                                    blank=True, null=True)
    quality = models.ForeignKey(DatingQuality, verbose_name=_("Quality"),
                                on_delete=models.SET_NULL,
                                blank=True, null=True)
    precise_dating = models.TextField(_("Precise dating"), blank=True,
                                      null=True)

    class Meta:
        verbose_name = _("Dating")
        verbose_name_plural = _("Datings")

    def __str__(self):
        start_date = self.start_date and str(self.start_date) or ""
        end_date = self.end_date and str(self.end_date) or ""
        if not start_date and not end_date:
            return str(self.period)
        return "%s (%s-%s)" % (self.period, start_date, end_date)

    HISTORY_ATTR = ["period", "start_date", "end_date", "dating_type",
                    "quality", "precise_dating"]

    def history_compress(self):
        values = {}
        for attr in self.HISTORY_ATTR:
            val = getattr(self, attr)
            if hasattr(val, 'history_compress'):
                val = val.history_compress()
            elif hasattr(val, 'isoformat'):
                val = val.isoformat()
            elif val is None:
                val = ''
            else:
                val = str(val)
            values[attr] = val
        return values

    @classmethod
    def history_decompress(cls, full_value, create=False):
        if not full_value:
            return []
        full_res = []
        for value in full_value:
            res = {}
            for key in value:
                val = value[key]
                if val == '':
                    val = None
                elif key in ("period", "dating_type", "quality"):
                    field = cls._meta.get_field(key)
                    q = field.related_model.objects.filter(txt_idx=val)
                    if q.count():
                        val = q.all()[0]
                    else:  # do not exist anymore in db
                        val = None
                elif key in ("start_date", "end_date"):
                    val = int(val)
                res[key] = val
            if create:
                res = cls.objects.create(**res)
            full_res.append(res)
        return full_res

    @classmethod
    def is_identical(cls, dating_1, dating_2):
        """
        Compare two dating attribute by attribute and return True if all
        attribute is identical
        """
        for attr in ["period", "start_date", "end_date", "dating_type",
                     "quality", "precise_dating"]:
            value1 = getattr(dating_1, attr)
            value2 = getattr(dating_2, attr)
            if attr == "precise_dating":
                value1 = value1.strip()
                value2 = value2.strip()
            if value1 != value2:
                return False
        return True

    def context_records_lbl(self):
        return " - ".join(
            [cr.cached_label for cr in self.context_records.all()]
        )
    context_records_lbl.short_description = _("Context record")
    context_records_lbl.admin_order_field = "context_records__cached_label"

    def finds_lbl(self):
        return " - ".join(
            [f.cached_label for f in self.find.all()]
        )
    finds_lbl.short_description = _("Find")
    finds_lbl.admin_order_field = "find__cached_label"

    @classmethod
    def fix_dating_association(cls, obj):
        """
        Fix redundant m2m dating association (usually after imports)
        """
        current_datings = []
        for dating in obj.datings.order_by('pk').all():
            key = (dating.period.pk, dating.start_date, dating.end_date,
                   dating.dating_type, dating.quality, dating.precise_dating)
            if key not in current_datings:
                current_datings.append(key)
                continue
            dating.delete()


class Unit(GeneralType):
    order = models.IntegerField(_("Order"))
    parent = models.ForeignKey(
        "Unit", verbose_name=_("Parent context record type"),
        on_delete=models.SET_NULL, blank=True, null=True)

    class Meta:
        verbose_name = _("Context record Type")
        verbose_name_plural = _("Context record Types")
        ordering = ('order', 'label')

    def __str__(self):
        return self.label


post_save.connect(post_save_cache, sender=Unit)
post_delete.connect(post_save_cache, sender=Unit)


class ActivityType(GeneralType):
    order = models.IntegerField(_("Order"))

    class Meta:
        verbose_name = _("Activity Type")
        verbose_name_plural = _("Activity Types")
        ordering = ('order',)

    def __str__(self):
        return self.label


post_save.connect(post_save_cache, sender=ActivityType)
post_delete.connect(post_save_cache, sender=ActivityType)


class IdentificationType(GeneralType):
    order = models.IntegerField(_("Order"))

    class Meta:
        verbose_name = _("Identification Type")
        verbose_name_plural = _("Identification Types")
        ordering = ('order', 'label')

    def __str__(self):
        return self.label


post_save.connect(post_save_cache, sender=IdentificationType)
post_delete.connect(post_save_cache, sender=IdentificationType)


class ExcavationTechnicType(GeneralType):
    class Meta:
        verbose_name = _("Excavation technique type")
        verbose_name_plural = _("Excavation technique types")
        ordering = ('label',)


post_save.connect(post_save_cache, sender=ExcavationTechnicType)
post_delete.connect(post_save_cache, sender=ExcavationTechnicType)


class DocumentationType(GeneralType):
    class Meta:
        verbose_name = _("Documentation type")
        verbose_name_plural = _("Documentation types")
        ordering = ('label',)


post_save.connect(post_save_cache, sender=DocumentationType)
post_delete.connect(post_save_cache, sender=DocumentationType)


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(BulkUpdatedItem, DocumentItem, BaseHistorizedItem,
                    QRCodeItem, GeoItem, OwnPerms, ValueGetter, ShortMenuItem,
                    RelationItem):
    SLUG = 'contextrecord'
    APP = "archaeological-context-records"
    MODEL = "context-record"
    SHOW_URL = 'show-contextrecord'
    EXTERNAL_ID_KEY = 'context_record_external_id'
    EXTERNAL_ID_DEPENDENCIES = ['base_finds']
    TABLE_COLS = ['label', 'operation__common_name', 'town__name',
                  'parcel__cached_label', 'unit']
    if settings.COUNTRY == 'fr':
        TABLE_COLS.insert(1, 'operation__code_patriarche')
    TABLE_COLS_FOR_OPE = ['label', 'parcel', 'unit',
                          'datings__period__label', 'description']
    COL_LABELS = {
        'datings__period__label': _("Periods"),
        'datings__period': _("Datings (period)"),
        'detailled_related_context_records': _("Related context records"),
        'operation__code_patriarche': _("Operation (Patriarche code)"),
        'operation__common_name': _("Operation (name)"),
        'parcel__external_id': _("Parcel (external ID)"),
        'town__name': _("Town"),
        'town': _("Town"),
        'parcel__year': _("Parcel (year)"),
        'section__parcel_number': _("Parcel"),
        'parcel__cached_label': _("Parcel"),
    }
    CONTEXTUAL_TABLE_COLS = {
        'full': {
            'related_context_records': 'detailled_related_context_records'
        }
    }

    # search parameters
    EXTRA_REQUEST_KEYS = {
        'town': 'town__pk',
        'town__name': 'town__name',
        'parcel__cached_label': 'parcel__cached_label',
        'operation__year': 'operation__year__contains',
        'year': 'operation__year__contains',
        'operation__code_patriarche': 'operation__code_patriarche',
        'operation__operation_code': 'operation__operation_code',
        'operation__common_name': 'operation__common_name',
        'datings__period': 'datings__period__pk',
        'parcel_0': 'operation__parcels__section',
        'parcel_1': 'operation__parcels__parcel_number',
        'parcel_2': 'operation__parcels__public_domain',
        'label': 'label__icontains',
        'archaeological_sites': 'operation__archaeological_sites__pk',
        'cached_label': 'cached_label__icontains',
        'datings__period__label': 'datings__period__label',
        'operation_id': 'operation_id',
    }
    RELATION_TYPES_PREFIX = {'ope_relation_types': 'operation__',
                             'cr_relation_types': ''}
    # alternative names of fields for searches
    ALT_NAMES = {
        'label': SearchAltName(
            pgettext_lazy("key for text search", "id"),
            'label__iexact'
        ),
        'town': SearchAltName(
            pgettext_lazy("key for text search", "town"),
            'town__cached_label__iexact'
        ),
        'operation__year': SearchAltName(
            pgettext_lazy("key for text search", "operation-year"),
            'operation__year'
        ),
        'operation__code_patriarche': SearchAltName(
            pgettext_lazy("key for text search", "patriarche"),
            'operation__code_patriarche__iexact'
        ),
        'operation__operation_code': SearchAltName(
            pgettext_lazy("key for text search", "operation-code"),
            'operation__operation_code'
        ),
        'operation__cached_label': SearchAltName(
            pgettext_lazy("key for text search", "operation"),
            'operation__cached_label__icontains'
        ),
        'archaeological_site': SearchAltName(
            pgettext_lazy("key for text search", "site"),
            'archaeological_site__cached_label__icontains'
        ),
        'ope_relation_types': SearchAltName(
            pgettext_lazy("key for text search", "operation-relation-type"),
            'ope_relation_types'
        ),
        'datings__period': SearchAltName(
            pgettext_lazy("key for text search", "period"),
            'datings__period__label__iexact'
        ),
        'unit': SearchAltName(
            pgettext_lazy("key for text search", "unit-type"),
            'unit__label__iexact'
        ),
        'parcel': SearchAltName(
            pgettext_lazy("key for text search", "parcel"),
            'parcel__cached_label__iexact'
        ),
        'cr_relation_types': SearchAltName(
            pgettext_lazy("key for text search", "record-relation-type"),
            'cr_relation_types'
        ),
    }
    ALT_NAMES.update(BaseHistorizedItem.ALT_NAMES)

    PARENT_ONLY_SEARCH_VECTORS = ["operation", "archaeological_site", "parcel"]
    BASE_SEARCH_VECTORS = [
        SearchVectorConfig("cached_label"),
        SearchVectorConfig("label"),
        SearchVectorConfig("location"),
        SearchVectorConfig("town__name"),
        SearchVectorConfig("interpretation", 'local'),
        SearchVectorConfig("filling", 'local'),
        SearchVectorConfig("datings_comment", 'local'),
        SearchVectorConfig("identification__label"),
        SearchVectorConfig("activity__label"),
        SearchVectorConfig("excavation_technic__label")]
    M2M_SEARCH_VECTORS = [
        SearchVectorConfig("datings__period__label", 'local')
    ]
    UP_MODEL_QUERY = {
        "operation": (
            pgettext_lazy("key for text search", "operation"),
            'cached_label'),
        "site": (
            pgettext_lazy("key for text search", "site"),
            'cached_label'),
    }
    RELATIVE_SESSION_NAMES = [
        ('operation', 'operation__pk'),
        ('site', 'archaeological_site__pk'),
        ('file', 'operation__associated_file__pk'),
    ]
    HISTORICAL_M2M = [
        'datings', 'documentations'
    ]

    history = HistoricalRecords(bases=[HistoryModel])
    objects = ExternalIdManager()

    # fields
    external_id = models.TextField(_("External ID"), blank=True, null=True)
    auto_external_id = models.BooleanField(
        _("External ID is set automatically"), default=False)
    parcel = models.ForeignKey(
        Parcel, verbose_name=_("Parcel"), related_name='context_record',
        on_delete=models.SET_NULL, blank=True, null=True)
    town = models.ForeignKey(
        Town, verbose_name=_("Town"), related_name='context_record',
        on_delete=models.SET_NULL, blank=True, null=True)
    operation = models.ForeignKey(Operation, verbose_name=_("Operation"),
                                  related_name='context_record')
    archaeological_site = models.ForeignKey(
        ArchaeologicalSite, verbose_name=_("Archaeological site"),
        on_delete=models.SET_NULL,
        blank=True, null=True, related_name='context_records')
    label = models.CharField(_("ID"), max_length=200)
    description = models.TextField(_("Description"), blank=True, null=True)
    comment = models.TextField(_("General comment"), blank=True, null=True)
    opening_date = models.DateField(_("Opening date"),
                                    blank=True, null=True)
    closing_date = models.DateField(_("Closing date"), blank=True, null=True)
    length = models.FloatField(_("Length (m)"), blank=True, null=True)
    width = models.FloatField(_("Width (m)"), blank=True, null=True)
    thickness = models.FloatField(_("Thickness (m)"), blank=True,
                                  null=True)
    diameter = models.FloatField(_("Diameter (m)"), blank=True, null=True)
    depth = models.FloatField(_("Depth (m)"), blank=True, null=True)
    depth_of_appearance = models.FloatField(
        _("Depth of appearance (m)"), blank=True, null=True)
    location = models.TextField(
        _("Location"), blank=True, null=True,
        help_text=_("A short description of the location of the context "
                    "record"))
    datings = models.ManyToManyField(Dating, related_name='context_records')
    documentations = models.ManyToManyField(DocumentationType, blank=True)
    datings_comment = models.TextField(_("Comment on datings"), blank=True,
                                       null=True)
    unit = models.ForeignKey(Unit, verbose_name=_("Context record type"),
                             on_delete=models.SET_NULL,
                             related_name='+', blank=True, null=True)
    filling = models.TextField(_("Filling"), blank=True, null=True)
    interpretation = models.TextField(_("Interpretation"), blank=True,
                                      null=True)
    taq = models.IntegerField(
        _("TAQ"), blank=True, null=True,
        help_text=_("\"Terminus Ante Quem\" the context record can't have "
                    "been created after this date"))
    taq_estimated = models.IntegerField(
        _("Estimated TAQ"), blank=True, null=True,
        help_text=_("Estimation of a \"Terminus Ante Quem\""))
    tpq = models.IntegerField(
        _("TPQ"), blank=True, null=True,
        help_text=_("\"Terminus Post Quem\" the context record can't have "
                    "been created before this date"))
    tpq_estimated = models.IntegerField(
        _("Estimated TPQ"), blank=True, null=True,
        help_text=_("Estimation of a \"Terminus Post Quem\""))
    identification = models.ForeignKey(
        IdentificationType, blank=True, null=True,
        on_delete=models.SET_NULL,
        verbose_name=_("Identification"),)
    activity = models.ForeignKey(ActivityType, blank=True, null=True,
                                 on_delete=models.SET_NULL,
                                 verbose_name=_("Activity"),)
    excavation_technic = models.ForeignKey(
        ExcavationTechnicType, blank=True, null=True,
        on_delete=models.SET_NULL,
        verbose_name=_("Excavation technique"))
    related_context_records = models.ManyToManyField(
        'ContextRecord', through='RecordRelations', blank=True)
    documents = models.ManyToManyField(
        Document, related_name='context_records', verbose_name=_("Documents"),
        blank=True)
    main_image = models.ForeignKey(
        Document, related_name='main_image_context_records',
        on_delete=models.SET_NULL,
        verbose_name=_("Main image"), blank=True, null=True)
    cached_label = models.TextField(_("Cached name"), null=True, blank=True,
                                    db_index=True)

    class Meta:
        verbose_name = _("Context Record")
        verbose_name_plural = _("Context Record")
        permissions = (
            ("view_contextrecord", "Can view all Context Records"),
            ("view_own_contextrecord", "Can view own Context Record"),
            ("add_own_contextrecord", "Can add own Context Record"),
            ("change_own_contextrecord", "Can change own Context Record"),
            ("delete_own_contextrecord", "Can delete own Context Record"),
        )
        ordering = ('cached_label',)

    def natural_key(self):
        return (self.external_id, )

    @property
    def name(self):
        return self.label or ""

    @property
    def short_class_name(self):
        return pgettext("short", "Context record")

    def __str__(self):
        return self.short_label or ""

    def get_values(self, prefix='', no_values=False, no_base_finds=True):
        values = super(ContextRecord, self).get_values(prefix=prefix,
                                                       no_values=no_values)
        if prefix and no_base_finds:
            return values
        values[prefix + 'base_finds'] = [
            bf.get_values(prefix=prefix, no_values=True)
            for bf in self.base_finds.distinct().all()
        ]
        return values

    def get_town_centroid(self):
        if self.town:
            return self.town.center, self._meta.verbose_name
        if self.archaeological_site:
            centroid = self.archaeological_site.get_town_centroid()
            if centroid:
                return centroid
        return self.operation.get_town_centroid()

    def get_town_polygons(self):
        if self.town:
            return self.town.limit, self._meta.verbose_name
        if self.archaeological_site:
            polys = self.archaeological_site.get_town_polygons()
            if polys:
                return polys
        return self.operation.get_town_polygons()

    def get_precise_points(self):
        precise_points = super(ContextRecord, self).get_precise_points()
        if precise_points:
            return precise_points
        if self.archaeological_site:
            precise_points = self.archaeological_site.get_precise_points()
            if precise_points:
                return precise_points
        return self.operation.get_precise_points()

    def get_precise_polygons(self):
        precise_poly = super(ContextRecord, self).get_precise_polygons()
        if precise_poly:
            return precise_poly
        if self.archaeological_site:
            precise_poly = self.archaeological_site.get_precise_polygons()
            if precise_poly:
                return precise_poly
        return self.operation.get_precise_polygons()

    @classmethod
    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)]
            kwargs = {'operation_id': operation_id}
        elif parcel_id:
            where = "parcel_id = %s"
            args = [int(parcel_id)]
            kwargs = {'parcel_id': parcel_id}
        else:
            return
        kwargs['transaction_id'] = transaction_id

        profile = get_current_profile()

        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=profile.operation_prefix,
                   ope_prefix=profile.default_operation_prefix,
                   join=settings.JOINT, where=where)
        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):
        return settings.JOINT.join([str(item) for item in [
            self.operation.get_reference(), self.parcel, self.label] if item])

    @property
    def relation_label(self):
        return self.label

    @property
    def show_url(self):
        return reverse('show-contextrecord', args=[self.pk, ''])

    @classmethod
    def get_query_owns(cls, ishtaruser):
        q = cls._construct_query_own(
            'operation__', Operation._get_query_owns_dicts(ishtaruser)
        ) | cls._construct_query_own(
            'base_finds__find__basket__',
            [{"shared_with": ishtaruser, "shared_write_with": ishtaruser}]
        ) | cls._construct_query_own('', [
            {'history_creator': ishtaruser.user_ptr},
            {'operation__end_date__isnull': True}
        ])
        return q

    @classmethod
    def get_owns(cls, user, menu_filtr=None, limit=None,
                 values=None, get_short_menu_class=None):
        replace_query = None
        if menu_filtr and 'operation' in menu_filtr:
            replace_query = Q(operation=menu_filtr['operation'])
        owns = super(ContextRecord, cls).get_owns(
            user, replace_query=replace_query,
            limit=limit, values=values,
            get_short_menu_class=get_short_menu_class)
        return cls._return_get_owns(owns, values, get_short_menu_class)

    def full_label(self):
        return str(self)

    def _real_label(self):
        if not self.operation.code_patriarche:
            return
        return settings.JOINT.join((self.operation.code_patriarche,
                                    self.label))

    def _temp_label(self):
        if self.operation.code_patriarche:
            return
        return settings.JOINT.join([str(lbl) for lbl in [
            self.operation.year, self.operation.operation_code, self.label]
            if lbl])

    def _generate_cached_label(self):
        return self.full_label()

    def _get_associated_cached_labels(self):
        from archaeological_finds.models import Find, BaseFind
        return list(Find.objects.filter(base_finds__context_record=self).all())\
            + list(BaseFind.objects.filter(context_record=self).all())

    def _cached_labels_bulk_update(self):
        self.base_finds.model.cached_label_bulk_update(
            context_record_id=self.pk)
        return True

    def _get_base_image_path(self):
        return self.operation._get_base_image_path() + \
               "/{}/{}".format(self.SLUG, slugify(self.label or "00"))

    @property
    def archaeological_site_reference(self):
        if self.archaeological_site:
            return self.archaeological_site.reference
        if self.operation.archaeological_sites.count():
            return "-".join(
                [a.reference for a in self.operation.archaeological_sites.all()]
            )
        return ""

    @property
    def reference(self):
        if not self.operation:
            return "00"
        return self.full_label()

    def get_department(self):
        if not self.operation:
            return "00"
        return self.operation.get_department()

    def get_town_label(self):
        if not self.operation:
            return "00"
        return self.operation.get_town_label()

    @classmethod
    def get_periods(cls, slice='year', fltr={}):
        q = cls.objects
        if fltr:
            q = q.filter(**fltr)
        if slice == 'year':
            years = set()
            for res in list(q.values('operation__start_date')):
                if res['operation__start_date']:
                    yr = res['operation__start_date'].year
                    years.add(yr)
            return list(years)
        return []

    @classmethod
    def get_by_year(cls, year, fltr={}):
        q = cls.objects
        if fltr:
            q = q.filter(**fltr)
        return q.filter(operation__start_date__year=year)

    @classmethod
    def get_operations(cls):
        return [dct['operation__pk']
                for dct in cls.objects.values('operation__pk').distinct()]

    @classmethod
    def get_by_operation(cls, operation_id):
        return cls.objects.filter(operation__pk=operation_id)

    @classmethod
    def get_total_number(cls, fltr={}):
        q = cls.objects
        if fltr:
            q = q.filter(**fltr)
        return q.count()

    def detailled_related_context_records(self):
        crs = []
        for cr in self.right_relations.all():
            crs.append("{} ({})".format(cr.right_record,
                                         cr.relation_type.get_tiny_label()))
        return " ; ".join(crs)

    def find_docs_q(self):
        return Document.objects.filter(finds__base_finds__context_record=self)

    def fix(self):
        """
        Fix redundant m2m dating association (usually after imports)
        """
        Dating.fix_dating_association(self)

    def save(self, *args, **kwargs):
        super(ContextRecord, self).save(*args, **kwargs)
        if (not self.town and self.parcel) or (
                self.parcel and self.parcel.town != self.town):
            self.town = self.parcel.town
            self.skip_history_when_saving = True
            self.save()


def context_record_post_save(sender, **kwargs):
    cached_label_changed(sender=sender, **kwargs)
    post_save_geo(sender=sender, **kwargs)


post_save.connect(context_record_post_save, sender=ContextRecord)
m2m_changed.connect(document_attached_changed,
                    sender=ContextRecord.documents.through)

for attr in ContextRecord.HISTORICAL_M2M:
    m2m_changed.connect(m2m_historization_changed,
                        sender=getattr(ContextRecord, attr).through)


class RelationType(GeneralRelationType):
    class Meta:
        verbose_name = _("Relation type")
        verbose_name_plural = _("Relation types")
        ordering = ('order', 'label')


class RecordRelations(GeneralRecordRelations, models.Model):
    MAIN_ATTR = 'left_record'
    left_record = models.ForeignKey(ContextRecord,
                                    related_name='right_relations')
    right_record = models.ForeignKey(ContextRecord,
                                     related_name='left_relations')
    relation_type = models.ForeignKey(RelationType)
    TABLE_COLS = [
        "left_record__label", "left_record__unit", "left_record__parcel",
        "relation_type",
        "right_record__label", "right_record__unit", "right_record__parcel",
    ]
    COL_LABELS = {
        "left_record__label": _("ID (left)"),
        "left_record__unit": _("Context record type (left)"),
        "left_record__parcel": _("Parcel (left)"),
        "left_record__description": _("Description (left)"),
        "left_record__datings__period": _("Periods (left)"),
        "relation_type": _("Relation type"),
        "right_record__label": _("ID (right)"),
        "right_record__unit": _("Context record type (right)"),
        "right_record__parcel": _("Parcel (right)"),
        "right_record__description": _("Description (right)"),
        "right_record__datings__period": _("Periods (right)")
    }

    # search parameters
    EXTRA_REQUEST_KEYS = {
        "left_record__operation": "left_record__operation__pk"
    }

    class Meta:
        verbose_name = _("Record relation")
        verbose_name_plural = _("Record relations")
        permissions = [
            ("view_recordrelation", "Can view all Context record relations"),
        ]


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,
            left_record_id,
            relation_type_id
          FROM archaeological_context_records_recordrelations;

    -- deactivate deletion
    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",
        "right_record__datings__period", "right_record__description"]
    COL_LABELS = {
        "relation_type": _("Relation type"),
        "right_record__label": _("ID"),
        "right_record__unit": _("Context record type"),
        "right_record__parcel": _("Parcel"),
        "right_record__description": _("Description"),
        "right_record__datings__period": _("Periods")
    }

    # search parameters
    EXTRA_REQUEST_KEYS = {
        "left_record_id": "left_record_id"
    }
    left_record = models.ForeignKey(ContextRecord, related_name='+',
                                    on_delete=models.DO_NOTHING)
    right_record = models.ForeignKey(ContextRecord, related_name='+',
                                     on_delete=models.DO_NOTHING)
    relation_type = models.ForeignKey(RelationType, related_name='+',
                                      on_delete=models.DO_NOTHING)

    class Meta:
        managed = False
        db_table = 'record_relations'
        unique_together = ('id', 'right_record')
        permissions = [
            ("view_recordrelation", "Can view all record relations - view"),
        ]

    @classmethod
    def general_types(cls):
        return []

    def __str__(self):
        return "{} \"{}\"".format(self.relation_type, self.right_record)