summaryrefslogtreecommitdiff
path: root/ishtar_common/views.py
blob: a45d4ad081c3e6bdd56f4a85a665d02010ee8c4d (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
#!/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.

import csv
import datetime
import importlib
import json
import logging
import os
import unicodedata

from django.apps import apps
from django.conf import settings
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import redirect_to_login
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse, NoReverseMatch
from django.db.models import Q
from django.forms.models import modelformset_factory
from django.http import HttpResponse, Http404, HttpResponseRedirect, \
    HttpResponseBadRequest, JsonResponse
from django.shortcuts import redirect, render
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext, ugettext_lazy as _
from django.views.generic import ListView, TemplateView, View
from django.views.generic.edit import CreateView, DeleteView, FormView, \
    UpdateView
from extra_views import ModelFormSetView
from markdown import markdown

from . import models
from archaeological_context_records.models import ContextRecord
from archaeological_files.forms import DashboardForm as DashboardFormFile
from archaeological_files.models import File
from archaeological_finds.forms import DashboardTreatmentForm, \
    DashboardTreatmentFileForm
from archaeological_finds.models import Find, Treatment, TreatmentFile
from archaeological_operations.forms import DashboardForm as DashboardFormOpe
from archaeological_operations.models import Operation, ArchaeologicalSite
from archaeological_warehouse.models import Warehouse
from ishtar_common import forms_common as forms
from ishtar_common import wizards
from ishtar_common.forms import FinalForm, FinalDeleteForm
from ishtar_common.models import get_current_profile
from ishtar_common.templatetags.link_to_window import simple_link_to_window
from ishtar_common.utils import clean_session_cache, CSV_OPTIONS, \
    get_field_labels_from_path, get_random_item_image_link, shortify, \
    dict_to_tuple
from ishtar_common.widgets import JQueryAutoComplete

from .views_item import CURRENT_ITEM_KEYS, CURRENT_ITEM_KEYS_DICT, \
    check_permission, display_item, get_item, new_item, show_item

logger = logging.getLogger(__name__)


def status(request):
    return HttpResponse('OK')


def index(request):
    """
    Main page
    """
    dct = {'warnings': []}
    if settings.PROJECT_SLUG == 'default':
        dct['warnings'].append(_(
            u"PROJECT_SLUG is set to \"default\". Change it in your "
            u"local_settings (or ask your admin to do it)."))
    profile = get_current_profile()
    if profile.slug == 'default':
        dct['warnings'].append(_(
            u"The slug of your current profile is set to \"default\". Change it "
            u"on the administration page (or ask your admin to do it)."))
    image = get_random_item_image_link(request)
    if hasattr(profile, 'homepage') and profile.homepage:
        dct['homepage'] = markdown(profile.homepage)
        if '{random_image}' in dct['homepage']:
            dct['homepage'] = dct['homepage'].replace(
                '{random_image}', image)
    else:
        dct['random_image'] = image
    try:
        return render(request, 'index.html', dct)
    except NoReverseMatch:
        # probably rights exception (rights revoked)
        logout(request)
        return render(request, 'index.html', dct)

person_search_wizard = wizards.PersonSearch.as_view(
    [('general-person_search', forms.PersonFormSelection)],
    label=_(u"Person search"),
    url_name='person_search',)

person_creation_wizard = wizards.PersonWizard.as_view(
    [('identity-person_creation', forms.SimplePersonForm),
     ('person_type-person_creation', forms.PersonTypeForm),
     ('final-person_creation', FinalForm)],
    label=_(u"New person"),
    url_name='person_creation')

person_modification_wizard = wizards.PersonModifWizard.as_view(
    [('selec-person_modification', forms.PersonFormSelection),
     ('identity-person_modification', forms.SimplePersonForm),
     ('person_type-person_creation', forms.PersonTypeForm),
     ('final-person_modification', FinalForm)],
    label=_(u"Person modification"),
    url_name='person_modification')


def person_modify(request, pk):
    person_modification_wizard(request)
    wizards.PersonModifWizard.session_set_value(
        request, 'selec-person_modification', 'pk', pk, reset=True)
    return redirect(reverse('person_modification',
                            kwargs={'step': 'identity-person_modification'}))


person_deletion_wizard = wizards.PersonDeletionWizard.as_view(
    [('selec-person_deletion', forms.PersonFormSelection),
     ('final-person_deletion', FinalDeleteForm)],
    label=_(u"Person deletion"),
    url_name='person_deletion',)

organization_search_wizard = wizards.OrganizationSearch.as_view(
    [('general-organization_search', forms.OrganizationFormSelection)],
    label=_(u"Organization search"),
    url_name='organization_search',)

organization_creation_wizard = wizards.OrganizationWizard.as_view(
    [('identity-organization_creation', forms.OrganizationForm),
     ('final-organization_creation', FinalForm)],
    label=_(u"New organization"),
    url_name='organization_creation')

organization_modification_wizard = wizards.OrganizationModifWizard.as_view(
    [('selec-organization_modification', forms.OrganizationFormSelection),
     ('identity-organization_modification', forms.OrganizationForm),
     ('final-organization_modification', FinalForm)],
    label=_(u"Organization modification"),
    url_name='organization_modification')


def organization_modify(request, pk):
    organization_modification_wizard(request)
    wizards.OrganizationModifWizard.session_set_value(
        request, 'selec-organization_modification', 'pk', pk, reset=True)
    return redirect(
        reverse('organization_modification',
                kwargs={'step': 'identity-organization_modification'}))


organization_deletion_wizard = wizards.OrganizationDeletionWizard.as_view(
    [('selec-organization_deletion', forms.OrganizationFormSelection),
     ('final-organization_deletion', FinalDeleteForm)],
    label=_(u"Organization deletion"),
    url_name='organization_deletion',)

account_wizard_steps = [
    ('selec-account_management', forms.PersonUserFormSelection),
    ('account-account_management', forms.AccountForm),
    ('profile-account_management', forms.ProfileFormset),
    ('final-account_management', forms.FinalAccountForm)]

account_management_wizard = wizards.AccountWizard.as_view(
    account_wizard_steps,
    label=_(u"Account management"),
    url_name='account_management',)

account_deletion_wizard = wizards.IshtarUserDeletionWizard.as_view(
    [('selec-account_deletion', forms.AccountFormSelection),
     ('final-account_deletion', FinalDeleteForm)],
    label=_(u"Account deletion"),
    url_name='account_deletion',)


def get_autocomplete_generic(model, extra=None):
    if not extra:
        extra = {'available': True}

    def func(request):
        q = request.GET.get('term')
        query = Q(**extra)
        if not q:
            q = ""
        for q in q.split(' '):
            if not q:
                continue
            query = query & Q(label__icontains=q)
        limit = 20
        objects = model.objects.filter(query)[:limit]
        get_label = lambda x: x.full_label() if hasattr(x, 'full_label') \
            else str(x)
        data = json.dumps([{'id': obj.pk, 'value': get_label(obj)}
                           for obj in objects])
        return HttpResponse(data, content_type='text/plain')
    return func


def hide_shortcut_menu(request):
    request.session['SHORTCUT_SHOW'] = 'off'
    return HttpResponse('OK', content_type='text/plain')


def show_shortcut_menu(request):
    request.session['SHORTCUT_SHOW'] = 'on'
    return HttpResponse('OK', content_type='text/plain')


def activate_all_search(request):
    request.session['SHORTCUT_SEARCH'] = 'all'
    return HttpResponse('OK', content_type='text/plain')


def activate_own_search(request):
    request.session['SHORTCUT_SEARCH'] = 'own'
    return HttpResponse('OK', content_type='text/plain')


def activate_advanced_shortcut_menu(request):
    if not hasattr(request.user, 'ishtaruser'):
        return HttpResponse('KO', content_type='text/plain')
    request.user.ishtaruser.advanced_shortcut_menu = True
    request.user.ishtaruser.save()
    return HttpResponse('OK', content_type='text/plain')


def activate_simple_shortcut_menu(request):
    if not hasattr(request.user, 'ishtaruser'):
        return HttpResponse('KO', content_type='text/plain')
    request.user.ishtaruser.advanced_shortcut_menu = False
    request.user.ishtaruser.save()
    return HttpResponse('OK', content_type='text/plain')


def shortcut_menu(request):
    profile = get_current_profile()
    CURRENT_ITEMS = []
    if profile.files:
        CURRENT_ITEMS.append((_(u"Archaeological file"), File))
    CURRENT_ITEMS.append((_(u"Operation"), Operation))
    if profile.archaeological_site:
        CURRENT_ITEMS.append((profile.get_site_label(), ArchaeologicalSite))
    if profile.context_record:
        CURRENT_ITEMS.append((_(u"Context record"), ContextRecord))
    if profile.warehouse:
        CURRENT_ITEMS.append((_(u"Warehouse"), Warehouse))
    if profile.find:
        CURRENT_ITEMS.append((_(u"Find"), Find))
    if profile.warehouse:
        CURRENT_ITEMS.append((_(u"Treatment request"), TreatmentFile))
        CURRENT_ITEMS.append((_(u"Treatment"), Treatment))
    if hasattr(request.user, 'ishtaruser') and \
            request.user.ishtaruser.advanced_shortcut_menu:
        dct = {
            'current_menu': [], 'menu': [],
            'SHORTCUT_SEARCH': request.session['SHORTCUT_SEARCH']
            if 'SHORTCUT_SEARCH' in request.session else 'own',
            'SHORTCUT_SHOW': request.session['SHORTCUT_SHOW']
            if 'SHORTCUT_SHOW' in request.session else 'on'
        }

        current_selected_labels = []
        for lbl, model in CURRENT_ITEMS:
            model_name = model.SLUG
            current = model_name in request.session \
                and request.session[model_name]
            if current:
                try:
                    item = model.objects.get(pk=int(current))
                    item_label = shortify(str(item), 60)
                    current_selected_labels.append(item_label)
                except model.DoesNotExist:
                    pass
            dct['menu'].append((
                lbl, model_name, current or 0,
                JQueryAutoComplete(
                    reverse('get-' + model.SLUG + '-shortcut'),
                    model).render(
                        model.SLUG + '-shortcut', value=current,
                        attrs={'id': 'current_' + model.SLUG})))
        dct['current_selected_labels'] = current_selected_labels
        return render(
            request, 'ishtar/blocks/advanced_shortcut_menu.html', dct
        )
    dct = {
        'current_menu': [],
        'SHORTCUT_SHOW': request.session['SHORTCUT_SHOW']
        if 'SHORTCUT_SHOW' in request.session else 'off'
    }
    current_selected_labels = []
    current_selected_item = {}
    labels = {}
    for lbl, model in CURRENT_ITEMS:
        new_selected_item = None
        model_name = model.SLUG
        cls = ''
        current = model_name in request.session and request.session[model_name]
        items = []
        current_items = []
        labels[model_name] = {}
        lbl_key = "cached_label"
        if model.SLUG == "warehouse":
            lbl_key = "name"
        values = ['id', lbl_key]

        owns = model.get_owns(
            request.user, menu_filtr=current_selected_item,
            limit=100, values=values, get_short_menu_class=True) or []
        for item, shortmenu_class in owns:
            pk = str(item['id'])
            if shortmenu_class == 'basket':
                pk = "basket-" + pk
            # prevent duplicates
            if pk in current_items:
                continue
            current_items.append(pk)
            selected = pk == current
            item_label = shortify(item[lbl_key], 60)
            if selected:
                cls = shortmenu_class
                new_selected_item = pk
            labels[model_name][str(pk)] = item_label
            items.append((pk, item_label,
                          selected, shortmenu_class))
        # selected is not in owns - add it to the list
        if not new_selected_item and current:
            try:
                item = model.objects.get(pk=int(current))
                new_selected_item = item.pk
                item_label = shortify(str(item), 60)
                labels[model_name][str(item.pk)] = item_label
                items.append((item.pk, item_label,
                              True, item.get_short_menu_class(item.pk)))
            except (model.DoesNotExist, ValueError):
                pass
        if items:
            dct['current_menu'].append((lbl, model_name, cls, items))
        if new_selected_item:
            current_selected_item[model_name] = new_selected_item
            if str(new_selected_item) in labels[model_name]:
                current_selected_labels.append(
                    labels[model_name][str(new_selected_item)])
    dct['current_selected_labels'] = current_selected_labels
    return render(request, 'ishtar/blocks/shortcut_menu.html', dct)


def get_current_items(request):
    currents = {}
    for key, model in CURRENT_ITEM_KEYS:
        currents[key] = None
        if key in request.session and request.session[key]:
            try:
                currents[key] = model.objects.get(pk=int(request.session[key]))
            except (ValueError, model.DoesNotExist):
                continue
    return currents


def unpin(request, item_type, cascade=False):
    if item_type not in CURRENT_ITEM_KEYS_DICT.keys():
        logger.warning("unpin unknow type: {}".format(item_type))
        return HttpResponse('nok')
    if 'administrativeact' in item_type:
        request.session[item_type] = ''
        return HttpResponse('ok')
    request.session['treatment'] = ''
    if item_type == 'treatment' and not cascade:
        return HttpResponse('ok')
    request.session['treatmentfile'] = ''
    if item_type == 'treatmentfile' and not cascade:
        return HttpResponse('ok')
    request.session['find'] = ''
    if item_type == 'find' and not cascade:
        return HttpResponse('ok')
    request.session['warehouse'] = ''
    if item_type == 'warehouse' and not cascade:
        return HttpResponse('ok')
    request.session['contextrecord'] = ''
    if item_type == 'contextrecord' and not cascade:
        return HttpResponse('ok')
    request.session['site'] = ''
    if item_type == 'site' and not cascade:
        return HttpResponse('ok')
    request.session['operation'] = ''
    if item_type == 'operation' and not cascade:
        return HttpResponse('ok')
    request.session['file'] = ''
    if item_type == 'file' and not cascade:
        return HttpResponse('ok')


def update_current_item(request, item_type=None, pk=None):
    if not item_type or not pk:
        if not request.is_ajax() and not request.method == 'POST':
            raise Http404
        item_type = request.POST['item']
        if 'value' in request.POST and 'item' in request.POST:
            request.session[item_type] = request.POST['value']
    else:
        request.session[item_type] = str(pk)
    request.session['SHORTCUT_SEARCH'] = 'all'

    currents = get_current_items(request)

    # re-init when descending item are not relevant
    if item_type == 'file' and currents['file'] and currents['operation'] and \
            currents['operation'].associated_file != currents['file']:
        request.session["operation"] = ''
        currents['operation'] = None

    if item_type in ('operation', 'file') and currents['contextrecord'] and \
            (not request.session.get("operation", None) or
             currents['contextrecord'].operation != currents['operation']):
        request.session["contextrecord"] = ''
        currents['contextrecord'] = None

    from archaeological_finds.models import Find
    if item_type in ('contextrecord', 'operation', 'file') and \
        currents['find'] and \
        (not request.session.get("contextrecord", None) or
         not Find.objects.filter(
             downstream_treatment__isnull=True,
             base_finds__context_record__pk=request.session["contextrecord"],
             pk=currents['find'].pk).count()):
        request.session["find"] = ''
        currents['find'] = None

    # re-init ascending item with relevant values
    if item_type == "find" and currents['find']:
        from archaeological_context_records.models import ContextRecord
        q = ContextRecord.objects.filter(
            base_finds__find__pk=currents['find'].pk)
        if q.count():
            currents['contextrecord'] = q.all()[0]
            request.session['contextrecord'] = str(
                currents['contextrecord'].pk)
    if item_type in ("find", 'contextrecord') and currents['contextrecord']:
        currents['operation'] = currents['contextrecord'].operation
        request.session['operation'] = str(currents['operation'].pk)
    if item_type in ("find", 'contextrecord', 'operation') and \
            currents['operation']:
        currents['file'] = currents['operation'].associated_file
        request.session['file'] = str(currents['file'].pk) if currents['file'] \
            else None
    return HttpResponse('ok')


def pin_search(request, item_type):
    key = "pin-search-" + item_type
    if not item_type or not (
            request.is_ajax() and request.method == 'POST'
            and 'value' in request.POST):
        raise Http404
    request.session[key] = request.POST['value']
    if not request.POST['value']:
        # empty all
        unpin(request, item_type, cascade=True)
    else:
        unpin(request, item_type)
    return HttpResponse('ok')


def get_by_importer(request, slug, data_type='json', full=False,
                    force_own=False, **dct):
    q = models.ImporterType.objects.filter(slug=slug)
    if not q.count():
        res = ''
        if data_type == "json":
            res = '{}'
        return HttpResponse(res, content_type='text/plain')
    imp = q.all()[0].get_importer_class()
    cols, col_names = [], []
    for formater in imp.LINE_EXPORT_FORMAT:
        if not formater:
            cols.append('')
            col_names.append("")
            continue
        cols.append(formater.export_field_name)
        col_names.append(formater.label)
    obj_name = imp.OBJECT_CLS.__name__.lower()
    return get_item(
        imp.OBJECT_CLS, 'get_' + obj_name, obj_name, own_table_cols=cols
    )(request, data_type, full, force_own, col_names=col_names, **dct)


def autocomplete_person_permissive(request, person_types=None,
                                   attached_to=None, is_ishtar_user=None):
    return autocomplete_person(
        request, person_types=person_types, attached_to=attached_to,
        is_ishtar_user=is_ishtar_user, permissive=True)


def autocomplete_user(request):
    if not request.user.has_perm('ishtar_common.view_person', models.Person):
        return HttpResponse('[]', content_type='text/plain')
    q = request.GET.get('term')
    limit = request.GET.get('limit', 20)
    try:
        limit = int(limit)
    except ValueError:
        return HttpResponseBadRequest()
    query = Q()
    for q in q.split(' '):
        qu = (Q(ishtaruser__person__name__icontains=q) |
              Q(ishtaruser__person__surname__icontains=q) |
              Q(first_name__icontains=q) |
              Q(last_name__icontains=q))
        query = query & qu
    users = models.User.objects.filter(query)[:limit]
    data = json.dumps([
        {'id': user.pk,
         'value': str(user.ishtaruser)}
        for user in users if user and user.ishtaruser])
    return HttpResponse(data, content_type='text/plain')


def autocomplete_ishtaruser(request):
    if not request.user.has_perm('ishtar_common.view_person', models.Person):
        return HttpResponse('[]', content_type='text/plain')
    q = request.GET.get('term', '')
    limit = request.GET.get('limit', 20)
    try:
        limit = int(limit)
    except ValueError:
        return HttpResponseBadRequest()
    query = Q()
    for q in q.split(' '):
        qu = (Q(person__name__icontains=q) |
              Q(person__surname__icontains=q) |
              Q(person__raw_name__icontains=q))
        query = query & qu
    users = models.IshtarUser.objects.filter(query)[:limit]
    data = json.dumps([
        {'id': user.pk,
         'value': str(user)}
        for user in users])
    return HttpResponse(data, content_type='text/plain')


def autocomplete_person(request, person_types=None, attached_to=None,
                        is_ishtar_user=None, permissive=False):
    all_items = request.user.has_perm('ishtar_common.view_person',
                                      models.Person)
    own_items = False
    if not all_items:
        own_items = request.user.has_perm('ishtar_common.view_own_person',
                                          models.Person)
    if not all_items and not own_items or not request.GET.get('term'):
        return HttpResponse('[]', content_type='text/plain')
    q = request.GET.get('term')
    limit = request.GET.get('limit', 20)
    try:
        limit = int(limit)
    except ValueError:
        return HttpResponseBadRequest()
    query = Q()
    for q in q.split(' '):
        qu = (Q(name__icontains=q) | Q(surname__icontains=q) |
              Q(email__icontains=q) | Q(attached_to__name__icontains=q))
        if permissive:
            qu = qu | Q(raw_name__icontains=q)
        query = query & qu
    if attached_to:
        query = query & Q(attached_to__pk__in=attached_to.split('_'))

    if person_types and str(person_types) != '0':
        try:
            typs = [int(tp) for tp in person_types.split('_') if tp]
            typ = models.PersonType.objects.filter(pk__in=typs).all()
            query = query & Q(person_types__in=typ)
        except (ValueError, ObjectDoesNotExist):
            pass
    if is_ishtar_user:
        query = query & Q(ishtaruser__isnull=False)
    if own_items:
        if not hasattr(request.user, 'ishtaruser'):
            return HttpResponse(json.dumps([]), content_type='text/plain')
        query &= models.Person.get_query_owns(request.user.ishtaruser)
    persons = models.Person.objects.filter(query)[:limit]
    data = json.dumps([{'id': person.pk, 'value': str(person)}
                       for person in persons if person])
    return HttpResponse(data, content_type='text/plain')


def autocomplete_department(request):
    if not request.GET.get('term'):
        return HttpResponse('[]', content_type='text/plain')
    q = request.GET.get('term')
    q = unicodedata.normalize("NFKD", q).encode('ascii', 'ignore').decode()
    query = Q()
    for q in q.split(' '):
        extra = (Q(label__icontains=q) | Q(number__istartswith=q))
        query = query & extra
    limit = 20
    departments = models.Department.objects.filter(query)[:limit]
    data = json.dumps([{'id': department.pk, 'value': str(department)}
                       for department in departments])
    return HttpResponse(data, content_type='text/plain')


def autocomplete_town(request):
    if not request.GET.get('term'):
        return HttpResponse(content_type='text/plain')
    q = request.GET.get('term')
    q = unicodedata.normalize("NFKD", q).encode('ascii', 'ignore').decode()
    query = Q()
    for q in q.split(' '):
        extra = Q(name__icontains=q)
        if settings.COUNTRY == 'fr':
            extra |= Q(numero_insee__istartswith=q)
        query &= extra
    limit = 20
    towns = models.Town.objects.filter(query)[:limit]
    data = json.dumps([{'id': town.pk, 'value': str(town)}
                       for town in towns])
    return HttpResponse(data, content_type='text/plain')


def autocomplete_advanced_town(request, department_id=None, state_id=None):
    if not request.GET.get('term'):
        return HttpResponse(content_type='text/plain')
    q = request.GET.get('term')
    q = unicodedata.normalize("NFKD", q).encode('ascii', 'ignore').decode()
    query = Q()
    for q in q.split(' '):
        extra = Q(name__icontains=q)
        if settings.COUNTRY == 'fr':
            extra = extra | Q(numero_insee__istartswith=q)
            if not department_id:
                extra = extra | Q(departement__label__istartswith=q)
        query = query & extra
    if department_id:
        query = query & Q(departement__number__iexact=department_id)
    if state_id:
        query = query & Q(departement__state__number__iexact=state_id)
    limit = 20
    towns = models.Town.objects.filter(query)[:limit]
    result = []
    for town in towns:
        val = town.name
        if hasattr(town, 'numero_insee'):
            val += " (%s)" % town.numero_insee
        result.append({'id': town.pk, 'value': val})
    data = json.dumps(result)
    return HttpResponse(data, content_type='text/plain')


def department_by_state(request, state_id=''):
    if not state_id:
        data = []
    else:
        departments = models.Department.objects.filter(state__number=state_id)
        data = json.dumps([{'id': department.pk, 'number': department.number,
                            'value': str(department)}
                           for department in departments])
    return HttpResponse(data, content_type='text/plain')


def autocomplete_organization(request, orga_type=None):
    if (not request.user.has_perm('ishtar_common.view_organization',
        models.Organization) and
        not request.user.has_perm('ishtar_common.view_own_organization',
                                  models.Organization)
       and not request.user.ishtaruser.has_right(
            'person_search', session=request.session)):
        return HttpResponse('[]', content_type='text/plain')
    if not request.GET.get('term'):
        return HttpResponse('[]', content_type='text/plain')
    q = request.GET.get('term')
    query = Q()
    for q in q.split(' '):
        extra = Q(cached_label__icontains=q)
        query = query & extra
    if orga_type:
        try:
            typs = [int(tp) for tp in orga_type.split('_') if tp]
            typ = models.OrganizationType.objects.filter(pk__in=typs).all()
            query = query & Q(organization_type__in=typ)
        except (ValueError, ObjectDoesNotExist):
            pass
    limit = 15
    organizations = models.Organization.objects.filter(query)[:limit]
    data = json.dumps([{'id': org.pk, 'value': str(org)}
                       for org in organizations])
    return HttpResponse(data, content_type='text/plain')


def autocomplete_author(request):
    if not request.user.has_perm('ishtar_common.view_author', models.Author)\
       and not request.user.has_perm('ishtar_common.view_own_author',
                                     models.Author):
        return HttpResponse('[]', content_type='text/plain')
    if not request.GET.get('term'):
        return HttpResponse('[]', content_type='text/plain')
    q = request.GET.get('term')
    query = Q()
    for q in q.split(' '):
        extra = Q(person__name__icontains=q) | \
            Q(person__surname__icontains=q) | \
            Q(person__email__icontains=q) | \
            Q(author_type__label__icontains=q)
        query = query & extra
    limit = 15
    authors = models.Author.objects.filter(query)[:limit]
    data = json.dumps([{'id': author.pk, 'value': str(author)}
                       for author in authors])
    return HttpResponse(data, content_type='text/plain')


new_person = new_item(models.Person, forms.PersonForm)
new_person_noorga = new_item(models.Person, forms.NoOrgaPersonForm)
new_organization = new_item(models.Organization, forms.OrganizationForm)
show_organization = show_item(models.Organization, 'organization')
get_organization = get_item(models.Organization, 'get_organization',
                            'organization')
new_author = new_item(models.Author, forms.AuthorForm)
show_person = show_item(models.Person, 'person')

get_person = get_item(models.Person, 'get_person', 'person')

get_person_for_account = get_item(
    models.Person,
    'get_person', 'person',
    own_table_cols=models.Person.TABLE_COLS_ACCOUNT)

get_ishtaruser = get_item(models.IshtarUser, 'get_ishtaruser', 'ishtaruser')


def action(request, action_slug, obj_id=None, *args, **kwargs):
    """
    Action management
    """
    if not check_permission(request, action_slug, obj_id):
        not_permitted_msg = ugettext(u"Operation not permitted.")
        return HttpResponse(not_permitted_msg)
    request.session['CURRENT_ACTION'] = action_slug
    dct = {}
    globals_dct = globals()
    if action_slug in globals_dct:
        return globals_dct[action_slug](request, dct, obj_id, *args, **kwargs)
    return render(request, 'index.html', dct)


def dashboard_main(request, dct, obj_id=None, *args, **kwargs):
    """
    Main dashboard
    """
    app_list = []
    profile = models.get_current_profile()
    if profile.files:
        app_list.append((_(u"Archaeological files"), 'files'))
    app_list.append((_(u"Operations"), 'operations'))
    if profile.context_record:
        app_list.append((_(u"Context records"), 'contextrecords'))
    if profile.find:
        app_list.append((_(u"Finds"), 'finds'))
    if profile.warehouse:
        app_list.append((_(u"Treatment requests"), 'treatmentfiles'))
        app_list.append((_(u"Treatments"), 'treatments'))
    dct = {'app_list': app_list}
    return render(request, 'ishtar/dashboards/dashboard_main.html', dct)


DASHBOARD_FORMS = {
    'files': DashboardFormFile, 'operations': DashboardFormOpe,
    'treatments': DashboardTreatmentForm,
    'treatmentfiles': DashboardTreatmentFileForm
}


def dashboard_main_detail(request, item_name):
    """
    Specific tab of the main dashboard
    """
    if item_name == 'users':
        dct = {'ishtar_users': models.UserDashboard()}
        return render(
            request, 'ishtar/dashboards/dashboard_main_detail_users.html', dct)
    form = None
    slicing, date_source, fltr, show_detail = 'year', None, {}, False
    profile = models.get_current_profile()
    has_form = (item_name == 'files' and profile.files) \
        or item_name == 'operations' \
        or (item_name in ('treatmentfiles', 'treatments')
            and profile.warehouse)
    if has_form:
        slicing = 'month'
    if item_name in DASHBOARD_FORMS:
        if request.method == 'POST':
            form = DASHBOARD_FORMS[item_name](request.POST)
            if form.is_valid():
                slicing = form.cleaned_data['slicing']
                fltr = form.get_filter()
                if hasattr(form, 'get_date_source'):
                    date_source = form.get_date_source()
                if hasattr(form, 'get_show_detail'):
                    show_detail = form.get_show_detail()
        else:
            form = DASHBOARD_FORMS[item_name]()
    lbl, dashboard = None, None
    dashboard_kwargs = {}
    if has_form:
        dashboard_kwargs = {'slice': slicing, 'fltr': fltr,
                            'show_detail': show_detail}
        # date_source is only relevant when the form has set one
        if date_source:
            dashboard_kwargs['date_source'] = date_source
    if item_name == 'files' and profile.files:
        lbl, dashboard = (_(u"Archaeological files"),
                          models.Dashboard(File, **dashboard_kwargs))
    elif item_name == 'operations':
        from archaeological_operations.models import Operation
        lbl, dashboard = (_(u"Operations"),
                          models.Dashboard(Operation, **dashboard_kwargs))
    elif item_name == 'contextrecords' and profile.context_record:
        lbl, dashboard = (
            _(u"Context records"),
            models.Dashboard(ContextRecord, slice=slicing, fltr=fltr))
    elif item_name == 'finds' and profile.find:
        lbl, dashboard = (_(u"Finds"), models.Dashboard(Find,
                                                        slice=slicing,
                                                        fltr=fltr))
    elif item_name == 'treatmentfiles' and profile.warehouse:
        lbl, dashboard = (
            _(u"Treatment requests"),
            models.Dashboard(TreatmentFile, **dashboard_kwargs))
    elif item_name == 'treatments' and profile.warehouse:
        if 'date_source' not in dashboard_kwargs:
            dashboard_kwargs['date_source'] = 'start'
        lbl, dashboard = (
            _(u"Treatments"),
            models.Dashboard(Treatment, **dashboard_kwargs))
    if not lbl:
        raise Http404
    dct = {'lbl': lbl, 'dashboard': dashboard,
           'item_name': item_name.replace('-', '_'),
           'VALUE_QUOTE': '' if slicing == "year" else "'",
           'form': form, 'slicing': slicing}
    n = datetime.datetime.now()
    dct['unique_id'] = dct['item_name'] + "_" + \
        '%d_%d_%d' % (n.minute, n.second, n.microsecond)
    return render(request, 'ishtar/dashboards/dashboard_main_detail.html', dct)


def reset_wizards(request):
    # dynamically execute each reset_wizards of each ishtar app
    for app in settings.INSTALLED_APPS:
        if app == 'ishtar_common':
            # no need for infinite recursion
            continue
        try:
            module = __import__(app)
        except ImportError:
            continue
        if hasattr(module, 'views') and hasattr(module.views, 'reset_wizards'):
            module.views.reset_wizards(request)
    return redirect(reverse('start'))

ITEM_PER_PAGE = 20


def merge_action(model, form, key):
    def merge(request, page=1):
        current_url = key + '_merge'
        if not page:
            page = 1
        page = int(page)
        FormSet = modelformset_factory(
            model.merge_candidate.through, form=form,
            formset=forms.MergeFormSet, extra=0)
        q = model.merge_candidate.through.objects
        context = {'current_url': current_url,
                   'current_page': page,
                   'max_page': q.count() / ITEM_PER_PAGE}
        if page < context["max_page"]:
            context['next_page'] = page + 1
        if page > 1:
            context['previous_page'] = page - 1

        item_nb = page * ITEM_PER_PAGE
        item_nb_1 = item_nb + ITEM_PER_PAGE
        from_key = 'from_' + key
        to_key = 'to_' + key
        queryset = q.all().order_by(from_key + '__name')[item_nb:item_nb_1]
        FormSet.from_key = from_key
        FormSet.to_key = to_key
        if request.method == 'POST':
            context['formset'] = FormSet(request.POST, queryset=queryset)
            if context['formset'].is_valid():
                context['formset'].merge()
                return redirect(reverse(current_url, kwargs={'page': page}))
        else:
            context['formset'] = FormSet(queryset=queryset)
        return render(request, 'ishtar/merge_' + key + '.html', context)

    return merge


person_merge = merge_action(models.Person, forms.MergePersonForm, 'person')
organization_merge = merge_action(
    models.Organization,
    forms.MergeOrganizationForm,
    'organization'
)


class IshtarMixin(object):
    page_name = u""

    def get_context_data(self, **kwargs):
        context = super(IshtarMixin, self).get_context_data(**kwargs)
        context['page_name'] = self.page_name
        return context


class JSONResponseMixin:
    """
    Render a JSON response.
    """
    def render_to_response(self, context, **response_kwargs):
        return JsonResponse(
            self.get_data(context),
            **response_kwargs
        )

    def get_data(self, context):
        return context


class LoginRequiredMixin(object):
    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(request, *args,
                                                        **kwargs)


class AdminLoginRequiredMixin(LoginRequiredMixin):
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_staff:
            return redirect(reverse('start'))
        return super(AdminLoginRequiredMixin, self).dispatch(
            request, *args, **kwargs)


class ProfileEdit(LoginRequiredMixin, FormView):
    template_name = 'ishtar/forms/profile.html'
    form_class = forms.ProfilePersonForm

    def dispatch(self, request, *args, **kwargs):
        if kwargs.get('pk'):
            try:
                profile = models.UserProfile.objects.get(
                    pk=kwargs['pk'], person__ishtaruser__user_ptr=request.user)
            except models.UserProfile.DoesNotExist:
                # cannot edit a profile that is not yours...
                return redirect(reverse('index'))
            current_changed = False
            # the profile edited became the current profile
            if not profile.current:
                current_changed = True
                profile.current = True
            # force post-save in case of many current profile
            profile.save()
            if current_changed:
                clean_session_cache(request.session)
        return super(ProfileEdit, self).dispatch(request, *args, **kwargs)

    def get_success_url(self):
        return reverse('profile')

    def get_form_kwargs(self):
        kwargs = super(ProfileEdit, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

    def get_context_data(self, **kwargs):
        data = super(ProfileEdit, self).get_context_data(**kwargs)
        data['page_name'] = _(u"Current profile")
        return data

    def form_valid(self, form):
        form.save(self.request.session)
        return HttpResponseRedirect(self.get_success_url())


class DisplayItemView(IshtarMixin, TemplateView):
    template_name = 'ishtar/display_item.html'

    def dispatch(self, request, *args, **kwargs):
        if not self.request.user.is_authenticated:
            return redirect_to_login(reverse("display-item", kwargs=kwargs))
        return super(DisplayItemView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, *args, **kwargs):
        data = super(DisplayItemView, self).get_context_data(*args, **kwargs)
        pk = str(kwargs.get('pk')) + '/'
        item_url = '/show-' + kwargs.get('item_type')
        data['show_url'] = item_url + "/" + pk
        return data


class QRCodeView(IshtarMixin, LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
        model_name = "".join(
            [part.capitalize() for part in kwargs.get('model_name').split('-')]
        )
        app = kwargs.get('app').replace('-', "_")
        try:
            model = apps.get_model(app, model_name)
            item = model.objects.get(pk=kwargs.get("pk"))
            assert hasattr(item, 'qrcode')
        except (LookupError, model.DoesNotExist, AssertionError):
            raise Http404()

        if not item.qrcode or not item.qrcode.name:
            item.generate_qrcode(request=self.request)

        if not item.qrcode or not item.qrcode.name:  # generation has failed
            raise Http404()

        with open(settings.MEDIA_ROOT + os.sep + item.qrcode.name, "rb") as f:
            return HttpResponse(f.read(), content_type="image/png")


class GlobalVarEdit(IshtarMixin, AdminLoginRequiredMixin, ModelFormSetView):
    template_name = 'ishtar/formset.html'
    model = models.GlobalVar
    factory_kwargs = {
        'extra': 1,
        'can_delete': True
    }
    page_name = _(u"Global variables")
    fields = ['slug', 'value', 'description']


class NewImportView(IshtarMixin, LoginRequiredMixin, CreateView):
    template_name = 'ishtar/form.html'
    model = models.Import
    form_class = forms.NewImportForm
    page_name = _(u"New import")

    def get_success_url(self):
        return reverse('current_imports')

    def get_form_kwargs(self):
        kwargs = super(NewImportView, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

    def form_valid(self, form):
        user = models.IshtarUser.objects.get(pk=self.request.user.pk)
        self.object = form.save(user=user)
        return HttpResponseRedirect(self.get_success_url())


class ImportListView(IshtarMixin, LoginRequiredMixin, ListView):
    template_name = 'ishtar/import_list.html'
    model = models.Import
    page_name = _(u"Current imports")
    current_url = 'current_imports'

    def get_queryset(self):
        q = self.model.objects.exclude(state='AC')
        if self.request.user.is_superuser:
            return q.order_by('-pk')
        user = models.IshtarUser.objects.get(pk=self.request.user.pk)
        return q.filter(user=user).order_by('-pk')

    def post(self, request, *args, **kwargs):
        for field in request.POST:
            if not field.startswith('import-action-') or \
               not request.POST[field]:
                continue
            # prevent forged forms
            try:
                imprt = models.Import.objects.get(pk=int(field.split('-')[-1]))
            except (models.Import.DoesNotExist, ValueError):
                continue
            if not self.request.user.is_superuser:
                # user can only edit his own imports
                user = models.IshtarUser.objects.get(pk=self.request.user.pk)
                if imprt.user != user:
                    continue
            action = request.POST[field]
            if action == 'D':
                return HttpResponseRedirect(reverse('import_delete',
                                                    kwargs={'pk': imprt.pk}))
            elif action == 'A':
                imprt.initialize(user=self.request.user.ishtaruser,
                                 session_key=request.session.session_key)
            elif action == 'I':
                if settings.USE_BACKGROUND_TASK:
                    imprt.delayed_importation(
                        request, request.session.session_key
                    )
                else:
                    imprt.importation()
            elif action == 'CH':
                if settings.USE_BACKGROUND_TASK:
                    imprt.delayed_check_modified(request.session.session_key)
                else:
                    imprt.check_modified()
            elif action == 'IS':
                if imprt.current_line is None:
                    imprt.current_line = imprt.skip_lines
                    imprt.save()
                return HttpResponseRedirect(
                    reverse('import_step_by_step',
                            args=[imprt.pk, imprt.current_line + 1])
                )
            elif action == 'AC':
                imprt.archive()
        return HttpResponseRedirect(reverse(self.current_url))

    def get_context_data(self, **kwargs):
        dct = super(ImportListView, self).get_context_data(**kwargs)
        dct['autorefresh_available'] = settings.USE_BACKGROUND_TASK
        return dct


class ImportStepByStepView(IshtarMixin, LoginRequiredMixin, TemplateView):
    template_name = 'ishtar/import_step_by_step.html'
    page_name = _(u"Import step by step")
    current_url = 'import_step_by_step'

    def get_import(self):
        try:
            self.imprt_obj = models.Import.objects.get(
                pk=int(self.kwargs['pk'])
            )
        except (models.Import.DoesNotExist, ValueError):
            raise Http404
        if not self.request.user.is_superuser:
            # user can only edit his own imports
            user = models.IshtarUser.objects.get(pk=self.request.user.pk)
            if self.imprt_obj.user != user:
                raise Http404
        if not hasattr(self, 'current_line_number'):
            self.current_line_number = int(self.kwargs['line_number']) - 1

    def update_csv(self, request):
        prefix = 'col-'
        submited_line = [(int(k[len(prefix):]), request.POST[k])
                         for k in request.POST if k.startswith(prefix)]
        updated_line = [value for line, value in sorted(submited_line)]
        filename = self.imprt_obj.imported_file.path
        with open(filename, 'r', encoding=self.imprt_obj.encoding) as f:
            reader = csv.reader(f)
            lines = []
            for idx, line in enumerate(reader):
                if idx == self.current_line_number:
                    line = updated_line
                line = [v.encode(self.imprt_obj.encoding, errors='replace')
                        for v in line]
                lines.append(line)
        with open(filename, 'w') as f:
            writer = csv.writer(f, **CSV_OPTIONS)
            writer.writerows(lines)

    def import_line(self, request, *args, **kwargs):
        try:
            self.imprt, data = self.imprt_obj.importation(
                line_to_process=self.current_line_number,
                return_importer_and_data=True
            )
        except IOError as e:
            self.errors = [str(e)]
            return super(ImportStepByStepView, self).get(request, *args,
                                                         **kwargs)
        if self.imprt_obj.get_number_of_lines() >= self.current_line_number:
            self.current_line_number += 1
        else:
            self.current_line_number = None
        self.imprt_obj.current_line = self.current_line_number
        self.imprt_obj.save()
        return self.current_line_number

    def post(self, request, *args, **kwargs):
        if not request.POST or request.POST.get('valid', None) not in (
                'change-csv', 'import', 'change-page'):
            return self.get(request, *args, **kwargs)
        self.get_import()

        if request.POST.get('valid') == 'change-page':
            return HttpResponseRedirect(
                reverse('import_step_by_step',
                        args=[self.imprt_obj.pk,
                              request.POST.get('line-to-go', None)]))

        if request.POST.get('valid') == 'change-csv':
            self.update_csv(request)
            return self.get(request, *args, **kwargs)

        if not self.import_line(request, *args, **kwargs):
            return HttpResponseRedirect(reverse('current_imports'))
        else:
            return HttpResponseRedirect(
                reverse('import_step_by_step',
                        args=[self.imprt_obj.pk, self.current_line_number + 1]))

    def get(self, request, *args, **kwargs):
        self.get_import()
        self.imprt = None
        self.errors, self.new_data = None, None
        try:
            self.imprt, data = self.imprt_obj.importation(
                simulate=True,
                line_to_process=self.current_line_number,
                return_importer_and_data=True
            )
        except IOError as e:
            self.errors = [None, None, str(e)]
            return super(ImportStepByStepView, self).get(request, *args,
                                                         **kwargs)
        if not data or not data[0]:
            self.errors = self.imprt.errors
            if not self.errors:
                self.errors = [("", "", _(u"No data provided"))]
        else:
            self.new_data = data[:]
        return super(ImportStepByStepView, self).get(request, *args, **kwargs)

    def get_pagination(self, dct):
        pagination_step = 5
        only_modified = not self.kwargs.get('all_pages', False)
        dct['all'] = not only_modified
        dct['import_url'] = 'import_step_by_step' if only_modified else \
            'import_step_by_step_all'
        line_nb = self.imprt_obj.get_number_of_lines()
        total_line_nb = self.imprt_obj.skip_lines + line_nb
        delta = 0

        already_imported = []
        if self.imprt_obj.imported_line_numbers:
            already_imported = self.imprt_obj.imported_line_numbers.split(',')
        changes = []
        if self.imprt_obj.changed_line_numbers:
            changes = self.imprt_obj.changed_line_numbers.split(',')

        dct['page_is_last'] = self.current_line_number == line_nb
        # label, number, enabled, is_imported, has_changes
        dct['page_numbers'] = []
        # first pass for the delta
        current = 0
        for idx in range(self.imprt_obj.skip_lines, total_line_nb):
            imported = str(idx) in already_imported
            changed = str(idx) in changes
            if only_modified and (imported or not changed):
                continue
            current += 1
            if idx == self.current_line_number - 1:
                delta = int(current / pagination_step)

        current, has_next, previous = 0, False, None
        for idx in range(self.imprt_obj.skip_lines, total_line_nb):
            if current >= ((delta + 1) * pagination_step):
                has_next = idx
                break
            imported = str(idx) in already_imported
            changed = str(idx) in changes
            if only_modified and (imported or not changed):
                continue
            current += 1
            if current <= (delta * pagination_step):
                previous = idx
                continue
            nb = idx + 1
            dct['page_numbers'].append((nb, nb, True, imported, changed))

        if previous:
            dct['page_numbers'].insert(0,
                (_(u"Previous"), previous + 1, True, False,
                 True)
            )
        else:
            dct['page_numbers'].insert(0,
                (_(u"Previous"), self.imprt_obj.skip_lines, False, False, True)
            )
        if has_next:
            dct['page_numbers'].append((_(u"Next"), has_next + 1, True, False,
                                        True))
        else:
            dct['page_numbers'].append((_(u"Next"), total_line_nb, False,
                                        False, True))

    def get_context_data(self, **kwargs):
        dct = super(ImportStepByStepView, self).get_context_data(**kwargs)
        dct['import'] = self.imprt_obj
        dct['line_number_displayed'] = self.current_line_number + 1
        dct['line_is_imported'] = self.imprt_obj.line_is_imported(
            self.current_line_number)
        self.get_pagination(dct)
        dct['errors'] = self.errors
        if self.errors:
            if self.imprt.current_csv_line:
                headers = [f.label if f else _(u"Not imported")
                           for f in self.imprt.get_formaters()]
                dct['values'] = zip(
                    range(1, len(headers) + 1), headers,
                    self.imprt.current_csv_line
                )
            return dct
        headers, self.path_to_column, interpreted_values = [], {}, []
        for idx, formater in enumerate(self.imprt.get_formaters()):
            if not formater:
                headers.append(_(u"Not imported"))
                interpreted_values.append(u"&ndash;")
                continue
            lbl = formater.label
            if formater.comment:
                lbl += u' <i data-toggle="tooltip" class="fa '
                lbl += u'fa-question-circle"'
                lbl += u' aria-hidden="true" title="{}">'.format(
                    formater.comment.replace(u'"', u'&quot;'))
                lbl += u'</i>'
            headers.append(lbl)

            field_name = ""
            if formater.field_name:
                field_name = formater.field_name[0]
            if formater.export_field_name:
                field_name = formater.export_field_name[0]
            value = self.new_data[0].copy()

            field_name_tuple = field_name.split(u'__')
            # associate each path level to this column
            while field_name_tuple:
                current_field_name = u'__'.join(field_name_tuple)
                if current_field_name not in self.path_to_column:
                    self.path_to_column[current_field_name] = []
                self.path_to_column[current_field_name].append(idx)
                field_name_tuple.pop()

            for idx_split, key in enumerate(field_name.split('__')):
                if isinstance(value, dict) and key in value:
                    value = value[key]
                elif not idx_split:
                    # no key reach
                    value = None
                else:
                    break
            value = self.get_value(value)
            interpreted_values.append(value)

        dct['values'] = zip(
            range(1, len(headers) + 1), headers, self.imprt.current_csv_line,
            interpreted_values
        )

        new_objects = {}
        for path, cls, new_dct in self.imprt.new_objects:
            # transform path to explicit label
            label = self.transform_path_to_label(self.imprt.OBJECT_CLS, path)

            created_dict = {}
            for k, val in new_dct.items():
                if val in ('', None, [], [None]):
                    continue
                created_dict[k] = val
            # check if it is not previously created
            dct_key = []
            for key, value in created_dict.items():
                if isinstance(value, list):
                    value = tuple(sorted(value))
                if isinstance(value, dict):
                    value = dict_to_tuple(value)
                dct_key.append((key, value))
            key = (cls, tuple(sorted(dct_key)))
            if key in list(new_objects.keys()):
                # regroup it
                new_objects[key][0].append(label)
                continue

            # values - transform key to explicit label
            value_dct = self.transform_keys_to_label(path, cls, created_dict)

            for k in value_dct.keys():
                value_dct[k] = self.get_value(value_dct[k])

            new_objects[key] = ([label], cls, value_dct)

        dct['new_objects'] = [
            [u" &ndash; ".join(lbls), cls, new_dct]
            for lbls, cls, new_dct in new_objects.values()
        ]

        updated_objects = []
        main_changed = False
        for path, obj, values, updated_values in self.imprt.updated_objects:

            # transform path to explicit label
            label = self.transform_path_to_label(self.imprt.OBJECT_CLS, path)

            # transform key into explicit label
            values = self.transform_keys_to_label(path, obj.__class__, values)

            # add current values and changed bool
            old_and_updated = {}
            for k in updated_values.keys():
                current_value = getattr(obj, k)
                updated_value = updated_values[k]
                if hasattr(current_value, 'all'):
                    current_value = list(current_value.all())
                    changed = False
                    for v in updated_value:
                        if v not in current_value:
                            changed = True
                    current_value = self.list_to_html(current_value)
                    updated_value = self.list_to_html(updated_value)
                else:
                    changed = current_value != updated_value
                    current_value = self.get_value(current_value)
                    updated_value = self.get_value(updated_value)
                main_changed |= changed
                old_and_updated[k] = [current_value, updated_value,
                                      changed]

            # transform key into explicit label
            old_and_updated = self.transform_keys_to_label(path, obj.__class__,
                                                           old_and_updated)
            updated_objects.append((label, obj, values, old_and_updated))

        dct['have_change'] = main_changed or self.imprt.new_objects
        if dct["have_change"]:
            self.imprt_obj.add_changed_line(self.current_line_number)
        else:
            self.imprt_obj.remove_changed_line(self.current_line_number)

        dct['updated_objects'] = []
        dct['matched_objects'] = []
        for path, obj, values, old_and_updated in updated_objects:
            if old_and_updated:
                dct['updated_objects'].append(
                    (path, obj, values, old_and_updated)
                )
            else:
                dct['matched_objects'].append(
                    (path, obj, values)
                )
        dct['ambiguous_objects'] = self.imprt.ambiguous_objects
        dct['not_find_objects'] = self.imprt.not_find_objects
        return dct

    def transform_path_to_label(self, cls, path):
        label = u" > ".join(
            str(l)
            for l in get_field_labels_from_path(cls, path)
        )
        if not label:
            label = str(cls._meta.verbose_name)
        return label

    def transform_keys_to_label(self, path, cls, dct):
        value_dct = {}
        for k in dct:
            label = get_field_labels_from_path(cls, [k])
            if label:
                label = str(label[0])
            else:
                label = k

            concat_path = u"__".join(list(path) + [k])
            if concat_path in self.path_to_column:
                for col in self.path_to_column[concat_path]:
                    col += 1
                    label += u" <a href=\"#col-{}\">"\
                             u"<span class=\"badge badge-info\"> {} {} </span>"\
                             u"</a>".format(col, _(u"Col. "), col)
            value_dct[label] = dct[k]
        return value_dct

    def list_to_html(self, lst):
        if not lst:
            return _(u"* empty *")
        return u"<ul class='list-group'><li class='list-group-item'>" + \
               u"</li><li class='list-group-item'>".join([
                   self.get_value(item) for item in lst
               ]) + u"</li></ul>"

    def get_value(self, item):
        if hasattr(item, 'SHOW_URL'):
            return u"{}{}".format(str(item), simple_link_to_window(item))
        if hasattr(item, 'explicit_label'):
            return item.explicit_label
        if item in (None, [], [None]):
            return _(u"* empty *")
        if isinstance(item, list):
            return self.list_to_html(item)
        return str(item)


class ImportListTableView(ImportListView):
    template_name = 'ishtar/import_table.html'
    current_url = 'current_imports_table'

    def get_context_data(self, **kwargs):
        dct = super(ImportListTableView, self).get_context_data(**kwargs)
        dct['AJAX'] = True
        dct['MESSAGES'] = []
        request = self.request
        if 'messages' in request.session and \
                request.session['messages']:
            for message, message_type in request.session['messages']:
                dct['MESSAGES'].append((message, message_type))
            request.session['messages'] = []
        if 'current_import_id' in request.session and \
                request.session['current_import_id']:
            dct['refreshed_pks'] = request.session.pop('current_import_id')
        return dct


class ImportOldListView(ImportListView):
    page_name = _(u"Old imports")
    current_url = 'old_imports'

    def get_queryset(self):
        q = self.model.objects.filter(state='AC')
        if self.request.user.is_superuser:
            return q.order_by('-creation_date')
        user = models.IshtarUser.objects.get(pk=self.request.user.pk)
        return q.filter(user=user).order_by('-creation_date')


class ImportLinkView(IshtarMixin, LoginRequiredMixin, ModelFormSetView):
    template_name = 'ishtar/formset_import_match.html'
    model = models.TargetKey
    page_name = _(u"Link unmatched items")
    factory_kwargs = {
        'extra': 0,
    }
    form_class = forms.TargetKeyForm
    formset_class = forms.TargetKeyFormset

    def get_formset_kwargs(self):
        kwargs = super(ImportLinkView, self).get_formset_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

    def get_queryset(self):
        return self.model.objects.filter(
            is_set=False, associated_import=self.kwargs['pk'])

    def get_success_url(self):
        return reverse('import_link_unmatched', args=[self.kwargs['pk']])


class ImportDeleteView(IshtarMixin, LoginRequiredMixin, DeleteView):
    template_name = 'ishtar/import_delete.html'
    model = models.Import
    page_name = _(u"Delete import")

    def get_success_url(self):
        return reverse('current_imports')


class PersonCreate(LoginRequiredMixin, CreateView):
    model = models.Person
    form_class = forms.BasePersonForm
    template_name = 'ishtar/person_form.html'

    def get_success_url(self):
        return reverse('person_edit', args=[self.object.pk])


class PersonEdit(LoginRequiredMixin, UpdateView):
    model = models.Person
    form_class = forms.BasePersonForm
    template_name = 'ishtar/person_form.html'

    def get_success_url(self):
        return reverse('person_edit', args=[self.object.pk])


class ManualMergeMixin(object):
    def form_valid(self, form):
        self.items = form.get_items()
        return super(ManualMergeMixin, self).form_valid(form)

    def get_success_url(self):
        return reverse(
            self.redir_url,
            args=[u"_".join([str(item.pk) for item in self.items])])


class PersonManualMerge(ManualMergeMixin, IshtarMixin, LoginRequiredMixin,
                        FormView):
    form_class = forms.PersonMergeFormSelection
    template_name = 'ishtar/form.html'
    page_name = _(u"Merge persons")
    current_url = 'person-manual-merge'
    redir_url = 'person_manual_merge_items'


class ManualMergeItemsMixin(object):
    def get_form_kwargs(self):
        kwargs = super(ManualMergeItemsMixin, self).get_form_kwargs()
        kwargs['items'] = self.kwargs['pks'].split('_')
        return kwargs

    def form_valid(self, form):
        self.item = form.merge()
        return super(ManualMergeItemsMixin, self).form_valid(form)

    def get_success_url(self):
        return reverse('display-item', args=[self.item_type, self.item.pk])


class PersonManualMergeItems(
        ManualMergeItemsMixin, IshtarMixin,
        LoginRequiredMixin, FormView):
    form_class = forms.PersonMergeIntoForm
    template_name = 'ishtar/form.html'
    page_name = _(u"Select the main person")
    current_url = 'person-manual-merge-items'
    item_type = 'person'


class OrgaManualMerge(ManualMergeMixin, IshtarMixin, LoginRequiredMixin,
                      FormView):
    form_class = forms.OrgaMergeFormSelection
    template_name = 'ishtar/form.html'
    page_name = _(u"Merge organization")
    current_url = 'orga-manual-merge'
    redir_url = 'orga_manual_merge_items'


class OrgaManualMergeItems(
        ManualMergeItemsMixin, IshtarMixin,
        LoginRequiredMixin, FormView):
    form_class = forms.OrgaMergeIntoForm
    template_name = 'ishtar/form.html'
    page_name = _(u"Select the main organization")
    current_url = 'orga-manual-merge-items'
    item_type = 'organization'


class OrganizationCreate(LoginRequiredMixin, CreateView):
    model = models.Organization
    form_class = forms.BaseOrganizationForm
    template_name = 'ishtar/organization_form.html'
    form_prefix = "orga"

    def get_form_kwargs(self):
        kwargs = super(OrganizationCreate, self).get_form_kwargs()
        if hasattr(self.form_class, 'form_prefix'):
            kwargs.update({'prefix': self.form_class.form_prefix})
        return kwargs

    def get_success_url(self):
        return reverse('organization_edit', args=[self.object.pk])


class OrganizationEdit(LoginRequiredMixin, UpdateView):
    model = models.Organization
    form_class = forms.BaseOrganizationForm
    template_name = 'ishtar/organization_form.html'

    def get_form_kwargs(self):
        kwargs = super(OrganizationEdit, self).get_form_kwargs()
        if hasattr(self.form_class, 'form_prefix'):
            kwargs.update({'prefix': self.form_class.form_prefix})
        return kwargs

    def get_success_url(self):
        return reverse('organization_edit', args=[self.object.pk])


class OrganizationPersonCreate(LoginRequiredMixin, CreateView):
    model = models.Person
    form_class = forms.BaseOrganizationPersonForm
    template_name = 'ishtar/organization_person_form.html'
    relative_label = _("Corporation manager")

    def get_context_data(self, *args, **kwargs):
        data = super(OrganizationPersonCreate, self).get_context_data(*args,
                                                                      **kwargs)
        data['relative_label'] = self.relative_label
        return data

    def get_success_url(self):
        return reverse('organization_person_edit', args=[self.object.pk])


class OrganizationPersonEdit(LoginRequiredMixin, UpdateView):
    model = models.Person
    form_class = forms.BaseOrganizationPersonForm
    template_name = 'ishtar/organization_person_form.html'
    relative_label = _("Corporation manager")

    def get_context_data(self, *args, **kwargs):
        data = super(OrganizationPersonEdit, self).get_context_data(*args,
                                                                    **kwargs)
        data['relative_label'] = self.relative_label
        return data

    def get_success_url(self):
        return reverse('organization_person_edit', args=[self.object.pk])


# documents

show_document = show_item(models.Document, 'document')
get_document = get_item(models.Document, 'get_document', 'document',
                        search_form=forms.DocumentSelect)
display_document = display_item(models.Document)


document_search_wizard = wizards.DocumentSearch.as_view(
    [('selec-document_search', forms.DocumentFormSelection)],
    label=_(u"Document: search"),
    url_name='search-document',
)


class DocumentFormMixin(IshtarMixin, LoginRequiredMixin):
    form_class = forms.DocumentForm
    template_name = 'ishtar/forms/document.html'
    model = models.Document

    def get_success_url(self):
        return reverse('display-document', args=[self.object.pk])


class DocumentCreateView(DocumentFormMixin, CreateView):
    page_name = _(u"Document creation")

    def get_form_kwargs(self):
        kwargs = super(DocumentCreateView, self).get_form_kwargs()
        initial = kwargs.get('initial', {})
        for related_key in models.Document.RELATED_MODELS_ALT:
            model = models.Document._meta.get_field(related_key).related_model
            if model.SLUG in self.request.GET:
                try:
                    item = model.objects.get(pk=self.request.GET[model.SLUG])
                except model.DoesNotExist:
                    continue
                initial[related_key] = str(item.pk)
        if initial:
            kwargs['initial'] = initial
        return kwargs


class DocumentSelectView(IshtarMixin, LoginRequiredMixin,
                         FormView):
    form_class = forms.DocumentFormSelection
    template_name = 'ishtar/form.html'
    redir_url = 'edit-document'

    def form_valid(self, form):
        self.pk = form.cleaned_data['pk']
        return super(DocumentSelectView, self).form_valid(form)

    def get_success_url(self):
        return reverse(self.redir_url, args=[self.pk])


class DocumentEditView(DocumentFormMixin, UpdateView):
    page_name = _(u"Document modification")

    def get_form_kwargs(self):
        kwargs = super(DocumentEditView, self).get_form_kwargs()
        try:
            document = models.Document.objects.get(pk=self.kwargs.get('pk'))
            assert check_permission(self.request, 'document/edit', document.pk)
        except (AssertionError, models.Document.DoesNotExist):
            raise Http404()
        initial = {}
        for k in list(self.form_class.base_fields.keys()) + \
                models.Document.RELATED_MODELS:
            value = getattr(document, k)
            if hasattr(value, 'all'):
                value = ",".join([str(v.pk) for v in value.all()])
            if hasattr(value, 'pk'):
                value = value.pk
            initial[k] = value
        # main image initialisation
        if document.image:
            kwargs["main_items_fields"] = {}
            for k in models.Document.RELATED_MODELS:
                kwargs["main_items_fields"][k] = []
                for related_item in getattr(document, k).all():
                    key = "{}_{}_main_image".format(k, related_item.pk)
                    kwargs["main_items_fields"][k].append(
                        (key, u"{} - {}".format(
                            _(u"Main image for"), related_item)))
                    if related_item.main_image == document:
                        initial[key] = True
        kwargs['initial'] = initial
        return kwargs


document_deletion_steps = [
    ('selec-document_deletion', forms.DocumentFormSelection),
    ('final-document_deletion', FinalDeleteForm)
]

document_deletion_wizard = wizards.DocumentDeletionWizard.as_view(
    document_deletion_steps,
    label=_(u"Document deletion"),
    url_name='document_deletion',)


def get_bookmark(request, pk):
    try:
        sq = models.SearchQuery.objects.get(
            pk=pk,
            profile__person__ishtaruser__user_ptr=request.user)
    except models.SearchQuery.DoesNotExist:
        raise Http404()
    slug = sq.content_type.model_class().SLUG
    return redirect(
        reverse(slug + '_search') + "?bookmark={}".format(sq.pk)
    )


def gen_generate_doc(model):

    def func(request, pk, template_pk=None):
        if not request.user.has_perm('view_' + model.SLUG, model):
            return HttpResponse(content_type='text/plain')
        try:
            item = model.objects.get(pk=pk)
            doc = item.publish(template_pk)
        except model.DoesNotExist:
            doc = None
        if doc:
            MIMES = {'odt': 'application/vnd.oasis.opendocument.text',
                     'ods': 'application/vnd.oasis.opendocument.spreadsheet'}
            ext = doc.split('.')[-1]
            doc_name = item.get_filename() + "." + ext
            mimetype = 'text/csv'
            if ext in MIMES:
                mimetype = MIMES[ext]
            with open(doc, "rb") as d:
                response = HttpResponse(d, content_type=mimetype)
                response['Content-Disposition'] = 'attachment; filename=%s' % \
                                                  doc_name
                return response
        return HttpResponse(content_type='text/plain')
    return func


class SearchQueryMixin(object):
    """
    Manage content type and profile init
    """
    def dispatch(self, request, *args, **kwargs):
        if not request.user.pk:
            raise Http404()
        try:
            self.profile = models.UserProfile.objects.get(
                current=True, person__ishtaruser__user_ptr=request.user)
        except models.UserProfile.DoesNotExist:
            # no current profile
            raise Http404()
        self.app_label = kwargs.get('app_label')
        self.model = kwargs.get('model')
        model = self.model
        if model == 'site':
            model = 'archaeologicalsite'
        try:
            self.content_type = ContentType.objects.get(
                app_label=self.app_label.replace('-', '_'),
                model=model.replace('-', '_')
            )
        except ContentType.DoesNotExist:
            raise Http404()
        return super(SearchQueryMixin, self).dispatch(request, *args, **kwargs)


class SearchQueryEdit(SearchQueryMixin, LoginRequiredMixin, FormView):
    template_name = 'ishtar/forms/search_query.html'
    form_class = forms.SearchQueryForm

    def get_form_kwargs(self):
        kwargs = super(SearchQueryEdit, self).get_form_kwargs()
        kwargs['profile'] = self.profile
        kwargs['content_type'] = self.content_type
        return kwargs

    def get_context_data(self, **kwargs):
        data = super(SearchQueryEdit, self).get_context_data(**kwargs)
        data['app_label'] = self.app_label
        data['model'] = self.model
        return data

    def form_valid(self, form):
        form.save()
        return HttpResponseRedirect(self.get_success_url())

    def get_success_url(self):
        return reverse('success', args=['bookmark'])


class BookmarkList(SearchQueryMixin, JSONResponseMixin, LoginRequiredMixin,
                   TemplateView):
    def get_data(self, context):
        q = models.SearchQuery.objects.filter(
            content_type=self.content_type,
            profile=self.profile
        )
        return {
            'bookmarks': [
                {'label': sq.label, 'query': sq.query,
                 'id': sq.id} for sq in q.all()]
        }


class SearchQueryDelete(LoginRequiredMixin, DeleteView):
    model = models.SearchQuery
    template_name = 'ishtar/forms/bookmark_delete.html'
    page_name = _(u"Delete bookmark")

    def dispatch(self, request, *args, **kwargs):
        if not request.user.pk:
            raise Http404()
        try:
            self.profile = models.UserProfile.objects.get(
                current=True, person__ishtaruser__user_ptr=request.user)
        except models.UserProfile.DoesNotExist:
            # no current profile
            raise Http404()
        try:
            self.search_query = models.SearchQuery.objects.get(
                profile=self.profile,
                pk=kwargs['pk']
            )
        except models.SearchQuery.DoesNotExist:
            raise Http404()
        return super(SearchQueryDelete, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        data = super(SearchQueryDelete, self).get_context_data(**kwargs)
        data['modal_size'] = "small"
        data['page_name'] = _(u"Bookmark - Delete")
        data['action_name'] = _(u"Delete")
        data['item'] = self.search_query.label
        data['url'] = reverse('bookmark-delete', args=[self.search_query.pk])
        return data

    def get_success_url(self):
        return reverse('success', args=['bookmark'])


class AlertList(JSONResponseMixin, LoginRequiredMixin,
                TemplateView):
    def dispatch(self, request, *args, **kwargs):
        if not request.user.pk:
            raise Http404()
        try:
            self.profile = models.UserProfile.objects.get(
                current=True, person__ishtaruser__user_ptr=request.user)
        except models.UserProfile.DoesNotExist:
            # no current profile
            raise Http404()
        return super(AlertList, self).dispatch(request, *args, **kwargs)

    def get_data(self, context):
        q = models.SearchQuery.objects.filter(
            profile=self.profile,
            is_alert=True
        )
        alerts = []
        for sq in q.all():
            model = sq.content_type.model_class()
            module = model.__module__.split('.')[0]
            views = importlib.import_module(module + '.views')
            get_view = getattr(views, "get_" + model.SLUG)
            nb = get_view(
                self.request,
                query={'search_vector': sq.query},
                count=True
            )
            alerts.append(
                {'label': sq.label, 'query_id': sq.pk,
                 'number': nb}
            )
        return {'alerts': alerts}


class QAItemForm(IshtarMixin, LoginRequiredMixin, FormView):
    template_name = 'ishtar/forms/qa_form.html'
    model = None
    form_class = None
    page_name = u""
    success_url = "/success/"
    modal_size = None  # large, small or None (medium)

    def get_quick_action(self):
        raise NotImplementedError()

    def dispatch(self, request, *args, **kwargs):
        assert self.model
        pks = [int(pk) for pk in kwargs.get('pks').split('-')]
        self.items = list(self.model.objects.filter(pk__in=pks))
        if not self.items:
            raise Http404()

        # check availability
        quick_action = self.get_quick_action()
        if not quick_action.is_available(
                user=request.user, session=request.session):
            for item in self.items:
                if not quick_action.is_available(
                        user=request.user, session=request.session, obj=item):
                    raise Http404()

        self.url = request.get_full_path()
        return super(QAItemForm, self).dispatch(request, *args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(QAItemForm, self).get_form_kwargs()
        kwargs['items'] = self.items
        return kwargs

    def get_context_data(self, **kwargs):
        data = super(QAItemForm, self).get_context_data(**kwargs)
        data['url'] = self.url
        data['items'] = self.items
        data['modal_size'] = self.modal_size
        data['page_name'] = u"{} &ndash; {}".format(
            self.model._meta.verbose_name, self.page_name)
        return data


class QAItemEditForm(QAItemForm):
    form_class_multi = None
    modal_size = "large"

    def get_quick_action(self):
        return self.model.QA_EDIT

    def dispatch(self, request, *args, **kwargs):
        self.confirm = kwargs.get('confirm', False) and True
        returned = super(QAItemEditForm, self).dispatch(request, *args,
                                                        **kwargs)
        return returned

    def get_form_class(self):
        if len(self.items) > 1 and self.form_class_multi:
            return self.form_class_multi
        return self.form_class

    def get_form_kwargs(self):
        kwargs = super(QAItemEditForm, self).get_form_kwargs()
        kwargs['confirm'] = self.confirm
        return kwargs

    def get_context_data(self, **kwargs):
        data = super(QAItemEditForm, self).get_context_data(**kwargs)
        data['page_name'] = u"{} &ndash; {}".format(
            self.model._meta.verbose_name, self.model.QA_EDIT.text)
        if self.confirm:
            if 'confirm' not in self.url:
                data['url'] = self.url.split('?')[0] + "confirm/"
            data['confirm'] = True
            data['action_name'] = _(u"Confirm")
        return data

    def form_valid(self, form):
        if not self.confirm:
            self.confirm = True
            return self.render_to_response(
                self.get_context_data(form=self.get_form()))
        return self.form_save(form)

    def form_save(self, form):
        form.save(self.items, self.request.user)
        return HttpResponseRedirect(reverse("success"))


class QAPersonForm(QAItemEditForm):
    model = models.Person
    form_class = forms.QAPersonFormMulti


class QADocumentForm(QAItemEditForm):
    model = models.Document
    form_class = forms.QADocumentFormMulti