aboutsummaryrefslogtreecommitdiffstats
path: root/server/views.py
blob: e264b68e64ef140e95994bace82b7e34637b7592 (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
import re
from django.shortcuts import render
from client.models import UserCode, Keys, DocumentType, Document, KeyVal
from django.conf import settings
from django.core.mail import send_mail
from client.models import GroupSettings, UserCode, Keys, DocumentType, Document, KeyVal
from django.contrib.auth.models import Group, Permission, User
from django.db.models import Q
from django.http import HttpResponseRedirect, FileResponse, HttpResponse
from django.db.models.deletion import ProtectedError
from django.template.loader import get_template
from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import user_passes_test
from django.contrib.sessions.backends.db import SessionStore

import csv
import dateparser
from datetime import datetime
from datetime import timedelta
import pytz
import pdfkit
from io import BytesIO
import base64
from PIL import Image, UnidentifiedImageError
import zipfile
import json
import threading

# custom staff check function for non primary group staff members
def isStaff(user):
    if user.is_staff:
        return True
    if user.has_perm("client.staff"):
        return True
    return False

# function to check if "aggiunto" has permission to view documents
def isCapi_enabled(user):
    groups = user.groups.values_list('name', flat=True)
    group = groups[0]
    settings = GroupSettings.objects.filter(group__name=group)
    if len(settings) != 0 and "capi" in groups:
        return settings[0].view_documents
    else:
        return False

# function to get group list based on permissions of user
def getGroups(request):
    user = request.user
    if user.is_staff:
        groups = list(user.groups.all())
    else:
        groups = list(user.groups.all())[1:]

    if user.is_superuser and request.session.get("superuser"):
        groups = list(Group.objects.all())
        if "superuser_group" in request.session:
            su_group = Group.objects.get(name=request.session["superuser_group"])
            if su_group in groups:
                groups.remove(su_group)
            groups = [su_group] + groups

    return groups

@user_passes_test(isStaff)
def index(request):
    context = {}

    groups = getGroups(request)

    q_obj = Q(group__in=groups)

    doc_types = DocumentType.objects.filter(q_obj & Q(enabled=True)).order_by("-id")

    # check for settings
    group_check = []
    for i in groups:
        if i.name == "capi":
            continue

        doc_view_check = ""
        settings = GroupSettings.objects.filter(group=i)

        # create settings if non existing
        if len(settings) == 0:
            settings = GroupSettings(group=i, view_documents=False)
        else:
            settings = settings[0]

        if settings.view_documents:
            doc_view_check = 'checked="checked"'
        
        group_check.append([i.name, doc_view_check])

    # check if changing settings
    if request.method == "POST" and request.user.is_staff:
        if request.user.is_superuser and "su_status" in request.POST:
            action = request.POST["su_status"]
            if action == "change":
                if "superuser" not in request.session:
                    request.session["superuser"] = True
                else:
                    request.session["superuser"] = not request.session["superuser"]

                if "superuser_group" not in request.session:
                    request.session["superuser_group"] = "reparto"
            elif action in ["diga", "muta", "reparto", "posto", "clan"]:
                request.session["superuser_group"] = action

            return HttpResponseRedirect("/server")

        for i in groups:
            settings = GroupSettings.objects.filter(group=i)

            # create settings if non existing
            if len(settings) == 0:
                settings = GroupSettings(group=i, view_documents=False)
            else:
                settings = settings[0]

            if i.name in request.POST:
                settings.view_documents = True
                settings.save()
            else:
                settings.view_documents = False
                settings.save()

        return HttpResponseRedirect("/server")

    context = {
        'docs': doc_types,
        'groups': group_check,
        'doc_view_check': doc_view_check,
    }

    return render(request, 'server/index.html', context)


@staff_member_required
def uapprove(request):
    context = {}
    data = []
    if request.method == "POST":
        # get group name and obj
        group = getGroups(request)[0]

        # parse text to array
        data = request.POST["codes"]
        data = split_codes(data)
        # check if format is right
        for i in range(len(data)):
            if not data[i].startswith("U"):
                data[i] = data[i] + " - Formato errato"
            elif not data[i][1:].isdigit():
                data[i] = data[i] + " - Formato errato"
            elif int(data[i][1:]) < 100000 or int(data[i][1:]) > 999999:
                data[i] = data[i] + " - Formato errato"
            elif UserCode.objects.filter(code=data[i][1:]).count() == 0:
                data[i] = data[i] + " - Invalido"
            else:
                usercode = UserCode.objects.filter(code=data[i][1:])[0]
                # if user not in any group add to the same group as staff
                if usercode.branca == None:
                    usercode.branca = group
                    usercode.save()
                    data[i] = data[i] + " - Ok"
                elif usercode.branca == group:
                    data[i] = data[i] + " - Già " + group.name
                else:
                    usercode.branca = group
                    usercode.save()
                    data[i] = data[i] + " - Ok, cambio branca"

    context = {
        'messages': data,
        'empty': len(data) == 0,
    }

    return render(request, 'server/approve_user.html', context)

def split_codes(str):
    out = []
    buffer = ""
    for i in str:
        if i.isdigit() or i == "U":
            buffer += i
            continue
        
        if i == "\n":
            out.append(buffer)
            buffer = ""

    if buffer != "":
        out.append(buffer)

    return out

@user_passes_test(isStaff)
def docapprove(request):
    context = {}
    data = []

    groups = getGroups(request)

    # setup variables for error text and success text
    error = False
    success = False
    error_text = ""
    success_text = ""

    document = None
    messages = []

    if request.method == "POST":
        # check if bulk approve or single
        if "codes" in request.POST:
            # parse text in array
            data = request.POST["codes"]
            data = split_codes(data)
            # check if code valid
            for i in range(len(data)):
                if not data[i].isdigit():
                    messages.append(data[i] + " - Formato errato")
                elif int(data[i]) < 100000 or int(data[i]) > 999999:
                    messages.append(data[i] + " - Formato errato")
                elif Document.objects.filter(code=data[i]).count() == 0:
                    messages.append(data[i] + " - Invalido")
                elif Document.objects.filter(code=data[i])[0].group not in groups:
                    # check if user has permission to approve document
                    messages.append(data[i] + " - Invalido")
                else:
                    document = Document.objects.filter(code=data[i])[0]

                    if document.group not in groups:
                        return

                    if document.status != 'wait' and document.status != 'ok':
                        return

                    if document.status == 'ok':
                        # do nothing document already approved
                        messages.append(data[i] + " - Già approvato")
                    else:
                        document.status = 'ok'
                        document.save()
                        messages.append(data[i] + " - Ok")

        elif "code" in request.POST:
            print("doing this")
            data = request.POST["code"]
            if not data.isdigit():
                error_text = "Formato codice errato"
                error = True
            elif int(data) < 100000 or int(data) > 999999:
                error_text = "Formato codice errato"
                error = True
            elif Document.objects.filter(code=data).count() == 0:
                error_text = "Codice invalido"
                error = True
            elif Document.objects.filter(code=data)[0].group not in groups:
                error_text = "Codice invalido"
                error = True
            else:
                # get document
                document = Document.objects.filter(code=data)[0]

                if document.group not in groups:
                    return

                if document.status != 'wait' and document.status != 'ok':
                    return

                # prepare success message
                if document.status == 'ok':
                    success_text = "File caricato"
                    success = True
                else:
                    document.status = 'ok'
                    document.save()
                    success_text = "Documento approvato e file caricato"
                    success = True

                # check for errors and upload files
                if "doc_sign" in request.FILES and not error:
                    myfile = request.FILES['doc_sign']
                    try:
                        im = Image.open(myfile)
                        im_io = BytesIO()
                        # compress image in WEBP
                        im.save(im_io, 'WEBP', quality=50)
                        document.signed_doc.save(data+"_"+myfile.name, im_io)
                        document.save()
                    except UnidentifiedImageError:
                        error = True
                        error_text = "Il file non è un immagine valida"
                else:
                    error = True
                    error_text = "Prego caricare un file"

    context = {
        'messages': messages,
        'empty': len(messages) == 0,
        "error": error,
        "error_text": error_text,
        "success": success,
        "success_text": success_text,
    }

    return render(request, 'server/approve_doc.html', context)

@staff_member_required
def approve_direct(request):
    # get groups that the user is manager of
    groups = getGroups(request)

    if request.method == "POST" and "doc_code" in request.POST:
        # if user submitted the form to approve a document
        doc_code = request.POST["doc_code"]
        if doc_code.isdigit():
            doc_code = int(doc_code)
        else:
            doc_code = -1

        document = Document.objects.filter(code=doc_code)

        # user modified manually the code
        if len(document) != 1:
            return

        document = document[0]

        # user modified the code to an invalid document
        if document.status != "wait":
            return

        # check if user has permission to approve document
        if document.group not in groups:
            return

        document.status = "ok"
        document.save()
        return render(request, 'server/approve_doc_direct.html', {"doc": document, "success": True})

    # if the user just opened the page
    if "code" in request.GET:
        doc_code = request.GET["code"]
        if doc_code.isdigit():
            doc_code = int(doc_code)
        else:
            doc_code = -1
    else:
        return render(request, 'server/approve_doc_direct.html', {"error": -1})

    document = Document.objects.filter(code=doc_code)

    if len(document) != 1:
        return render(request, 'server/approve_doc_direct.html', {"error": "Codice del documento invalido, riscansionare il codice"})

    document = document[0]

    if document.status != "wait":
        return render(request, 'server/approve_doc_direct.html', {"error": "Questo documento non è in attesa di approvazione"})

    # check if user has permission to approve document
    if document.group not in groups:
        return render(request, 'server/approve_doc_direct.html', {"error": "Non hai il permesso di approvare questo documento"})

    return render(request, 'server/approve_doc_direct.html', {"doc": document})

@staff_member_required
def ulist(request):
    context = {}
    # group name and obj
    groups = getGroups(request)

    if request.method == "POST":
        # request to remove uc from branca
        if request.POST["action"][0] == 'd':
            uc = UserCode.objects.get(id=request.POST["action"][1:])

            # check if user has permission to remove usercode
            if uc.branca not in groups:
                return

            uc.branca = None
            uc.save()

        # request to download document
        elif request.POST["action"][0] == 'f':
            document = Document.objects.get(id=request.POST["action"][1:])
            # check if user has permission to view document
            if document.group == groups[0]:
                vac_file = ""
                health_file = ""
                sign_doc_file = ""

                # prepare pictures in base64
                if document.medical_data:
                    if document.medical_data.vac_certificate.name:
                        with open(document.medical_data.vac_certificate.name, 'rb') as image_file:
                            vac_file = base64.b64encode(
                                image_file.read()).decode()

                    if document.medical_data.health_care_certificate.name:
                        with open(document.medical_data.health_care_certificate.name, 'rb') as image_file:
                            health_file = base64.b64encode(
                                image_file.read()).decode()
                if document.signed_doc:
                    with open(document.signed_doc.name, 'rb') as image_file:
                        sign_doc_file = base64.b64encode(
                            image_file.read()).decode()

                # get template and build context
                template = get_template('server/download_doc.html')
                doc = [document, KeyVal.objects.filter(
                    container=document), document.personal_data, document.medical_data, document.user.groups.values_list('name', flat=True)[0]]
                context = {'doc': doc, 'vac': vac_file,
                           'health': health_file, 'sign_doc_file': sign_doc_file}
                # render context
                html = template.render(context)
                # render pdf using wkhtmltopdf
                pdf = pdfkit.from_string(html, False)
                result = BytesIO(pdf)
                result.seek(0)
                return FileResponse(result, filename=document.usercode.first_name+"_"+document.usercode.last_name+"_"+document.document_type.name+".pdf")

    # list users with their documents
    usercodes = UserCode.objects.filter(branca__in=groups).select_related("user", "medic").order_by("last_name")

    vac_file = ["/server/media/", "/vac_certificate/usercode"]
    health_file = ["/server/media/", "/health_care_certificate/usercode"]

    context = {
        'users': usercodes,
        'vac_file': vac_file,
        'health_file': health_file,
    }
    return render(request, 'server/user_list.html', context)

@staff_member_required
def ulist_table(request):
    context = {}
    # group name and obj
    groups = getGroups(request)

    # list users with their documents
    usercodes = UserCode.objects.filter(branca__in=groups).select_related("user", "medic").order_by("user__last_name")

    vac_file = ["/server/media/", "/vac_certificate/usercode"]
    health_file = ["/server/media/", "/health_care_certificate/usercode"]

    context = {
        'users': usercodes,
        'vac_file': vac_file,
        'health_file': health_file,
    }
    return render(request, 'server/user_list_table.html', context)


@user_passes_test(isStaff)
def doctype(request):
    context = {}

    # error variables to throw at user
    error = False
    error_text = ""

    # init checkboxes
    public = True
    selfsign = True
    hidden = False
    personal = True
    medic = True
    custom = True
    message = True
    group_bool = True
    public_check = 'checked="checked"'
    selfsign_check = 'checked="checked"'
    hidden_check = 'checked="checked"'
    personal_check = 'checked="checked"'
    medic_check = 'checked="checked"'
    custom_check = 'checked="checked"'
    message_check = 'checked="checked"'
    group_check = 'checked="checked"'

    # if user not staff of primary get only non primary groups
    groups = getGroups(request)

    if request.method == "POST":
        # check if request to edit
        if request.POST["action"][0] == 'e':
            document_type = DocumentType.objects.get(id=request.POST["action"][1:])

            # check if user has permission on the document
            if document_type.group not in groups:
                return

            enabled_check = 'checked="checked"' if document_type.enabled else ""
            sign_check = 'checked="checked"' if not document_type.auto_sign else ""
            custom_message_check = 'checked="checked"' if document_type.custom_message else ""
            staff_only_check = 'checked="checked"' if document_type.staff_only else ""
            private_check = 'checked="checked"' if document_type.group_private else ""

            context = {
                'doc': document_type,
                "group": document_type.group.name,
                "enabled_check": enabled_check,
                "private_check": private_check,
                "sign_check": sign_check,
                "staff_only_check": staff_only_check,
                "custom_message_check": custom_message_check,
            }

            return docedit_wrapper(request, context)

        # check if request to download
        elif request.POST["action"][0] == 'p':
            document_type = DocumentType.objects.get(id=request.POST["action"][1:])

            # check if user has permission on the document
            if document_type.group not in groups:
                return

            docs = Document.objects.filter(document_type=document_type).select_related("personal_data", "medical_data", "user")
            
            # get time for filename
            current_time = datetime.strftime(datetime.now(), "%H_%M__%d_%m_%y")

            response = HttpResponse()
            response['Content-Disposition'] = 'attachment; filename="' + document_type.name.replace(' ', '_') + '_export_' + current_time + '.csv"'

            writer = csv.writer(response)

            # csv header
            header = ["Nome", "Cognome", "Email", "Branca", "Capo", "Stato", "Data di compilazione"]
            if document_type.personal_data:
                header += ["Nome dei genitori", "Via", "CAP", "Comune", "Nazionalita", "Data di nascita", "Telefono di casa", "Telefono", "Scuola", "Anno scolastico", "Numero AVS"]

            keys = []
            if document_type.custom_data:
                keys = Keys.objects.filter(container=document_type).values_list("key", flat=True)
                header += keys

            writer.writerow(header)

            for doc in docs:
                capo = "no"
                if "capi" in doc.user.groups.values_list('name', flat=True) or doc.user.is_staff:
                    capo = "si"

                write_data = [
                    doc.user.first_name,
                    doc.user.last_name,
                    doc.user.email,
                    doc.user.groups.values_list('name', flat=True)[0],
                    capo,
                    doc.status,
                    doc.compilation_date,
                ]
                if document_type.personal_data:
                    write_data += [
                        doc.personal_data.parent_name,
                        doc.personal_data.via,
                        doc.personal_data.cap,
                        doc.personal_data.country,
                        doc.personal_data.nationality,
                        doc.personal_data.born_date,
                        doc.personal_data.home_phone,
                        doc.personal_data.phone,
                        doc.personal_data.school,
                        doc.personal_data.year,
                        doc.personal_data.avs_number
                    ]

                if document_type.custom_data:
                    # add empty cell if no keyval present
                    keyvals = KeyVal.objects.filter(container=doc).values_list("key", "value")
                    for key in keys:
                        for keyval in keyvals:
                            if keyval[0] == key:
                                write_data.append(keyval[1])
                                break
                        else:
                            write_data.append("")

                writer.writerow(write_data)

            return response

        #check if request to download with medic data
        elif request.POST["action"][0] == 'm':
            document_type = DocumentType.objects.get(id=request.POST["action"][1:])

            # check if user has permission on the document
            if document_type.group not in groups:
                return

            docs = Document.objects.filter(document_type=document_type)
            
            # get time for filename
            current_time = datetime.strftime(datetime.now(), "%H_%M__%d_%m_%y")

            response = HttpResponse()
            response['Content-Disposition'] = 'attachment; filename="' + document_type.name.replace(' ', '_') + '_export_medic_' + current_time + '.csv"'

            writer = csv.writer(response)

            # csv header
            header = ["Nome", "Cognome", "Email", "Branca", "Capo", "Stato", "Data di compilazione", "Nome dei genitori", "Via", "CAP", "Comune", "Nazionalita", "Data di nascita", "Telefono di casa", "Telefono", "Scuola", "Anno scolastico", "Numero AVS", "Contatto d'emergenza", "Parentela del contatto", "Telefono d'emergenza", "Cellulare emergenza", "Indirizzo completo emergenza", "Cassa malati", "Ass. Infortuni", "Ass. RC", "Socio REGA", "Nome del medico", "Telefono medico", "Indirizzo medico", "Malattie", "Vacinazioni", "Data antitetanica", "Allergie", "Assume medicamenti", "Medicamenti", "Informazioni particolari", "Informazioni"]

            if document_type.custom_data:
                header += Keys.objects.filter(container=document_type).values_list("key", flat=True)

            writer.writerow(header)

            for doc in docs:
                capo = "no"
                if "capi" in doc.user.groups.values_list('name', flat=True) or doc.user.is_staff:
                    capo = "si"

                write_data = [
                        doc.user.first_name,
                        doc.user.last_name,
                        doc.user.email,
                        doc.user.groups.values_list('name', flat=True)[0],
                        capo,
                        doc.status,
                        doc.compilation_date,
                        doc.personal_data.parent_name,
                        doc.personal_data.via,
                        doc.personal_data.cap,
                        doc.personal_data.country,
                        doc.personal_data.nationality,
                        doc.personal_data.born_date,
                        doc.personal_data.home_phone,
                        doc.personal_data.phone,
                        doc.personal_data.school,
                        doc.personal_data.year,
                        doc.personal_data.avs_number
                ]
                if doc.medical_data:
                    write_data += [
                        doc.medical_data.emer_name,
                        doc.medical_data.emer_relative,
                        doc.medical_data.emer_phone,
                        doc.medical_data.cell_phone,
                        doc.medical_data.address,
                        doc.medical_data.health_care,
                        doc.medical_data.injuries,
                        doc.medical_data.rc,
                        doc.medical_data.rega,
                        doc.medical_data.medic_name,
                        doc.medical_data.medic_phone,
                        doc.medical_data.medic_address,
                        doc.medical_data.sickness,
                        doc.medical_data.vaccine,
                        doc.medical_data.tetanus_date,
                        doc.medical_data.allergy,
                        doc.medical_data.drugs_bool,
                        doc.medical_data.drugs,
                        doc.medical_data.misc_bool,
                        doc.medical_data.misc
                    ]

                if document_type.custom_data:
                    write_data += KeyVal.objects.filter(container=doc).values_list("value", flat=True)

                writer.writerow(write_data)

            return response

        # list all selected types
        for i in request.POST.keys():
            if i.isdigit():
                docc = DocumentType.objects.get(id=i)
                # check if user has permission
                if docc.group in groups:
                    # execute action
                    if request.POST["action"] == 'delete':
                        try:
                            docc.delete()
                        except ProtectedError:
                            error = True
                            error_text = "Non puoi eliminare un tipo a cui é collegato uno o piú documenti"
                    elif request.POST["action"] == 'hide':
                        docc.enabled = False
                        docc.save()
                    elif request.POST["action"] == 'show':
                        docc.enabled = True
                        docc.save()
                else:
                    error = True
                    error_text = "Non puoi modificare un documento non del tuo gruppo"

        # check which filters are applied
        public = "filter_public" in request.POST
        selfsign = "filter_selfsign" in request.POST
        hidden = "filter_hidden" in request.POST
        personal = "filter_personal" in request.POST
        medic = "filter_medic" in request.POST
        custom = "filter_custom" in request.POST
        message = "filter_message" in request.POST
        group_bool = "filter_group" in request.POST

        # check if request to clear filters
        if request.POST["action"] == 'clear':
            public = True
            selfsign = True
            hidden = False
            personal = True
            medic = True
            custom = True
            message = True
            group_bool = True

    # get documents from the list
    q_obj = Q(group__in=groups)

    public_types = DocumentType.objects.filter(q_obj)

    # apply filters
    if not public:
        public_types = public_types.filter(group_private=True)
        public_check = ""
    if not selfsign:
        public_types = public_types.filter(auto_sign=False)
        selfsign_check = ""
    if not hidden:
        public_types = public_types.filter(enabled=True)
        hidden_check = ""
    if not personal:
        public_types = public_types.filter(personal_data=False)
        personal_check = ""
    if not medic:
        public_types = public_types.filter(medical_data=False)
        medic_check = ""
    if not custom:
        public_types = public_types.filter(custom_data=False)
        custom_check = ""
    if not message:
        public_types = public_types.filter(custom_message=False)
        message_check = ""
    if not group_bool:
        public_types = public_types.filter(custom_group=False)
        group_check = ""

    context = {
        'docs': public_types,
        'public_check': public_check,
        'selfsign_check': selfsign_check,
        'hidden_check': hidden_check,
        'personal_check': personal_check,
        'medic_check': medic_check,
        'custom_check': custom_check,
        'message_check': message_check,
        'group_check': group_check,
        'error': error,
        'error_text': error_text,
    }
    return render(request, 'server/doc_type.html', context)

@user_passes_test(isStaff)
def custom_parameters_preview(request):
    context = {}
    if "param" not in request.GET:
        return render(request, 'server/doc_creation_preview.html', context)

    params = request.GET["param"]
    params = base64.b64decode(params).decode("utf-8")
    params = params.splitlines()
    keys = []
    for i in range(len(params)):
        dic = {}
        val = params[i]
        if val.startswith("!"):
            if len(val) >= 3:
                val = val[3:].split(",")[0]

        dic["key"] = val
        dic["key_extra"] = params[i]
        dic["id"] = i
        keys.append(dic)

    context["keys"] = keys
    return render(request, 'server/doc_creation_preview.html', context)

@user_passes_test(isStaff)
def doccreate(request):
    context = {}

    groups = getGroups(request)
    # if user is not staff of primary set default group to secondary and default public type
    if request.user.is_staff:
        group_private = False
        private_check = 'checked="checked"'
    else:
        group_private = True
        private_check = ''

    # get group obj
    group = groups[0]

    # init checkboxes
    enabled = False
    personal_data = False
    medical_data = False
    custom_data = False
    custom_group_bool = False
    staff_only = False
    name = ""
    custom_group = ""
    max_instances = 0

    enabled_check = 'checked="checked"'
    personal_check = 'checked="checked"'
    sign_check = 'checked="checked"'
    medical_check = ""
    custom_check = ""
    custom_message_check = ""
    staff_only_check = ""

    # if type create request sent
    if request.method == "POST":
        # gather inserted data
        enabled = "enabled" in request.POST.keys()
        auto_sign = "sign" not in request.POST.keys()
        group_private = "group_private" in request.POST.keys()
        personal_data = "personal_data" in request.POST.keys()
        medical_data = "medical_data" in request.POST.keys()
        custom_data = "custom_data" in request.POST.keys()
        custom_message = "custom_message" in request.POST.keys()
        staff_only = "staff_only" in request.POST.keys()
        custom_message_text = request.POST["custom_message_text"]

        # parse name with icons
        name = request.POST["name"]
        if "<" in name:
            context["error"] = "true"
            context["error_text"] = "Il nome non puo' contenere il carattere <"
            return render(request, 'server/doc_create.html', context)

        reg = r'\{[\s\S]*\}'
        name_split = re.split(reg, name)
        name_matches = re.findall(reg, name)
        name_matches = list(map(lambda x: "<i class='material-icons'>" + x[1:len(x)-1] + "</i>", name_matches)) + [""]
        name_arr = [val for pair in zip(name_split, name_matches) for val in pair]
        name = "".join(name_arr)

        custom_group = request.POST["custom_group"]

        if request.POST["max_instances"]:
            max_instances = request.POST["max_instances"]
            if not max_instances.isdigit():
                context["error"] = "true"
                context["error_text"] = "Il numero massimo di iscritti deve essere un numero"
                return render(request, 'server/doc_create.html', context)
            max_instances = int(max_instances)

        # if group not primary and not public throw error
        if group_private == True and not request.user.is_staff:
            context["error"] = "true"
            context["error_text"] = "Non puoi creare un documento non pubblico per un gruppo non primario"
            return render(request, 'server/doc_create.html', context)

        # if already existing name throw error
        if DocumentType.objects.filter(name=name).count() > 0:
            context["error"] = "true"
            context["error_text"] = "Questo nome esiste già. Prego usarne un altro."
            return render(request, 'server/doc_create.html', context)

        # check if custom group permissions not met or non public document
        if custom_group != "":
            if group_private == True:
                context["error"] = "true"
                context["error_text"] = "Non puoi creare un documento non pubblico per un gruppo non primario"
                return render(request, 'server/doc_create.html', context)
            if custom_group not in map(lambda x: x.name, groups):
                context["error"] = "true"
                context["error_text"] = "Non puoi creare un tipo assegnato ad un gruppo di cui non fai parte"
                return render(request, 'server/doc_create.html', context)
            else:
                group = Group.objects.filter(name=custom_group)[0]
                custom_group_bool = True

        # create type
        doctype = DocumentType(
            custom_group=custom_group_bool, auto_sign=auto_sign, custom_message=custom_message, custom_message_text=custom_message_text, name=name, enabled=enabled, group_private=group_private, group=group, personal_data=personal_data, medical_data=medical_data, custom_data=custom_data, staff_only=staff_only, max_instances=max_instances)
        doctype.save()

        # create custom keys
        if custom_data:
            custom = request.POST["custom"]
            custom = custom.splitlines()
            for i in custom:
                val = i
                if val.startswith("!"):
                    if len(val) >= 3:
                        val = val[3:].split(",")[0]

                key = Keys(key=val, key_extra=i, container=doctype)
                key.save()

        return HttpResponseRedirect('doctype')

    # build context
    context = {
        "enabled_check": enabled_check,
        "private_check": private_check,
        "sign_check": sign_check,
        "personal_check": personal_check,
        "medical_check": medical_check,
        "custom_check": custom_check,
        "staff_only_check": staff_only_check,
        "custom_message_check": custom_message_check,
    }

    return render(request, 'server/doc_create.html', context)

@user_passes_test(isStaff)
def docedit(request):
    # create an edit page with empty context
    return docedit_wrapper(request, {})

@user_passes_test(isStaff)
def docedit_wrapper(request, context):
    groups = getGroups(request)
    group = groups[0]

    if request.user.is_staff and "group" in context.keys():
        if context["group"] == group.name:
            context["group"] = ""

    if request.method == "POST":
        if "action" not in request.POST.keys():
            # get document
            doc = DocumentType.objects.get(id=request.POST["doc"])

            # check if user can edit type
            if doc.group not in groups:
                # user is cheating abort
                return

            # init variables
            custom_group_bool = False
            custom_group = ""
            max_instances = 0

            enabled_check = 'checked="checked"' if doc.enabled else ""
            sign_check = 'checked="checked"' if not doc.auto_sign else ""
            custom_message_check = 'checked="checked"' if doc.custom_message else ""
            staff_only_check = 'checked="checked"' if doc.staff_only else ""
            private_check = 'checked="checked"' if doc.group_private else ""

            context = {
                'doc': doc,
                "group": doc.group.name,
                "enabled_check": enabled_check,
                "private_check": private_check,
                "sign_check": sign_check,
                "staff_only_check": staff_only_check,
                "custom_message_check": custom_message_check,
            }

            if request.user.is_staff:
                if context["group"] == group.name:
                    context["group"] = ""

            # gather inserted data
            enabled = "enabled" in request.POST.keys()
            auto_sign = "sign" not in request.POST.keys()
            group_private = "group_private" in request.POST.keys()
            custom_message = "custom_message" in request.POST.keys()
            staff_only = "staff_only" in request.POST.keys()
            custom_message_text = request.POST["custom_message_text"]
            custom_group = request.POST["custom_group"]

            if request.POST["max_instances"]:
                max_instances = request.POST["max_instances"]
                if not max_instances.isdigit():
                    context["error"] = "true"
                    context["error_text"] = "Il numero massimo di iscritti deve essere un numero"
                    return render(request, 'server/doc_edit.html', context)
                max_instances = int(max_instances)

            # if group not primary and not public throw error
            if group_private == True and not request.user.is_staff:
                context["error"] = "true"
                context["error_text"] = "Non puoi creare un documento non pubblico per un gruppo non primario"
                return render(request, 'server/doc_edit.html', context)

            # check if custom group permissions not met or non public document
            if custom_group != "":
                if group_private == True:
                    context["error"] = "true"
                    context["error_text"] = "Non puoi creare un documento non pubblico per un gruppo non primario"
                    return render(request, 'server/doc_edit.html', context)
                if custom_group not in map(lambda x: x.name, groups):
                    context["error"] = "true"
                    context["error_text"] = "Non puoi creare un tipo assegnato ad un gruppo di cui non fai parte"
                    return render(request, 'server/doc_edit.html', context)
                else:
                    group = Group.objects.filter(name=custom_group)[0]
                    custom_group_bool = True

            # edit type
            doc.custom_group = custom_group_bool
            doc.auto_sign = auto_sign
            doc.custom_message = custom_message
            doc.custom_message_text = custom_message_text
            doc.enabled = enabled
            doc.group_private = group_private
            doc.group = group
            doc.staff_only = staff_only
            doc.max_instances = max_instances

            doc.save()

            return HttpResponseRedirect('doctype')

    return render(request, 'server/doc_edit.html', context)

@user_passes_test(isStaff)
def doclist(request):
    context = {}

    # group name and obj
    parent_groups = getGroups(request)

    # create typezone
    zurich = pytz.timezone('Europe/Zurich')

    # init error variables for users
    error = False
    error_text = ""

    # init checkboxes for filter
    hidden = False
    wait = True
    selfsign = True
    ok = True
    signdoc = False

    hidden_check = 'checked="checked"'
    wait_check = 'checked="checked"'
    selfsign_check = 'checked="checked"'
    ok_check = 'checked="checked"'
    signdoc_check = 'checked="checked"'

    # set default dates for filters
    newer = zurich.localize(dateparser.parse("1970-01-01"))
    older = zurich.localize(datetime.now())

    # init chips values
    owner = []
    types = []
    groups = []
    chips_owner = []
    chips_types = []
    chips_groups = []

    if request.method == "POST":
        # if download request
        if request.POST["action"][0] == 'k':
            document = Document.objects.get(id=request.POST["action"][1:])
            # check if user has permission to view doc
            if document.group in parent_groups:
                vac_file = ""
                health_file = ""
                sign_doc_file = ""

                # prepare images in base64
                if document.medical_data:
                    if document.medical_data.vac_certificate.name:
                        with open(document.medical_data.vac_certificate.name, 'rb') as image_file:
                            vac_file = base64.b64encode(
                                image_file.read()).decode()

                    if document.medical_data.health_care_certificate.name:
                        with open(document.medical_data.health_care_certificate.name, 'rb') as image_file:
                            health_file = base64.b64encode(
                                image_file.read()).decode()

                if document.signed_doc:
                    with open(document.signed_doc.name, 'rb') as image_file:
                        sign_doc_file = base64.b64encode(
                            image_file.read()).decode()

                # build with template and render
                template = get_template('server/download_doc.html')
                doc = [document, KeyVal.objects.filter(
                    container=document), document.personal_data, document.medical_data, document.user.groups.values_list('name', flat=True)[0]]
                context = {'doc': doc, 'vac': vac_file,
                           'health': health_file, 'sign_doc_file': sign_doc_file}
                html = template.render(context)
                pdf = pdfkit.from_string(html, False)
                result = BytesIO(pdf)
                result.seek(0)
                return FileResponse(result, as_attachment=True, filename=document.user.username+"_"+document.document_type.name+".pdf")

        # get selected documents and check if user has permission to view
        selected = []
        for i in request.POST.keys():
            if i.isdigit():
                docc = Document.objects.get(id=i)
                if docc.group in parent_groups:
                    selected.append(docc)

                    # execute action on selected documents
                    if request.POST["action"] == 'delete' and settings.DEBUG:
                        docc.delete()
                    elif request.POST["action"] == 'approve' and settings.DEBUG:
                        docc.status = 'ok'
                        docc.save()
                    elif request.POST["action"] == 'archive':
                        docc.status = 'archive'
                        if docc.medical_data:
                            docc.medical_data.delete()
                            docc.medical_data.save()
                            docc.medical_data = None
                        docc.save()
                    #elif request.POST["action"] == 'unarchive':
                    #    if docc.status == 'archive':
                    #        docc.status = 'ok'
                    #        docc.save()
                    #    else:
                    #        error = True
                    #        error_text = "Non puoi dearchiviare un documento non archiviato"

        # get filter values
        hidden = "filter_hidden" in request.POST
        wait = "filter_wait" in request.POST
        selfsign = "filter_selfsign" in request.POST
        ok = "filter_ok" in request.POST
        signdoc = "filter_signdoc" in request.POST
        newer = zurich.localize(dateparser.parse(request.POST["newer"]))
        older = zurich.localize(dateparser.parse(
            request.POST["older"]) + timedelta(days=1))
        owner = request.POST["owner"].split("^|")
        types = request.POST["type"].split("^|")
        groups = request.POST["groups"].split("^|")

        # clear filters
        if request.POST["action"] == 'clear':
            hidden = False
            wait = True
            selfsign = True
            ok = True
            signdoc = False
            newer = zurich.localize(dateparser.parse("1970-01-01"))
            older = zurich.localize(datetime.now())
            owner = []
            types = []
            groups = []

    # filter documents based on group of staff and date range
    q_obj = Q(group__in=parent_groups) & Q(compilation_date__range=[newer, older])

    # filter documents
    if not hidden:
        q_obj &= ~Q(status="archive")
        hidden_check = ""
    if not wait:
        q_obj &= ~Q(status="wait")
        wait_check = ""
    if not selfsign:
        q_obj &= ~Q(status="autosign")
        selfsign_check = ""
    if not ok:
        q_obj &= ~Q(status="ok")
        ok_check = ""
    if signdoc:
        q_obj &= ~Q(signed_doc="")
    else:
        signdoc_check = ""

    # filter types, owner, groups using chips
    if len(types) > 0:
        if types[0] != "":
            q_obj &= Q(document_type__name__in=types)
            chips_types += types

    if len(owner) > 0:
        if owner[0] != "":
            q_obj &= Q(user__username__in=list(map(lambda x: x.split("(")[0][:-1], owner)))
            chips_owner += owner

    if len(groups) > 0:
        if groups[0] != "":
            q_obj &= Q(user__groups__name__in=groups)
            chips_groups += groups

    # run query
    documents = Document.objects.filter(q_obj).select_related("personal_data", "medical_data", "document_type", "user")

    users = documents.values("usercode__first_name", "usercode__last_name")

    vac_file = ["/server/media/", "/vac_certificate/doc"]
    health_file = ["/server/media/", "/health_care_certificate/doc"]
    sign_doc_file = ["/server/media/", "/signed_doc/doc"]

    # get types and users for chips autocompletation
    if request.user.is_staff:
        auto_types = DocumentType.objects.filter(
            Q(group_private=False) | Q(group=getGroups(request)[0]))
    else:
        auto_types = DocumentType.objects.filter(Q(group_private=False))

    context = {
        "vac_file": vac_file,
        "health_file": health_file,
        "sign_doc_file": sign_doc_file,
        "types": auto_types,
        "users": users,
        "groups": Group.objects.all(),
        "docs": documents,
        "hidden_check": hidden_check,
        "wait_check": wait_check,
        "selfsign_check": selfsign_check,
        "ok_check": ok_check,
        "signdoc_check": signdoc_check,
        "newer": newer,
        "older": older,
        "chips_owner": chips_owner,
        "chips_type": chips_types,
        "chips_groups": chips_groups,
        'error': error,
        'error_text': error_text,
        'settings': settings,
        'total_count': documents.count,
        'diga_count': documents.filter(user__groups__name__contains="diga").count,
        'muta_count': documents.filter(user__groups__name__contains="muta").count,
        'reparto_count': documents.filter(user__groups__name__contains="reparto").count,
        'posto_count': documents.filter(user__groups__name__contains="posto").count,
        'clan_count': documents.filter(user__groups__name__contains="clan").count,
    }

    # check if download multiple documents
    if request.method == "POST":
        if "status" not in request.session:
            request.session['status'] = True

        if request.POST["action"] == "download" and len(selected) > 0 and request.session['status']:
            # save data in session
            request.session['status'] = False
            request.session['progress'] = 0
            request.session['total'] = len(selected)
            # run job
            threading.Thread(target=zip_documents, args=(selected, request.session.session_key)).start()
            # flag the client to check for updates
            context["task_id"] = "0"

    return render(request, 'server/doc_list.html', context)

@user_passes_test(isStaff)
def doclist_table(request):
    context = {}

    # group name and obj
    parent_groups = getGroups(request)

    # create typezone
    zurich = pytz.timezone('Europe/Zurich')

    # init error variables for users
    error = False
    error_text = ""

    # init checkboxes for filter
    hidden = False
    wait = True
    selfsign = True
    ok = True
    signdoc = False

    hidden_check = 'checked="checked"'
    wait_check = 'checked="checked"'
    selfsign_check = 'checked="checked"'
    ok_check = 'checked="checked"'
    signdoc_check = 'checked="checked"'

    # set default dates for filters
    newer = zurich.localize(dateparser.parse("1970-01-01"))
    older = zurich.localize(datetime.now())

    # init chips values
    owner = []
    types = []
    groups = []
    chips_owner = []
    chips_types = []
    chips_groups = []

    if request.method == "POST":
        # get filter values
        hidden = "filter_hidden" in request.POST
        wait = "filter_wait" in request.POST
        selfsign = "filter_selfsign" in request.POST
        ok = "filter_ok" in request.POST
        signdoc = "filter_signdoc" in request.POST
        newer = zurich.localize(dateparser.parse(request.POST["newer"]))
        older = zurich.localize(dateparser.parse(
            request.POST["older"]) + timedelta(days=1))
        owner = request.POST["owner"].split("^|")
        types = request.POST["type"].split("^|")
        groups = request.POST["groups"].split("^|")

        # clear filters
        if request.POST["action"] == 'clear':
            hidden = False
            wait = True
            selfsign = True
            ok = True
            signdoc = False
            newer = zurich.localize(dateparser.parse("1970-01-01"))
            older = zurich.localize(datetime.now())
            owner = []
            types = []
            groups = []

    # filter documents based on group of staff and date range
    q_obj = Q(group__in=parent_groups) & Q(compilation_date__range=[newer, older])

    # filter documents
    if not hidden:
        q_obj &= ~Q(status="archive")
        hidden_check = ""
    if not wait:
        q_obj &= ~Q(status="wait")
        wait_check = ""
    if not selfsign:
        q_obj &= ~Q(status="autosign")
        selfsign_check = ""
    if not ok:
        q_obj &= ~Q(status="ok")
        ok_check = ""
    if signdoc:
        q_obj &= ~Q(signed_doc="")
    else:
        signdoc_check = ""

    # filter types, owner, groups using chips
    if len(types) > 0:
        if types[0] != "":
            q_obj &= Q(document_type__name__in=types)
            chips_types += types

    if len(owner) > 0:
        if owner[0] != "":
            q_obj &= Q(user__username__in=list(map(lambda x: x.split("(")[0][:-1], owner)))
            chips_owner += owner

    if len(groups) > 0:
        if groups[0] != "":
            q_obj &= Q(user__groups__name__in=groups)
            chips_groups += groups

    # run query
    documents = Document.objects.filter(q_obj).select_related("personal_data", "medical_data", "document_type", "user")

    users = documents.values("usercode__first_name", "usercode__last_name")

    # get types and users for chips autocompletation
    if request.user.is_staff:
        auto_types = DocumentType.objects.filter(
            Q(group_private=False) | Q(group=getGroups(request)[0]))
    else:
        auto_types = DocumentType.objects.filter(Q(group_private=False))

    context = {
        "types": auto_types,
        "users": users,
        "groups": Group.objects.all(),
        "docs": documents,
        "hidden_check": hidden_check,
        "wait_check": wait_check,
        "selfsign_check": selfsign_check,
        "ok_check": ok_check,
        "signdoc_check": signdoc_check,
        "newer": newer,
        "older": older,
        "chips_owner": chips_owner,
        "chips_type": chips_types,
        "chips_groups": chips_groups,
        'error': error,
        'error_text': error_text,
        'settings': settings,
    }

    return render(request, 'server/doc_list_table.html', context)

@user_passes_test(isCapi_enabled)
def doclist_readonly(request):
    context = {}

    # group name and obj
    groups = request.user.groups.all()
    if request.user.is_staff:
        groups_view = []
    elif request.user.has_perm("client.staff"):
        groups_view = list(map(lambda x: x.group, GroupSettings.objects.filter(group=groups[0]).filter(view_documents=True)))
    else:
        groups_view = list(map(lambda x: x.group, GroupSettings.objects.filter(group__in=groups).filter(view_documents=True)))

    perm = Permission.objects.get(codename='staff')

    for i in groups_view:
        # get all users that are part of the group and are administrators but not request.user
        emails = User.objects.filter(groups__name=i).filter(Q(is_staff=True) | Q(user_permissions=perm)).filter(~Q(id=request.user.id)).values_list("email", flat=True)

        if not settings.DEBUG:
            send_mail(
                'Attenzione! ' + request.user.username + ' ha visionato i documenti del gruppo "' + i.name + '"',
                "Questo messaggio è stato inviato automaticamente dal sistema di iscrizioni digitali. Ti è arrivata questa mail perchè hai abilitato la possibilità a persone del gruppo capi di visionare i documenti. L'utente con username " + request.user.username + " e con nome registrato " + request.user.first_name + " " + request.user.last_name + " ha visionato dei documenti.",
                settings.DEFAULT_FROM_EMAIL,
                emails,
                fail_silently=False,
            )


    # create typezone
    zurich = pytz.timezone('Europe/Zurich')

    # init error variables for users
    error = False
    error_text = ""

    # init checkboxes for filter
    hidden = False
    wait = True
    selfsign = True
    ok = True
    signdoc = False

    hidden_check = 'checked="checked"'
    wait_check = 'checked="checked"'
    selfsign_check = 'checked="checked"'
    ok_check = 'checked="checked"'
    signdoc_check = 'checked="checked"'

    # set default dates for filters
    newer = zurich.localize(dateparser.parse("1970-01-01"))
    older = zurich.localize(datetime.now())

    # init chips values
    owner = []
    types = []
    groups = []
    chips_owner = []
    chips_types = []
    chips_groups = []

    if request.method == "POST":
        # if download request
        if request.POST["action"][0] == 'k':
            document = Document.objects.get(id=request.POST["action"][1:])
            # check if user has permission to view doc
            if document.group in groups_view:
                vac_file = ""
                health_file = ""
                sign_doc_file = ""

                # prepare images in base64
                if document.medical_data:
                    if document.medical_data.vac_certificate.name:
                        with open(document.medical_data.vac_certificate.name, 'rb') as image_file:
                            vac_file = base64.b64encode(
                                image_file.read()).decode()

                    if document.medical_data.health_care_certificate.name:
                        with open(document.medical_data.health_care_certificate.name, 'rb') as image_file:
                            health_file = base64.b64encode(
                                image_file.read()).decode()

                if document.signed_doc:
                    with open(document.signed_doc.name, 'rb') as image_file:
                        sign_doc_file = base64.b64encode(
                            image_file.read()).decode()

                # build with template and render
                template = get_template('server/download_doc.html')
                doc = [document, KeyVal.objects.filter(
                    container=document), document.personal_data, document.medical_data, document.user.groups.values_list('name', flat=True)[0]]
                context = {'doc': doc, 'vac': vac_file,
                           'health': health_file, 'sign_doc_file': sign_doc_file}
                html = template.render(context)
                pdf = pdfkit.from_string(html, False)
                result = BytesIO(pdf)
                result.seek(0)
                return FileResponse(result, as_attachment=True, filename=document.user.username+"_"+document.document_type.name+".pdf")

        # get selected documents and check if user has permission to view
        selected = []
        for i in request.POST.keys():
            if i.isdigit():
                docc = Document.objects.get(id=i)
                if docc.group in groups_view:
                    selected.append(docc)

        # get filter values
        hidden = "filter_hidden" in request.POST
        wait = "filter_wait" in request.POST
        selfsign = "filter_selfsign" in request.POST
        ok = "filter_ok" in request.POST
        signdoc = "filter_signdoc" in request.POST
        newer = zurich.localize(dateparser.parse(request.POST["newer"]))
        older = zurich.localize(dateparser.parse(
            request.POST["older"]) + timedelta(days=1))
        owner = request.POST["owner"].split("^|")
        types = request.POST["type"].split("^|")
        groups = request.POST["groups"].split("^|")

        # clear filters
        if request.POST["action"] == 'clear':
            hidden = False
            wait = True
            selfsign = True
            ok = True
            signdoc = False
            newer = zurich.localize(dateparser.parse("1970-01-01"))
            older = zurich.localize(datetime.now())
            owner = []
            types = []
            groups = []

    # filter documents based on group of staff and date range
    q_obj = Q(group__name__in=groups_view) & Q(compilation_date__range=[newer, older])

    # filter documents
    if not hidden:
        q_obj &= ~Q(status="archive")
        hidden_check = ""
    if not wait:
        q_obj &= ~Q(status="wait")
        wait_check = ""
    if not selfsign:
        q_obj &= ~Q(status="autosign")
        selfsign_check = ""
    if not ok:
        q_obj &= ~Q(status="ok")
        ok_check = ""
    if signdoc:
        q_obj &= ~Q(signed_doc="")
    else:
        signdoc_check = ""

    # filter types, owner, groups using chips
    if len(types) > 0:
        if types[0] != "":
            q_obj &= Q(document_type__name__in=types)
            chips_types += types

    if len(owner) > 0:
        if owner[0] != "":
            q_obj &= Q(user__username__in=list(map(lambda x: x.split("(")[0][:-1], owner)))
            chips_owner += owner

    if len(groups) > 0:
        if groups[0] != "":
            q_obj &= Q(user__groups__name__in=groups)
            chips_groups += groups

    # run query
    documents = Document.objects.filter(q_obj).select_related("personal_data", "medical_data", "document_type", "user")

    users = documents.values("usercode__first_name", "usercode__last_name")

    vac_file = ["/server/media/", "/vac_certificate/doc"]
    health_file = ["/server/media/", "/health_care_certificate/doc"]
    sign_doc_file = ["/server/media/", "/signed_doc/doc"]

    # get types and users for chips autocompletation
    auto_types = DocumentType.objects.filter(
        Q(group_private=False) | Q(group__in=groups_view))

    context = {
        "vac_file": vac_file,
        "health_file": health_file,
        "sign_doc_file": sign_doc_file,
        "types": auto_types,
        "users": users,
        "groups": Group.objects.all(),
        "docs": documents,
        "hidden_check": hidden_check,
        "wait_check": wait_check,
        "selfsign_check": selfsign_check,
        "ok_check": ok_check,
        "signdoc_check": signdoc_check,
        "newer": newer,
        "older": older,
        "chips_owner": chips_owner,
        "chips_type": chips_types,
        "chips_groups": chips_groups,
        'error': error,
        'error_text': error_text,
        'settings': settings,
        'total_count': documents.count,
        'diga_count': documents.filter(user__groups__name__contains="diga").count,
        'muta_count': documents.filter(user__groups__name__contains="muta").count,
        'reparto_count': documents.filter(user__groups__name__contains="reparto").count,
        'posto_count': documents.filter(user__groups__name__contains="posto").count,
        'clan_count': documents.filter(user__groups__name__contains="clan").count,
    }

    # check if download multiple documents
    if request.method == "POST":
        if "status" not in request.session:
            request.session['status'] = True

        if request.POST["action"] == "download" and len(selected) > 0 and request.session['status']:
            # save data in session
            request.session['status'] = False
            request.session['progress'] = 0
            request.session['total'] = len(selected)
            # run job
            threading.Thread(target=zip_documents, args=(selected, request.session.session_key)).start()
            # flag the client to check for updates
            context["task_id"] = "0"

    return render(request, 'server/doc_list_readonly.html', context)

def get_progress(request):
    # if user wants to download result
    if 'download' in request.GET:
        # if job is completed
        if request.session['status']:
            data = BytesIO(base64.b64decode(request.session['result']))
            data.seek(0)
            return FileResponse(data, as_attachment=True, filename="documents_" + datetime.now().strftime("%H_%M-%d_%m_%y") + ".zip")

    # otherwise return status
    data = [request.session['progress'], request.session['total'], request.session['status']]
    return HttpResponse(json.dumps(data))

def zip_documents(docs, session_key):
    files = []
    # get session
    session = SessionStore(session_key=session_key)
    for i in docs:
        vac_file = ""
        health_file = ""
        sign_doc_file = ""

        # prepare pictures in base64
        if i.medical_data:
            if i.medical_data.vac_certificate.name:
                with open(i.medical_data.vac_certificate.name, 'rb') as image_file:
                    vac_file = base64.b64encode(
                        image_file.read()).decode()

            if i.medical_data.health_care_certificate.name:
                with open(i.medical_data.health_care_certificate.name, 'rb') as image_file:
                    health_file = base64.b64encode(
                        image_file.read()).decode()
        if i.signed_doc:
            with open(i.signed_doc.name, 'rb') as image_file:
                sign_doc_file = base64.b64encode(
                    image_file.read()).decode()

        template = get_template('server/download_doc.html')
        doc = [i, KeyVal.objects.filter(
            container=i), i.personal_data, i.medical_data, i.user.groups.values_list('name', flat=True)[0]]
        context = {'doc': doc, 'vac': vac_file,
                    'health': health_file, 'sign_doc_file': sign_doc_file}
        # render context
        html = template.render(context)
        # render pdf using wkhtmltopdf
        pdf = pdfkit.from_string(html, False)
        filename = i.user.username+"_"+i.document_type.name+".pdf"
        # append file
        files.append((filename, pdf))
        session['progress'] += 1
        session.save()

    # zip documents
    mem_zip = BytesIO()
    with zipfile.ZipFile(mem_zip, mode="w",compression=zipfile.ZIP_DEFLATED) as zf:
        for f in files:
            zf.writestr(f[0], f[1])

    mem_zip.seek(0)
    # save result
    session['result'] = base64.b64encode(mem_zip.getvalue()).decode()
    session['status'] = True
    session.save()


@user_passes_test(isStaff)
def upload_doc(request):
    # setup group based on staff primary or not
    groups = getGroups(request)

    # setup variables for error text and success text
    error = False
    success = False
    error_text = ""
    success_text = ""

    document = None
    # parse document code and check for permissions
    if request.method == "POST":
        data = request.POST["code"]
        if not data.isdigit():
            error_text = "Formato codice errato"
            error = True
        elif int(data) < 100000 or int(data) > 999999:
            error_text = "Formato codice errato"
            error = True
        elif Document.objects.filter(code=data).count() == 0:
            error_text = "Codice invalido"
            error = True
        elif Document.objects.filter(code=data)[0].group not in groups:
            error_text = "Codice invalido"
            error = True
        else:
            # get document
            document = Document.objects.filter(code=data)[0]

            # prepare success message
            if document.status == 'ok':
                success_text = "File caricato"
                success = True
            else:
                document.status = 'ok'
                document.save()
                success_text = "Documento approvato e file caricato"
                success = True

            # check for errors and upload files
            if "doc_sign" in request.FILES and not error:
                myfile = request.FILES['doc_sign']
                try:
                    im = Image.open(myfile)
                    im_io = BytesIO()
                    # compress image in WEBP
                    im.save(im_io, 'WEBP', quality=50)
                    document.signed_doc.save(data+"_"+myfile.name, im_io)
                    document.save()
                except UnidentifiedImageError:
                    error = True
                    error_text = "Il file non è un immagine valida"
            else:
                error = True
                error_text = "Prego caricare un file"

    context = {
        "error": error,
        "error_text": error_text,
        "success": success,
        "success_text": success_text,
    }

    return render(request, 'server/upload_doc.html', context)


@user_passes_test(isStaff)
def docpreview(request):
    context = {}
    # check for permissions
    groups = getGroups(request)

    if request.method == "POST":
        # get document code
        code = request.POST["preview"]

        # check if code valid and user has permission
        if not code.isdigit():
            return render(request, 'server/download_doc.html', context)
        if Document.objects.filter(code=code).count() == 0:
            return render(request, 'server/download_doc.html', context)
        if Document.objects.filter(code=code)[0].group not in groups:
            return render(request, 'server/download_doc.html', context)

        # get document
        document = Document.objects.filter(code=code)[0]
        doc_group = document.group
        parent_group = document.user.groups.values_list('name', flat=True)[0]

        # user has not permission to view document
        if doc_group not in groups:
            return

        # prepare images in base64
        vac_file = ""
        health_file = ""
        sign_doc_file = ""
        if document.medical_data:
            if document.medical_data.vac_certificate.name:
                with open(document.medical_data.vac_certificate.name, 'rb') as image_file:
                    vac_file = base64.b64encode(image_file.read()).decode()

            if document.medical_data.health_care_certificate.name:
                with open(document.medical_data.health_care_certificate.name, 'rb') as image_file:
                    health_file = base64.b64encode(image_file.read()).decode()
        if document.signed_doc:
            with open(document.signed_doc.name, 'rb') as image_file:
                sign_doc_file = base64.b64encode(image_file.read()).decode()

        # prepare context
        doc = [document, KeyVal.objects.filter(
            container=document), document.personal_data, document.medical_data, parent_group]
        context = {'doc': doc, 'vac': vac_file,
                   'health': health_file, 'sign_doc_file': sign_doc_file}

    return render(request, 'server/download_doc.html', context)

@staff_member_required
@user_passes_test(lambda u: u.is_superuser)
def debug_uc(request):
    data = []
    users = User.objects.all()
    for u in users:
        data.append([u, UserCode.objects.filter(user=u)])

    context = {
        "data": data,
    }
    return render(request, 'server/debug_usercode.html', context)

@user_passes_test(isStaff)
def data_request(request):
    context = {}
    parent_group = getGroups(request)[0]

    if request.method == "POST":
        if "request" not in request.POST.keys():
            context["error"] = "Selezionare una richesta"
        elif request.POST["request"] == "email_all":
            users_email = UserCode.objects.filter(branca=parent_group).values_list("user__email", flat=True)
            data = "; ".join(users_email)
            context["data"] = data
        elif request.POST["request"] == "data_user":
            users = UserCode.objects.filter(branca=parent_group)

            # get time for filename
            current_time = datetime.strftime(datetime.now(), "%H_%M__%d_%m_%y")

            response = HttpResponse()
            response['Content-Disposition'] = 'attachment; filename="data_export_' + current_time + '.csv"'

            writer = csv.writer(response)

            # csv header
            writer.writerow(["Codice", "Nome", "Cognome", "Email", "Nome dei genitori", "Indirizzo", "NAP", "Luogo", "Nazionalita", "Nazionalità secondo G+S", "Data di nascita", "numero di telefono Altro", "numero di telefono Cellulare", "Scuola", "Classe scolastica", "Numero AVS"])

            for usercode in users:
                nationality = usercode.nationality

                nat_gs = ""
                if "svizzera" in nationality.lower():
                    nat_gs = "CH"
                elif "ch" == nationality.lower():
                    nat_gs = "CH"
                else:
                    nat_gs = "DIV"

                writer.writerow([
                    "U"+str(usercode.code),
                    usercode.first_name,
                    usercode.last_name,
                    usercode.user.email,
                    usercode.parent_name,
                    usercode.via,
                    usercode.cap,
                    usercode.country,
                    usercode.nationality,
                    nat_gs,
                    usercode.born_date,
                    usercode.home_phone,
                    usercode.phone,
                    usercode.school,
                    usercode.year,
                    usercode.avs_number
                ])

            return response

        elif request.POST["request"] == "data_user_medic":
            users = UserCode.objects.filter(branca=parent_group)

            # get time for filename
            current_time = datetime.strftime(datetime.now(), "%H_%M__%d_%m_%y")

            response = HttpResponse()
            response['Content-Disposition'] = 'attachment; filename="data_export_' + current_time + '.csv"'

            writer = csv.writer(response)

            # csv header
            writer.writerow(["Codice", "Nome", "Cognome", "Email", "Nome dei genitori", "Indirizzo", "NAP", "Luogo", "Nazionalita", "Nazionalità secondo G+S", "Data di nascita", "numero di telefono Altro", "numero di telefono Cellulare", "Scuola", "Classe scolastica", "Numero AVS", "Contatto d'emergenza", "Parentela del contatto", "Telefono d'emergenza", "Cellulare emergenza", "Indirizzo completo emergenza", "Cassa malati", "Ass. Infortuni", "Ass. RC", "Socio REGA", "Nome del medico", "Telefono medico", "Indirizzo medico", "Malattie", "Vacinazioni", "Data antitetanica", "Allergie", "Assume medicamenti", "Medicamenti", "Informazioni particolari", "Informazioni"])

            for usercode in users:
                medic = usercode.medic
                nationality = usercode.nationality

                nat_gs = ""
                if "svizzera" in nationality.lower():
                    nat_gs = "CH"
                elif "ch" == nationality.lower():
                    nat_gs = "CH"
                else:
                    nat_gs = "DIV"

                writer.writerow([
                    "U"+str(usercode.code),
                    usercode.first_name,
                    usercode.last_name,
                    usercode.user.email,
                    usercode.parent_name,
                    usercode.via,
                    usercode.cap,
                    usercode.country,
                    usercode.nationality,
                    nat_gs,
                    usercode.born_date,
                    usercode.home_phone,
                    usercode.phone,
                    usercode.school,
                    usercode.year,
                    usercode.avs_number,
                    medic.emer_name,
                    medic.emer_relative,
                    medic.emer_phone,
                    medic.cell_phone,
                    medic.address,
                    medic.health_care,
                    medic.injuries,
                    medic.rc,
                    medic.rega,
                    medic.medic_name,
                    medic.medic_phone,
                    medic.medic_address,
                    medic.sickness,
                    medic.vaccine,
                    medic.tetanus_date,
                    medic.allergy,
                    medic.drugs_bool,
                    medic.drugs,
                    medic.misc_bool,
                    medic.misc
                ])

            return response
    return render(request, 'server/data_request.html', context)

def media_request(request, id=0, t="", flag=""):
    if flag == "usercode":
        usercode = UserCode.objects.get(id=id)
        if request.user.is_staff:
            groups = getGroups(request)
            usercode_group = list(usercode.user.groups.all())[0]
            if usercode_group not in groups:
                return
        else:
            if usercode.user != request.user:
                return

        if t == "health_care_certificate":
            image_file = usercode.medic.health_care_certificate
        elif t == "vac_certificate":
            image_file = usercode.medic.vac_certificate

    elif flag == "doc":
        doc = Document.objects.get(id=id)
        doc_group = doc.group

        groups = getGroups(request)
        group_view = Group.objects.filter(name="capi") in groups and GroupSettings.objects.filter(group__name=doc_group).filter(view_documents=True).count() != 0

        # check if user can view media
        if request.user.is_staff:
            # user is staff
            if doc_group not in groups:
                return
        elif request.user.has_perm("client.staff"):
            # user is psudo-staff
            if doc_group not in groups[1:] and not group_view:
                return
        else:
            # is normal user
            if doc.user != request.user and not group_view:
                return

        if t == "health_care_certificate":
            image_file = doc.medical_data.health_care_certificate
        elif t == "vac_certificate":
            image_file = doc.medical_data.vac_certificate
        elif t == "signed_doc":
            image_file = doc.signed_doc

    return FileResponse(image_file, filename=image_file.name)