summaryrefslogtreecommitdiff
path: root/ishtar_common/models.py
blob: 1850f86c853bbc9ea008179cfecd24cdba94930a (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
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
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010-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.

"""
Models description
"""
import copy
import datetime
import inspect
from jinja2 import TemplateSyntaxError
import logging
import os
import re
import shutil
import tempfile
import time
from cStringIO import StringIO
from subprocess import Popen, PIPE

from PIL import Image
from django.conf import settings
from django.contrib.auth.models import User, Group
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.db import models
from django.contrib.postgres.fields import JSONField
from django.contrib.postgres.search import SearchVectorField, SearchVector
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse, NoReverseMatch
from django.core.validators import validate_slug
from django.db import connection
from django.db.models import Q, Max, Count
from django.db.models.signals import post_save, post_delete, m2m_changed
from django.db.utils import DatabaseError
from django.template.defaultfilters import slugify
from django.utils.functional import lazy
from django.utils.safestring import SafeUnicode, mark_safe
from django.utils.translation import ugettext_lazy as _, ugettext, \
    pgettext_lazy, activate, deactivate
from secretary import Renderer as SecretaryRenderer
from simple_history.models import HistoricalRecords as BaseHistoricalRecords
from unidecode import unidecode

from ishtar_common.alternative_configs import ALTERNATE_CONFIGS, \
    ALTERNATE_CONFIGS_CHOICES

from ishtar_common.model_merging import merge_model_objects
from ishtar_common.models_imports import ImporterModel, ImporterType, \
    ImporterDefault, ImporterDefaultValues, ImporterColumn, \
    ImporterDuplicateField, Regexp, ImportTarget, TargetKey, FormaterType, \
    Import, TargetKeyGroup
from ishtar_common.utils import get_cache, disable_for_loaddata, create_slug, \
    get_all_field_names, merge_tsvectors, cached_label_changed, \
    generate_relation_graph

__all__ = [
    'ImporterModel', 'ImporterType', 'ImporterDefault', 'ImporterDefaultValues',
    'ImporterColumn', 'ImporterDuplicateField', 'Regexp', 'ImportTarget',
    'TargetKey', 'FormaterType', 'Import', 'TargetKeyGroup'
]

logger = logging.getLogger(__name__)


def post_save_user(sender, **kwargs):
    user = kwargs['instance']

    if kwargs["created"]:
        try:
            IshtarUser.create_from_user(user)
        except DatabaseError:  # manage when db is not synced
            pass
    IshtarUser.set_superuser(user)


post_save.connect(post_save_user, sender=User)


class ValueGetter(object):
    _prefix = ""
    GET_VALUES_EXTRA = []
    COL_LABELS = {}
    GET_VALUE_EXCLUDE_FIELDS = ['search_vector', 'id']

    def get_values(self, prefix=''):
        if not prefix:
            prefix = self._prefix
        values = {}
        for field_name in get_all_field_names(self):
            if not hasattr(self, field_name) or \
                    field_name in self.GET_VALUE_EXCLUDE_FIELDS or \
                    field_name.endswith('_id'):
                continue
            value = getattr(self, field_name)
            if hasattr(value, 'get_values'):
                values.update(value.get_values(prefix + field_name + '_'))
            else:
                values[prefix + field_name] = value
        for extra_field in self.GET_VALUES_EXTRA:
            values[prefix + extra_field] = getattr(self, extra_field) or ''
        for key in values.keys():
            val = values[key]
            if val is None:
                val = ''
            else:
                val = unicode(val)
            if val.endswith('.None'):
                val = ''
            values[key] = val
        values['KEYS'] = u'\n'.join(values.keys())
        value_list = []
        for key in values.keys():
            if key in ('KEYS', 'VALUES'):
                continue
            value_list.append((key, unicode(values[key])))
        values['VALUES'] = u'\n'.join(
            [u"%s: %s" % (k, v) for k, v in sorted(value_list,
                                                   key=lambda x: x[0])])
        for global_var in GlobalVar.objects.all():
            values[global_var.slug] = global_var.value or ""
        return values

    @classmethod
    def get_empty_values(cls, prefix=''):
        if not prefix:
            prefix = cls._prefix
        values = {}
        for field_name in get_all_field_names(cls):
            values[prefix + field_name] = ''
        return values


class HistoricalRecords(BaseHistoricalRecords):
    def create_historical_record(self, instance, type):
        try:
            history_modifier = getattr(instance, 'history_modifier', None)
            assert history_modifier
        except (User.DoesNotExist, AssertionError):
            # on batch removing of users, user could have disappeared
            return
        manager = getattr(instance, self.manager_name)
        attrs = {}
        for field in instance._meta.fields:
            attrs[field.attname] = getattr(instance, field.attname)
        q_history = instance.history \
            .filter(history_modifier_id=history_modifier.pk) \
            .order_by('-history_date', '-history_id')
        if not q_history.count():
            manager.create(history_type=type,
                           history_date=datetime.datetime.now(), **attrs)
            return
        old_instance = q_history.all()[0]
        # multiple saving by the same user in a very short time are generaly
        # caused by post_save signals it is not relevant to keep them
        min_history_date = datetime.datetime.now() \
                           - datetime.timedelta(seconds=5)
        q = q_history.filter(history_date__isnull=False,
                             history_date__gt=min_history_date) \
            .order_by('-history_date', '-history_id')
        if q.count():
            return

        if 'history_date' not in attrs or not attrs['history_date']:
            attrs['history_date'] = datetime.datetime.now()
        # record a new version only if data have been changed
        for field in instance._meta.fields:
            if getattr(old_instance, field.attname) != attrs[field.attname]:
                manager.create(history_type=type, **attrs)
                return


def valid_id(cls):
    # valid ID validator for models
    def func(value):
        try:
            cls.objects.get(pk=value)
        except ObjectDoesNotExist:
            raise ValidationError(_(u"Not a valid item."))

    return func


def valid_ids(cls):
    def func(value):
        if "," in value:
            value = value.split(",")
        if type(value) not in (list, tuple):
            value = [value]
        for v in value:
            try:
                cls.objects.get(pk=v)
            except ObjectDoesNotExist:
                raise ValidationError(
                    _(u"A selected item is not a valid item."))

    return func


def is_unique(cls, field):
    # unique validator for models
    def func(value):
        query = {field: value}
        try:
            assert cls.objects.filter(**query).count() == 0
        except AssertionError:
            raise ValidationError(_(u"This item already exists."))

    return func


class OwnPerms(object):
    """
    Manage special permissions for object's owner
    """

    @classmethod
    def get_query_owns(cls, ishtaruser):
        """
        Query object to get own items
        """
        return None  # implement for each object

    def can_do(self, request, action_name):
        """
        Check permission availability for the current object.
        :param request: request object
        :param action_name: action name eg: "change_find" - "own" declinaison is
        checked
        :return: boolean
        """
        splited = action_name.split('_')
        action_own_name = splited[0] + '_own_' + '_'.join(splited[1:])
        return request.user.ishtaruser.has_right(
            action_name, request.session) or \
               (request.user.ishtaruser.has_right(
                   action_own_name, request.session)
                and self.is_own(request.user.ishtaruser))

    def is_own(self, user, alt_query_own=None):
        """
        Check if the current object is owned by the user
        """
        if isinstance(user, IshtarUser):
            ishtaruser = user
        elif hasattr(user, 'ishtaruser'):
            ishtaruser = user.ishtaruser
        else:
            return False
        if not alt_query_own:
            query = self.get_query_owns(ishtaruser)
        else:
            query = getattr(self, alt_query_own)(ishtaruser)
        if not query:
            return False
        query &= Q(pk=self.pk)
        return self.__class__.objects.filter(query).count()

    @classmethod
    def has_item_of(cls, user):
        """
        Check if the user own some items
        """
        if isinstance(user, IshtarUser):
            ishtaruser = user
        elif hasattr(user, 'ishtaruser'):
            ishtaruser = user.ishtaruser
        else:
            return False
        query = cls.get_query_owns(ishtaruser)
        if not query:
            return False
        return cls.objects.filter(query).count()

    @classmethod
    def _return_get_owns(cls, owns, values, get_short_menu_class,
                         label_key='cached_label'):
        if not owns:
            return []
        sorted_values = []
        if hasattr(cls, 'BASKET_MODEL'):
            owns_len = len(owns)
            for idx, item in enumerate(reversed(owns)):
                if get_short_menu_class:
                    item = item[0]
                if type(item) == cls.BASKET_MODEL:
                    basket = owns.pop(owns_len - idx - 1)
                    sorted_values.append(basket)
            sorted_values = list(reversed(sorted_values))
        if not values:
            if not get_short_menu_class:
                return sorted_values + list(
                    sorted(owns, key=lambda x: getattr(x, label_key)))
            return sorted_values + list(
                sorted(owns, key=lambda x: getattr(x[0], label_key)))
        if not get_short_menu_class:
            return sorted_values + list(
                sorted(owns, key=lambda x: x[label_key]))
        return sorted_values + list(
            sorted(owns, key=lambda x: x[0][label_key]))

    @classmethod
    def get_owns(cls, user, replace_query=None, limit=None, values=None,
                 get_short_menu_class=False, menu_filtr=None):
        """
        Get Own items
        """
        if not replace_query:
            replace_query = {}
        if hasattr(user, 'is_authenticated') and not user.is_authenticated():
            returned = cls.objects.filter(pk__isnull=True)
            if values:
                returned = []
            return returned
        if isinstance(user, User):
            try:
                ishtaruser = IshtarUser.objects.get(user_ptr=user)
            except IshtarUser.DoesNotExist:
                returned = cls.objects.filter(pk__isnull=True)
                if values:
                    returned = []
                return returned
        elif isinstance(user, IshtarUser):
            ishtaruser = user
        else:
            if values:
                return []
            return cls.objects.filter(pk__isnull=True)
        items = []
        if hasattr(cls, 'BASKET_MODEL'):
            items = list(cls.BASKET_MODEL.objects.filter(user=ishtaruser).all())
        query = cls.get_query_owns(ishtaruser)
        if not query and not replace_query:
            returned = cls.objects.filter(pk__isnull=True)
            if values:
                returned = []
            return returned
        if query:
            q = cls.objects.filter(query)
        else:  # replace_query
            q = cls.objects.filter(replace_query)
        if values:
            q = q.values(*values)
        if limit:
            items += list(q.order_by('-pk')[:limit])
        else:
            items += list(q.order_by(*cls._meta.ordering).all())
        if get_short_menu_class:
            if values:
                if 'id' not in values:
                    raise NotImplementedError(
                        "Call of get_owns with get_short_menu_class option and"
                        " no 'id' in values is not implemented")
                my_items = []
                for i in items:
                    if hasattr(cls, 'BASKET_MODEL') and \
                            type(i) == cls.BASKET_MODEL:
                        dct = dict([(k, getattr(i, k)) for k in values])
                        my_items.append(
                            (dct, cls.BASKET_MODEL.get_short_menu_class(i.pk)))
                    else:
                        my_items.append((i, cls.get_short_menu_class(i['id'])))
                items = my_items
            else:
                items = [(i, cls.get_short_menu_class(i.pk)) for i in items]
        return items

    @classmethod
    def _get_query_owns_dicts(cls, ishtaruser):
        """
        List of query own dict to construct the query.
        Each dict are join with an AND operator, each dict key, values are
        joined with OR operator
        """
        return []

    @classmethod
    def _construct_query_own(cls, prefix, dct_list):
        q = None
        for subquery_dict in dct_list:
            subquery = None
            for k in subquery_dict:
                subsubquery = Q(**{prefix + k: subquery_dict[k]})
                if subquery:
                    subquery |= subsubquery
                else:
                    subquery = subsubquery
            if not subquery:
                continue
            if q:
                q &= subquery
            else:
                q = subquery
        return q


class Cached(object):
    slug_field = 'txt_idx'

    @classmethod
    def refresh_cache(cls):
        cache_ckey, current_keys = get_cache(cls, ['_current_keys'])
        if not current_keys:
            return
        for keys in current_keys:
            if len(keys) == 2 and keys[0] == '__slug':
                cls.get_cache(keys[1], force=True)
            elif keys[0] == '__get_types':
                default = None
                empty_first = True
                exclude = []
                if len(keys) >= 2:
                    default = keys.pop()
                    if len(keys) > 1:
                        empty_first = bool(keys.pop())
                    exclude = keys[1:]
                cls.get_types(
                    exclude=exclude, empty_first=empty_first, default=default,
                    force=True)
            elif keys[0] == '__get_help':
                cls.get_help(force=True)

    @classmethod
    def _add_cache_key_to_refresh(cls, keys):
        cache_ckey, current_keys = get_cache(cls, ['_current_keys'])
        if type(current_keys) != list:
            current_keys = []
        if keys not in current_keys:
            current_keys.append(keys)
            cache.set(cache_ckey, current_keys, settings.CACHE_TIMEOUT)

    @classmethod
    def get_cache(cls, slug, force=False):
        cache_key, value = get_cache(cls, ['__slug', slug])
        if not force and value:
            return value
        try:
            k = {cls.slug_field: slug}
            obj = cls.objects.get(**k)
            cache.set(cache_key, obj, settings.CACHE_TIMEOUT)
            return obj
        except cls.DoesNotExist:
            cache.set(cache_key, None, settings.CACHE_TIMEOUT)
            return None


@disable_for_loaddata
def post_save_cache(sender, **kwargs):
    sender.refresh_cache()


class ExternalIdManager(models.GeoManager):
    def get_by_natural_key(self, external_id):
        return self.get(external_id=external_id)


class SlugModelManager(models.Manager):
    def get_by_natural_key(self, slug):
        return self.get(slug=slug)


class TypeManager(models.Manager):
    def get_by_natural_key(self, txt_idx):
        return self.get(txt_idx=txt_idx)


class GeneralType(Cached, models.Model):
    """
    Abstract class for "types"
    """
    label = models.TextField(_(u"Label"))
    txt_idx = models.TextField(
        _(u"Textual ID"), validators=[validate_slug],
        unique=True,
        help_text=_(
            u"The slug is the standardized version of the name. It contains "
            u"only lowercase letters, numbers and hyphens. Each slug must "
            u"be unique."))
    comment = models.TextField(_(u"Comment"), blank=True, null=True)
    available = models.BooleanField(_(u"Available"), default=True)
    HELP_TEXT = u""
    objects = TypeManager()

    class Meta:
        abstract = True

    def __unicode__(self):
        return self.label

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

    @property
    def explicit_label(self):
        return u"{} ({})".format(self.label, self._meta.verbose_name)

    @classmethod
    def create_default_for_test(cls):
        return [cls.objects.create(label='Test %d' % i) for i in range(5)]

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

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

    @classmethod
    def get_or_create(cls, slug, label=''):
        """
        Get or create a new item.

        :param slug: textual id
        :param label: label for initialization if the item doesn't exist (not
        mandatory)

        :return: instancied item of the base class
        """

        item = cls.get_cache(slug)
        if item:
            return item
        item, created = cls.objects.get_or_create(
            txt_idx=slug, defaults={'label': label})
        return item

    @classmethod
    def get_or_create_pk(cls, slug):
        """
        Get an id from a slug. Create the associated item if needed.

        :param slug: textual id

        :return: id of the item (string)
        """
        return unicode(cls.get_or_create(slug).pk)

    @classmethod
    def get_or_create_pks(cls, slugs):
        """
        Get and merge a list of ids from a slug list. Create the associated
        items if needed.

        :param slugs: textual ids

        :return: string with ids separated by "_"
        """
        items = []
        for slug in slugs:
            items.append(str(cls.get_or_create(slug).pk))
        return u"_".join(items)

    @classmethod
    def get_help(cls, dct={}, exclude=[], force=False):
        keys = ['__get_help']
        keys += [u"{}".format(ex) for ex in exclude]
        keys += [u'{}-{}'.format(unicode(k), dct[k]) for k in dct]
        cache_key, value = get_cache(cls, keys)
        if value and not force:
            return mark_safe(value)
        help_text = cls.HELP_TEXT
        c_rank = -1
        help_items = u"\n"
        for item in cls.get_types(dct=dct, instances=True, exclude=exclude):
            if hasattr(item, '__iter__'):
                pk = item[0]
                item = cls.objects.get(pk=pk)
                item.rank = c_rank + 1
                if hasattr(item, 'parent'):
                    c_item = item
                    parents = []
                    while c_item.parent:
                        parents.append(c_item.parent.label)
                        c_item = c_item.parent
                    parents.reverse()
                    parents.append(item.label)
                    item.label = u" / ".join(parents)
            if not item.comment:
                continue
            if c_rank > item.rank:
                help_items += u"</dl>\n"
            elif c_rank < item.rank:
                help_items += u"<dl>\n"
            c_rank = item.rank
            help_items += u"<dt>%s</dt><dd>%s</dd>" % (
                item.label, u"<br/>".join(item.comment.split('\n')))
        c_rank += 1
        if c_rank:
            help_items += c_rank * u"</dl>"
        if help_text or help_items != u'\n':
            help_text = help_text + help_items
        else:
            help_text = u""
        cache.set(cache_key, help_text, settings.CACHE_TIMEOUT)
        return mark_safe(help_text)

    @classmethod
    def _get_initial_types(cls, initial, type_pks, instance=False):
        new_vals = []
        if not initial:
            return []
        if type(initial) not in (list, tuple):
            initial = [initial]
        for value in initial:
            try:
                pk = int(value)
            except (ValueError, TypeError):
                continue
            if pk in type_pks:
                continue
            try:
                extra_type = cls.objects.get(pk=pk)
                if instance:
                    new_vals.append(extra_type)
                else:
                    new_vals.append((extra_type.pk, unicode(extra_type)))
            except cls.DoesNotExist:
                continue
        return new_vals

    @classmethod
    def get_types(cls, dct={}, instances=False, exclude=[], empty_first=True,
                  default=None, initial=None, force=False):
        types = []
        if not instances and empty_first and not default:
            types = [('', '--')]
        types += cls._pre_get_types(dct, instances, exclude,
                                    default, force)
        if not initial:
            return types
        new_vals = cls._get_initial_types(initial, [idx for idx, lbl in types])
        types += new_vals
        return types

    @classmethod
    def _pre_get_types(cls, dct={}, instances=False, exclude=[],
                       default=None, force=False):
        # cache
        cache_key = None
        if not instances:
            keys = ['__get_types']
            keys += [u"{}".format(ex) for ex in exclude] + \
                    [u"{}".format(default)]
            keys += [u'{}-{}'.format(unicode(k), dct[k]) for k in dct]
            cache_key, value = get_cache(cls, keys)
            if value and not force:
                return value
        base_dct = dct.copy()
        if hasattr(cls, 'parent'):
            if not cache_key:
                return cls._get_parent_types(
                    base_dct, instances, exclude=exclude,
                    default=default)
            vals = [v for v in cls._get_parent_types(
                base_dct, instances, exclude=exclude,
                default=default)]
            cache.set(cache_key, vals, settings.CACHE_TIMEOUT)
            return vals

        if not cache_key:
            return cls._get_types(base_dct, instances, exclude=exclude,
                                  default=default)
        vals = [
            v for v in cls._get_types(base_dct, instances, exclude=exclude,
                                      default=default)
        ]
        cache.set(cache_key, vals, settings.CACHE_TIMEOUT)
        return vals

    @classmethod
    def _get_types(cls, dct=None, instances=False, exclude=None, default=None):
        if not dct:
            dct = {}
        if not exclude:
            exclude = []
        dct['available'] = True
        if default:
            try:
                default = cls.objects.get(txt_idx=default)
                yield (default.pk, _(unicode(default)))
            except cls.DoesNotExist:
                pass
        items = cls.objects.filter(**dct)
        if default and default != "None":
            if hasattr(default, 'txt_idx'):
                exclude.append(default.txt_idx)
            else:
                exclude.append(default)
        if exclude:
            items = items.exclude(txt_idx__in=exclude)
        for item in items.order_by(*cls._meta.ordering).all():
            if instances:
                item.rank = 0
                yield item
            else:
                yield (item.pk, _(unicode(item))
                if item and unicode(item) else '')

    @classmethod
    def _get_childs_list(cls, dct=None, exclude=None, instances=False):
        if not dct:
            dct = {}
        if not exclude:
            exclude = []
        if 'parent' in dct:
            dct.pop('parent')
        childs = cls.objects.filter(**dct)
        if exclude:
            childs = childs.exclude(txt_idx__in=exclude)
        if hasattr(cls, 'order'):
            childs = childs.order_by('order')
        res = {}
        if instances:
            for item in childs.all():
                parent_id = item.parent_id or 0
                if parent_id not in res:
                    res[parent_id] = []
                res[parent_id].append(item)
        else:
            for item in childs.values("id", "parent_id", "label").all():
                parent_id = item["parent_id"] or 0
                if parent_id not in res:
                    res[parent_id] = []
                res[parent_id].append((item["id"], item["label"]))
        return res

    PREFIX = "&#x2502; "
    PREFIX_EMPTY = "&nbsp; "
    PREFIX_MEDIUM = "&#x251C; "
    PREFIX_LAST = "&#x2514; "
    PREFIX_CODES = [u"\u2502", u"\u251C", u"\u2514"]

    @classmethod
    def _get_childs(cls, item, child_list, prefix=0, instances=False,
                    is_last=False, last_of=None):
        if not last_of:
            last_of = []

        prefix += 1
        current_child_lst = []
        if item in child_list:
            current_child_lst = child_list[item]

        lst = []
        total = len(current_child_lst)
        for idx, child in enumerate(current_child_lst):
            mylast_of = last_of[:]
            if instances:
                child.rank = prefix
                lst.append(child)
            else:
                p = ''
                cprefix = prefix
                while cprefix:
                    cprefix -= 1
                    if not cprefix:
                        if (idx + 1) == total:
                            p += cls.PREFIX_LAST
                        else:
                            p += cls.PREFIX_MEDIUM
                    elif is_last:
                        if mylast_of:
                            clast = mylast_of.pop(0)
                            if clast:
                                p += cls.PREFIX_EMPTY
                            else:
                                p += cls.PREFIX
                        else:
                            p += cls.PREFIX_EMPTY
                    else:
                        p += cls.PREFIX
                lst.append((
                    child[0], SafeUnicode(p + unicode(_(child[1])))
                ))
            clast_of = last_of[:]
            clast_of.append(idx + 1 == total)
            if instances:
                child_id = child.id
            else:
                child_id = child[0]
            for sub_child in cls._get_childs(
                    child_id, child_list, prefix, instances,
                    is_last=((idx + 1) == total), last_of=clast_of):
                lst.append(sub_child)
        return lst

    @classmethod
    def _get_parent_types(cls, dct={}, instances=False, exclude=[],
                          default=None):
        dct['available'] = True
        dct['parent'] = None
        items = cls.objects.filter(**dct)
        if exclude:
            items = items.exclude(txt_idx__in=exclude)
        if hasattr(cls, 'order'):
            items = items.order_by('order')

        child_list = cls._get_childs_list(dct, exclude, instances)

        if 0 in child_list:
            for item in child_list[0]:
                if instances:
                    item.rank = 0
                    item_id = item.pk
                    yield item
                else:
                    item_id = item[0]
                    yield item
                for child in cls._get_childs(
                        item_id, child_list, instances=instances):
                    yield child

    def save(self, *args, **kwargs):
        if not self.id and not self.label:
            self.label = u" ".join(u" ".join(self.txt_idx.split('-'))
                                   .split('_')).title()
        if not self.txt_idx:
            self.txt_idx = slugify(self.label)[:100]

        # clean old keys
        if self.pk:
            old = self.__class__.objects.get(pk=self.pk)
            content_type = ContentType.objects.get_for_model(self.__class__)
            if slugify(self.label) != slugify(old.label):
                ItemKey.objects.filter(
                    object_id=self.pk, key=slugify(old.label),
                    content_type=content_type).delete()
            if self.txt_idx != old.txt_idx:
                ItemKey.objects.filter(
                    object_id=self.pk, key=old.txt_idx,
                    content_type=content_type).delete()

        obj = super(GeneralType, self).save(*args, **kwargs)
        self.generate_key(force=True)
        return obj

    def add_key(self, key, force=False, importer=None, group=None,
                user=None):
        content_type = ContentType.objects.get_for_model(self.__class__)
        if not importer and not force and ItemKey.objects.filter(
                key=key, content_type=content_type).count():
            return
        filtr = {'key': key, 'content_type': content_type}
        if group:
            filtr['group'] = group
        elif user:
            filtr['user'] = user
        else:
            filtr['importer'] = importer
        if force:
            ItemKey.objects.filter(**filtr).exclude(object_id=self.pk).delete()
        filtr['object_id'] = self.pk
        ItemKey.objects.get_or_create(**filtr)

    def generate_key(self, force=False):
        for key in (slugify(self.label), self.txt_idx):
            self.add_key(key)

    def get_keys(self, importer):
        keys = [self.txt_idx]
        content_type = ContentType.objects.get_for_model(self.__class__)
        base_q = Q(content_type=content_type, object_id=self.pk)
        subquery = Q(importer__isnull=True, user__isnull=True,
                     group__isnull=True)
        subquery |= Q(user__isnull=True, group__isnull=True,
                      importer=importer)
        if importer.user:
            subquery |= Q(user=importer.user, group__isnull=True,
                          importer=importer)
        if importer.associated_group:
            subquery |= Q(user__isnull=True, group=importer.associated_group,
                          importer=importer)
        q = ItemKey.objects.filter(base_q & subquery)
        for ik in q.exclude(key=self.txt_idx).all():
            keys.append(ik.key)
        return keys

    @classmethod
    def generate_keys(cls):
        # content_type = ContentType.objects.get_for_model(cls)
        for item in cls.objects.all():
            item.generate_key()


class HierarchicalType(GeneralType):
    parent = models.ForeignKey('self', blank=True, null=True,
                               verbose_name=_(u"Parent"))

    class Meta:
        abstract = True

    def full_label(self):
        lbls = [self.label]
        item = self
        while item.parent:
            item = item.parent
            lbls.append(item.label)
        return u" > ".join(reversed(lbls))


class ItemKey(models.Model):
    key = models.TextField(_(u"Key"))
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    importer = models.ForeignKey(
        Import, null=True, blank=True,
        help_text=_(u"Specific key to an import"))
    user = models.ForeignKey('IshtarUser', blank=True, null=True)
    group = models.ForeignKey(TargetKeyGroup, blank=True, null=True)

    def __unicode__(self):
        return self.key


def get_image_path(instance, filename):
    # when using migrations instance is not a real ImageModel instance
    if not hasattr(instance, '_get_image_path'):
        return u"upload/{}".format(filename)
    return instance._get_image_path(filename)


class ImageModel(models.Model):
    image = models.ImageField(upload_to=get_image_path, blank=True, null=True,
                              max_length=255)
    thumbnail = models.ImageField(upload_to=get_image_path, blank=True,
                                  null=True, max_length=255)
    IMAGE_MAX_SIZE = settings.IMAGE_MAX_SIZE
    THUMB_MAX_SIZE = settings.THUMB_MAX_SIZE
    IMAGE_PREFIX = ''

    class Meta:
        abstract = True

    def _get_image_path(self, filename):
        return u"{}/{}".format(self._get_base_image_path(), filename)

    def _get_base_image_path(self):
        return u"upload"

    def has_changed(self, field):
        if not self.pk:
            return True
        manager = getattr(self.__class__, 'objects')
        old = getattr(manager.get(pk=self.pk), field)
        return not getattr(self, field) == old

    def create_thumb(self, image, size):
        """Returns the image resized to fit inside a box of the given size"""
        image.thumbnail(size, Image.ANTIALIAS)
        temp = StringIO()
        image.save(temp, 'jpeg')
        temp.seek(0)
        return SimpleUploadedFile('temp', temp.read())

    def save(self, *args, **kwargs):
        if 'force_copy' in kwargs:
            kwargs.pop('force_copy')
            super(ImageModel, self).save(*args, **kwargs)
            return
        # manage images
        if not self.has_changed('image'):
            return super(ImageModel, self).save(*args, **kwargs)
        if not self.image:
            self.thumbnail = None
            return super(ImageModel, self).save(*args, **kwargs)

        # # generate thumbnail
        # convert to jpg
        filename = os.path.splitext(os.path.split(self.image.name)[-1])[0]
        old_path = self.image.path
        filename = "%s.jpg" % filename
        try:
            image = Image.open(self.image.file)
            # convert to RGB
            if image.mode not in ('L', 'RGB'):
                image = image.convert('RGB')

            # resize if necessary
            if self.IMAGE_MAX_SIZE:
                self.image.save(filename,
                                self.create_thumb(image, self.IMAGE_MAX_SIZE),
                                save=False)

            if old_path != self.image.path:
                try:
                    os.remove(old_path)
                except OSError:
                    # already clean
                    pass

            # save the thumbnail
            thumb_filename = self._get_thumb_name(filename)
            self.thumbnail.save(
                thumb_filename,
                self.create_thumb(image, self.THUMB_MAX_SIZE),
                save=False)
        except IOError:
            pass
        return super(ImageModel, self).save(*args, **kwargs)

    def _get_thumb_name(self, filename):
        splited = filename.split('.')
        return u"{}-thumb.{}".format(
            u".".join(splited[:-1]), splited[-1]
        )


class HistoryError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


PRIVATE_FIELDS = ('id', 'history_modifier', 'order')


class BulkUpdatedItem(object):
    @classmethod
    def bulk_recursion(cls, transaction_id, extra_args):
        """
        Prevent infinite recursion. Should not happen but wrong manipulation
        in the database or messy imports can generate circular relations

        :param transaction_id: current transaction ID (unix time) - if null
        a transaction ID is generated
        :param extra_args: arguments dealing with
        :return: (transaction ID, is a recursion)
        """
        if not transaction_id:
            transaction_id = unicode(time.time())
        args = ['cached_label_bulk_update', transaction_id] + extra_args
        key, val = get_cache(cls, args)
        if val:
            return transaction_id, True
        cache.set(key, 1, settings.CACHE_SMALLTIMEOUT)
        return transaction_id, False


class RelationItem(models.Model):
    """
    Items with relation between them
    """
    relation_image = models.FileField(
        _(u"Generated relation image (SVG)"), null=True, blank=True,
        upload_to=get_image_path
    )

    class Meta:
        abstract = True

    def generate_relation_image(self):
        generate_relation_graph(self)


class JsonDataSection(models.Model):
    content_type = models.ForeignKey(ContentType)
    name = models.CharField(_(u"Name"), max_length=200)
    order = models.IntegerField(_(u"Order"), default=10)

    class Meta:
        verbose_name = _(u"Json data - Menu")
        verbose_name_plural = _(u"Json data - Menus")
        ordering = ['order', 'name']

    def __unicode__(self):
        return u"{} - {}".format(self.content_type, self.name)


JSON_VALUE_TYPES = (
    ('T', _(u"Text")),
    ('LT', _(u"Long text")),
    ('I', _(u"Integer")),
    ('B', _(u"Boolean")),
    ('F', _(u"Float")),
    ('D', _(u"Date")),
    ('C', _(u"Choices")),
)


class JsonDataField(models.Model):
    name = models.CharField(_(u"Name"), max_length=200)
    content_type = models.ForeignKey(ContentType)
    key = models.CharField(
        _(u"Key"), max_length=200,
        help_text=_(u"Value of the key in the JSON schema. For hierarchical "
                    u"key use \"__\" to explain it. For instance for the key "
                    u"'my_subkey' with data such as {'my_key': {'my_subkey': "
                    u"'value'}}, its value will be reached with my_key__my_subkey."))
    display = models.BooleanField(_(u"Display"), default=True)
    value_type = models.CharField(_(u"Type"), default="T", max_length=10,
                                  choices=JSON_VALUE_TYPES)
    order = models.IntegerField(_(u"Order"), default=10)
    search_index = models.BooleanField(_(u"Use in search indexes"),
                                       default=False)
    section = models.ForeignKey(JsonDataSection, blank=True, null=True)
    custom_forms = models.ManyToManyField(
        "CustomForm", blank=True, through="CustomFormJsonField")

    class Meta:
        verbose_name = _(u"Json data - Field")
        verbose_name_plural = _(u"Json data - Fields")
        ordering = ['order', 'name']

    def __unicode__(self):
        return u"{} - {}".format(self.content_type, self.name)

    def clean(self):
        if not self.section:
            return
        if self.section.content_type != self.content_type:
            raise ValidationError(
                _(u"Content types of the field and of the menu do not match"))


class JsonData(models.Model):
    data = JSONField(default={}, db_index=True, blank=True)

    class Meta:
        abstract = True

    def pre_save(self):
        if not self.data:
            self.data = {}

    @property
    def json_sections(self):
        sections = []
        try:
            content_type = ContentType.objects.get_for_model(self)
        except ContentType.DoesNotExists:
            return sections
        fields = list(JsonDataField.objects.filter(
            content_type=content_type, display=True, section__isnull=True
        ).all())  # no section fields

        fields += list(JsonDataField.objects.filter(
            content_type=content_type, display=True, section__isnull=False
        ).order_by('section__order', 'order').all())

        for field in fields:
            value = None
            data = self.data.copy()
            for key in field.key.split('__'):
                if key in data:
                    value = copy.copy(data[key])
                    data = data[key]
                else:
                    value = None
                    break
            if value is None:
                continue
            if type(value) in (list, tuple):
                value = u" ; ".join([unicode(v) for v in value])
            section_name = field.section.name if field.section else None
            if not sections or section_name != sections[-1][0]:
                # if section name is identical it is the same
                sections.append((section_name, []))
            sections[-1][1].append((field.name, value))
        return sections


class Imported(models.Model):
    imports = models.ManyToManyField(
        Import, blank=True,
        related_name="imported_%(app_label)s_%(class)s")

    class Meta:
        abstract = True


class FullSearch(models.Model):
    search_vector = SearchVectorField(_("Search vector"), blank=True, null=True,
                                      help_text=_("Auto filled at save"))
    BASE_SEARCH_VECTORS = []
    PROPERTY_SEARCH_VECTORS = []
    INT_SEARCH_VECTORS = []
    M2M_SEARCH_VECTORS = []
    PARENT_SEARCH_VECTORS = []
    # prevent circular dependency
    PARENT_ONLY_SEARCH_VECTORS = []

    class Meta:
        abstract = True

    @classmethod
    def general_types(cls):
        for k in get_all_field_names(cls):
            field = cls._meta.get_field(k)
            if not hasattr(field, 'rel') or not field.rel:
                continue
            rel_model = field.rel.to
            if issubclass(rel_model, (GeneralType, HierarchicalType)):
                yield k

    def update_search_vector(self, save=True, exclude_parent=False):
        """
        Update the search vector
        :param save: True if you want to save the object immediately
        :return: True if modified
        """
        if not hasattr(self, 'search_vector'):
            return
        if not self.pk:
            # logger.warning("Cannot update search vector before save or "
            #                "after deletion.")
            return
        if not self.BASE_SEARCH_VECTORS and not self.M2M_SEARCH_VECTORS \
                and not self.INT_SEARCH_VECTORS \
                and not self.PROPERTY_SEARCH_VECTORS \
                and not self.PARENT_SEARCH_VECTORS:
            logger.warning("No search_vectors defined for {}".format(
                self.__class__))
            return
        if getattr(self, '_search_updated', None):
            return
        self._search_updated = True

        old_search = ""
        if self.search_vector:
            old_search = self.search_vector[:]
        search_vectors = []
        base_q = self.__class__.objects.filter(pk=self.pk)

        # many to many have to be queried one by one otherwise only one is fetch
        for M2M_SEARCH_VECTOR in self.M2M_SEARCH_VECTORS:
            key = M2M_SEARCH_VECTOR.split('__')[0]
            rel_key = getattr(self, key)
            for item in rel_key.values('pk').all():
                query_dct = {key + "__pk": item['pk']}
                q = copy.copy(base_q).filter(**query_dct)
                q = q.annotate(
                    search=SearchVector(
                        M2M_SEARCH_VECTOR,
                        config=settings.ISHTAR_SEARCH_LANGUAGE)
                ).values('search')
                search_vectors.append(q.all()[0]['search'])

        # int/float are not well managed by the SearchVector
        for INT_SEARCH_VECTOR in self.INT_SEARCH_VECTORS:
            q = base_q.values(INT_SEARCH_VECTOR)
            search_vectors.append(
                "'{}':1".format(q.all()[0][INT_SEARCH_VECTOR]))

        if not exclude_parent:
            # copy parent vector fields
            for PARENT_SEARCH_VECTOR in self.PARENT_SEARCH_VECTORS:
                parent = getattr(self, PARENT_SEARCH_VECTOR)
                if hasattr(parent, 'all'):  # m2m
                    for p in parent.all():
                        search_vectors.append(p.search_vector)
                elif parent:
                    search_vectors.append(parent.search_vector)

        for PARENT_ONLY_SEARCH_VECTOR in self.PARENT_ONLY_SEARCH_VECTORS:
            parent = getattr(self, PARENT_ONLY_SEARCH_VECTOR)
            if hasattr(parent, 'all'):  # m2m
                for p in parent.all():
                    search_vectors.append(
                        p.update_search_vector(save=False, exclude_parent=True)
                    )
            elif parent:
                search_vectors.append(
                    parent.update_search_vector(save=False, exclude_parent=True)
                )

        if self.BASE_SEARCH_VECTORS:
            # query "simple" fields
            q = base_q.values(*self.BASE_SEARCH_VECTORS)
            res = q.all()[0]
            for base_search_vector in self.BASE_SEARCH_VECTORS:
                data = res[base_search_vector]
                data = unidecode(unicode(data))
                with connection.cursor() as cursor:
                    cursor.execute("SELECT to_tsvector(%s)", [data])
                    row = cursor.fetchone()
                    search_vectors.append(row[0])

        if self.PROPERTY_SEARCH_VECTORS:
            for attr in self.PROPERTY_SEARCH_VECTORS:
                data = getattr(self, attr)
                if callable(data):
                    data = data()
                if not data:
                    continue
                data = unicode(data)
                with connection.cursor() as cursor:
                    cursor.execute("SELECT to_tsvector(%s)", [data])
                    row = cursor.fetchone()
                    search_vectors.append(row[0])

        if hasattr(self, 'data') and self.data:
            content_type = ContentType.objects.get_for_model(self)
            for json_field in JsonDataField.objects.filter(
                    content_type=content_type,
                    search_index=True).all():
                data = copy.deepcopy(self.data)
                no_data = False
                for key in json_field.key.split('__'):
                    if key not in data:
                        no_data = True
                        break
                    data = data[key]
                if no_data:
                    continue
                with connection.cursor() as cursor:
                    cursor.execute("SELECT to_tsvector(%s)",
                                   [data])
                    row = cursor.fetchone()
                    search_vectors.append(row[0])
        new_search_vector = merge_tsvectors(search_vectors)
        changed = old_search != new_search_vector
        if save and changed:
            self.search_vector = new_search_vector
            self.skip_history_when_saving = True
            self.save()
        elif not save:
            return new_search_vector
        return changed


class FixAssociated(object):
    ASSOCIATED = {}

    def fix_associated(self):
        for key in self.ASSOCIATED:
            item = getattr(self, key)
            if not item:
                continue
            dct = self.ASSOCIATED[key]
            for dct_key in dct:
                subkey, ctype = dct_key
                expected_values = dct[dct_key]
                if not isinstance(expected_values, (list, tuple)):
                    expected_values = [expected_values]
                if hasattr(ctype, "txt_idx"):
                    try:
                        expected_values = [ctype.objects.get(txt_idx=v)
                                           for v in expected_values]
                    except ctype.DoesNotExist:
                        # type not yet initialized
                        return
                current_vals = getattr(item, subkey)
                is_many = False
                if hasattr(current_vals, "all"):
                    is_many = True
                    current_vals = current_vals.all()
                else:
                    current_vals = [current_vals]
                is_ok = False
                for current_val in current_vals:
                    if current_val in expected_values:
                        is_ok = True
                        break
                if is_ok:
                    continue
                # the first value is used
                new_value = expected_values[0]
                if is_many:
                    getattr(item, subkey).add(new_value)
                else:
                    setattr(item, subkey, new_value)


class DocumentItem(object):
    @property
    def images(self):
        if not hasattr(self, 'documents'):
            return Document.objects.none()
        return self.documents.filter(
            image__isnull=False).exclude(image="").order_by("pk")

    def get_extra_actions(self, request):
        """
        For sheet template: return "Add document / image" action
        """
        # url, base_text, icon, extra_text, extra css class, is a quick action
        actions = []

        if not hasattr(self, 'SLUG'):
            return actions

        can_add_doc = self.can_do(request, 'add_document')
        if can_add_doc:
            actions = [
                (
                    reverse("create-document") + "?{}={}".format(self.SLUG,
                                                                 self.pk),
                    _(u"Add document/image"),
                    "fa fa-plus",
                    _(u"doc./image"),
                    "",
                    False
                )
            ]
        return actions


class BaseHistorizedItem(DocumentItem, FullSearch, Imported, JsonData,
                         FixAssociated):
    """
    Historized item with external ID management.
    All historized items are searchable and have a data json field.
    """
    IS_BASKET = False
    EXTERNAL_ID_KEY = ''
    EXTERNAL_ID_DEPENDENCIES = []
    history_modifier = models.ForeignKey(
        User, related_name='+', on_delete=models.SET_NULL,
        verbose_name=_(u"Last editor"), blank=True, null=True)
    history_creator = models.ForeignKey(
        User, related_name='+', on_delete=models.SET_NULL,
        verbose_name=_(u"Creator"), blank=True, null=True)
    last_modified = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

    @classmethod
    def get_verbose_name(cls):
        return cls._meta.verbose_name

    def update_external_id(self, save=False):
        if not self.EXTERNAL_ID_KEY or (
                self.external_id and not self.auto_external_id):
            return
        external_id = get_external_id(self.EXTERNAL_ID_KEY, self)
        if external_id == self.external_id:
            return
        self.auto_external_id = True
        self.external_id = external_id
        self._cached_label_checked = False
        if save:
            self.skip_history_when_saving = True
            self.save()
        return external_id

    def get_previous(self, step=None, date=None, strict=True):
        """
        Get a "step" previous state of the item
        """
        assert step or date
        historized = self.history.all()
        item = None
        if step:
            if len(historized) <= step:
                # silently return the last step if too far in the history
                item = historized[len(historized) - 1]
            else:
                item = historized[step]
        else:
            for step, item in enumerate(historized):
                if item.history_date == date:
                    break
            # ended with no match
            if item.history_date != date:
                return
        item._step = step
        if len(historized) != (step + 1):
            item._previous = historized[step + 1].history_date
        else:
            item._previous = None
        if step > 0:
            item._next = historized[step - 1].history_date
        else:
            item._next = None
        item.history_date = historized[step].history_date
        model = self.__class__
        for k in get_all_field_names(model):
            field = model._meta.get_field(k)
            if hasattr(field, 'rel') and field.rel:
                if not hasattr(item, k + '_id'):
                    setattr(item, k, getattr(self, k))
                    continue
                val = getattr(item, k + '_id')
                if not val:
                    setattr(item, k, None)
                    continue
                try:
                    val = field.rel.to.objects.get(pk=val)
                    setattr(item, k, val)
                except ObjectDoesNotExist:
                    if strict:
                        raise HistoryError(u"The class %s has no pk %d" % (
                            unicode(field.rel.to), val))
                    setattr(item, k, None)
        item.pk = self.pk
        return item

    @property
    def last_edition_date(self):
        try:
            return self.history.order_by('-history_date').all()[0].history_date
        except (AttributeError, IndexError):
            return

    @property
    def history_creation_date(self):
        try:
            return self.history.order_by('history_date').all()[0].history_date
        except (AttributeError, IndexError):
            return

    def rollback(self, date):
        """
        Rollback to a previous state
        """
        to_del, new_item = [], None
        for item in self.history.all():
            if item.history_date == date:
                new_item = item
                break
            to_del.append(item)
        if not new_item:
            raise HistoryError(u"The date to rollback to doesn't exist.")
        try:
            for f in self._meta.fields:
                k = f.name
                if k != 'id' and hasattr(self, k):
                    if not hasattr(new_item, k):
                        k = k + "_id"
                    setattr(self, k, getattr(new_item, k))
            try:
                self.history_modifier = User.objects.get(
                    pk=new_item.history_modifier_id)
            except User.ObjectDoesNotExist:
                pass
            # force label regeneration
            self._cached_label_checked = False
            self.save()
        except:
            raise HistoryError(u"The rollback has failed.")
        # clean the obsolete history
        for historized_item in to_del:
            historized_item.delete()

    def values(self):
        values = {}
        for f in self._meta.fields:
            k = f.name
            if k != 'id':
                values[k] = getattr(self, k)
        return values

    def get_show_url(self):
        try:
            return reverse('show-' + self.__class__.__name__.lower(),
                           args=[self.pk, ''])
        except NoReverseMatch:
            return

    @property
    def associated_filename(self):
        if [True for attr in ('get_town_label', 'get_department', 'reference',
                              'short_class_name') if not hasattr(self, attr)]:
            return ''
        items = [slugify(self.get_department()),
                 slugify(self.get_town_label()).upper(),
                 slugify(self.short_class_name),
                 slugify(self.reference),
                 slugify(self.name or '').replace('-', '_').capitalize()]
        last_edition_date = self.last_edition_date
        if last_edition_date:
            items.append(last_edition_date.strftime('%Y%m%d'))
        else:
            items.append('00000000')
        return u"-".join([unicode(item) for item in items])

    def save(self, *args, **kwargs):
        created = not self.pk
        if not getattr(self, 'skip_history_when_saving', False):
            assert hasattr(self, 'history_modifier')
            if created:
                self.history_creator = self.history_modifier
        # external ID can have related item not available before save
        external_id_updated = kwargs.pop('external_id_updated') \
            if 'external_id_updated' in kwargs else False
        if not created and not external_id_updated:
            self.update_external_id()
        super(BaseHistorizedItem, self).save(*args, **kwargs)
        if created and self.update_external_id():
            # force resave for external ID creation
            self.skip_history_when_saving = True
            self._updated_id = True
            return self.save(external_id_updated=True)
        for dep in self.EXTERNAL_ID_DEPENDENCIES:
            for obj in getattr(self, dep).all():
                obj.update_external_id(save=True)
        self.fix_associated()
        return True


LOGICAL_TYPES = (
    ('above', _(u"Above")),
    ('bellow', _(u"Bellow")),
    ('equal', _(u"Equal"))
)


class GeneralRelationType(GeneralType):
    order = models.IntegerField(_(u"Order"), default=1)
    symmetrical = models.BooleanField(_(u"Symmetrical"))
    tiny_label = models.CharField(_(u"Tiny label"), max_length=50,
                                  blank=True, null=True)
    inverse_relation = models.ForeignKey(
        'self', verbose_name=_(u"Inverse relation"), blank=True,
        null=True)
    logical_relation = models.CharField(
        verbose_name=_(u"Logical relation"), max_length=10,
        choices=LOGICAL_TYPES, blank=True, null=True)

    class Meta:
        abstract = True

    def clean(self):
        # cannot have symmetrical and an inverse_relation
        if self.symmetrical and self.inverse_relation:
            raise ValidationError(
                _(u"Cannot have symmetrical and an inverse_relation"))

    def get_tiny_label(self):
        return self.tiny_label or self.label or u""

    def save(self, *args, **kwargs):
        obj = super(GeneralRelationType, self).save(*args, **kwargs)
        # after saving check that the inverse_relation of the inverse_relation
        # point to the saved object
        if self.inverse_relation \
                and (not self.inverse_relation.inverse_relation
                     or self.inverse_relation.inverse_relation != self):
            self.inverse_relation.inverse_relation = self
            self.inverse_relation.symmetrical = False
            self.inverse_relation.save()
        return obj


class GeneralRecordRelations(object):
    @classmethod
    def general_types(cls):
        return ['relation_type']

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

        # after saving create the symetric or the inverse relation

        sym_rel_type = self.relation_type
        if not self.relation_type.symmetrical:
            sym_rel_type = self.relation_type.inverse_relation

        # no symetric/inverse is defined
        if not sym_rel_type:
            return

        dct = {'right_record': self.left_record,
               'left_record': self.right_record, 'relation_type': sym_rel_type}
        self.__class__.objects.get_or_create(**dct)
        return self


def post_delete_record_relation(sender, instance, **kwargs):
    # delete symmetrical or inverse relation
    sym_rel_type = instance.relation_type
    if not instance.relation_type.symmetrical:
        sym_rel_type = instance.relation_type.inverse_relation

    # no symetric/inverse is defined
    if not sym_rel_type:
        return
    dct = {'right_record_id': instance.left_record_id,
           'left_record_id': instance.right_record_id,
           'relation_type': sym_rel_type}
    q = instance.__class__.objects.filter(**dct)
    if q.count():
        q.delete()


class SearchQuery(models.Model):
    label = models.TextField(_(u"Label"), blank=True)
    query = models.TextField(_(u"Query"), blank=True)
    content_type = models.ForeignKey(ContentType,
                                     verbose_name=_(u"Content type"))
    profile = models.ForeignKey("UserProfile", verbose_name=_(u"Profile"))
    is_alert = models.BooleanField(_(u"Is an alert"), default=False)

    class Meta:
        verbose_name = _(u"Search query")
        verbose_name_plural = _(u"Search queries")
        ordering = ['label']

    def __unicode__(self):
        return unicode(self.label)


class ShortMenuItem(object):
    """
    Item available in the short menu
    """
    UP_MODEL_QUERY = {}

    @classmethod
    def get_short_menu_class(cls, pk):
        return ''

    @property
    def short_class_name(self):
        return ""


class QuickAction(object):
    """
    Quick action available from tables
    """

    def __init__(self, url, icon_class='', text='', target=None, rights=None,
                 module=None):
        self.url = url
        self.icon_class = icon_class
        self.text = text
        self.rights = rights
        self.target = target
        self.module = module
        assert self.target in ('one', 'many', None)

    def is_available(self, user, session=None, obj=None):
        if self.module and not getattr(get_current_profile(), self.module):
            return False
        if not self.rights:  # no restriction
            return True
        if not user or not hasattr(user, 'ishtaruser') or not user.ishtaruser:
            return False
        user = user.ishtaruser

        for right in self.rights:
            if user.has_perm(right, session=session, obj=obj):
                return True
        return False

    @property
    def rendered_icon(self):
        if not self.icon_class:
            return ""
        return u"<i class='{}' aria-hidden='true'></i>".format(self.icon_class)

    @property
    def base_url(self):
        if self.target is None:
            url = reverse(self.url)
        else:
            # put arbitrary pk for the target
            url = reverse(self.url, args=[0])
            url = url[:-2]  # all quick action url have to finish with the
            # pk of the selected item and a "/"
        return url


class MainItem(ShortMenuItem):
    """
    Item with quick actions available from tables
    """
    QUICK_ACTIONS = []

    @classmethod
    def get_quick_actions(cls, user, session=None, obj=None):
        """
        Get a list of (url, title, icon, target) actions for an user
        """
        qas = []
        for action in cls.QUICK_ACTIONS:
            if not action.is_available(user, session=session, obj=obj):
                continue
            qas.append([action.base_url,
                        mark_safe(action.text),
                        mark_safe(action.rendered_icon),
                        action.target or ""])
        return qas


class LightHistorizedItem(BaseHistorizedItem):
    history_date = models.DateTimeField(default=datetime.datetime.now)

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        super(LightHistorizedItem, self).save(*args, **kwargs)
        return self


PARSE_FORMULA = re.compile("{([^}]*)}")


def _deduplicate(value):
    new_values = []
    for v in value.split(u'-'):
        if v not in new_values:
            new_values.append(v)
    return u'-'.join(new_values)


FORMULA_FILTERS = {
    'upper': lambda x: x.upper(),
    'lower': lambda x: x.lower(),
    'capitalize': lambda x: x.capitalize(),
    'slug': lambda x: slugify(x),
    'deduplicate': _deduplicate
}


def get_external_id(key, item):
    profile = get_current_profile()
    if not hasattr(profile, key):
        return
    formula = getattr(profile, key)
    dct = {}
    for fkey in PARSE_FORMULA.findall(formula):
        filtered = fkey.split(u'|')
        initial_key = fkey[:]
        fkey = filtered[0]
        filters = []
        for filtr in filtered[1:]:
            if filtr in FORMULA_FILTERS:
                filters.append(FORMULA_FILTERS[filtr])
        if fkey.startswith('settings__'):
            dct[fkey] = getattr(settings, fkey[len('settings__'):]) or ''
            continue
        obj = item
        for k in fkey.split('__'):
            try:
                obj = getattr(obj, k)
            except ObjectDoesNotExist:
                obj = None
            if callable(obj):
                obj = obj()
            if obj is None:
                break
        if obj is None:
            dct[initial_key] = ''
        else:
            dct[initial_key] = unicode(obj)
        for filtr in filters:
            dct[initial_key] = filtr(dct[initial_key])
    values = formula.format(**dct).split('||')
    value = values[0]
    for filtr in values[1:]:
        if filtr not in FORMULA_FILTERS:
            value += u'||' + filtr
            continue
        value = FORMULA_FILTERS[filtr](value)
    return value


CURRENCY = ((u"€", _(u"Euro")),
            (u"$", _(u"US dollar")))
FIND_INDEX_SOURCE = ((u"O", _(u"Operations")),
                     (u"CR", _(u"Context records")))
SITE_LABELS = [('site', _(u"Site")), ('entity', _(u"Archaeological entity"))]

TRANSLATED_SITE_LABELS = {
    'site': {
        'search': _(u"Site search"),
        'new': _(u"New site"),
        'modification': _(u"Site modification"),
        'deletion': _(u"Site deletion"),
    },
    'entity': {
        'search': _(u"Archaeological entity search"),
        'new': _(u"New archaeological entity"),
        'modification': _(u"Archaeological entity modification"),
        'deletion': _(u"Archaeological entity deletion"),
    },
}


class IshtarSiteProfile(models.Model, Cached):
    slug_field = 'slug'
    label = models.TextField(_(u"Name"))
    slug = models.SlugField(_(u"Slug"), unique=True)
    active = models.BooleanField(_(u"Current active"), default=False)
    experimental_feature = models.BooleanField(
        _(u"Activate experimental feature"), default=False)
    description = models.TextField(_(u"Description"), null=True, blank=True)
    config = models.CharField(
        _(u"Alternate configuration"), max_length=200,
        choices=ALTERNATE_CONFIGS_CHOICES,
        help_text=_(u"Choose an alternate configuration for label, "
                    u"index management"),
        null=True, blank=True
    )
    files = models.BooleanField(_(u"Files module"), default=False)
    archaeological_site = models.BooleanField(
        _(u"Archaeological site module"), default=False)
    archaeological_site_label = models.CharField(
        _(u"Archaeological site type"), max_length=200,
        choices=SITE_LABELS,
        default='site'
    )
    context_record = models.BooleanField(_(u"Context records module"),
                                         default=False)
    find = models.BooleanField(_(u"Finds module"), default=False,
                               help_text=_(u"Need context records module"))
    find_index = models.CharField(
        _(u"Find index is based on"), default='O', max_length=2,
        choices=FIND_INDEX_SOURCE,
        help_text=_(u"To prevent irrelevant indexes, change this parameter "
                    u"only if there is no find in the database"))
    warehouse = models.BooleanField(
        _(u"Warehouses module"), default=False,
        help_text=_(u"Need finds module"))
    preservation = models.BooleanField(_(u"Preservation module"),
                                       default=False)
    mapping = models.BooleanField(_(u"Mapping module"), default=False)
    underwater = models.BooleanField(_(u"Underwater module"), default=False)
    parcel_mandatory = models.BooleanField(
        _(u"Parcel are mandatory for context records"), default=True)
    homepage = models.TextField(
        _(u"Home page"), null=True, blank=True,
        help_text=_(u"Homepage of Ishtar - if not defined a default homepage "
                    u"will appear. Use the markdown syntax. {random_image} "
                    u"can be used to display a random image."))
    operation_prefix = models.CharField(
        _(u"Main operation code prefix"), default=u'OA', null=True, blank=True,
        max_length=20
    )
    default_operation_prefix = models.CharField(
        _(u"Default operation code prefix"), default=u'OP', null=True,
        blank=True, max_length=20
    )
    operation_region_code = models.CharField(
        _(u"Operation region code"), null=True, blank=True,
        max_length=5
    )
    file_external_id = models.TextField(
        _(u"File external id"),
        default=u"{year}-{numeric_reference}",
        help_text=_(u"Formula to manage file external ID. "
                    u"Change this with care. With incorrect formula, the "
                    u"application might be unusable and import of external "
                    u"data can be destructive."))
    parcel_external_id = models.TextField(
        _(u"Parcel external id"),
        default=u"{associated_file__external_id}{operation__code_patriarche}-"
                u"{town__numero_insee}-{section}{parcel_number}",
        help_text=_(u"Formula to manage parcel external ID. "
                    u"Change this with care. With incorrect formula, the "
                    u"application might be unusable and import of external "
                    u"data can be destructive."))
    context_record_external_id = models.TextField(
        _(u"Context record external id"),
        default=u"{parcel__external_id}-{label}",
        help_text=_(u"Formula to manage context record external ID. "
                    u"Change this with care. With incorrect formula, the "
                    u"application might be unusable and import of external "
                    u"data can be destructive."))
    base_find_external_id = models.TextField(
        _(u"Base find external id"),
        default=u"{context_record__external_id}-{label}",
        help_text=_(u"Formula to manage base find external ID. "
                    u"Change this with care. With incorrect formula, the "
                    u"application might be unusable and import of external "
                    u"data can be destructive."))
    find_external_id = models.TextField(
        _(u"Find external id"),
        default=u"{get_first_base_find__context_record__external_id}-{label}",
        help_text=_(u"Formula to manage find external ID. "
                    u"Change this with care. With incorrect formula, the "
                    u"application might be unusable and import of external "
                    u"data can be destructive."))
    container_external_id = models.TextField(
        _(u"Container external id"),
        default=u"{responsible__external_id}-{index}",
        help_text=_(u"Formula to manage container external ID. "
                    u"Change this with care. With incorrect formula, the "
                    u"application might be unusable and import of external "
                    u"data can be destructive."))
    warehouse_external_id = models.TextField(
        _(u"Warehouse external id"),
        default=u"{name|slug}",
        help_text=_(u"Formula to manage warehouse external ID. "
                    u"Change this with care. With incorrect formula, the "
                    u"application might be unusable and import of external "
                    u"data can be destructive."))
    person_raw_name = models.TextField(
        _(u"Raw name for person"),
        default=u"{name|upper} {surname}",
        help_text=_(u"Formula to manage person raw_name. "
                    u"Change this with care. With incorrect formula, the "
                    u"application might be unusable and import of external "
                    u"data can be destructive."))
    find_use_index = models.BooleanField(_(u"Use auto index for finds"),
                                         default=True)
    currency = models.CharField(_(u"Currency"), default=u"€",
                                choices=CURRENCY, max_length=5)

    class Meta:
        verbose_name = _(u"Ishtar site profile")
        verbose_name_plural = _(u"Ishtar site profiles")
        ordering = ['label']

    def __unicode__(self):
        return unicode(self.label)

    def has_overload(self, key):
        return self.config and self.config in ALTERNATE_CONFIGS and \
               hasattr(ALTERNATE_CONFIGS[self.config], key)

    @classmethod
    def get_current_profile(cls, force=False):
        cache_key, value = get_cache(cls, ['is-current-profile'])
        if value and not force:
            return value
        q = cls.objects.filter(active=True)
        if not q.count():
            obj = cls.objects.create(
                label="Default profile", slug='default', active=True)
        else:
            obj = q.all()[0]
        cache.set(cache_key, obj, settings.CACHE_TIMEOUT)
        return obj

    @classmethod
    def get_default_site_label(cls, key=None):
        return cls.get_current_profile().get_site_label(key)

    def get_site_label(self, key=None):
        if not key:
            return unicode(dict(SITE_LABELS)[self.archaeological_site_label])
        return unicode(
            TRANSLATED_SITE_LABELS[self.archaeological_site_label][key]
        )

    def save(self, *args, **kwargs):
        raw = False
        if 'raw' in kwargs:
            raw = kwargs.pop('raw')
        super(IshtarSiteProfile, self).save(*args, **kwargs)
        obj = self
        if raw:
            return obj
        q = self.__class__.objects.filter(active=True).exclude(slug=self.slug)
        if obj.active and q.count():
            for profile in q.all():
                profile.active = False
                profile.save(raw=True)
        changed = False
        if not obj.active and not q.count():
            obj.active = True
            changed = True
        if obj.warehouse and not obj.find:
            obj.find = True
            changed = True
        if obj.find and not obj.context_record:
            obj.context_record = True
            changed = True
        if changed:
            obj = obj.save(raw=True)
        return obj


def get_current_profile(force=False):
    return IshtarSiteProfile.get_current_profile(force=force)


def cached_site_changed(sender, **kwargs):
    get_current_profile(force=True)
    from ishtar_common.menus import Menu
    MAIN_MENU = Menu(None)
    MAIN_MENU.init()
    MAIN_MENU.reinit_menu_for_all_user()


post_save.connect(cached_site_changed, sender=IshtarSiteProfile)
post_delete.connect(cached_site_changed, sender=IshtarSiteProfile)


class CustomForm(models.Model):
    name = models.CharField(_(u"Name"), max_length=250)
    form = models.CharField(_(u"Form"), max_length=250)
    available = models.BooleanField(_(u"Available"), default=True)
    enabled = models.BooleanField(
        _(u"Enable this form"), default=True,
        help_text=_(u"Disable with caution: disabling a form with mandatory "
                    u"fields may lead to database errors."))
    apply_to_all = models.BooleanField(
        _(u"Apply to all"), default=False,
        help_text=_(u"Apply this form to all users. If set to True, selecting "
                    u"user and user type is useless."))
    users = models.ManyToManyField('IshtarUser', blank=True)
    user_types = models.ManyToManyField('PersonType', blank=True)

    class Meta:
        verbose_name = _(u"Custom form")
        verbose_name_plural = _(u"Custom forms")
        ordering = ['name', 'form']

    def __unicode__(self):
        return u"{} - {}".format(self.name, self.form)

    def users_lbl(self):
        users = [unicode(user) for user in self.users.all()]
        return " ; ".join(users)

    users_lbl.short_description = _(u"Users")

    def user_types_lbl(self):
        user_types = [unicode(u) for u in self.user_types.all()]
        return " ; ".join(user_types)

    user_types_lbl.short_description = _(u"User types")

    @classmethod
    def register(cls):
        if hasattr(cls, '_register') and hasattr(cls, '_register_fields'):
            return cls._register, cls._register_fields
        cache_key, value = get_cache(cls.__class__, ['dct-forms'],
                                     app_label='ishtar_common')
        cache_key_fields, value_fields = get_cache(
            cls.__class__, ['dct-fields'], app_label='ishtar_common')
        if value and value_fields:
            cls._register = value
            cls._register_fields = value_fields
            return cls._register, cls._register_fields
        cls._register, cls._register_fields = {}, {}
        # ideally should be improved but only used in admin
        from ishtar_common.admin import ISHTAR_FORMS
        from ishtar_common.forms import CustomForm as CustomFormForm

        for app_form in ISHTAR_FORMS:
            app_name = app_form.__package__
            if app_name == "archaeological_files_pdl":
                app_name = "archaeological_files"
            for form in dir(app_form):
                if 'Form' not in form and 'Select' not in form:
                    # not very clean... but do not treat inappropriate items
                    continue
                form = getattr(app_form, form)
                if not inspect.isclass(form) \
                        or not issubclass(form, CustomFormForm) \
                        or not getattr(form, 'form_slug', None):
                    continue
                model_name = form.form_slug.split('-')[0].replace('_', '')
                if app_name not in cls._register_fields:
                    cls._register_fields[app_name] = []
                if model_name not in cls._register_fields[app_name]:
                    cls._register_fields[app_name].append(model_name)
                cls._register[form.form_slug] = form
        return cls._register, cls._register_fields

    def get_form_class(self):
        register, register_fields = self.register()
        if self.form not in self._register:
            return
        return register[self.form]

    def get_available_json_fields(self):
        register, register_fields = self.register()
        if self.form not in self._register:
            return []
        current_form = register[self.form]
        app_name = current_form.__module__.split('.')[0]
        if app_name == "archaeological_files_pdl":
            app_name = "archaeological_files"
        if app_name not in register_fields:
            return []
        res = []
        for model_name in register_fields[app_name]:
            q = ContentType.objects.filter(app_label=app_name,
                                           model=model_name)
            if not q.count():
                continue
            ct = q.all()[0]
            for json_field in JsonDataField.objects.filter(
                    content_type=ct).all():
                res.append((json_field.pk, u"{} ({})".format(
                    json_field.name,
                    dict(JSON_VALUE_TYPES)[json_field.value_type])))
        return res


class ExcludedField(models.Model):
    custom_form = models.ForeignKey(CustomForm, related_name='excluded_fields')
    field = models.CharField(_(u"Field"), max_length=250)

    class Meta:
        verbose_name = _(u"Excluded field")
        verbose_name_plural = _(u"Excluded fields")


class CustomFormJsonField(models.Model):
    custom_form = models.ForeignKey(CustomForm, related_name='json_fields')
    json_field = models.ForeignKey(JsonDataField,
                                   related_name='custom_form_details')
    label = models.CharField(_(u"Label"), max_length=200, blank=True,
                             default='')
    order = models.IntegerField(verbose_name=_(u"Order"), default=1)
    help_text = models.TextField(_(u"Help"), blank=True, null=True)

    class Meta:
        verbose_name = _(u"Custom form - Json data field")
        verbose_name_plural = _(u"Custom form - Json data fields")


class GlobalVar(models.Model, Cached):
    slug = models.SlugField(_(u"Variable name"), unique=True)
    description = models.TextField(_(u"Description of the variable"),
                                   null=True, blank=True)
    value = models.TextField(_(u"Value"), null=True, blank=True)

    class Meta:
        verbose_name = _(u"Global variable")
        verbose_name_plural = _(u"Global variables")
        ordering = ['slug']

    def __unicode__(self):
        return unicode(self.slug)


def cached_globalvar_changed(sender, **kwargs):
    if not kwargs['instance']:
        return
    var = kwargs['instance']
    cache_key, value = get_cache(GlobalVar, var.slug)
    cache.set(cache_key, var.value, settings.CACHE_TIMEOUT)


post_save.connect(cached_globalvar_changed, sender=GlobalVar)


class UserDashboard:
    def __init__(self):
        types = IshtarUser.objects.values('person__person_types',
                                          'person__person_types__label')
        self.types = types.annotate(number=Count('pk')) \
            .order_by('person__person_types')


class DashboardFormItem(object):
    """
    Provide methods to manage statistics
    """

    def _get_or_set_stats(self, funcname, update,
                          timeout=settings.CACHE_TIMEOUT):
        key, val = get_cache(self.__class__, [funcname, self.pk])
        if not update and val is not None:
            return val
        val = getattr(self, funcname)()
        cache.set(key, val, timeout)
        return val

    @classmethod
    def get_periods(cls, slice='month', fltr={}, date_source='creation'):
        date_var = date_source + '_date'
        q = cls.objects.filter(**{date_var + '__isnull': False})
        if fltr:
            q = q.filter(**fltr)
        if slice == 'year':
            return [res[date_var].year for res in list(q.values(date_var)
                                                       .annotate(
                Count("id")).order_by())]
        elif slice == 'month':
            return [(res[date_var].year, res[date_var].month)
                    for res in list(q.values(date_var)
                                    .annotate(Count("id")).order_by())]
        return []

    @classmethod
    def get_by_year(cls, year, fltr={}, date_source='creation'):
        date_var = date_source + '_date'
        q = cls.objects.filter(**{date_var + '__isnull': False})
        if fltr:
            q = q.filter(**fltr)
        return q.filter(
            **{date_var + '__year': year}).order_by('pk').distinct('pk')

    @classmethod
    def get_by_month(cls, year, month, fltr={}, date_source='creation'):
        date_var = date_source + '_date'
        q = cls.objects.filter(**{date_var + '__isnull': False})
        if fltr:
            q = q.filter(**fltr)
        q = q.filter(
            **{date_var + '__year': year, date_var + '__month': month})
        return q.order_by('pk').distinct('pk')

    @classmethod
    def get_total_number(cls, fltr=None):
        q = cls.objects
        if fltr:
            q = q.filter(**fltr)
        return q.order_by('pk').distinct('pk').count()


class Dashboard(object):
    def __init__(self, model, slice='year', date_source=None, show_detail=None,
                 fltr=None):
        if not fltr:
            fltr = {}
        # don't provide date_source if it is not relevant
        self.model = model
        self.total_number = model.get_total_number(fltr)
        self.show_detail = show_detail
        history_model = self.model.history.model
        # last edited - created
        self.recents, self.lasts = [], []
        for last_lst, modif_type in ((self.lasts, '+'), (self.recents, '~')):
            last_ids = history_model.objects.values('id') \
                .annotate(hd=Max('history_date'))
            last_ids = last_ids.filter(history_type=modif_type)
            from archaeological_finds.models import Find
            if self.model == Find:
                last_ids = last_ids.filter(
                    downstream_treatment_id__isnull=True)
                if modif_type == '+':
                    last_ids = last_ids.filter(
                        upstream_treatment_id__isnull=True)
            last_ids = last_ids.order_by('-hd').distinct().all()[:5]
            for idx in last_ids:
                try:
                    obj = self.model.objects.get(pk=idx['id'])
                except:
                    # deleted object are always referenced in history
                    continue
                obj.history_date = idx['hd']
                last_lst.append(obj)
        # years
        base_kwargs = {'fltr': fltr.copy()}
        if date_source:
            base_kwargs['date_source'] = date_source
        periods_kwargs = copy.deepcopy(base_kwargs)
        periods_kwargs['slice'] = slice
        self.periods = model.get_periods(**periods_kwargs)
        self.periods = list(set(self.periods))
        self.periods.sort()
        if not self.total_number or not self.periods:
            return
        kwargs_num = copy.deepcopy(base_kwargs)
        self.serie_labels = [_(u"Total")]
        # numbers
        if slice == 'year':
            self.values = [('year', "",
                            list(reversed(self.periods)))]
            self.numbers = [model.get_by_year(year, **kwargs_num).count()
                            for year in self.periods]
            self.values += [('number', _(u"Number"),
                             list(reversed(self.numbers)))]
        if slice == 'month':
            periods = list(reversed(self.periods))
            self.periods = ["%d-%s-01" % (p[0], ('0' + str(p[1]))
            if len(str(p[1])) == 1 else p[1]) for p in periods]
            self.values = [('month', "", self.periods)]
            if show_detail:
                for dpt, lbl in settings.ISHTAR_DPTS:
                    self.serie_labels.append(unicode(dpt))
                    idx = 'number_' + unicode(dpt)
                    kwargs_num['fltr']["towns__numero_insee__startswith"] = \
                        unicode(dpt)
                    numbers = [model.get_by_month(*p.split('-')[:2],
                                                  **kwargs_num).count()
                               for p in self.periods]
                    self.values += [(idx, dpt, list(numbers))]
                # put "Total" at the end
                self.serie_labels.append(self.serie_labels.pop(0))
            kwargs_num = base_kwargs.copy()
            self.numbers = [model.get_by_month(*p.split('-')[:2],
                                               **kwargs_num).count()
                            for p in self.periods]
            self.values += [('number', _(u"Total"),
                             list(self.numbers))]
        # calculate
        self.average = self.get_average()
        self.variance = self.get_variance()
        self.standard_deviation = self.get_standard_deviation()
        self.median = self.get_median()
        self.mode = self.get_mode()
        # by operation
        if not hasattr(model, 'get_by_operation'):
            return
        operations = model.get_operations()
        operation_numbers = [model.get_by_operation(op).count()
                             for op in operations]
        # calculate
        self.operation_average = self.get_average(operation_numbers)
        self.operation_variance = self.get_variance(operation_numbers)
        self.operation_standard_deviation = self.get_standard_deviation(
            operation_numbers)
        self.operation_median = self.get_median(operation_numbers)
        operation_mode_pk = self.get_mode(dict(zip(operations,
                                                   operation_numbers)))
        if operation_mode_pk:
            from archaeological_operations.models import Operation
            self.operation_mode = unicode(Operation.objects
                                          .get(pk=operation_mode_pk))

    def get_average(self, vals=[]):
        if not vals:
            vals = self.numbers[:]
        return sum(vals) / len(vals)

    def get_variance(self, vals=[]):
        if not vals:
            vals = self.numbers[:]
        avrg = self.get_average(vals)
        return self.get_average([(x - avrg) ** 2 for x in vals])

    def get_standard_deviation(self, vals=[]):
        if not vals:
            vals = self.numbers[:]
        return round(self.get_variance(vals) ** 0.5, 3)

    def get_median(self, vals=[]):
        if not vals:
            vals = self.numbers[:]
        len_vals = len(vals)
        vals.sort()
        if (len_vals % 2) == 1:
            return vals[len_vals / 2]
        else:
            return (vals[len_vals / 2 - 1] + vals[len_vals / 2]) / 2.0

    def get_mode(self, vals={}):
        if not vals:
            vals = dict(zip(self.periods, self.numbers[:]))
        mx = max(vals.values())
        for v in vals:
            if vals[v] == mx:
                return v


class DocumentTemplate(models.Model):
    CLASSNAMES = (('archaeological_operations.models.AdministrativeAct',
                   _(u"Administrative Act")),)
    name = models.CharField(_(u"Name"), max_length=100)
    slug = models.SlugField(_(u"Slug"), blank=True, null=True, max_length=100,
                            unique=True)
    template = models.FileField(
        _(u"Template"), upload_to="templates/%Y/")
    associated_object_name = models.CharField(
        _(u"Associated object"), max_length=100, choices=CLASSNAMES)
    available = models.BooleanField(_(u"Available"), default=True)
    objects = SlugModelManager()

    class Meta:
        verbose_name = _(u"Document template")
        verbose_name_plural = _(u"Document templates")
        ordering = ['associated_object_name', 'name']

    def __unicode__(self):
        return self.name

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

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = create_slug(DocumentTemplate, self.name)
        return super(DocumentTemplate, self).save(*args, **kwargs)

    @classmethod
    def get_tuples(cls, dct={}, empty_first=True):
        dct['available'] = True
        if empty_first:
            yield ('', '----------')
        items = cls.objects.filter(**dct)
        for item in items.distinct().order_by(*cls._meta.ordering).all():
            yield (item.pk, _(unicode(item)))

    def publish(self, c_object):
        tempdir = tempfile.mkdtemp("-ishtardocs")
        output_name = tempdir + os.path.sep + \
                      slugify(self.name.replace(' ', '_').lower()) + u'-' + \
                      datetime.date.today().strftime('%Y-%m-%d') + \
                      u"." + self.template.name.split('.')[-1]
        values = c_object.get_values()
        engine = SecretaryRenderer()
        try:
            result = engine.render(self.template, **values)
        except TemplateSyntaxError as e:
            raise TemplateSyntaxError(e.message, e.lineno)
        output = open(output_name, 'wb')
        output.write(result)
        return output_name

    def convert_from_v1(self):
        """
        Convert the current template from v1 to v2.
        """
        from old.ooo_replace import ooo_replace
        from archaeological_operations.models import AdministrativeAct

        old_dir = settings.MEDIA_ROOT + "/templates/v1/"
        if not os.path.exists(old_dir):
            os.makedirs(old_dir)
        shutil.copy(settings.MEDIA_ROOT + self.template.name, old_dir)

        tempdir = tempfile.mkdtemp("-ishtardocs")
        output_name = tempdir + os.path.sep + self.template.name.split(
            os.sep)[-1]

        objects = []
        filters = [
            {'operation__isnull': False},
            {'associated_file__isnull': False},
            {'treatment_file__isnull': False},
            {'treatment__isnull': False},
        ]
        for filtr in filters:
            q = AdministrativeAct.objects.filter(**filtr)
            if q.count():
                objects.append(q.all()[0])

        if not objects:
            return
        values = {}
        for obj in objects:
            values.update(obj.get_values())
        for key in values:
            values[key] = "{{ " + key + " }}"

        ooo_replace(self.template, output_name, values)
        shutil.move(output_name, settings.MEDIA_ROOT + self.template.name)
        return output_name


class NumberManager(models.Manager):
    def get_by_natural_key(self, number):
        return self.get(number=number)


class State(models.Model):
    label = models.CharField(_(u"Label"), max_length=30)
    number = models.CharField(_(u"Number"), unique=True, max_length=3)
    objects = NumberManager()

    class Meta:
        verbose_name = _(u"State")
        ordering = ['number']

    def __unicode__(self):
        return self.label

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


class Department(models.Model):
    label = models.CharField(_(u"Label"), max_length=30)
    number = models.CharField(_(u"Number"), unique=True, max_length=3)
    state = models.ForeignKey('State', verbose_name=_(u"State"), blank=True,
                              null=True)
    objects = NumberManager()

    class Meta:
        verbose_name = _(u"Department")
        verbose_name_plural = _(u"Departments")
        ordering = ['number']

    def __unicode__(self):
        return self.label

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


class Address(BaseHistorizedItem):
    address = models.TextField(_(u"Address"), null=True, blank=True)
    address_complement = models.TextField(_(u"Address complement"), null=True,
                                          blank=True)
    postal_code = models.CharField(_(u"Postal code"), max_length=10, null=True,
                                   blank=True)
    town = models.CharField(_(u"Town"), max_length=70, null=True, blank=True)
    country = models.CharField(_(u"Country"), max_length=30, null=True,
                               blank=True)
    alt_address = models.TextField(_(u"Other address: address"), null=True,
                                   blank=True)
    alt_address_complement = models.TextField(
        _(u"Other address: address complement"), null=True, blank=True)
    alt_postal_code = models.CharField(_(u"Other address: postal code"),
                                       max_length=10, null=True, blank=True)
    alt_town = models.CharField(_(u"Other address: town"), max_length=70,
                                null=True, blank=True)
    alt_country = models.CharField(_(u"Other address: country"),
                                   max_length=30, null=True, blank=True)
    phone = models.CharField(_(u"Phone"), max_length=18, null=True, blank=True)
    phone_desc = models.CharField(_(u"Phone description"), max_length=300,
                                  null=True, blank=True)
    phone2 = models.CharField(_(u"Phone description 2"), max_length=18,
                              null=True, blank=True)
    phone_desc2 = models.CharField(_(u"Phone description 2"), max_length=300,
                                   null=True, blank=True)
    phone3 = models.CharField(_(u"Phone 3"), max_length=18, null=True,
                              blank=True)
    phone_desc3 = models.CharField(_(u"Phone description 3"), max_length=300,
                                   null=True, blank=True)
    raw_phone = models.TextField(_(u"Raw phone"), blank=True, null=True)
    mobile_phone = models.CharField(_(u"Mobile phone"), max_length=18,
                                    null=True, blank=True)
    email = models.EmailField(
        _(u"Email"), max_length=300, blank=True, null=True)
    alt_address_is_prefered = models.BooleanField(
        _(u"Alternative address is prefered"), default=False)
    history = HistoricalRecords()

    class Meta:
        abstract = True

    def simple_lbl(self):
        return unicode(self)

    def full_address(self):
        lbl = self.simple_lbl()
        if lbl:
            lbl += u"\n"
        lbl += self.address_lbl()
        return lbl

    def address_lbl(self):
        lbl = u''
        prefix = ''
        if self.alt_address_is_prefered:
            prefix = 'alt_'
        if getattr(self, prefix + 'address'):
            lbl += getattr(self, prefix + 'address')
        if getattr(self, prefix + 'address_complement'):
            if lbl:
                lbl += "\n"
            lbl += getattr(self, prefix + 'address_complement')
        postal_code = getattr(self, prefix + 'postal_code')
        town = getattr(self, prefix + 'town')
        if postal_code or town:
            if lbl:
                lbl += "\n"
            lbl += u"{}{}{}".format(
                postal_code or '',
                " " if postal_code and town else '',
                town or '')
        if self.phone:
            if lbl:
                lbl += u"\n"
            lbl += u"{} {}".format(unicode(_("Tel: ")), self.phone)
        if self.mobile_phone:
            if lbl:
                lbl += u"\n"
            lbl += u"{} {}".format(unicode(_("Mobile: ")), self.mobile_phone)
        if self.email:
            if lbl:
                lbl += u"\n"
            lbl += u"{} {}".format(unicode(_("Email: ")), self.email)
        return lbl


class Merge(models.Model):
    merge_key = models.TextField(_("Merge key"), blank=True, null=True)
    merge_candidate = models.ManyToManyField("self", blank=True)
    merge_exclusion = models.ManyToManyField("self", blank=True)
    archived = models.NullBooleanField(default=False,
                                       blank=True, null=True)
    # 1 for one word similarity, 2 for two word similarity, etc.
    MERGE_CLEMENCY = None
    EMPTY_MERGE_KEY = '--'

    class Meta:
        abstract = True

    def generate_merge_key(self):
        if self.archived:
            return
        self.merge_key = slugify(self.name if self.name else '')
        if not self.merge_key:
            self.merge_key = self.EMPTY_MERGE_KEY
        self.merge_key = self.merge_key

    def generate_merge_candidate(self):
        if self.archived:
            return
        if not self.merge_key:
            self.generate_merge_key()
            self.save(merge_key_generated=True)
        if not self.pk or self.merge_key == self.EMPTY_MERGE_KEY:
            return
        q = self.__class__.objects \
            .exclude(pk=self.pk) \
            .exclude(merge_exclusion=self) \
            .exclude(merge_candidate=self) \
            .exclude(archived=True)
        if not self.MERGE_CLEMENCY:
            q = q.filter(merge_key=self.merge_key)
        else:
            subkeys_front = u"-".join(
                self.merge_key.split('-')[:self.MERGE_CLEMENCY])
            subkeys_back = u"-".join(
                self.merge_key.split('-')[-self.MERGE_CLEMENCY:])
            q = q.filter(Q(merge_key__istartswith=subkeys_front) |
                         Q(merge_key__iendswith=subkeys_back))
        for item in q.all():
            self.merge_candidate.add(item)

    def save(self, *args, **kwargs):
        # prevent circular save
        merge_key_generated = False
        if 'merge_key_generated' in kwargs:
            merge_key_generated = kwargs.pop('merge_key_generated')
        self.generate_merge_key()
        item = super(Merge, self).save(*args, **kwargs)
        if not merge_key_generated:
            self.generate_merge_candidate()
        return item

    def archive(self):
        self.archived = True
        self.save()
        for m in self.merge_candidate.all():
            m.delete()
        for m in self.merge_exclusion.all():
            m.delete()

    def merge(self, item):
        merge_model_objects(self, item)
        self.generate_merge_candidate()


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


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

organization_type_pk_lazy = lazy(OrganizationType.get_or_create_pk, unicode)
organization_type_pks_lazy = lazy(OrganizationType.get_or_create_pks, unicode)


class OrganizationManager(models.Manager):
    def get_by_natural_key(self, name, organization_type):
        return self.get(name=name, organization_type__txt_idx=organization_type)


class Organization(Address, Merge, OwnPerms, ValueGetter):
    TABLE_COLS = ('name', 'organization_type', 'town')
    SHOW_URL = 'show-organization'

    # search parameters
    EXTRA_REQUEST_KEYS = {}
    BASE_SEARCH_VECTORS = ['name', 'town']

    # alternative names of fields for searches
    ALT_NAMES = {
        'name': (
            pgettext_lazy("key for text search", u"name"),
            'name__iexact'
        ),
        'organization_type': (
            pgettext_lazy("key for text search", u"type"),
            'organization_type__label__iexact'
        ),
    }
    for v in ALT_NAMES.values():
        for language_code, language_lbl in settings.LANGUAGES:
            activate(language_code)
            EXTRA_REQUEST_KEYS[unicode(v[0])] = v[1]
            deactivate()
    objects = OrganizationManager()

    # fields
    name = models.CharField(_(u"Name"), max_length=500)
    organization_type = models.ForeignKey(OrganizationType,
                                          verbose_name=_(u"Type"))
    cached_label = models.TextField(_(u"Cached name"), null=True, blank=True,
                                    db_index=True)
    history = HistoricalRecords()

    class Meta:
        verbose_name = _(u"Organization")
        verbose_name_plural = _(u"Organizations")
        permissions = (
            ("view_organization", u"Can view all Organizations"),
            ("view_own_organization", u"Can view own Organization"),
            ("add_own_organization", u"Can add own Organization"),
            ("change_own_organization", u"Can change own Organization"),
            ("delete_own_organization", u"Can delete own Organization"),
        )

    def simple_lbl(self):
        if self.name:
            return self.name
        return u"{} - {}".format(self.organization_type,
                                 self.town or "")

    def natural_key(self):
        return (self.name, self.organization_type.txt_idx)

    def __unicode__(self):
        if self.cached_label:
            return self.cached_label
        self.save()
        return self.cached_label

    def _generate_cached_label(self):
        if self.name:
            return self.name
        attrs = ["organization_type", "address", "town"]
        items = [unicode(getattr(self, attr))
                 for attr in attrs if getattr(self, attr)]
        if not items:
            items = [unicode(_(u"unknown organization"))]
        return u" - ".join(items)

    def generate_merge_key(self):
        self.merge_key = slugify(self.name if self.name else '')
        if not self.merge_key:
            self.merge_key = self.EMPTY_MERGE_KEY
        if self.town:
            self.merge_key += "-" + slugify(self.town or '')
        if self.address:
            self.merge_key += "-" + slugify(self.address or '')

    @property
    def associated_filename(self):
        values = [unicode(getattr(self, attr))
                  for attr in ('organization_type', 'name')
                  if getattr(self, attr)]
        return slugify(u"-".join(values))


post_save.connect(cached_label_changed, sender=Organization)


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


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

person_type_pk_lazy = lazy(PersonType.get_or_create_pk, unicode)
person_type_pks_lazy = lazy(PersonType.get_or_create_pks, unicode)


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


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


class PersonManager(models.Manager):
    def get_by_natural_key(
            self, name, surname, attached_to_name,
            attached_to_organization_type):
        q = {"name": name, "surname": surname}
        if attached_to_name:
            q['attached_to__name'] = attached_to_name
        if attached_to_organization_type:
            q['attached_to__organization_type__txt_idx'] = \
                attached_to_organization_type
        return self.get(**q)


class Person(Address, Merge, OwnPerms, ValueGetter):
    _prefix = 'person_'
    TYPE = (
        ('Mr', _(u'Mr')),
        ('Ms', _(u'Miss')),
        ('Mr and Miss', _(u'Mr and Mrs')),
        ('Md', _(u'Mrs')),
        ('Dr', _(u'Doctor')),
    )
    TABLE_COLS = ('name', 'surname', 'raw_name', 'email', 'person_types_list',
                  'attached_to', 'town')
    TABLE_COLS_ACCOUNT = ('name', 'surname', 'raw_name', 'email',
                          'profiles_list', 'attached_to', 'town')
    SHOW_URL = 'show-person'
    MODIFY_URL = 'person_modify'
    BASE_SEARCH_VECTORS = ['name', 'surname', 'raw_name', 'town',
                           'attached_to__name', 'email']

    # search parameters
    REVERSED_BOOL_FIELDS = ['ishtaruser__isnull']
    EXTRA_REQUEST_KEYS = {
        'ishtaruser__isnull': 'ishtaruser__isnull',
        'attached_to': 'attached_to',
    }
    COL_LABELS = {
        'attached_to': _(u"Organization")
    }

    # alternative names of fields for searches
    ALT_NAMES = {
        'name': (
            pgettext_lazy("key for text search", u"name"),
            'name__iexact'
        ),
        'surname': (
            pgettext_lazy("key for text search", u"surname"),
            'surname__iexact'
        ),
        'email': (
            pgettext_lazy("key for text search", u"email"),
            'email__iexact'
        ),
        'person_types': (
            pgettext_lazy("key for text search", u"type"),
            'person_types__label__iexact'
        ),
        'attached_to': (
            pgettext_lazy("key for text search", u"organization"),
            'attached_to__cached_label__iexact'
        ),
        'ishtaruser__isnull': (
            pgettext_lazy("key for text search", u"has-account"),
            'ishtaruser__isnull'
        ),
    }
    for v in ALT_NAMES.values():
        for language_code, language_lbl in settings.LANGUAGES:
            activate(language_code)
            EXTRA_REQUEST_KEYS[unicode(v[0])] = v[1]
            deactivate()
    objects = PersonManager()

    # fields
    old_title = models.CharField(_(u"Title"), max_length=100, choices=TYPE,
                                 blank=True, null=True)
    title = models.ForeignKey(TitleType, verbose_name=_(u"Title"),
                              blank=True, null=True)
    salutation = models.CharField(_(u"Salutation"), max_length=200,
                                  blank=True, null=True)
    surname = models.CharField(_(u"Surname"), max_length=50, blank=True,
                               null=True)
    name = models.CharField(_(u"Name"), max_length=200, blank=True,
                            null=True)
    raw_name = models.CharField(_(u"Raw name"), max_length=300, blank=True,
                                null=True)
    contact_type = models.CharField(_(u"Contact type"), max_length=300,
                                    blank=True, null=True)
    comment = models.TextField(_(u"Comment"), blank=True, null=True)
    person_types = models.ManyToManyField(PersonType, verbose_name=_(u"Types"))
    attached_to = models.ForeignKey(
        'Organization', related_name='members', on_delete=models.SET_NULL,
        verbose_name=_(u"Is attached to"), blank=True, null=True)
    cached_label = models.TextField(_(u"Cached name"), null=True, blank=True,
                                    db_index=True)
    history = HistoricalRecords()

    class Meta:
        verbose_name = _(u"Person")
        verbose_name_plural = _(u"Persons")
        permissions = (
            ("view_person", u"Can view all Persons"),
            ("view_own_person", u"Can view own Person"),
            ("add_own_person", u"Can add own Person"),
            ("change_own_person", u"Can change own Person"),
            ("delete_own_person", u"Can delete own Person"),
        )

    def natural_key(self):
        if not self.attached_to:
            return (self.name, self.surname, '', '')
        return (self.name, self.surname, self.attached_to.name,
                self.attached_to.organization_type.txt_idx)

    @property
    def full_title(self):
        return u" ".join(
            [unicode(getattr(self, attr))
             for attr in ['title', 'salutation'] if getattr(self, attr)])

    @property
    def current_profile(self):
        q = self.profiles.filter(current=True)
        if q.count():
            return q.all()[0]
        q = self.profiles
        nb = q.count()
        if not nb:
            return
        # arbitrary set the first one as the current
        profile = q.all()[0]
        profile.current = True
        profile.save()
        return profile

    def simple_lbl(self):
        values = [unicode(getattr(self, attr)) for attr in ('surname', 'name')
                  if getattr(self, attr)]
        if not values and self.raw_name:
            values = [self.raw_name]
        return u" ".join(values)

    def __unicode__(self):
        if self.cached_label:
            return self.cached_label
        self.save()
        return self.cached_label

    def _generate_cached_label(self):
        lbl = get_external_id('person_raw_name', self)
        if not lbl:
            return u"-"
        if self.attached_to:
            attached_to = unicode(self.attached_to)
            lbl += u" ({})".format(attached_to)
        return lbl

    def fancy_str(self):
        values = ["<strong>"]
        values += [unicode(getattr(self, attr)) for attr in ('surname', 'name')
                   if getattr(self, attr)]
        if not values and self.raw_name:
            values += [self.raw_name]
        values += ["</strong>"]

        if self.attached_to:
            attached_to = unicode(self.attached_to)
            if values:
                values.append(u'-')
            values.append(attached_to)
        return u" ".join(values)

    def get_values(self, prefix=''):
        values = super(Person, self).get_values(prefix=prefix)
        if not self.attached_to:
            values.update(
                Person.get_empty_values(prefix=prefix + 'attached_to_'))
        return values

    person_types_list_lbl = _(u"Types")

    @property
    def person_types_list(self):
        return u", ".join([unicode(pt) for pt in self.person_types.all()])

    profiles_list_lbl = _(u"Profiles")

    @property
    def profiles_list(self):
        return u", ".join([unicode(p) for p in self.profiles.all()])

    def generate_merge_key(self):
        if self.name and self.name.strip():
            self.merge_key = slugify(self.name.strip()) + \
                             ((u'-' + slugify(self.surname.strip()))
                              if self.surname else u'')
        elif self.raw_name and self.raw_name.strip():
            self.merge_key = slugify(self.raw_name.strip())
        elif self.attached_to:
            self.merge_key = self.attached_to.merge_key
        else:
            self.merge_key = self.EMPTY_MERGE_KEY
        if self.merge_key != self.EMPTY_MERGE_KEY and self.attached_to:
            self.merge_key += "-" + self.attached_to.merge_key

    def is_natural(self):
        return not self.attached_to

    def has_right(self, right_name, session=None, obj=None):
        if '.' in right_name:
            right_name = right_name.split('.')[-1]
        res, cache_key = "", ""
        if session:
            cache_key = 'session-{}-{}'.format(session.session_key, right_name)
            res = cache.get(cache_key)
            if res in (True, False):
                return res
            # list all cache key in order to clean them on profile change
            cache_key_list = 'sessionlist-{}'.format(session.session_key)
            key_list = cache.get(cache_key_list, [])
            key_list.append(cache_key)
            cache.set(cache_key_list, key_list, settings.CACHE_TIMEOUT)
        if type(right_name) in (list, tuple):
            res = bool(self.profiles.filter(
                current=True,
                profile_type__txt_idx__in=right_name).count()) or \
                  bool(self.profiles.filter(
                      current=True,
                      profile_type__groups__permissions__codename__in=right_name
                  ).count()) or \
                  bool(self.ishtaruser.user_ptr.groups.filter(
                      permissions__codename__in=right_name
                  ).count()) or \
                  bool(self.ishtaruser.user_ptr.user_permissions.filter(
                      codename__in=right_name).count())
        else:
            res = bool(
                self.profiles.filter(
                    current=True,
                    profile_type__txt_idx=right_name).count()) or \
                  bool(self.profiles.filter(
                      current=True,
                      profile_type__groups__permissions__codename=right_name
                  ).count()) or \
                  bool(self.ishtaruser.user_ptr.groups.filter(
                      permissions__codename__in=[right_name]
                  ).count()) or \
                  bool(self.ishtaruser.user_ptr.user_permissions.filter(
                      codename__in=[right_name]).count())
        if session:
            cache.set(cache_key, res, settings.CACHE_TIMEOUT)
        return res

    def full_label(self):
        values = []
        if self.title:
            values = [self.title.label]
        values += [unicode(getattr(self, attr))
                   for attr in ('salutation', 'surname', 'name')
                   if getattr(self, attr)]
        if not values and self.raw_name:
            values = [self.raw_name]
        if self.attached_to:
            values.append(u"- " + unicode(self.attached_to))
        return u" ".join(values)

    @property
    def associated_filename(self):
        values = [unicode(getattr(self, attr))
                  for attr in ('surname', 'name', 'attached_to')
                  if getattr(self, attr)]
        return slugify(u"-".join(values))

    def docs_q(self):
        return Document.objects.filter(authors__person=self)

    def operation_docs_q(self):
        return Document.objects.filter(
            authors__person=self, operations__pk__isnull=False)

    def contextrecord_docs_q(self):
        return Document.objects.filter(
            authors__person=self, context_records__pk__isnull=False)

    def find_docs_q(self):
        return Document.objects.filter(
            authors__person=self, finds__pk__isnull=False)

    def save(self, *args, **kwargs):
        super(Person, self).save(*args, **kwargs)
        raw_name = get_external_id('person_raw_name', self)
        if raw_name and self.raw_name != raw_name:
            self.raw_name = raw_name
            self.save()
        if hasattr(self, 'responsible_town_planning_service'):
            for fle in self.responsible_town_planning_service.all():
                fle.save()  # force update of raw_town_planning_service
        if hasattr(self, 'general_contractor'):
            for fle in self.general_contractor.all():
                fle.save()  # force update of raw_general_contractor

    @classmethod
    def get_query_owns(cls, ishtaruser):
        return \
            Q(operation_scientist_responsability__collaborators__ishtaruser
              =ishtaruser) | \
            Q(operation_scientist_responsability__scientist__ishtaruser
              =ishtaruser) | \
            Q(operation_collaborator__collaborators__ishtaruser
              =ishtaruser) | \
            Q(operation_collaborator__scientist__ishtaruser=ishtaruser)


post_save.connect(cached_label_changed, sender=Person)


class ProfileType(GeneralType):
    groups = models.ManyToManyField(Group, verbose_name=_(u"Groups"),
                                    blank=True)

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


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


class ProfileTypeSummary(ProfileType):
    class Meta:
        proxy = True
        verbose_name = _(u"Profile type summary")
        verbose_name_plural = _(u"Profile types summary")


class UserProfile(models.Model):
    name = models.CharField(_(u"Name"), blank=True, default=u"", max_length=100)
    profile_type = models.ForeignKey(
        ProfileType, verbose_name=_(u"Profile type"))
    areas = models.ManyToManyField("Area", verbose_name=_(u"Areas"),
                                   blank=True, related_name='profiles')
    current = models.BooleanField(_(u"Current profile"), default=False)
    show_field_number = models.BooleanField(
        _(u"Show field number"), default=False)
    auto_pin = models.BooleanField(_(u"Automatically pin"), default=False)
    display_pin_menu = models.BooleanField(_(u"Display pin menu"),
                                           default=False)
    person = models.ForeignKey(
        Person, verbose_name=_(u"Person"), related_name='profiles')

    class Meta:
        verbose_name = _(u"User profile")
        verbose_name_plural = _(u"User profiles")
        unique_together = (('name', 'profile_type', 'person'),)

    def __unicode__(self):
        lbl = self.name or unicode(self.profile_type)
        if not self.areas.count():
            return lbl
        return u"{} ({})".format(lbl, u", ".join(
            [unicode(area) for area in self.areas.all()]))

    @property
    def query_towns(self):
        return Town.objects.filter(
            Q(areas__profiles=self) | Q(areas__parent__profiles=self) |
            Q(areas__parent__parent__profiles=self) |
            Q(areas__parent__parent__parent__profiles=self) |
            Q(areas__parent__parent__parent__parent__profiles=self)
        )

    @property
    def area_labels(self):
        return u", ".join([unicode(area) for area in self.areas.all()])

    def duplicate(self, **kwargs):
        areas = [area for area in self.areas.all()]
        new_item = self
        new_item.pk = None
        name = self.name
        for key in kwargs:
            if key == 'name':
                name = kwargs[key]
            setattr(new_item, key, kwargs[key])
        while UserProfile.objects.filter(
                name=name, profile_type=self.profile_type,
                person=self.person).count():
            name += unicode(_(u" - duplicate"))
        new_item.name = name
        new_item.save()
        for area in areas:
            new_item.areas.add(area)
        return new_item

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        super(UserProfile, self).save(
            force_insert=force_insert, force_update=force_update, using=using,
            update_fields=update_fields)

        # only one current profile per user
        if not self.current:
            return
        q = UserProfile.objects.filter(
            person=self.person, current=True).exclude(pk=self.pk)
        if not q.count():
            return
        for p in q.all():
            p.current = False
            p.save()


def post_save_userprofile(sender, **kwargs):
    if not kwargs.get('instance'):
        return
    instance = kwargs.get('instance')
    instance.person.ishtaruser.show_field_number(update=True)


post_save.connect(post_save_userprofile, sender=UserProfile)


class IshtarUser(FullSearch):
    SLUG = "ishtaruser"
    TABLE_COLS = ('username', 'person__name', 'person__surname',
                  'person__email', 'person__person_types_list',
                  'person__attached_to__name')
    BASE_SEARCH_VECTORS = [
        'user_ptr__username', 'person__name', 'person__surname',
        'person__email', 'person__town', 'person__attached_to__name']

    # search parameters
    EXTRA_REQUEST_KEYS = {
        'person__person_types_list': 'person__person_types__label'
    }

    COL_LABELS = {
        'person__attached_to__name': _(u"Organization"),
        'username': _(u"Username")
    }

    # alternative names of fields for searches
    ALT_NAMES = {
        'username': (
            pgettext_lazy("key for text search", u"username"),
            'user_ptr__username__iexact'
        ),
        'name': (
            pgettext_lazy("key for text search", u"name"),
            'person__name__iexact'
        ),
        'surname': (
            pgettext_lazy("key for text search", u"surname"),
            'person__surname__iexact'
        ),
        'email': (
            pgettext_lazy("key for text search", u"email"),
            'person__email__iexact'
        ),
        'person_types': (
            pgettext_lazy("key for text search", u"type"),
            'person__person_types__label__iexact'
        ),
        'attached_to': (
            pgettext_lazy("key for text search", u"organization"),
            'person__attached_to__cached_label__iexact'
        ),
    }
    for v in ALT_NAMES.values():
        for language_code, language_lbl in settings.LANGUAGES:
            activate(language_code)
            EXTRA_REQUEST_KEYS[unicode(v[0])] = v[1]
            deactivate()

    # fields
    user_ptr = models.OneToOneField(User, primary_key=True,
                                    related_name='ishtaruser')
    person = models.OneToOneField(Person, verbose_name=_(u"Person"),
                                  related_name='ishtaruser')
    advanced_shortcut_menu = models.BooleanField(
        _(u"Advanced shortcut menu"), default=False)

    class Meta:
        verbose_name = _(u"Ishtar user")
        verbose_name_plural = _(u"Ishtar users")

    def __unicode__(self):
        return unicode(self.person)

    def show_field_number(self, update=False):
        cache_key, value = get_cache(self.__class__, ['show_field_number'])
        if not update and value is not None:
            return value
        value = False
        if self.current_profile:
            value = self.current_profile.show_field_number
        cache.set(cache_key, value, settings.CACHE_TIMEOUT)
        return value

    @property
    def current_profile_name(self):
        q = UserProfile.objects.filter(current=True, person__ishtaruser=self)
        if q.count():
            vals = q.values('profile_type__label', 'name').all()[0]
            return vals['name'] or vals['profile_type__label']
        profile = self.person.current_profile
        if not profile:
            return u""
        return unicode(profile)

    @property
    def current_profile(self):
        return self.person.current_profile

    @classmethod
    def set_superuser(cls, user):
        q = cls.objects.filter(user_ptr=user)
        if not q.count():
            return
        ishtaruser = q.all()[0]
        person = ishtaruser.person
        admin, created = ProfileType.objects.get_or_create(
            txt_idx='administrator')
        if user.is_superuser:
            if UserProfile.objects.filter(
                    profile_type=admin, person=person).count():
                return
            UserProfile.objects.get_or_create(
                profile_type=admin, person=person,
                defaults={'current': True})
        else:
            q = UserProfile.objects.filter(
                profile_type=admin, person=person)
            if q.count():
                for up in q.all():
                    up.delete()

    @classmethod
    def create_from_user(cls, user):
        default = user.username
        surname = user.first_name or default
        name = user.last_name or default
        email = user.email
        person = Person.objects.create(surname=surname,
                                       name=name, email=email,
                                       history_modifier=user)
        isht_user = cls.objects.create(user_ptr=user, person=person)
        return isht_user

    def has_right(self, right_name, session=None):
        return self.person.has_right(right_name, session=session)

    def has_perm(self, perm, model=None, session=None, obj=None):
        return self.person.has_right(perm, session=session, obj=None)

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


class Basket(FullSearch, OwnPerms):
    """
    Abstract class for a basket
    Subclass must be defined with an "items" ManyToManyField
    """
    IS_BASKET = True
    label = models.CharField(_(u"Label"), max_length=1000)
    comment = models.TextField(_(u"Comment"), blank=True, null=True)
    user = models.ForeignKey(
        IshtarUser, blank=True, null=True, related_name='%(class)ss',
        verbose_name=_(u"Owner"))
    available = models.BooleanField(_(u"Available"), default=True)
    shared_with = models.ManyToManyField(
        IshtarUser, verbose_name=_(u"Shared (read) with"), blank=True,
        related_name='shared_%(class)ss'
    )
    shared_write_with = models.ManyToManyField(
        IshtarUser, verbose_name=_(u"Shared (read/edit) with"), blank=True,
        related_name='shared_write_%(class)ss'
    )

    TABLE_COLS = ['label', 'user']

    BASE_SEARCH_VECTORS = ['label', 'comment']
    M2M_SEARCH_VECTORS = ['items']

    class Meta:
        abstract = True
        unique_together = (('label', 'user'),)

    def __unicode__(self):
        return self.label

    @classmethod
    def BASE_REQUEST(cls, request):
        if not request.user or not getattr(request.user, 'ishtaruser', None):
            return Q(pk=None)
        ishtaruser = request.user.ishtaruser
        return Q(user=ishtaruser) | Q(shared_with=ishtaruser) | Q(
            shared_write_with=ishtaruser)

    @property
    def cached_label(self):
        return unicode(self)

    @property
    def full_label(self):
        return u"{} - {}".format(self.label, self.user)

    @classmethod
    def get_short_menu_class(cls, pk):
        return 'basket'

    @property
    def associated_filename(self):
        return "{}-{}".format(datetime.date.today().strftime(
            "%Y-%m-%d"), slugify(self.label))

    @classmethod
    def get_query_owns(cls, ishtaruser):
        return Q(user=ishtaruser) | Q(shared_with=ishtaruser) | Q(
            shared_write_with=ishtaruser)

    @classmethod
    def get_write_query_owns(cls, ishtaruser):
        return Q(user=ishtaruser)

    def duplicate(self, label=None, ishtaruser=None):
        """
        Duplicate the basket. Items in basket are copied but not shared users
        :param label: if provided use the name
        :param ishtaruser: if provided an alternate user is used
        :return: the new basket
        """
        items = list(self.items.all())
        new_item = self
        new_item.pk = None
        if ishtaruser:
            new_item.user = ishtaruser
        if not label:
            label = new_item.label
        while self.__class__.objects.filter(
                label=label, user=new_item.user).count():
            label += unicode(_(u" - duplicate"))
        new_item.label = label
        new_item.save()
        for item in items:
            new_item.items.add(item)
        return new_item


class AuthorType(GeneralType):
    order = models.IntegerField(_(u"Order"), default=1)

    class Meta:
        verbose_name = _(u"Author type")
        verbose_name_plural = _(u"Author types")
        ordering = ['order', 'label']


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


class AuthorManager(models.Manager):
    def get_by_natural_key(
            self, name, surname, attached_to_name,
            attached_to_organization_type, author_type):
        q = {"person__name": name, "person__surname": surname,
             "author_type__txt_idx": author_type}
        if attached_to_name:
            q['person__attached_to__name'] = attached_to_name
        if attached_to_organization_type:
            q['person__attached_to__organization_type__txt_idx'] = \
                attached_to_organization_type
        return self.get(**q)


class Author(FullSearch):
    PARENT_SEARCH_VECTORS = ['person']
    SLUG = "author"

    person = models.ForeignKey(Person, verbose_name=_(u"Person"),
                               related_name='author')
    author_type = models.ForeignKey(AuthorType, verbose_name=_(u"Author type"))
    cached_label = models.TextField(_(u"Cached name"), null=True, blank=True,
                                    db_index=True)
    objects = AuthorManager()

    class Meta:
        verbose_name = _(u"Author")
        verbose_name_plural = _(u"Authors")
        ordering = ('author_type__order', 'person__name')
        permissions = (
            ("view_author", u"Can view all Authors"),
            ("view_own_author", u"Can view own Author"),
            ("add_own_author", u"Can add own Author"),
            ("change_own_author", u"Can change own Author"),
            ("delete_own_author", u"Can delete own Author"),
        )

    def __unicode__(self):
        if self.cached_label:
            return self.cached_label
        self.save()
        return self.cached_label

    def natural_key(self):
        return self.person.natural_key() + (self.author_type.txt_idx,)

    def _generate_cached_label(self):
        return unicode(self.person) + settings.JOINT + \
               unicode(self.author_type)

    def fancy_str(self):
        return self.person.fancy_str() + settings.JOINT + \
               unicode(self.author_type)

    def related_sources(self):
        return list(self.treatmentsource_related.all()) + \
               list(self.operationsource_related.all()) + \
               list(self.findsource_related.all()) + \
               list(self.contextrecordsource_related.all())


post_save.connect(cached_label_changed, sender=Author)


class SourceType(HierarchicalType):
    class Meta:
        verbose_name = _(u"Source type")
        verbose_name_plural = _(u"Source types")
        ordering = ['label']


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


class SupportType(GeneralType):
    class Meta:
        verbose_name = _(u"Support type")
        verbose_name_plural = _(u"Support types")


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


class Format(GeneralType):
    class Meta:
        verbose_name = _(u"Format type")
        verbose_name_plural = _(u"Format types")
        ordering = ['label']


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


class LicenseType(GeneralType):
    url = models.URLField(_(u"URL"), blank=True, null=True)

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


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


class Document(OwnPerms, ImageModel, FullSearch, Imported):
    # order is important: put the image in the first match found
    # other will be symbolic links
    RELATED_MODELS = [
        'treatment_files', 'treatments', 'finds', 'context_records',
        'operations', 'sites', 'warehouses', 'files'
    ]
    # same fields but in order for forms
    RELATED_MODELS_ALT = [
        'finds', 'context_records', 'operations', 'sites', 'files',
        'warehouses', 'treatments', 'treatment_files',
    ]
    SLUG = 'document'
    LINK_SPLIT = u"<||>"

    TABLE_COLS = ['title', 'source_type', 'cache_related_label',
                  'authors__cached_label',
                  'associated_url']
    COL_LINK = ['associated_url']
    BASE_SEARCH_VECTORS = ['title', 'source_type__label', 'external_id',
                           'reference', 'description', 'comment',
                           'additional_information']
    PARENT_SEARCH_VECTORS = ['authors', ]

    BOOL_FIELDS = ['duplicate']

    COL_LABELS = {"authors__cached_label": _(u"Authors")}

    CACHED_LABELS = ['cache_related_label']
    EXTRA_REQUEST_KEYS = {
        "operations": "operations__pk",
        "context_records": "context_records__pk",
        "context_records__operation": "context_records__operation__pk",
        "finds": "finds__pk",
        "finds__base_finds__context_record":
            "finds__base_finds__context_record__pk",
        "finds__base_finds__context_record__operation":
            "finds__base_finds__context_record__operation__pk",
        'authors__cached_label': 'authors__cached_label',
        'authors__person__pk': 'authors__person__pk',
    }

    # alternative names of fields for searches
    ALT_NAMES = {
        'authors': (
            pgettext_lazy("key for text search", u"author"),
            'authors__cached_label__iexact'
        ),
        'title': (
            pgettext_lazy("key for text search", u"title"),
            'title__iexact'
        ),
        'source_type': (
            pgettext_lazy("key for text search", u"type"),
            'source_type__label__iexact'
        ),
        'reference': (
            pgettext_lazy("key for text search", u"reference"),
            'reference__iexact'
        ),
        'internal_reference': (
            pgettext_lazy("key for text search", u"internal-reference"),
            'internal_reference__iexact'
        ),
        'description': (
            pgettext_lazy("key for text search", u"description"),
            'description__iexact'
        ),
        'comment': (
            pgettext_lazy("key for text search", u"comment"),
            'comment__iexact'
        ),
        'additional_information': (
            pgettext_lazy("key for text search", u"additional-information"),
            'additional_information__iexact'
        ),
        'duplicate': (
            pgettext_lazy("key for text search", u"has-duplicate"),
            'duplicate'
        ),
        'operation': (
            pgettext_lazy("key for text search", u"operation"),
            'operations__cached_label__iexact'
        ),
        'context_record': (
            pgettext_lazy("key for text search", u"context-record"),
            'context_records__cached_label__iexact'
        ),
        'find': (
            pgettext_lazy("key for text search", u"find"),
            'finds__cached_label__iexact'
        ),
        'file': (
            pgettext_lazy("key for text search", u"file"),
            'files__cached_label__iexact'
        ),
        'site': (
            pgettext_lazy("key for text search", u"site"),
            'sites__cached_label__iexact'
        ),
        'warehouse': (
            pgettext_lazy("key for text search", u"warehouse"),
            'warehouses__name__iexact'
        ),
    }
    for v in ALT_NAMES.values():
        for language_code, language_lbl in settings.LANGUAGES:
            activate(language_code)
            EXTRA_REQUEST_KEYS[unicode(v[0])] = v[1]
            deactivate()
    objects = ExternalIdManager()

    RELATED_MODELS_ALT = [
        'finds', 'context_records', 'operations', 'sites', 'files',
        'warehouses', 'treatments', 'treatment_files',
    ]
    RELATIVE_SESSION_NAMES = [
        ('find', 'finds__pk'),
        ('contextrecord', 'context_records__pk'),
        ('operation', 'operations__pk'),
        ('site', 'sites__pk'),
        ('file', 'files__pk'),
        ('warehouse', 'warehouses__pk'),
        ('treatment', 'treatments__pk'),
        ('treatmentfile', 'treatment_files__pk'),
    ]

    UP_MODEL_QUERY = {
        "operation": (pgettext_lazy("key for text search", u"operation"),
                      'cached_label'),
        "contextrecord": (pgettext_lazy("key for text search",
                                        u"context-record"), 'cached_label'),
        "file": (pgettext_lazy("key for text search", u"file"), 'cached_label'),
        "find": (pgettext_lazy("key for text search", u"find"), 'cached_label'),
        "site": (pgettext_lazy("key for text search", u"site"), 'cached_label'),
        "warehouse": (pgettext_lazy("key for text search", u"warehouse"),
                      'cached_label'),
        "treatment": (pgettext_lazy("key for text search", u"treatment"),
                      'cached_label'),
        "treatmentfile": (pgettext_lazy("key for text search",
                                        u"treatment-file"), 'cached_label'),
    }

    title = models.TextField(_(u"Title"), blank=True, default='')
    associated_file = models.FileField(
        upload_to=get_image_path, blank=True, null=True, max_length=255)
    index = models.IntegerField(verbose_name=_(u"Index"), blank=True,
                                null=True)
    external_id = models.TextField(_(u"External ID"), null=True, blank=True)
    reference = models.TextField(_(u"Ref."), null=True, blank=True)
    internal_reference = models.TextField(_(u"Internal ref."), null=True,
                                          blank=True)
    source_type = models.ForeignKey(SourceType, verbose_name=_(u"Type"),
                                    null=True, blank=True)
    licenses = models.ManyToManyField(LicenseType, verbose_name=_(u"License"),
                                      blank=True)
    support_type = models.ForeignKey(SupportType, verbose_name=_(u"Support"),
                                     blank=True, null=True, )
    format_type = models.ForeignKey(Format, verbose_name=_(u"Format"),
                                    blank=True, null=True, )
    scale = models.CharField(_(u"Scale"), max_length=30, null=True,
                             blank=True)
    authors = models.ManyToManyField(Author, verbose_name=_(u"Authors"),
                                     related_name="documents")
    authors_raw = models.CharField(verbose_name=_(u"Authors (raw)"),
                                   blank=True, null=True, max_length=250)
    associated_url = models.URLField(
        blank=True, null=True, max_length=1000,
        verbose_name=_(u"Numerical ressource (web address)"))
    receipt_date = models.DateField(blank=True, null=True,
                                    verbose_name=_(u"Receipt date"))
    creation_date = models.DateField(blank=True, null=True,
                                     verbose_name=_(u"Creation date"))
    receipt_date_in_documentation = models.DateField(
        blank=True, null=True,
        verbose_name=_(u"Receipt date in documentation"))
    item_number = models.IntegerField(_(u"Number of items"), default=1)
    description = models.TextField(_(u"Description"), blank=True, null=True)
    comment = models.TextField(_(u"Comment"), blank=True, null=True)
    additional_information = models.TextField(_(u"Additional information"),
                                              blank=True, null=True)
    duplicate = models.NullBooleanField(_(u"Has a duplicate"), blank=True,
                                        null=True)
    associated_links = models.TextField(_(u"Symbolic links"), blank=True,
                                        null=True)
    cache_related_label = models.TextField(
        _(u"Related"), blank=True, null=True, db_index=True,
        help_text=_(u"Cached value - do not edit"))

    class Meta:
        verbose_name = _(u"Document")
        verbose_name_plural = _(u"Documents")
        ordering = ('title',)
        permissions = (
            ("view_document",
             ugettext(u"Can view all Documents")),
            ("view_own_document",
             ugettext(u"Can view own Document")),
            ("add_own_document",
             ugettext(u"Can add own Document")),
            ("change_own_document",
             ugettext(u"Can change own Document")),
            ("delete_own_document",
             ugettext(u"Can delete own Document")),
        )

    def __unicode__(self):
        return self.title

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

    """
    @property
    def code(self):
        if not self.index:
            return u"{}-".format(self.operation.code_patriarche or '')
        return u"{}-{:04d}".format(self.operation.code_patriarche or '',
                                   self.index)
    """

    @property
    def associated_file_name(self):
        if not self.associated_file:
            return ""
        return os.path.basename(self.associated_file.name)

    @property
    def images(self):
        # mimic a queryset pointing to himself
        return Document.objects.filter(
            pk=self.pk, image__isnull=False).exclude(image='')

    @property
    def has_related(self):
        for rel in self.RELATED_MODELS:
            if getattr(self, rel).count():
                return True
        return False

    @classmethod
    def get_query_owns(cls, ishtaruser):
        Operation = cls.operations.rel.related_model
        ArchaeologicalSite = cls.sites.rel.related_model
        q = cls._construct_query_own(
            'operations__', Operation._get_query_owns_dicts(ishtaruser)
        ) | cls._construct_query_own(
            'sites__', ArchaeologicalSite._get_query_owns_dicts(ishtaruser)
        )
        return q

    def get_associated_operation(self):
        raise NotImplementedError()

    @property
    def associated_filename(self):
        values = [unicode(getattr(self, attr))
                  for attr in ('source_type', 'title')
                  if getattr(self, attr)]
        return slugify(u"-".join(values))

    def _get_base_image_paths(self):
        if self.pk:  # m2m not available if not created...
            for related_model in self.RELATED_MODELS:
                q = getattr(self, related_model).all()
                if q.count():
                    item = q.all()[0]
                    yield item._get_base_image_path()

    def _get_base_image_path(self):
        for path in self._get_base_image_paths():
            return path
        return u"upload"

    def _get_available_filename(self, path, test_link=None):
        """
        Get a filename not used
        If name already used - generate a name with schema:
        [base]-[current_number + 1].[suffix]

        :param path: base path
        :param test_link: test if an existing path match with this link
        :return: if test_link is not None, (new_path, link_match) otherwise
        the new_path
        """

        file_split = path.split('.')
        suffix, base = "", ""
        if len(file_split) > 1:
            base = u".".join(file_split[0:-1])
            suffix = file_split[-1]
        else:
            base = path
        base_split = base.split('-')
        current_nb = 0
        if len(base_split) > 1:
            try:
                current_nb = int(base_split[-1])
                base = u"-".join(base_split[0:-1]) + u"-"
            except ValueError:
                pass

        while os.path.exists(path):
            if test_link and os.path.islink(path) \
                    and os.readlink(path) == test_link:
                return path, True
            current_nb += 1
            path = u"{}-{}.{}".format(base, current_nb, suffix)
        if test_link:
            return path, False
        return path

    def _move_image(self):
        """
        Move to the relevant path and create appropriate symbolic links

        :return: list of associated links
        """
        reference_path = None
        initial_path = self.image.path
        filename = os.path.basename(initial_path)
        links = []

        for related_model in self.RELATED_MODELS:
            q = getattr(self, related_model).all()
            if q.count():
                item = q.all()[0]
                base_path = item._get_base_image_path()
                new_path = base_path + u"/" + filename
                if not reference_path:
                    reference_path = settings.MEDIA_ROOT + new_path
                    # correct path
                    if initial_path == reference_path:
                        continue
                    if not os.path.exists(os.path.dirname(reference_path)):
                        os.makedirs(os.path.dirname(reference_path))

                    reference_path = self._get_available_filename(
                        reference_path)
                    try:
                        os.rename(initial_path, reference_path)
                        os.rename(self.thumbnail.path,
                                  self._get_thumb_name(reference_path))
                        self.image.name = reference_path[
                                          len(settings.MEDIA_ROOT):]
                        self.save(no_path_change=True)
                    except OSError:
                        # file probably not on harddrive - will be cleaned
                        pass
                    continue
                # create a link
                new_path = settings.MEDIA_ROOT + new_path
                if not os.path.exists(os.path.dirname(new_path)):
                    os.makedirs(os.path.dirname(new_path))
                new_path, match = self._get_available_filename(
                    new_path, test_link=reference_path)
                links.append(new_path)
                if match:  # the current link is correct
                    continue
                os.symlink(reference_path, new_path)
        return links

    def related_label(self):
        items = []
        for rel_attr in reversed(self.RELATED_MODELS):
            for item in getattr(self, rel_attr).all():
                items.append(unicode(item))
        return u" ; ".join(items)

    def _generate_cache_related_label(self):
        return self.related_label()

    def save(self, *args, **kwargs):
        no_path_change = 'no_path_change' in kwargs \
                         and kwargs.pop('no_path_change')
        super(Document, self).save(*args, **kwargs)

        if self.image and not no_path_change and \
                not getattr(self, '_no_path_change', False):
            links = self._move_image()
            links = self.LINK_SPLIT.join(links)
            if links != self.associated_links:
                self.associated_links = links
                self.save(no_path_change=True)


post_save.connect(cached_label_changed, sender=Document)


class Arrondissement(models.Model):
    name = models.CharField(u"Nom", max_length=30)
    department = models.ForeignKey(Department, verbose_name=u"Département")

    def __unicode__(self):
        return settings.JOINT.join((self.name, unicode(self.department)))


class Canton(models.Model):
    name = models.CharField(u"Nom", max_length=30)
    arrondissement = models.ForeignKey(Arrondissement,
                                       verbose_name=u"Arrondissement")

    def __unicode__(self):
        return settings.JOINT.join(
            (self.name, unicode(self.arrondissement)))


class TownManager(models.GeoManager):
    def get_by_natural_key(self, numero_insee, year):
        return self.get(numero_insee=numero_insee, year=year)


class Town(Imported, models.Model):
    name = models.CharField(_(u"Name"), max_length=100)
    surface = models.IntegerField(_(u"Surface (m2)"), blank=True, null=True)
    center = models.PointField(_(u"Localisation"), srid=settings.SRID,
                               blank=True, null=True)
    limit = models.MultiPolygonField(_(u"Limit"), blank=True, null=True)
    numero_insee = models.CharField(u"Code commune (numéro INSEE)",
                                    max_length=120)
    departement = models.ForeignKey(
        Department, verbose_name=_(u"Department"), null=True, blank=True)
    year = models.IntegerField(
        _("Year of creation"), null=True, blank=True,
        help_text=_(u"Filling this field is relevant to distinguish old towns "
                    u"from new towns."))
    children = models.ManyToManyField(
        'Town', verbose_name=_(u"Town children"), blank=True,
        related_name='parents')
    cached_label = models.CharField(_(u"Cached name"), max_length=500,
                                    null=True, blank=True, db_index=True)
    objects = TownManager()

    class Meta:
        verbose_name = _(u"Town")
        verbose_name_plural = _(u"Towns")
        if settings.COUNTRY == 'fr':
            ordering = ['numero_insee']
            unique_together = (('numero_insee', 'year'),)

    def natural_key(self):
        return (self.numero_insee, self.year)

    def __unicode__(self):
        if self.cached_label:
            return self.cached_label
        self.save()
        return self.cached_label

    @property
    def label_with_areas(self):
        label = [self.name]
        if self.numero_insee:
            label.append(u"({})".format(self.numero_insee))
        for area in self.areas.all():
            label.append(u" - ")
            label.append(area.full_label)
        return u" ".join(label)

    def generate_geo(self, force=False):
        force = self.generate_limit(force=force)
        self.generate_center(force=force)
        self.generate_area(force=force)

    def generate_limit(self, force=False):
        if not force and self.limit:
            return
        parents = None
        if not self.parents.count():
            return
        for parent in self.parents.all():
            if not parent.limit:
                return
            if not parents:
                parents = parent.limit
            else:
                parents = parents.union(parent.limit)
        # if union is a simple polygon make it a multi
        if 'MULTI' not in parents.wkt:
            parents = parents.wkt.replace('POLYGON', 'MULTIPOLYGON(') + ")"
        if not parents:
            return
        self.limit = parents
        self.save()
        return True

    def generate_center(self, force=False):
        if not force and (self.center or not self.limit):
            return
        self.center = self.limit.centroid
        if not self.center:
            return False
        self.save()
        return True

    def generate_area(self, force=False):
        if not force and (self.surface or not self.limit):
            return
        self.surface = self.limit.transform(settings.SURFACE_SRID,
                                            clone=True).area
        if not self.surface:
            return False
        self.save()
        return True

    def update_town_code(self):
        if not self.numero_insee or not self.children.count() or not self.year:
            return
        old_num = self.numero_insee[:]
        numero = old_num.split('-')[0]
        self.numero_insee = u"{}-{}".format(numero, self.year)
        if self.numero_insee != old_num:
            return True

    def _generate_cached_label(self):
        cached_label = self.name
        if settings.COUNTRY == "fr" and self.numero_insee:
            dpt_len = 2
            if self.numero_insee.startswith('97') or \
                    self.numero_insee.startswith('98') or \
                    self.numero_insee[0] not in ('0', '1', '2', '3', '4', '5',
                                                 '6', '7', '8', '9'):
                dpt_len = 3
            cached_label = u"%s - %s" % (self.name, self.numero_insee[:dpt_len])
        if self.year and self.children.count():
            cached_label += u" ({})".format(self.year)
        return cached_label


def post_save_town(sender, **kwargs):
    cached_label_changed(sender, **kwargs)
    town = kwargs['instance']
    town.generate_geo()
    if town.update_town_code():
        town.save()


post_save.connect(post_save_town, sender=Town)


def town_child_changed(sender, **kwargs):
    town = kwargs['instance']
    if town.update_town_code():
        town.save()


m2m_changed.connect(town_child_changed, sender=Town.children.through)


class Area(HierarchicalType):
    towns = models.ManyToManyField(Town, verbose_name=_(u"Towns"), blank=True,
                                   related_name='areas')
    reference = models.CharField(_(u"Reference"), max_length=200, blank=True,
                                 null=True)
    parent = models.ForeignKey(
        'self', blank=True, null=True, verbose_name=_(u"Parent"),
        help_text=_(u"Only four level of parent are managed."),
        related_name='children', on_delete=models.SET_NULL
    )

    class Meta:
        verbose_name = _(u"Area")
        verbose_name_plural = _(u"Areas")
        ordering = ('label',)

    def __unicode__(self):
        if not self.reference:
            return self.label
        return u"{} ({})".format(self.label, self.reference)

    @property
    def full_label(self):
        label = [unicode(self)]
        if self.parent:
            label.append(self.parent.full_label)
        return u" / ".join(label)


class OperationType(GeneralType):
    order = models.IntegerField(_(u"Order"), default=1)
    preventive = models.BooleanField(_(u"Is preventive"), default=True)
    judiciary = models.BooleanField(_(u"Is judiciary"), default=False)

    class Meta:
        verbose_name = _(u"Operation type")
        verbose_name_plural = _(u"Operation types")
        ordering = ['judiciary', '-preventive', 'order', 'label']

    @classmethod
    def get_types(cls, dct=None, instances=False, exclude=None,
                  empty_first=True, default=None, initial=None):
        dct = dct or {}
        exclude = exclude or []
        initial = initial or []
        tuples = []
        dct['available'] = True
        if not instances and empty_first and not default:
            tuples.append(('', '--'))
        if default and not instances:
            try:
                default = cls.objects.get(txt_idx=default)
                tuples.append((default.pk, _(unicode(default))))
            except cls.DoesNotExist:
                pass
        items = cls.objects.filter(**dct)
        if default and not instances:
            exclude.append(default.txt_idx)
        if exclude:
            items = items.exclude(txt_idx__in=exclude)
        current_preventive, current_judiciary, current_lst = None, None, None
        item_list = list(items.order_by(*cls._meta.ordering).all())
        new_vals = cls._get_initial_types(initial, [i.pk for i in item_list],
                                          instance=True)
        item_list += new_vals
        for item in item_list:
            item.rank = 0
        if instances:
            return item_list
        for item in item_list:
            if not current_lst or item.preventive != current_preventive \
                    or item.judiciary != current_judiciary:
                if current_lst:
                    tuples.append(current_lst)
                if item.judiciary:
                    gp_lbl = _(u"Judiciary")
                elif item.preventive:
                    gp_lbl = _(u"Preventive")
                else:
                    gp_lbl = _(u"Research")
                current_lst = [gp_lbl, []]
                current_preventive = item.preventive
                current_judiciary = item.judiciary
            current_lst[1].append((item.pk, _(unicode(item))))
        if current_lst:
            tuples.append(current_lst)
        return tuples

    @classmethod
    def is_preventive(cls, ope_type_id, key=''):
        try:
            op_type = cls.objects.get(pk=ope_type_id)
        except cls.DoesNotExist:
            return False
        if not key:
            return op_type.preventive
        return key == op_type.txt_idx

    @classmethod
    def is_judiciary(cls, ope_type_id):
        try:
            op_type = cls.objects.get(pk=ope_type_id)
        except cls.DoesNotExist:
            return False
        return op_type.judiciary


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


class SpatialReferenceSystem(GeneralType):
    order = models.IntegerField(_(u"Order"), default=10)
    auth_name = models.CharField(
        _(u"Authority name"), default=u'EPSG', max_length=256)
    srid = models.IntegerField(_(u"Authority SRID"))

    class Meta:
        verbose_name = _(u"Spatial reference system")
        verbose_name_plural = _(u"Spatial reference systems")
        ordering = ('label',)


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


class AdministrationScript(models.Model):
    path = models.CharField(_(u"Filename"), max_length=30)
    name = models.TextField(_(u"Name"),
                            null=True, blank=True)

    class Meta:
        verbose_name = _(u"Administration script")
        verbose_name_plural = _(u"Administration scripts")
        ordering = ['name']

    def __unicode__(self):
        return unicode(self.name)


SCRIPT_STATE = (("S", _(u"Scheduled")),
                ("P", _(u"In progress")),
                ("FE", _(u"Finished with errors")),
                ("F", _(u"Finished")),
                )

SCRIPT_STATE_DCT = dict(SCRIPT_STATE)


class AdministrationTask(models.Model):
    script = models.ForeignKey(AdministrationScript)
    state = models.CharField(_(u"State"), max_length=2, choices=SCRIPT_STATE,
                             default='S')
    creation_date = models.DateTimeField(default=datetime.datetime.now)
    launch_date = models.DateTimeField(null=True, blank=True)
    finished_date = models.DateTimeField(null=True, blank=True)
    result = models.TextField(_(u"Result"), null=True, blank=True)

    class Meta:
        verbose_name = _(u"Administration task")
        verbose_name_plural = _(u"Administration tasks")
        ordering = ['script']

    def __unicode__(self):
        state = _(u"Unknown")
        if self.state in SCRIPT_STATE_DCT:
            state = unicode(SCRIPT_STATE_DCT[self.state])
        return u"{} - {} - {}".format(self.script, self.creation_date,
                                      state)

    def execute(self):
        if self.state != 'S':
            return
        self.launch_date = datetime.datetime.now()

        script_dir = settings.ISHTAR_SCRIPT_DIR

        if not script_dir:
            self.result = unicode(
                _(u"ISHTAR_SCRIPT_DIR is not set in your "
                  u"local_settings. Contact your administrator."))
            self.state = 'FE'
            self.finished_date = datetime.datetime.now()
            self.save()
            return

        if '..' in script_dir:
            self.result = unicode(
                _(u"Your ISHTAR_SCRIPT_DIR is containing "
                  u"dots \"..\". As it can refer to relative "
                  u"paths, it can be a security issue and this is "
                  u"not allowed. Only put a full path."))
            self.state = 'FE'
            self.finished_date = datetime.datetime.now()
            self.save()
            return

        if not os.path.isdir(script_dir):
            self.result = unicode(
                _(u"Your ISHTAR_SCRIPT_DIR: \"{}\" is not a valid directory.")
            ).format(script_dir)
            self.state = 'FE'
            self.finished_date = datetime.datetime.now()
            self.save()
            return

        script_name = None
        # only script inside the script directory can be executed
        for name in os.listdir(script_dir):
            if name == self.script.path:
                if os.path.isfile(os.path.join(script_dir, name)):
                    script_name = os.path.join(script_dir, name)
                break
        if not script_name:
            self.result = unicode(
                _(u"Script \"{}\" is not available in your script directory. "
                  u"Check your configuration.")
            ).format(self.script.path)
            self.state = 'FE'
            self.finished_date = datetime.datetime.now()
            self.save()
            return
        self.state = 'P'
        self.save()

        self.finished_date = datetime.datetime.now()
        try:
            session = Popen([script_name], stdout=PIPE, stderr=PIPE)
            stdout, stderr = session.communicate()
        except OSError as e:
            self.state = 'FE'
            self.result = u"Error executing \"{}\" script: {}".format(
                self.script.path, e)
            self.save()
            return

        self.finished_date = datetime.datetime.now()
        if stderr:
            self.state = 'FE'
            self.result = u"Error: {}".format(stderr.decode('utf-8'))
        else:
            self.state = 'F'
            self.result = u"{}".format(stdout.decode('utf-8'))
        self.save()