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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
|
#!/usr/bin/env python3
# -*- 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 collections import OrderedDict
import datetime
import uuid
from django.conf import settings
from django.contrib.gis.db import models
from django.contrib.postgres.indexes import GinIndex
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.db.models import Q, Max, Count
from django.db.models.signals import post_save, post_delete, m2m_changed, pre_delete
from django.template.defaultfilters import slugify
from ishtar_common.utils import ugettext_lazy as _, pgettext_lazy
from django.apps import apps
from ishtar_common.data_importer import post_importer_action, pre_importer_action
from ishtar_common.model_managers import ExternalIdManager, UUIDModelManager
from ishtar_common.models import ValueGetter, get_current_profile
from ishtar_common.models_common import (
GeneralType,
LightHistorizedItem,
OwnPerms,
Address,
post_save_cache,
DashboardFormItem,
document_attached_changed,
SearchAltName,
DynamicRequest,
GeoItem,
CompleteIdentifierItem,
SearchVectorConfig,
DocumentItem,
QuickAction,
MainItem,
Merge,
)
from ishtar_common.model_merging import merge_model_objects
from ishtar_common.utils import (
cached_label_changed,
cached_label_and_geo_changed,
get_generated_id,
)
from ishtar_common.data_importer import ImporterError
class DivisionContainer(DashboardFormItem):
DIVISION_TEMPLATE = """<a class="display_details"
href="#" onclick="load_window('/show-container/{id}/')">
<i class="fa fa-info-circle" aria-hidden="true"></i>
</a> {container} {ref}"""
BASE_QUERY_LOCATION = "location"
@property
def pk(self):
# of course implemented by models.Model
raise NotImplemented
def get_max_division_number(self):
raise NotImplemented
@property
def start_division_number(self):
raise NotImplemented
@property
def division_labels(self):
if not self.get_max_division_number() + 1:
return []
start = self.start_division_number
return [
"{} {}".format(_("Level"), idx + start + 1)
for idx in range(self.get_max_division_number() + 1)
]
@property
def number_divisions(self):
q = {
self.BASE_QUERY_LOCATION + "__id": self.pk,
"container_type__stationary": True,
}
return Container.objects.filter(**q).count()
@property
def number_containers(self):
q = {
self.BASE_QUERY_LOCATION + "__id": self.pk,
"container_type__stationary": False,
}
return Container.objects.filter(**q).count()
@property
def number_of_finds_hosted(self):
Find = apps.get_model("archaeological_finds", "Find")
q = {
"container__{}__id".format(self.BASE_QUERY_LOCATION): self.pk,
}
return Find.objects.filter(**q).count()
@property
def number_of_finds(self):
Find = apps.get_model("archaeological_finds", "Find")
q = {
"container_ref__{}__id".format(self.BASE_QUERY_LOCATION): self.pk,
}
return Find.objects.filter(**q).count()
@property
def number_of_containers(self):
return Container.objects.filter(**{self.BASE_QUERY_LOCATION: self}).count()
def _number_of_finds_by_place(self):
Find = apps.get_model("archaeological_finds", "Find")
return self._number_of_items_by_place(
Find, division_key="inside_container__container__"
)
@property
def number_of_finds_by_place(self, update=False):
return self._get_or_set_stats(
"_number_of_finds_by_place", update, expected_type=list
)
def _number_of_containers_by_place(self):
return self._number_of_items_by_place(
ContainerTree, "container_parent__", "container__children"
)
@property
def number_of_containers_by_place(self, update=False):
return self._get_or_set_stats(
"_number_of_containers_by_place", update, expected_type=list
)
def _get_divisions(self, current_path, remaining_division, depth=0):
if not remaining_division:
return [current_path]
remaining_division.pop(0)
query_location = self.BASE_QUERY_LOCATION
for __ in range(depth):
query_location = "parent__" + query_location
base_q = Container.objects.filter(**{query_location: self})
q = base_q
if self.BASE_QUERY_LOCATION == "location":
exclude = "parent_"
for idx in range(depth):
exclude += "_parent_"
q = base_q.filter(**{exclude + "id": None})
elif not depth and not current_path:
q = base_q.filter(parent_id=self.pk)
for idx, p in enumerate(reversed(current_path)):
parent_id, __ = p
key = "parent__" * (idx + 1) + "id"
q = q.filter(**{key: parent_id})
res = []
old_ref, ct = None, None
if not q.count():
return [current_path]
q = q.values(
"id", "reference", "container_type__label", "container_type_id"
).order_by("container_type__label", "reference")
for ref in q.all():
if ref["reference"] == old_ref and ref["container_type__label"] == ct:
continue
old_ref = ref["reference"]
ct = ref["container_type__label"]
cpath = current_path[:]
lbl = self.DIVISION_TEMPLATE.format(
id=ref["id"],
container=ref["container_type__label"],
ref=ref["reference"],
)
cpath.append((ref["id"], lbl))
query = {
"containers__parent__reference": ref["reference"],
"containers__parent__container_type_id": ref["container_type_id"],
"containers__" + self.BASE_QUERY_LOCATION: self,
}
remaining_division = list(ContainerType.objects.filter(**query).distinct())
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 container type and the full reference.
"""
q = {"containers__" + self.BASE_QUERY_LOCATION: self}
if self.BASE_QUERY_LOCATION == "location":
q["containers__parent"] = None
top_divisions = list(ContainerType.objects.filter(**q).distinct())
divisions = self._get_divisions([], top_divisions)
return divisions
def _number_of_items_by_place(self, model, division_key, count_filter=None):
res = {}
paths = self.available_division_tuples[:]
for path in paths:
cpath = []
for container_id, lbl in path:
cpath.append((container_id, lbl))
if tuple(cpath) in res:
continue
q = model.objects
for idx, p in enumerate(reversed(cpath)):
container_id, __ = p
div_key = division_key + "parent__" * idx
attrs = {div_key + "id": container_id}
q = q.filter(**attrs)
if count_filter:
q = q.filter(**{count_filter: None})
res[tuple(cpath)] = q.distinct().count()
res = [(k, res[k]) for k in res]
final_res, current_res, depth = [], [], 1
len_divisions = self.get_max_division_number() + 1
for path, nb in sorted(res, key=lambda x: (len(x[0]), x[0])):
if len(path) > len_divisions:
continue
if depth != len(path):
final_res.append(current_res[:])
current_res = []
depth = len(path)
if path[-1] == "-":
continue
path = [k[1] for k in path]
path = path + ["" for __ in range(len_divisions - len(path))]
current_res.append((path, nb))
final_res.append(current_res[:])
return final_res
class WarehouseType(GeneralType):
class Meta:
verbose_name = _("Warehouse type")
verbose_name_plural = _("Warehouse types")
ordering = ("label",)
post_save.connect(post_save_cache, sender=WarehouseType)
post_delete.connect(post_save_cache, sender=WarehouseType)
NO_DIVISION_ERROR = _("The division number {} has not been set for the warehouse {}.")
class Warehouse(
Address,
DocumentItem,
GeoItem,
CompleteIdentifierItem,
OwnPerms,
MainItem,
DivisionContainer,
ValueGetter,
):
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"),
]
COL_LABELS = {
"warehouse_type__label": _("Type"),
}
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=_("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(_("Name"), max_length=200)
warehouse_type = models.ForeignKey(WarehouseType, verbose_name=_("Warehouse type"))
person_in_charge = models.ForeignKey(
"ishtar_common.Person",
on_delete=models.SET_NULL,
related_name="warehouse_in_charge",
verbose_name=_("Person in charge"),
null=True,
blank=True,
)
organization = models.ForeignKey(
"ishtar_common.Organization",
blank=True,
null=True,
related_name="warehouses",
verbose_name=_("Organization"),
on_delete=models.SET_NULL,
)
comment = models.TextField(_("Comment"), blank=True, default="")
associated_divisions = models.ManyToManyField(
"WarehouseDivision",
verbose_name=_("Divisions"),
blank=True,
through="WarehouseDivisionLink",
)
documents = models.ManyToManyField(
"ishtar_common.Document",
related_name="warehouses",
verbose_name=_("Documents"),
blank=True,
)
main_image = models.ForeignKey(
"ishtar_common.Document",
related_name="main_image_warehouses",
on_delete=models.SET_NULL,
verbose_name=_("Main image"),
blank=True,
null=True,
)
external_id = models.TextField(_("External ID"), blank=True, default="")
auto_external_id = models.BooleanField(
_("External ID is set automatically"), default=False
)
max_division_number = models.IntegerField(
_("Maximum number of divisions"),
default=0,
help_text=_("Automatically generated"),
)
SUB_ADDRESSES = ["organization", "person_in_charge"]
class Meta:
verbose_name = _("Warehouse")
verbose_name_plural = _("Warehouses")
permissions = (
("view_warehouse", "Can view all Warehouses"),
("view_own_warehouse", "Can view own Warehouse"),
("add_own_warehouse", "Can add own Warehouse"),
("change_own_warehouse", "Can change own Warehouse"),
("delete_own_warehouse", "Can delete own Warehouse"),
)
indexes = [
GinIndex(fields=["data"]),
]
def __str__(self):
return self.name
def get_container_type_by_place(self, place: int):
"""
Container type by place based on the default organisation of the
warehouse
:param place: place number
:return: container type, other location or None, None
"""
q = WarehouseDivisionLink.objects.filter(warehouse=self).order_by("order")
previous_container_types = []
for idx, division_link in enumerate(q.all()):
if idx == place:
current_container_type = division_link.container_type
break
previous_container_types.append(division_link.container_type_id)
else:
return None, None
return current_container_type, previous_container_types
@post_importer_action
def add_localisations(self, context, value):
self._add_localisations(context, value)
add_localisations.post_save = True
def _add_localisations(self, context, value, return_errors=False):
"""
Add localisations for this warehouse.
Get the default localisation types and set each reference from the
value separated by ";"
:param value: references of localisations separated by ;
:param return_errors: return error message default is False
:return: return an error message if return_errors set to True
"""
value = value.strip()
if not value:
if return_errors:
return None, _("No value")
return
import_object = None
if context and "import_object" in context:
import_object = context["import_object"]
TMP_SEMI_COLON = "|#|#|"
value = value.replace("\\;", TMP_SEMI_COLON) # manage ";" used by a ref
values = value.split(";")
divisions = list(
WarehouseDivisionLink.objects.filter(warehouse=self).order_by("order")
)
parent = None
for idx, value in enumerate(values):
if idx >= len(divisions):
if return_errors:
return str(
_(
"{} values for only {} default divisions set for "
"warehouse {}"
)
).format(len(values), len(divisions), self.name)
return
value = value.replace(TMP_SEMI_COLON, ";").strip()
if not value or value == "-":
continue
parent, created = Container.objects.get_or_create(
location=self,
reference=value,
container_type_id=divisions[idx].container_type_id,
parent=parent,
)
if created and import_object:
parent.imports.add(import_object)
@property
def short_label(self):
return self.name
def natural_key(self):
return (self.uuid,)
def _get_base_image_path(self):
return "{}/{}".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)
OrganizationType = apps.get_model("ishtar_common", "OrganizationType")
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
Organization = apps.get_model("ishtar_common", "Organization")
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)
elif k in ("precise_town", "email"):
setattr(self, k, None)
else:
setattr(self, k, "")
self.save()
def get_max_division_number(self):
return self.max_division_number
@property
def start_division_number(self):
return 0
@property
def default_location_types(self):
return [
wd.container_type.label
for wd in WarehouseDivisionLink.objects.filter(warehouse=self)
.order_by("order")
.all()
if wd.container_type
]
@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}]
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.containers.all()):
if Container.objects.filter(index=container.index, location=self).count():
container.index = (
Container.objects.filter(location=self)
.exclude(id=container.id)
.all()
.aggregate(Max("index"))["index__max"]
+ 1
)
container.location = 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_generated_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 WarehouseDivision(GeneralType):
class Meta:
verbose_name = _("Warehouse division type")
verbose_name_plural = _("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, container_type):
return self.get(
warehouse__uuid=warehouse, container_type__txt_idx=container_type
)
class ContainerType(GeneralType):
stationary = models.BooleanField(
_("Stationary"),
default=False,
help_text=_(
"Container that will not usually be moved. Ex: building, "
"room, span, shelf. Stationary containers are not automatically numbered."
),
)
length = models.IntegerField(_("Length (mm)"), blank=True, null=True)
width = models.IntegerField(_("Width (mm)"), blank=True, null=True)
height = models.IntegerField(_("Height (mm)"), blank=True, null=True)
volume = models.FloatField(_("Volume (l)"), blank=True, null=True)
tare_weight = models.FloatField(_("Tare weight (g)"), blank=True, null=True)
reference = models.CharField(_("Ref."), max_length=300, blank=True, null=True)
order = models.IntegerField(_("Order"), default=10)
class Meta:
verbose_name = _("Container type")
verbose_name_plural = _("Container types")
ordering = (
"order",
"label",
)
post_save.connect(post_save_cache, sender=ContainerType)
post_delete.connect(post_save_cache, sender=ContainerType)
class WarehouseDivisionLink(models.Model):
RELATED_SET_NAME = "divisions"
RELATED_ATTRS = ["order", "container_type"]
RELATIVE_MODELS = {Warehouse: "warehouse"}
warehouse = models.ForeignKey(Warehouse, related_name="divisions")
container_type = models.ForeignKey(ContainerType, blank=True, null=True)
division = models.ForeignKey(
WarehouseDivision, help_text=_("Deprecated - do not use"), blank=True, null=True
)
order = models.IntegerField(_("Order"), default=10)
objects = WarehouseDivisionLinkManager()
class Meta:
ordering = ("warehouse", "order")
unique_together = ("warehouse", "division")
def __str__(self):
return "{} - {}".format(self.warehouse, self.container_type)
def natural_key(self):
return self.warehouse.uuid, self.container_type.txt_idx
class ContainerTree(models.Model):
CREATE_SQL = """
CREATE VIEW containers_tree AS
WITH RECURSIVE rel_tree AS (
SELECT c.id AS container_id, c.parent_id AS container_parent_id,
1 AS level
FROM archaeological_warehouse_container c
WHERE c.parent_id is NOT NULL
UNION ALL
SELECT p.container_id AS container_id,
c.parent_id as container_parent_id,
p.level + 1
FROM archaeological_warehouse_container c, rel_tree p
WHERE c.id = p.container_parent_id
AND c.parent_id is NOT NULL
AND p.level < 10 -- prevent recursive...
)
SELECT DISTINCT container_id, container_parent_id, level
FROM rel_tree;
CREATE VIEW container_tree AS
SELECT DISTINCT y.container_id, y.container_parent_id
FROM (SELECT * FROM containers_tree) y
ORDER BY y.container_id, y.container_parent_id;
-- deactivate deletion, update
CREATE RULE containers_tree_del AS
ON DELETE TO containers_tree
DO INSTEAD DELETE FROM archaeological_warehouse_container where id=NULL;
CREATE RULE container_tree_del AS
ON DELETE TO container_tree
DO INSTEAD DELETE FROM archaeological_warehouse_container where id=NULL;
CREATE RULE containers_tree_update AS
ON UPDATE TO containers_tree
DO INSTEAD UPDATE archaeological_warehouse_container set id=id
WHERE id=NULL;
CREATE RULE container_tree_update AS
ON UPDATE TO container_tree
DO INSTEAD UPDATE archaeological_warehouse_container set id=id
WHERE id=NULL;
CREATE RULE containers_tree_insert AS
ON INSERT TO containers_tree
DO INSTEAD UPDATE archaeological_warehouse_container set id=id
WHERE id=NULL;
CREATE RULE container_tree_insert AS
ON INSERT TO container_tree
DO INSTEAD UPDATE archaeological_warehouse_container set id=id
WHERE id=NULL;
"""
DELETE_SQL = """
DROP VIEW IF EXISTS container_tree;
DROP VIEW IF EXISTS containers_tree;
"""
container = models.OneToOneField(
"archaeological_warehouse.Container",
verbose_name=_("Container"),
related_name="container_tree_child",
primary_key=True,
)
container_parent = models.ForeignKey(
"archaeological_warehouse.Container",
verbose_name=_("Container parent"),
related_name="container_tree_parent",
)
class Meta:
managed = False
db_table = "containers_tree"
class Container(
DocumentItem,
Merge,
LightHistorizedItem,
CompleteIdentifierItem,
GeoItem,
OwnPerms,
MainItem,
DivisionContainer,
ValueGetter,
):
SLUG = "container"
APP = "archaeological-warehouse"
MODEL = "container"
SHOW_URL = "show-container"
DELETE_URL = "delete-container"
NEW_QUERY_ENGINE = True
TABLE_COLS = [
"container_type__label",
"reference",
"location__name",
"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"),
]
PARENT_SEARCH_VECTORS = ["parent"]
STATISTIC_MODALITIES_OPTIONS = OrderedDict(
[
("location__name", _("Location (warehouse)")),
("responsibility__name", _("Responsibility (warehouse)")),
(
"finds__base_finds__context_record__operation__cached_label",
_("Operation"),
),
("container_type__label", _("Container type")),
]
)
STATISTIC_MODALITIES = [key for key, lbl in STATISTIC_MODALITIES_OPTIONS.items()]
GET_VALUES_EXCLUDE_FIELDS = ValueGetter.GET_VALUES_EXCLUDE_FIELDS + [
"inside_container",
"parent",
]
# search parameters
EXTRA_REQUEST_KEYS = {
"location": "location__pk",
"location__name": "location__name",
"location_id": "location__pk",
"responsibility_id": "responsibility__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",
# dynamic tables
"container_tree_child__container_parent__id": "container_tree_child__container_parent__id",
}
COL_LABELS = {
"cached_location": _("Location - index"),
"cached_division": _("Precise localisation"),
"container_type__label": _("Type"),
"location__name": _("Warehouse"),
}
GEO_LABEL = "cached_label"
CACHED_LABELS = [
"cached_division",
"cached_label",
"cached_location",
"cached_weight",
]
# alternative names of fields for searches
ALT_NAMES = {
"location_name": SearchAltName(
pgettext_lazy("key for text search", "location"), "location__name__iexact"
),
"responsibility_name": SearchAltName(
pgettext_lazy("key for text search", "responsibility"),
"responsibility__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", "empty"), "finds"),
"parent": SearchAltName(
pgettext_lazy("key for text search", "parent-container"),
"parent__cached_label__iexact"
),
"contain_containers": SearchAltName(
pgettext_lazy("key for text search", "contain-containers"),
"children__isnull",
),
"is_stationary": SearchAltName(
pgettext_lazy("key for text search", "is-stationary"),
"container_type__stationary",
),
}
REVERSED_BOOL_FIELDS = [
"children__isnull",
"documents__image__isnull",
"documents__associated_file__isnull",
"documents__associated_url__isnull",
]
BOOL_FIELDS = ["container_type__stationary"]
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_EDIT = QuickAction(
url="container-qa-bulk-update",
icon_class="fa fa-pencil",
text=_("Bulk update"),
target="many",
rights=["change_container", "change_own_container"],
)
QA_LOCK = QuickAction(
url="container-qa-lock",
icon_class="fa fa-lock",
text=_("Lock/Unlock"),
target="many",
rights=["change_container", "change_own_container"],
)
QUICK_ACTIONS = [QA_EDIT, QA_LOCK]
BASE_QUERY_LOCATION = "container_tree_child__container_parent"
objects = UUIDModelManager()
# fields
uuid = models.UUIDField(default=uuid.uuid4)
location = models.ForeignKey(
Warehouse, verbose_name=_("Location (warehouse)"), related_name="containers"
)
responsible = models.ForeignKey(
Warehouse,
verbose_name=_("Responsible warehouse"),
related_name="owned_containers",
blank=True,
null=True,
help_text=_("Deprecated - do not use"),
)
responsibility = models.ForeignKey(
Warehouse,
verbose_name=_("Responsibility"),
related_name="responsibilities",
blank=True,
null=True,
help_text=_("Warehouse that owns the container"),
)
container_type = models.ForeignKey(
ContainerType, verbose_name=_("Container type"), related_name="containers"
)
reference = models.TextField(_("Container ref."))
comment = models.TextField(_("Comment"), blank=True, default="")
cached_label = models.TextField(
_("Localisation"), blank=True, default="", db_index=True
)
cached_location = models.TextField(
_("Cached location"), blank=True, default="", db_index=True
)
cached_division = models.TextField(
_("Cached division"), blank=True, default="", db_index=True
)
parent = models.ForeignKey(
"Container",
verbose_name=_("Parent container"),
on_delete=models.SET_NULL,
related_name="children",
blank=True,
null=True,
)
index = models.IntegerField(_("Container ID"), blank=True, null=True, db_index=True)
weight = models.FloatField(_("Measured weight (g)"), blank=True, null=True)
calculated_weight = models.FloatField(
_("Calculated weight (g)"), blank=True, null=True
)
cached_weight = models.FloatField(
_("Cached weight (g)"),
blank=True,
null=True,
help_text=_("Entered weight if available otherwise calculated weight."),
)
old_reference = models.TextField(_("Old reference"), blank=True, default="")
external_id = models.TextField(_("External ID"), blank=True, default="")
auto_external_id = models.BooleanField(
_("External ID is set automatically"), default=False
)
documents = models.ManyToManyField(
"ishtar_common.Document",
related_name="containers",
verbose_name=_("Documents"),
blank=True,
)
main_image = models.ForeignKey(
"ishtar_common.Document",
related_name="main_image_containers",
on_delete=models.SET_NULL,
verbose_name=_("Main image"),
blank=True,
null=True,
)
DISABLE_POLYGONS = False
MERGE_ATTRIBUTE = "get_merge_key"
MERGE_STRING_FIELDS = ["old_reference"]
class Meta:
verbose_name = _("Container")
verbose_name_plural = _("Containers")
ordering = (
"location",
"index",
"cached_label",
)
unique_together = [("location", "container_type", "parent", "reference")]
permissions = (
("view_container", "Can view all Containers"),
("view_own_container", "Can view own Container"),
("add_own_container", "Can add own Container"),
("change_own_container", "Can change own Container"),
("delete_own_container", "Can delete own Container"),
)
indexes = [
GinIndex(fields=["data"]),
]
def __str__(self):
return self.cached_label or ""
@property
def start_division_number(self):
depth = 1
parent = self.parent
parents = []
while parent and parent.pk not in parents:
parents.append(parent.pk)
depth += 1
parent = parent.parent
return depth
def get_max_division_number(self):
return self.location.get_max_division_number() - self.start_division_number
@property
def number_of_finds_hosted(self):
count = super(Container, self).number_of_finds_hosted
Find = apps.get_model("archaeological_finds", "Find")
count += Find.objects.filter(container_id=self.pk).count()
return count
@property
def number_of_finds(self):
count = super(Container, self).number_of_finds
Find = apps.get_model("archaeological_finds", "Find")
count += Find.objects.filter(container_ref_id=self.pk).count()
return count
@property
def name(self):
return "{} - {}".format(self.container_type.name, self.reference)
@property
def short_label(self):
return "{} {}".format(self.container_type.label, self.reference)
@property
def parent_external_id(self):
if not self.parent:
return self.location.external_id
return self.parent.external_id
@property
def calculated_weight_kg(self):
if self.calculated_weight:
return self.calculated_weight / 1000
@property
def weight_kg(self):
if self.weight:
return self.weight / 1000
def natural_key(self):
return (self.uuid,)
@classmethod
@pre_importer_action
def import_get_location(cls, context, value):
if context.get("container_type", None) and context.get("reference", None):
try:
context["location"] = Warehouse.objects.get(external_id=value)
return
except Warehouse.DoesNotExist:
pass
for k in list(context.keys()):
if k != "import_get_location":
context.pop(k)
def _generate_cached_label(self):
return self.precise_location
def _generate_cached_location(self):
if not self.index:
return self.location.name
items = [self.location.name, str(self.index)]
cached_label = " - ".join(items)
return cached_label
def _generate_cached_weight(self):
return self.weight if self.weight else self.calculated_weight
def _calculate_weight(self) -> bool:
"""
Calculate the weight of the contained finds + tare weight of the
container
:return: True if calculated weight is changed
"""
profile = get_current_profile()
if (
profile.calculate_weight_on_full
and self.finds.filter(weight__isnull=True).count()
):
weight = None
else:
weight = sum(
w
for w in self.finds.filter(weight__isnull=False)
.values_list("weight", flat=True)
.all()
)
weight += (
self.container_type.tare_weight
if self.container_type.tare_weight
else 0
)
if weight != self.calculated_weight:
self.calculated_weight = weight
return True
return False
@property
def get_calculated_weight_percent(self):
if not self.calculated_weight or not self.weight:
return 0
return (self.calculated_weight - self.weight) / self.calculated_weight * 100
@property
def get_cached_division(self):
return self._generate_cached_division()
@property
def get_merge_key(self):
try:
return str(self.location.uuid) + "|" + self._generate_cached_division()
except Warehouse.DoesNotExist:
return
def put_document_by_key(self, value, key):
Document = apps.get_model("ishtar_common", "Document")
try:
doc = Document.objects.get(**{key: value})
except Document.DoesNotExist:
raise ImporterError(
str(_("Document with {}: {} does not " "exists")).format(key, value)
)
except Document.MultipleObjectsReturned:
raise ImporterError(
str(_("Multiple document with {}: {}")).format(key, value)
)
doc.container_id = self.pk
doc.container_ref_id = self.pk
doc.skip_history_when_saving = True
doc.save()
@post_importer_action
def put_document_by_external_id(self, context, value):
self.put_document_by_key(value, "external_id")
put_document_by_external_id.post_save = True
@post_importer_action
def put_document_by_reference(self, context, value):
self.put_document_by_key(value, "reference")
put_document_by_reference.post_save = True
@post_importer_action
def put_document_by_internal_reference(self, context, value):
self.put_document_by_key(value, "internal_reference")
put_document_by_internal_reference.post_save = True
@post_importer_action
def put_document_by_complete_identifier(self, context, value):
self.put_document_by_key(value, "complete_identifier")
put_document_by_complete_identifier.post_save = True
def _generate_cached_division(self):
parents = []
parent = self.parent
c_ids = []
while parent:
if parent.id in c_ids: # prevent cyclic
break
c_ids.append(parent.id)
parents.append(parent)
parent = parent.parent
locas = [
"{} {}".format(loca.container_type.name, loca.reference)
for loca in reversed(parents)
]
try:
locas.append("{} {}".format(self.container_type.name, self.reference))
except ObjectDoesNotExist: # generate too early on item creation
pass
return " | ".join(locas)
def _get_base_image_path(self):
return self.location._get_base_image_path() + "/" + self.external_id
def _prevent_parent_infinite_loop(self) -> bool:
"""
Check there is no infinite loop in parents. If a loop is detected, set the
parent to null.
:return: True if parent has been changed
"""
parent = self.parent
parents = []
while parent:
if parent.id in parents:
self.parent = None # break the loop arbitrary
return True
parents.append(parent.id)
parent = parent.parent
return False
@classmethod
def _change_child_location(cls, parent):
for child in (
cls.objects.filter(parent=parent).exclude(location=parent.location).all()
):
if child.location != parent.location:
child.location = parent.location
child.save()
cls._change_child_location(child)
def merge(self, item, keep_old=False, exclude_fields=None):
# merge child containers
child_references = {}
for child in Container.objects.filter(parent=self).all():
key = (child.reference, child.container_type.pk)
child_references[key] = child
for child in Container.objects.filter(parent=item).all():
key = (child.reference, child.container_type.pk)
if key in child_references.keys():
# parent field can cause integrity error before the end of
# the merge
child_references[key].index = None
child_references[key].merge(child, exclude_fields=["parent"])
else:
child.parent = self
child.save()
self._change_child_location(self)
super(Container, self).merge(
item, keep_old=keep_old, exclude_fields=exclude_fields
)
@classmethod
def get_query_owns(cls, ishtaruser):
return Q(history_creator=ishtaruser.user_ptr) | Q(
location__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()
def contained_documents(self):
if not self.pk:
return
Document = apps.get_model("ishtar_common", "Document")
return Document.objects.filter(container_id=self.pk)
def contained_documents_ref(self):
if not self.pk:
return
Document = apps.get_model("ishtar_common", "Document")
return Document.objects.filter(container_ref_id=self.pk)
@property
def associated_filename(self):
filename = datetime.date.today().strftime("%Y-%m-%d")
filename += "-" + self.reference
filename += "-" + self.location.name
filename += "-" + str(self.index)
if self.cached_division is None:
self.skip_history_when_saving = True
self.save()
if self.cached_division:
filename += "-" + self.cached_division
return slugify(filename)
@property
def precise_location(self):
location = self.location.name
if self.cached_division:
location += " " + self.cached_division
return location
def get_localisations(self):
"""
Get precise localisation of the container in the warehouse.
:return: tuple of strings with localisations
"""
localisations = []
parent = self.parent
while parent:
localisations.append(parent)
parent = parent.parent
localisations.append(self.location)
return reversed(localisations)
def get_localisation(self, place):
locas = list(self.get_localisations())
if len(locas) < (place + 1):
return ""
return locas[place]
@property
def localisation_1(self):
return self.get_localisation(1)
@property
def localisation_2(self):
return self.get_localisation(2)
@property
def localisation_3(self):
return self.get_localisation(3)
@property
def localisation_4(self):
return self.get_localisation(4)
@property
def localisation_5(self):
return self.get_localisation(5)
@property
def localisation_6(self):
return self.get_localisation(6)
@property
def localisation_7(self):
return self.get_localisation(7)
@property
def localisation_8(self):
return self.get_localisation(8)
@property
def localisation_9(self):
return self.get_localisation(9)
def set_localisation(self, place, value, static=False, return_errors=False):
"""
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
:param static: do not create new containers
:param return_errors: return error message
:return: the container location object or None if the place does not
exist - return also error message if return_errors set to True
"""
value = value.strip()
if not value or value == "-":
if return_errors:
return None, _("No value")
return
error_msg = str(NO_DIVISION_ERROR).format(place + 1, self.location)
(
current_container_type,
previous_container_types,
) = self.location.get_container_type_by_place(place)
if not current_container_type:
# no division link set at this place
if return_errors:
return None, error_msg
return
# modify existing
## first localisation is the warehouse
current_localisations = list(self.get_localisations())[1:]
current_localisation, current_parent = None, None
for loca in current_localisations:
if loca.container_type == current_container_type:
if loca.reference == value:
current_localisation = loca
break
elif loca.container_type_id in previous_container_types:
current_parent = loca
if not current_localisation:
dct = {
"reference": value,
"container_type": current_container_type,
"parent": current_parent,
"location": self.location,
}
q = Container.objects.filter(**dct)
if q.count():
current_localisation = q.all()[0]
else:
if static:
if return_errors:
error_msg = str(
_("The division {} {} do not exist for the " "location {}.")
).format(current_container_type, value, self.location)
return None, error_msg
return
current_localisation = Container.objects.create(**dct)
self.parent = current_localisation
self.save()
if return_errors:
return current_localisation, None
return current_localisation
def set_static_localisation(self, place, value):
return self.set_localisation(place, value, static=True)
@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
@post_importer_action
def set_static_localisation_1(self, context, value):
return self.set_static_localisation(0, value)
set_static_localisation_1.post_save = True
@post_importer_action
def set_static_localisation_2(self, context, value):
return self.set_static_localisation(1, value)
set_static_localisation_2.post_save = True
@post_importer_action
def set_static_localisation_3(self, context, value):
return self.set_static_localisation(2, value)
set_static_localisation_3.post_save = True
@post_importer_action
def set_static_localisation_4(self, context, value):
return self.set_static_localisation(3, value)
set_static_localisation_4.post_save = True
@post_importer_action
def set_static_localisation_5(self, context, value):
return self.set_static_localisation(4, value)
set_static_localisation_5.post_save = True
@post_importer_action
def set_static_localisation_6(self, context, value):
return self.set_static_localisation(5, value)
set_static_localisation_6.post_save = True
@post_importer_action
def set_static_localisation_7(self, context, value):
return self.set_static_localisation(6, value)
set_static_localisation_7.post_save = True
@post_importer_action
def set_static_localisation_8(self, context, value):
return self.set_static_localisation(7, value)
set_static_localisation_8.post_save = True
@post_importer_action
def set_static_localisation_9(self, context, value):
return self.set_static_localisation(8, value)
set_static_localisation_9.post_save = True
DOC_VALUES = [
(
"operation_",
_(
"Associated operation - use it with caution, "
"only return the first found operation"
),
),
(
"context_record_",
_(
"Associated context record - use it with caution, "
"only return the first found context_record"
),
),
("material_types", _("Material types inside the container - string")),
("material_types_code", _("Material types code - string")),
("finds", _("List of associated finds")),
]
def get_material_types_code(self) -> str:
"""
Return pipe separated material type code inside a container
"""
materials = set()
for material in self.finds.exclude(
material_types__code__isnull=True
).values_list("material_types__code", flat=True):
materials.add(material)
return "|".join(sorted(materials))
def get_material_types(self) -> str:
"""
Return comma separated string of material types inside a container
"""
materials = set()
for material in self.finds.exclude(
material_types__label__isnull=True
).values_list("material_types__label", flat=True):
materials.add(material)
return ", ".join(sorted(materials))
def get_values(self, prefix="", no_values=False, filtr=None, **kwargs):
values = super(Container, self).get_values(
prefix=prefix, no_values=no_values, filtr=filtr, **kwargs
)
from_find = prefix.startswith("container_") or prefix.startswith(
"container_ref_"
)
base_exclude = [prefix + "container", prefix + "container_ref"]
if (
(not filtr or prefix + "finds" in filtr)
and not from_find
and "finds" not in kwargs.get("exclude", [])
):
kwargs["exclude"] = base_exclude[:]
kwargs["no_base_finds"] = True
# prevent recursive call
values[prefix + "finds"] = [
f.get_values(prefix=prefix, no_values=True, filtr=filtr, **kwargs)
for f in self.finds.distinct().all()
]
if not self.finds.count():
return values
operation_in_filter = filtr and any(
k for k in filtr if k.startswith(prefix + "operation")
)
cr_in_filter = filtr and any(
k for k in filtr if k.startswith(prefix + "context_record")
)
if not filtr or prefix + "material_types" in filtr:
values[prefix + "material_types"] = self.get_material_types()
if not filtr or prefix + "material_types_code" in filtr:
values[prefix + "material_types_code"] = self.get_material_types_code()
if not from_find and (not filtr or operation_in_filter or cr_in_filter):
# assume that only one operation is in this container...
# you should know what you are doing when using theses variables
f = self.finds.all()[0]
bf = f.get_first_base_find()
if bf:
cr = bf.context_record
if not filtr or cr_in_filter:
kwargs["exclude"] = base_exclude[:] + [prefix + "operation"]
kwargs["force_no_base_finds"] = True
for k, v in cr.get_values(
prefix=prefix, no_values=True, filtr=None, **kwargs
).items():
values[prefix + "context_record_" + k] = v
if not filtr or operation_in_filter:
kwargs["exclude"] = base_exclude[:] + [prefix + "context_records"]
for k, v in cr.operation.get_values(
prefix=prefix, no_values=True, filtr=None, **kwargs
).items():
values[prefix + "operation_" + k] = v
return values
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]),
_("Add treatment"),
"fa fa-flask",
"",
"",
False,
),
]
return actions
def pre_save(self):
if self.parent == self:
self.parent = None
if not self.responsibility_id and not self.responsibility:
if self.location_id:
self.responsibility_id = self.location_id
else:
try:
self.responsibility = self.location
except Warehouse.DoesNotExist:
return
if self.container_type.stationary:
return
q = Container.objects.filter(index=self.index, location=self.location)
if self.id:
q = q.exclude(id=self.id)
if self.index and not q.count():
return
q = Container.objects.filter(location=self.location, index__isnull=False)
self.index = (
int(q.all().aggregate(Max("index"))["index__max"]) + 1
if q.count()
else 1
)
if not self.cached_division:
self.cached_division = self._generate_cached_division()
def _update_warehouse_max_division(self):
number = 0
parent = self.parent_id
while parent:
number += 1
parent = Container.objects.filter(pk=parent).values_list("parent_id")[0][0]
if number > self.location.max_division_number:
self.location.max_division_number = number
self.location.save()
def save(self, *args, **kwargs):
self.pre_save()
super(Container, self).save(*args, **kwargs)
self._change_child_location(self)
updated = False
updated += self._prevent_parent_infinite_loop()
self._update_warehouse_max_division()
updated += self._calculate_weight()
if not self.index and not self.container_type.stationary:
self.skip_history_when_saving = True
q = (
Container.objects.filter(location=self.location, index__isnull=False)
.exclude(pk=self.pk)
.order_by("-index")
)
self.index = q.all()[0].index + 1 if q.count() else 1
updated = True
self.skip_history_when_saving = True
if not self.external_id or self.auto_external_id:
external_id = get_generated_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)
# TODO: to be deleted???
"""
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()
"""
def container_pre_delete(sender, **kwargs):
instance = kwargs["instance"]
q = Container.objects.filter(container_tree_child__container_parent=instance)
q.update(cached_division="")
def container_post_delete(sender, **kwargs):
instance = kwargs["instance"]
q = Container.objects.filter(cached_division="", location=instance.location)
for c in q.all():
c.save()
post_save.connect(container_post_save, sender=Container)
pre_delete.connect(container_pre_delete, sender=Container)
post_delete.connect(container_post_delete, sender=Container)
m2m_changed.connect(document_attached_changed, sender=Container.documents.through)
class ContainerLocalisationManager(models.Manager):
# TODO: to be deleted....
def get_by_natural_key(self, container, warehouse, container_type):
return self.get(
container__uuid=container,
division__warehouse__uuid=warehouse,
division__container_type__txt_idx=container_type,
)
class ContainerLocalisation(models.Model):
# TODO: to be deleted....
container = models.ForeignKey(
Container, verbose_name=_("Container"), related_name="division"
)
division = models.ForeignKey(WarehouseDivisionLink, verbose_name=_("Division"))
reference = models.CharField(_("Reference"), max_length=200, default="")
objects = ContainerLocalisationManager()
TO_BE_DELETED = True
class Meta:
verbose_name = _("Container localisation")
verbose_name_plural = _("Container localisations")
ordering = ("container", "division__order")
def __str__(self):
return " - ".join((str(self.container), str(self.division), self.reference))
def natural_key(self):
return (
self.container.uuid,
self.division.warehouse.uuid,
self.division.container_type.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)
|