summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/models.py
blob: 63d4497acda3f317eff32a37e49b3bbd1dbfc467 (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
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/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.

import datetime
import uuid

from django.contrib.gis.db import models
from django.contrib.postgres.indexes import GinIndex
from django.core.urlresolvers import reverse
from django.db.models import Q, Max
from django.db.models.signals import post_save, post_delete, m2m_changed
from django.template.defaultfilters import slugify
from ishtar_common.utils import ugettext_lazy as _, pgettext_lazy

from ishtar_common.data_importer import post_importer_action
from ishtar_common.model_managers import ExternalIdManager, UUIDModelManager
from ishtar_common.models import Document, GeneralType, get_external_id, \
    LightHistorizedItem, OwnPerms, Address, Person, post_save_cache, \
    DashboardFormItem, ShortMenuItem, Organization, OrganizationType, \
    document_attached_changed, SearchAltName, DynamicRequest, GeoItem, \
    QRCodeItem, SearchVectorConfig, DocumentItem, QuickAction, MainItem
from ishtar_common.model_merging import merge_model_objects
from ishtar_common.utils import cached_label_changed, \
    cached_label_and_geo_changed


class WarehouseType(GeneralType):
    class Meta:
        verbose_name = _(u"Warehouse type")
        verbose_name_plural = _(u"Warehouse types")
        ordering = ('label',)


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


class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem,
                OwnPerms, MainItem):
    SLUG = 'warehouse'
    APP = "archaeological-warehouse"
    MODEL = "warehouse"
    SHOW_URL = 'show-warehouse'
    DELETE_URL = 'delete-warehouse'
    TABLE_COLS = ['name', 'warehouse_type__label']
    NEW_QUERY_ENGINE = True
    BASE_SEARCH_VECTORS = [
        SearchVectorConfig("name"),
        SearchVectorConfig("warehouse_type__label"),
        SearchVectorConfig("external_id"),
        SearchVectorConfig("town"),
        SearchVectorConfig("precise_town__name"),
        SearchVectorConfig("comment", "local"),
    ]

    EXTRA_REQUEST_KEYS = {
        "warehouse_type__label": "warehouse_type__label",
        # used by dynamic_table_documents
        "person_in_charge__pk": "person_in_charge__pk",
    }
    # alternative names of fields for searches
    ALT_NAMES = {
        'name': SearchAltName(
            pgettext_lazy("key for text search", "name"),
            'name__iexact'
        ),
        'warehouse_type': SearchAltName(
            pgettext_lazy("key for text search", "type"),
            'warehouse_type__label__iexact'
        ),
        'town':
            SearchAltName(
                pgettext_lazy("key for text search", "town"),
                'precise_town__cached_label__iexact'
            ),
    }
    GEO_LABEL = "name"
    DOWN_MODEL_UPDATE = ["containers"]
    CACHED_LABELS = []

    QA_LOCK = QuickAction(
        url="warehouse-qa-lock", icon_class="fa fa-lock",
        text=_(u"Lock/Unlock"), target="many",
        rights=['change_warehouse', 'change_own_warehouse']
    )
    QUICK_ACTIONS = [QA_LOCK]

    objects = UUIDModelManager()

    uuid = models.UUIDField(default=uuid.uuid4)
    name = models.CharField(_(u"Name"), max_length=200)
    warehouse_type = models.ForeignKey(WarehouseType,
                                       verbose_name=_(u"Warehouse type"))
    person_in_charge = models.ForeignKey(
        Person, on_delete=models.SET_NULL, related_name='warehouse_in_charge',
        verbose_name=_(u"Person in charge"), null=True, blank=True)
    organization = models.ForeignKey(
        Organization, blank=True, null=True, related_name='warehouses',
        verbose_name=_("Organization"), on_delete=models.SET_NULL)
    comment = models.TextField(_(u"Comment"), null=True, blank=True)
    associated_divisions = models.ManyToManyField(
        'WarehouseDivision', verbose_name=_("Divisions"), blank=True,
        through='WarehouseDivisionLink'
    )
    documents = models.ManyToManyField(
        Document, related_name='warehouses', verbose_name=_(u"Documents"),
        blank=True)
    main_image = models.ForeignKey(
        Document, related_name='main_image_warehouses',
        on_delete=models.SET_NULL,
        verbose_name=_(u"Main image"), blank=True, null=True)
    external_id = models.TextField(_(u"External ID"), blank=True, null=True)
    auto_external_id = models.BooleanField(
        _(u"External ID is set automatically"), default=False)
    SUB_ADDRESSES = ["organization", "person_in_charge"]

    class Meta:
        verbose_name = _(u"Warehouse")
        verbose_name_plural = _(u"Warehouses")
        permissions = (
            ("view_warehouse", u"Can view all Warehouses"),
            ("view_own_warehouse", u"Can view own Warehouse"),
            ("add_own_warehouse", u"Can add own Warehouse"),
            ("change_own_warehouse", u"Can change own Warehouse"),
            ("delete_own_warehouse", u"Can delete own Warehouse"),
        )
        indexes = [
            GinIndex(fields=['data']),
        ]

    def __str__(self):
        return self.name

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

    def _get_base_image_path(self):
        return u"{}/{}".format(self.SLUG, self.external_id)

    def create_attached_organization(self):
        """
        Create an attached organization from warehouse fields
        """
        dct_orga = {}
        for k in Address.FIELDS:
            dct_orga[k] = getattr(self, k)

        q = OrganizationType.objects.filter(txt_idx="warehouse")
        if q.count():
            orga_type = q.all()[0]
        else:
            orga_type, __ = OrganizationType.objects.get_or_create(
                txt_idx="undefined",
                defaults={"label": _("Undefined")}
            )
        dct_orga["organization_type"] = orga_type
        dct_orga["name"] = self.name
        orga = Organization.objects.create(**dct_orga)
        orga.save()  # force duplicates
        self.organization = orga
        for k in Address.FIELDS:
            if k == "alt_address_is_prefered":
                setattr(self, k, False)
            else:
                setattr(self, k, None)
        self.save()

    @property
    def location_types(self):
        return [
            wd.division.label
            for wd in WarehouseDivisionLink.objects.filter(
                warehouse=self).order_by('order').all()
        ]

    @property
    def associated_filename(self):
        return datetime.date.today().strftime('%Y-%m-%d') + '-' + \
            slugify(str(self))

    @classmethod
    def get_query_owns(cls, ishtaruser):
        return cls._construct_query_own(
            '', cls._get_query_owns_dicts(ishtaruser))

    @classmethod
    def _get_query_owns_dicts(cls, ishtaruser):
        return [{'person_in_charge__ishtaruser': ishtaruser}]

    @property
    def number_of_finds(self):
        from archaeological_finds.models import Find
        return Find.objects.filter(container__responsible=self).count()

    @property
    def number_of_finds_hosted(self):
        from archaeological_finds.models import Find
        return Find.objects.filter(container__location=self).count()

    @property
    def number_of_containers(self):
        return Container.objects.filter(location=self).count()

    def _get_divisions(self, current_path, remaining_division, depth=0):
        if not remaining_division:
            return [current_path]
        current_division = remaining_division.pop(0)
        q = ContainerLocalisation.objects.filter(
            division=current_division,
        )
        for div, ref in current_path:
            q = q.filter(
                container__division__division=div,
                container__division__reference=ref
            )
        res = []
        old_ref = None
        if not q.count():
            return [current_path]
        for ref in q.values('reference').order_by('reference').all():
            if ref['reference'] == old_ref:
                continue
            old_ref = ref['reference']
            cpath = current_path[:]
            cpath.append((current_division, ref['reference']))
            for r in self._get_divisions(cpath, remaining_division[:],
                                         depth + 1):
                res.append(r)
        return res

    @property
    def available_division_tuples(self):
        """
        :return: ordered list of available paths. Each path is a list of
        tuple with the WarehouseDivisionLink and the reference.
        """
        divisions = list(
            WarehouseDivisionLink.objects.filter(warehouse=self
                                                 ).order_by('order').all())
        return self._get_divisions([], divisions)

    def _number_of_items_by_place(self, model, division_key='division'):
        res = {}
        paths = self.available_division_tuples[:]
        for path in paths:
            q = model.objects
            cpath = []
            for division, ref in path:
                cpath.append(ref)
                attrs = {
                    division_key + "__division": division,
                    division_key + "__reference": ref
                }
                q = q.filter(**attrs)
                if tuple(cpath) not in res:
                    res[tuple(cpath)] = q.count()
        res = [(k, res[k]) for k in res]
        final_res, current_res, depth = [], [], 1
        len_divisions = WarehouseDivisionLink.objects.filter(
            warehouse=self).count()
        for path, nb in sorted(res, key=lambda x: (len(x[0]), x[0])):
            if depth != len(path):
                final_res.append(current_res[:])
                current_res = []
                depth = len(path)
            if path[-1] == '-':
                continue
            path = list(path) + ['' for idx in range(len_divisions - len(path))]
            current_res.append((path, nb))
        final_res.append(current_res[:])
        return final_res

    def _number_of_finds_by_place(self):
        from archaeological_finds.models import Find
        return self._number_of_items_by_place(
            Find, division_key='container__division')

    @property
    def number_of_finds_by_place(self, update=False):
        return self._get_or_set_stats('_number_of_finds_by_place', update)

    def _number_of_containers_by_place(self):
        return self._number_of_items_by_place(Container)

    @property
    def number_of_containers_by_place(self, update=False):
        return self._get_or_set_stats('_number_of_containers_by_place', update)

    def merge(self, item, keep_old=False):
        # do not recreate missing divisions
        available_divisions = [
            wd.division
            for wd in WarehouseDivisionLink.objects.filter(warehouse=self)
        ]
        for container in list(item.containers.all()):
            container.location = self
            container.save()
            for loca in ContainerLocalisation.objects.filter(
                    container=container).all():
                if loca.division.division in available_divisions:
                    div = WarehouseDivisionLink.objects.get(
                        warehouse=self,
                        division=loca.division.division
                    )
                    ContainerLocalisation.objects.create(
                        container=container, division=div,
                        reference=loca.reference
                    )
                loca.delete()
            container.save()  # force label regeneration
        for container in list(item.owned_containers.all()):
            if Container.objects.filter(index=container.index,
                                        responsible=self).count():
                container.index = Container.objects.filter(
                    responsible=self).all().aggregate(
                    Max("index"))["index__max"] + 1
            container.responsible = self
            container.save()
        for wdiv in WarehouseDivisionLink.objects.filter(warehouse=item).all():
            wdiv.delete()
        merge_model_objects(self, item, keep_old=keep_old)

    def save(self, *args, **kwargs):
        self.update_search_vector()
        super(Warehouse, self).save(*args, **kwargs)

        self.skip_history_when_saving = True
        if not self.external_id or self.auto_external_id:
            external_id = get_external_id('warehouse_external_id', self)
            if external_id != self.external_id:
                self.auto_external_id = True
                self.external_id = external_id
                self._cached_label_checked = False
                self.save()
                return


m2m_changed.connect(document_attached_changed,
                    sender=Warehouse.documents.through)

post_save.connect(cached_label_and_geo_changed, sender=Warehouse)


class Collection(LightHistorizedItem):
    name = models.CharField(_(u"Name"), max_length=200,
                            null=True, blank=True)
    description = models.TextField(_(u"Description"), null=True, blank=True)
    warehouse = models.ForeignKey(Warehouse, verbose_name=_(u"Warehouse"),
                                  related_name='collections')

    class Meta:
        verbose_name = _(u"Collection")
        verbose_name_plural = _(u"Collection")
        ordering = ('name',)
        indexes = [
            GinIndex(fields=['data']),
        ]

    def __str__(self):
        return self.name


class WarehouseDivision(GeneralType):
    class Meta:
        verbose_name = _(u"Warehouse division type")
        verbose_name_plural = _(u"Warehouse division types")


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


class WarehouseDivisionLinkManager(models.Manager):
    def get_by_natural_key(self, warehouse, division):
        return self.get(warehouse__uuid=warehouse,
                        division__txt_idx=division)


class WarehouseDivisionLink(models.Model):
    RELATED_SET_NAME = "divisions"
    RELATED_ATTRS = ["order"]
    RELATIVE_MODELS = {Warehouse: 'warehouse'}
    warehouse = models.ForeignKey(Warehouse, related_name='divisions')
    division = models.ForeignKey(WarehouseDivision)
    order = models.IntegerField(_("Order"), default=10)
    objects = WarehouseDivisionLinkManager()

    class Meta:
        ordering = ('warehouse', 'order')
        unique_together = ('warehouse', 'division')

    def __str__(self):
        return u"{} - {}".format(self.warehouse, self.division)

    def natural_key(self):
        return self.warehouse.uuid, self.division.txt_idx


class ContainerType(GeneralType):
    length = models.IntegerField(_(u"Length (mm)"), blank=True, null=True)
    width = models.IntegerField(_(u"Width (mm)"), blank=True, null=True)
    height = models.IntegerField(_(u"Height (mm)"), blank=True, null=True)
    volume = models.FloatField(_(u"Volume (l)"), blank=True, null=True)
    reference = models.CharField(_(u"Ref."), max_length=300, blank=True,
                                 null=True)

    class Meta:
        verbose_name = _(u"Container type")
        verbose_name_plural = _(u"Container types")
        ordering = ('label',)


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


class Container(DocumentItem, LightHistorizedItem, QRCodeItem, GeoItem,
                OwnPerms, MainItem):
    SLUG = 'container'
    APP = "archaeological-warehouse"
    MODEL = "container"
    SHOW_URL = 'show-container'
    DELETE_URL = 'delete-container'
    NEW_QUERY_ENGINE = True
    TABLE_COLS = ['reference', 'container_type__label', 'cached_location',
                  'cached_division', 'old_reference']
    IMAGE_PREFIX = 'containers/'
    BASE_SEARCH_VECTORS = [
        SearchVectorConfig("reference"),
        SearchVectorConfig("container_type__label"),
        SearchVectorConfig("cached_location"),
        SearchVectorConfig("old_reference"),
        SearchVectorConfig("comment", "local"),
    ]
    M2M_SEARCH_VECTORS = [
        SearchVectorConfig("division__reference"),
        SearchVectorConfig("division__division__division__label"),
    ]

    # search parameters
    EXTRA_REQUEST_KEYS = {
        'location': 'location__pk',
        'location_id': 'location__pk',
        'responsible_id': 'responsible__pk',
        'container_type': 'container_type__pk',
        'reference': 'reference__icontains',
        'old_reference': 'old_reference__icontains',
        'finds__base_finds__context_record__operation':
            'finds__base_finds__context_record__operation',
        'finds__base_finds__context_record':
            'finds__base_finds__context_record',
        'finds': 'finds',
        'container_type__label': 'container_type__label',
    }
    COL_LABELS = {
        'cached_location': _(u"Location - index"),
        'cached_division': _(u"Precise localisation"),
        'container_type__label': _(u"Type")
    }
    GEO_LABEL = "cached_label"
    CACHED_LABELS = ['cached_division', 'cached_label', 'cached_location', ]

    # alternative names of fields for searches
    ALT_NAMES = {
        'location_name': SearchAltName(
            pgettext_lazy("key for text search", "location"),
            'location__name__iexact'
        ),
        'responsible_name': SearchAltName(
            pgettext_lazy("key for text search", "responsible-warehouse"),
            'responsible__name__iexact'
        ),
        'container_type': SearchAltName(
            pgettext_lazy("key for text search", "type"),
            'container_type__label__iexact'
        ),
        'reference': SearchAltName(
            pgettext_lazy("key for text search", "reference"),
            'reference__iexact'
        ),
        'old_reference': SearchAltName(
            pgettext_lazy("key for text search", "old-reference"),
            'old_reference__iexact'
        ),
        'comment': SearchAltName(
            pgettext_lazy("key for text search", "comment"),
            'comment__iexact'
        ),
        'operation_town':
            SearchAltName(
                pgettext_lazy("key for text search", "operation-town"),
                'finds__base_finds__context_record__operation__'
                'towns__cached_label__iexact'
            ),
        'operation_scientist':
            SearchAltName(
                pgettext_lazy("key for text search", "operation-scientist"),
                'finds__base_finds__context_record__operation__'
                'scientist__cached_label__iexact'
            ),
        'code_patriarche':
            SearchAltName(
                pgettext_lazy("key for text search", "code-patriarche"),
                'finds__base_finds__context_record__operation__'
                'code_patriarche__iexact'
            ),
        'archaeological_sites':
            SearchAltName(
                pgettext_lazy("key for text search", "site"),
                'finds__base_finds__context_record__operation__'
                'archaeological_sites__cached_label__icontains'),
        'archaeological_sites_name':
            SearchAltName(
                pgettext_lazy("key for text search", "site-name"),
                'finds__base_finds__context_record__operation__'
                'archaeological_sites__name__iexact'),
        'archaeological_sites_context_record':
            SearchAltName(
                pgettext_lazy("key for text search", "context-record-site"),
                'finds__base_finds__context_record__archaeological_site__'
                'cached_label__icontains'),
        'archaeological_sites_context_record_name':
            SearchAltName(
                pgettext_lazy("key for text search",
                              "context-record-site-name"),
                'finds__base_finds__context_record__archaeological_site__'
                'name__iexact'),
        'context_record':
            SearchAltName(
                pgettext_lazy("key for text search", "context-record"),
                'finds__base_finds__context_record__cached_label__icontains'),
        'find_label':
            SearchAltName(
                pgettext_lazy("key for text search", "find-label"),
                'finds__label__icontains'),
        'find_denomination':
            SearchAltName(
                pgettext_lazy("key for text search", "find-denomination"),
                'finds__denomination__icontains'),
        'material_types':
            SearchAltName(
                pgettext_lazy("key for text search", "material"),
                'finds__material_types__label__iexact'),
        'object_types':
            SearchAltName(
                pgettext_lazy("key for text search", "object-type"),
                'finds__object_types__label__iexact'),
        'preservation_to_considers':
            SearchAltName(
                pgettext_lazy("key for text search", "preservation"),
                'finds__preservation_to_considers__label__iexact'),
        'conservatory_state':
            SearchAltName(
                pgettext_lazy("key for text search", "conservatory"),
                'finds__conservatory_state__label__iexact'),
        'integrities':
            SearchAltName(
                pgettext_lazy("key for text search", "integrity"),
                'finds__integrities__label__iexact'),
        'remarkabilities':
            SearchAltName(
                pgettext_lazy("key for text search", "remarkability"),
                'finds__remarkabilities__label__iexact'),
        'alterations':
            SearchAltName(
                pgettext_lazy("key for text search", "alterations"),
                'finds__alterations__label__iexact'),
        'alteration_causes':
            SearchAltName(
                pgettext_lazy("key for text search", "alteration-causes"),
                'finds__alteration_causes__label__iexact'),
        'treatment_emergency':
            SearchAltName(
                pgettext_lazy("key for text search", "treatment-emergency"),
                'finds__treatment_emergency__label__iexact'),
        'description':
            SearchAltName(
                pgettext_lazy("key for text search", "find-description"),
                'finds__description__iexact'),
        'empty': SearchAltName(
            pgettext_lazy("key for text search", u"empty"),
            'finds'
        ),
        'no_finds': SearchAltName(
            pgettext_lazy("key for text search", u"no-associated-finds"),
            'finds_ref'
        ),

    }
    REVERSED_BOOL_FIELDS = [
        'documents__image__isnull',
        'documents__associated_file__isnull',
        'documents__associated_url__isnull',
    ]
    REVERSED_MANY_COUNTED_FIELDS = ['finds', 'finds_ref']

    ALT_NAMES.update(LightHistorizedItem.ALT_NAMES)
    ALT_NAMES.update(DocumentItem.ALT_NAMES)

    DYNAMIC_REQUESTS = {
        'division': DynamicRequest(
            label=_("Division -"),
            app_name='archaeological_warehouse', model_name='WarehouseDivision',
            form_key='division',
            search_key=pgettext_lazy("key for text search",
                                     'division'),
            type_query='division__division__division__txt_idx',
            search_query='division__reference__iexact'
        ),
    }

    QA_LOCK = QuickAction(
        url="container-qa-lock", icon_class="fa fa-lock",
        text=_(u"Lock/Unlock"), target="many",
        rights=['change_container', 'change_own_container']
    )
    QUICK_ACTIONS = [QA_LOCK]

    objects = UUIDModelManager()

    # fields
    uuid = models.UUIDField(default=uuid.uuid4)
    location = models.ForeignKey(
        Warehouse, verbose_name=_(u"Location (warehouse)"),
        related_name='containers')
    responsible = models.ForeignKey(
        Warehouse, verbose_name=_(u"Responsible warehouse"),
        related_name='owned_containers')
    container_type = models.ForeignKey(ContainerType,
                                       verbose_name=_("Container type"))
    reference = models.TextField(_(u"Container ref."))
    comment = models.TextField(_(u"Comment"), null=True, blank=True)
    cached_label = models.TextField(_(u"Localisation"), null=True, blank=True,
                                    db_index=True)
    cached_location = models.TextField(_(u"Cached location"),
                                       null=True, blank=True, db_index=True)
    cached_division = models.TextField(_(u"Cached division"),
                                       null=True, blank=True, db_index=True)
    index = models.IntegerField(u"Container ID", default=0)
    old_reference = models.TextField(_(u"Old reference"), blank=True, null=True)
    external_id = models.TextField(_(u"External ID"), blank=True, null=True)
    auto_external_id = models.BooleanField(
        _(u"External ID is set automatically"), default=False)
    documents = models.ManyToManyField(
        Document, related_name='containers', verbose_name=_(u"Documents"),
        blank=True)
    main_image = models.ForeignKey(
        Document, related_name='main_image_containers',
        on_delete=models.SET_NULL,
        verbose_name=_(u"Main image"), blank=True, null=True)

    class Meta:
        verbose_name = _(u"Container")
        verbose_name_plural = _(u"Containers")
        ordering = ('cached_label',)
        unique_together = ('index', 'responsible')
        permissions = (
            ("view_container", u"Can view all Containers"),
            ("view_own_container", u"Can view own Container"),
            ("add_own_container", u"Can add own Container"),
            ("change_own_container", u"Can change own Container"),
            ("delete_own_container", u"Can delete own Container"),
        )
        indexes = [
            GinIndex(fields=['data']),
        ]

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

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

    def _generate_cached_label(self):
        items = [self.reference, self.precise_location]
        cached_label = u" | ".join(items)
        return cached_label

    def _generate_cached_location(self):
        items = [self.location.name, str(self.index)]
        cached_label = u" - ".join(items)
        return cached_label

    def _generate_cached_division(self):
        locas = [
            u"{} {}".format(loca.division.division, loca.reference)
            for loca in ContainerLocalisation.objects.filter(
                container=self)
        ]
        return u" | ".join(locas)

    def _get_base_image_path(self):
        return self.responsible._get_base_image_path() + u"/" + self.external_id

    def merge(self, item, keep_old=False):
        locas = [
            cl.division.division.txt_idx
            for cl in ContainerLocalisation.objects.filter(container=self).all()
        ]
        for loca in ContainerLocalisation.objects.filter(container=item).all():
            if loca.division.division.txt_idx not in locas:
                loca.container = self
                loca.save()
            else:
                loca.delete()
        super(Container, self).merge(item, keep_old=keep_old)

    @classmethod
    def get_query_owns(cls, ishtaruser):
        return Q(history_creator=ishtaruser.user_ptr) | \
            Q(location__person_in_charge__ishtaruser=ishtaruser) | \
            Q(responsible__person_in_charge__ishtaruser=ishtaruser)

    def get_precise_points(self):
        precise_points = super(Container, self).get_precise_points()
        if precise_points:
            return precise_points
        return self.location.get_precise_points()

    def get_town_centroid(self):
        return self.location.get_town_centroid()

    def get_town_polygons(self):
        return self.location.get_town_polygons()

    @property
    def associated_filename(self):
        filename = datetime.date.today().strftime('%Y-%m-%d')
        filename += u'-' + self.reference
        filename += u"-" + self.location.name
        filename += u"-" + str(self.index)
        if self.cached_division is None:
            self.skip_history_when_saving = True
            self.save()
        if self.cached_division:
            filename += u"-" + self.cached_division
        return slugify(filename)

    @property
    def precise_location(self):
        location = self.location.name
        if self.cached_division:
            location += u" " + self.cached_division
        return location

    def get_localisations(self):
        """
        Get precise localisation of the container in the warehouse.

        :return: tuple of strings with localisations
        """
        return tuple((
            loca.reference
            for loca in ContainerLocalisation.objects.filter(
                container=self).order_by('division__order')
        ))

    def get_localisation(self, place):
        locas = self.get_localisations()
        if len(locas) < (place + 1):
            return ""
        return locas[place]

    @property
    def localisation_1(self):
        return self.get_localisation(0)

    @property
    def localisation_2(self):
        return self.get_localisation(1)

    @property
    def localisation_3(self):
        return self.get_localisation(2)

    @property
    def localisation_4(self):
        return self.get_localisation(3)

    @property
    def localisation_5(self):
        return self.get_localisation(4)

    @property
    def localisation_6(self):
        return self.get_localisation(5)

    @property
    def localisation_7(self):
        return self.get_localisation(6)

    @property
    def localisation_8(self):
        return self.get_localisation(7)

    @property
    def localisation_9(self):
        return self.get_localisation(8)

    def set_localisation(self, place, value):
        """
        Set the reference for the localisation number "place" (starting from 0)
        :param place: the number of the localisation
        :param value: the reference to be set
        :return: the container location object or None if the place does not
        exist
        """
        q = WarehouseDivisionLink.objects.filter(
            warehouse=self.location).order_by('order')
        for idx, division_link in enumerate(q.all()):
            if idx == place:
                break
        else:
            return
        dct = {'container': self, 'division': division_link}
        if not value:
            if ContainerLocalisation.objects.filter(**dct).count():
                c = ContainerLocalisation.objects.filter(**dct).all()[0]
                c.delete()
            return
        dct['defaults'] = {'reference': value}
        obj, created = ContainerLocalisation.objects.update_or_create(**dct)
        return obj

    @post_importer_action
    def set_localisation_1(self, context, value):
        return self.set_localisation(0, value)
    set_localisation_1.post_save = True

    @post_importer_action
    def set_localisation_2(self, context, value):
        return self.set_localisation(1, value)
    set_localisation_2.post_save = True

    @post_importer_action
    def set_localisation_3(self, context, value):
        return self.set_localisation(2, value)
    set_localisation_3.post_save = True

    @post_importer_action
    def set_localisation_4(self, context, value):
        return self.set_localisation(3, value)
    set_localisation_4.post_save = True

    @post_importer_action
    def set_localisation_5(self, context, value):
        return self.set_localisation(4, value)
    set_localisation_5.post_save = True

    @post_importer_action
    def set_localisation_6(self, context, value):
        return self.set_localisation(5, value)
    set_localisation_6.post_save = True

    @post_importer_action
    def set_localisation_7(self, context, value):
        return self.set_localisation(6, value)
    set_localisation_7.post_save = True

    @post_importer_action
    def set_localisation_8(self, context, value):
        return self.set_localisation(7, value)
    set_localisation_8.post_save = True

    @post_importer_action
    def set_localisation_9(self, context, value):
        return self.set_localisation(8, value)
    set_localisation_9.post_save = True

    def get_extra_actions(self, request):
        """
        extra actions for the sheet template
        """
        # url, base_text, icon, extra_text, extra css class, is a quick action
        actions = super(Container, self).get_extra_actions(request)
        can_edit_find = self.can_do(request, 'change_find')
        if can_edit_find:
            actions += [
                (reverse('container-add-treatment', args=[self.pk]),
                 _(u"Add treatment"), "fa fa-flask", "", "", False),
            ]
        return actions

    def pre_save(self):
        if not self.index and self.responsible_id:
            q = Container.objects.filter(responsible=self.responsible)
            if q.count():
                self.index = int(q.aggregate(Max("index"))["index__max"]) + 1
            else:
                self.index = 1

    def save(self, *args, **kwargs):
        self.pre_save()
        super(Container, self).save(*args, **kwargs)

        updated = False
        if not self.index:
            self.skip_history_when_saving = True
            q = Container.objects.filter(responsible=self.responsible).exclude(
                pk=self.pk).order_by('-index')
            if q.count():
                self.index = q.all()[0].index + 1
            else:
                self.index = 1
            updated = True

        self.skip_history_when_saving = True
        if not self.external_id or self.auto_external_id:
            external_id = get_external_id('container_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

        # remove old location in warehouse
        q = ContainerLocalisation.objects.filter(container=self).exclude(
            division__warehouse=self.location)
        for loca in q.all():
            loca.delete()


def container_post_save(sender, **kwargs):
    cached_label_and_geo_changed(sender=sender, **kwargs)
    if not kwargs.get('instance'):
        return
    instance = kwargs.get('instance')
    for loca in ContainerLocalisation.objects.filter(
            container=instance).exclude(
            division__warehouse=instance.location).all():
        q = WarehouseDivisionLink.objects.filter(
            warehouse=instance.location,
            division=loca.division.division
        )
        if not q.count():
            continue
        loca.division = q.all()[0]
        loca.save()


post_save.connect(container_post_save, sender=Container)
m2m_changed.connect(document_attached_changed,
                    sender=Container.documents.through)


class ContainerLocalisationManager(models.Manager):
    def get_by_natural_key(self, container, warehouse, division):
        return self.get(container__uuid=container,
                        division__warehouse__uuid=warehouse,
                        division__division__txt_idx=division)


class ContainerLocalisation(models.Model):
    container = models.ForeignKey(Container, verbose_name=_(u"Container"),
                                  related_name='division')
    division = models.ForeignKey(WarehouseDivisionLink,
                                 verbose_name=_(u"Division"))
    reference = models.CharField(_(u"Reference"), max_length=200, default='')
    objects = ContainerLocalisationManager()

    class Meta:
        verbose_name = _(u"Container localisation")
        verbose_name_plural = _(u"Container localisations")
        unique_together = ('container', 'division')
        ordering = ('container', 'division__order')

    def __str__(self):
        lbl = u" - ".join((str(self.container),
                           str(self.division), self.reference))
        return lbl

    def natural_key(self):
        return self.container.uuid, self.division.warehouse.uuid,\
               self.division.division.txt_idx

    def save(self, *args, **kwargs):
        super(ContainerLocalisation, self).save(*args, **kwargs)
        self.container.skip_history_when_saving = True
        cached_label_changed(Container, instance=self.container,
                             force_update=True)