aboutsummaryrefslogtreecommitdiffstats
path: root/server/views.py
blob: e8fbe8b14bfb9e34a48f438e40b13e67ffbb394e (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
from django.shortcuts import render
from client.models import 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.contenttypes.models import ContentType

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

progress = {}

# 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


@user_passes_test(isStaff)
def index(request):
    context = {}
    # primary group name + object
    parent_group = request.user.groups.values_list('name', flat=True)[
        0]
    group = Group.objects.get(name=parent_group)
    # users from younger to older
    users = User.objects.filter(groups__name=parent_group).order_by("-id")
    users_out = []

    # only send part of the user data, only if user is approved
    for user in users:
        if not user.has_perm("client.approved") and not user.is_staff:
            continue

        users_out.append([user.username, user.first_name,
                          user.last_name])

    # if user is staff of not primary show only public types
    if request.user.is_staff:
        public_types = DocumentType.objects.filter(
            Q(group_private=False) | Q(group=group) & Q(enabled=True)).order_by("-id")
    else:
        public_types = DocumentType.objects.filter(
            Q(group_private=False) & Q(enabled=True)).order_by("-id")

    # count documents of that type to show statistics
    docs = []
    for doc in public_types:
        ref_docs = Document.objects.filter(document_type=doc)
        docs.append([doc, len(ref_docs)])

    # don't list users if user is staff of not primary
    if request.user.is_staff:
        context = {
            'docs': docs,
            'users': users_out,
        }
    else:
        context = {
            'docs': docs,
        }
    return render(request, 'server/index.html', context)


@staff_member_required
def uapprove(request):
    context = {}
    data = []
    if request.method == "POST":
        # get group name and obj
        parent_group = request.user.groups.values_list('name', flat=True)[
            0]
        group = Group.objects.get(name=parent_group)

        # get permission object
        permission = Permission.objects.get(codename='approved')

        # parse text to array
        data = request.POST["codes"]
        data.replace("\r", "")
        data = data.split("\n")
        # 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 len(UserCode.objects.filter(code=data[i][1:])) == 0:
                data[i] = data[i] + " - Invalido"
            else:
                user = UserCode.objects.filter(code=data[i][1:])[0].user
                user.user_permissions.add(permission)
                # if user not in any group add to the same group as staff
                if len(user.groups.values_list('name', flat=True)) == 0:
                    user.groups.add(group)
                    data[i] = data[i] + " - Ok"
                else:
                    if user.groups.values_list('name', flat=True)[0] == parent_group:
                        # if user already in group do nothing
                        data[i] = data[i] + " - Ok"
                    else:
                        # if user in another group notify staff of group change
                        user.groups.clear()
                        user.groups.add(group)
                        data[i] = data[i] + " - Ok, cambio branca"

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

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


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

    # group name and obj
    parent_group = request.user.groups.values_list('name', flat=True)[
        0]
    group = Group.objects.get(name=parent_group)

    # if user not staff of primary has only controll of non primary groups
    if request.user.is_staff:
        groups = request.user.groups.values_list('name', flat=True)
    else:
        groups = request.user.groups.values_list('name', flat=True)[1:]

    if request.method == "POST":
        # parse text in array
        data = request.POST["codes"]
        data.replace("\r", "")
        data = data.split("\n")
        # check if code valid
        for i in range(len(data)):
            if not data[i].isdigit():
                data[i] = data[i] + " - Formato errato"
            elif int(data[i]) < 100000 or int(data[i]) > 999999:
                data[i] = data[i] + " - Formato errato"
            elif len(Document.objects.filter(code=data[i])) == 0:
                data[i] = data[i] + " - Invalido"
            elif Document.objects.filter(code=data[i])[0].group.name not in groups:
                # check if user has permission to approve document
                data[i] = data[i] + " - Invalido"
            else:
                document = Document.objects.filter(code=data[i])[0]
                if document.status == 'ok':
                    # do nothing document already approved
                    data[i] = data[i] + " - Giร  approvato"
                else:
                    document.status = 'ok'
                    document.save()
                    data[i] = data[i] + " - Ok"

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

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


@staff_member_required
def ulist(request):
    context = {}
    # group name and obj
    parent_group = request.user.groups.values_list('name', flat=True)[0]
    group = Group.objects.get(name=parent_group)

    if request.method == "POST":
        # request to download document
        if 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 == group:
                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, parent_group]
                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.user.username+"_"+document.document_type.name+".pdf")

        # deapprove user
        elif request.POST["action"][0] == 'd':
            user = User.objects.get(id=request.POST["action"][1:])
            # check if user has permission to deapprove user
            if user.groups.all()[0] == group:
                content_type = ContentType.objects.get_for_model(Document)
                permission = Permission.objects.get(
                    content_type=content_type, codename="approved")
                user.user_permissions.remove(permission)
                return HttpResponseRedirect("ulist")

    # list users with their documents
    users = User.objects.filter(
        groups__name=parent_group).order_by("first_name")
    out = []
    for user in users:
        # list only approved users
        if not user.has_perm("client.approved") and not user.is_staff:
            continue

        usercode = UserCode.objects.filter(user=user)[0]
        # get all user documents
        documents = Document.objects.filter(Q(user=user) & ~Q(
            status='archive') & Q(group__name=parent_group))

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

            if usercode.medic.health_care_certificate.name:
                with open(usercode.medic.health_care_certificate.name, 'rb') as image_file:
                    health_file = base64.b64encode(image_file.read()).decode()
        out.append([user, usercode, parent_group,
                    documents, vac_file, health_file])
    context = {'users': out}
    return render(request, 'server/user_list.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 request.method == "POST":
        selected = []
        # if user not staff of primary get only non primary groups
        if request.user.is_staff:
            parent_groups = request.user.groups.values_list('name', flat=True)
        else:
            parent_groups = request.user.groups.values_list('name', flat=True)[
                1:]

        # 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.name in parent_groups:
                    selected.append(docc)
                else:
                    error = True
                    error_text = "Non puoi modificare un documento non del tuo gruppo"

        # execute action on selected types
        for i in selected:
            if request.POST["action"] == 'delete':
                try:
                    i.delete()
                except ProtectedError:
                    error = True
                    error_text = "Non puoi eliminare un tipo a cui รฉ collegato uno o piรบ documenti"
            elif request.POST["action"] == 'hide':
                i.enabled = False
                i.save()
            elif request.POST["action"] == 'show':
                i.enabled = True
                i.save()

        # 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

    # group name and obj
    parent_group = request.user.groups.values_list('name', flat=True)[
        0]
    group = Group.objects.get(name=parent_group)

    # if user not staff of primary group show only public types
    if request.user.is_staff:
        public_types = DocumentType.objects.filter(
            Q(group_private=False) | Q(group=group))
    else:
        public_types = DocumentType.objects.filter(
            Q(group_private=False))

    # 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 = ""

    # get custom keys from types
    out = []
    for doc in public_types:
        custom_keys = Keys.objects.filter(container=doc)
        ref_docs = Document.objects.filter(document_type=doc)
        out.append([doc, custom_keys, len(ref_docs)])

    context = {
        'docs': out,
        '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 doccreate(request):
    context = {}

    # if user is not staff of primary set default group to secondary and default public type
    if request.user.is_staff:
        groups = request.user.groups.values_list('name', flat=True)
        parent_group = request.user.groups.values_list('name', flat=True)[
            0]
        group_private = False
        private_check = 'checked="checked"'
    else:
        groups = request.user.groups.values_list('name', flat=True)[1:]
        parent_group = request.user.groups.values_list('name', flat=True)[
            1]
        group_private = True
        private_check = ''

    # get group obj
    group = Group.objects.get(name=parent_group)

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

    enabled_check = 'checked="checked"'
    personal_check = 'checked="checked"'
    sign_check = 'checked="checked"'
    medical_check = ""
    custom_check = ""
    custom_message_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()
        custom_message_text = request.POST["custom_message_text"]
        name = request.POST["name"]
        custom_group = request.POST["custom_group"]

        # 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 len(DocumentType.objects.filter(name=name)) > 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 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=request.POST["name"], enabled=enabled, group_private=group_private, group=group, personal_data=personal_data, medical_data=medical_data, custom_data=custom_data)
        doctype.save()

        # create custom keys
        if custom_data:
            custom = request.POST["custom"]
            custom.replace("\r", "")
            custom = custom.split("\n")
            for i in custom:
                key = Keys(key=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,
        "custom_message_check": custom_message_check,
    }

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


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

    # group name and obj
    parent_group = request.user.groups.values_list('name', flat=True)[
        0]
    group = Group.objects.get(name=parent_group)

    # if user not staff of primary get secondary groups
    if request.user.is_staff:
        parent_groups = request.user.groups.values_list('name', flat=True)
    else:
        parent_groups = request.user.groups.values_list('name', flat=True)[1:]

    # 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.name 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, parent_group]
                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.name in parent_groups:
                    selected.append(docc)

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

        if len(selected) == 0:
            error = True
            error_text = "Seleziona almeno un documento"

        # 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
    q_obj = Q()
    for i in parent_groups:
        q_obj |= Q(group__name=i)

    documents = Document.objects.filter(q_obj)

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

    # filter date range
    documents = documents.filter(compilation_date__range=[newer, older])

    # filter types, owner, groups using chips
    if len(types) > 0:
        if types[0] != "":
            q_obj = Q()
            for t in types:
                q_obj |= Q(document_type__name=t)
                chips_types.append(t)

            documents = documents.filter(q_obj)

    if len(owner) > 0:
        if owner[0] != "":
            q_obj = Q()
            for u in owner:
                user = u.split("(")[0][:-1]
                q_obj |= Q(user__username=user)
                chips_owner.append(u)

            documents = documents.filter(q_obj)

    if len(groups) > 0:
        if groups[0] != "":
            q_obj = Q()
            for g in groups:
                q_obj |= Q(group__name=g)
                chips_groups.append(g)

            documents = documents.filter(q_obj)

    out = []
    for i in documents:
        # filter for confirmed with attachment documents and approved
        if signdoc:
            if i.status == "ok" and not i.signed_doc:
                continue

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

            if medical.health_care_certificate.name:
                with open(medical.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()

        doc_group = i.user.groups.values_list('name', flat=True)[0]

        out.append([i, KeyVal.objects.filter(container=i), personal,
                    medical, doc_group, vac_file, health_file, sign_doc_file])

    # get types and users for chips autocompletation
    auto_types = DocumentType.objects.filter(
        Q(group_private=False) | Q(group=group))
    users = User.objects.filter(groups__name=parent_group)

    context = {
        "types": auto_types,
        "users": users,
        "groups": parent_groups,
        "docs": out,
        "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,
    }

    if request.method == "POST":
        if request.POST["action"] == "download" and len(selected) > 0:
            code = request.user.username + "_" + str(random.randint(100, 999))
            progress[code] = [0, 0, False, request.user, None]
            threading.Thread(target=zip_documents, args=(selected, code)).start()
            context["task_id"] = code

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

def get_progress(request):
    if 'job' in request.GET:
        job_id = request.GET['job']
    else:
        return HttpResponse(json.dumps({"error": True}))

    if not job_id in progress.keys():
        return HttpResponse(json.dumps({"error": "Lavoro non esistente. Provare a richiedere di nuovo il lavoro."}))

    if 'download' in request.GET:
        if progress[job_id][3] == request.user:
            data = progress[job_id][4]
            del progress[job_id]
            return data
    data = progress[job_id][:3]
    return HttpResponse(json.dumps(data))

def zip_documents(docs, code):
    files = []
    total = len(docs)
    status = 0
    progress[code][0] = status
    progress[code][1] = total
    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"
        files.append((filename, pdf))
        status += 1
        progress[code][0] = status

    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)
    progress[code][4] = FileResponse(mem_zip, as_attachment=True, filename="documents_" + datetime.now().strftime("%H_%M-%d_%m_%y") + ".zip")
    progress[code][2] = True

@user_passes_test(isStaff)
def upload_doc(request):
    # setup group based on staff primary or not
    parent_group = request.user.groups.values_list('name', flat=True)[
        0]
    group = Group.objects.get(name=parent_group)
    if request.user.is_staff:
        groups = request.user.groups.values_list('name', flat=True)
    else:
        groups = request.user.groups.values_list('name', flat=True)[1:]

    # 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 len(Document.objects.filter(code=data)) == 0:
            error_text = "Codice invalido"
            error = True
        elif Document.objects.filter(code=data)[0].group.name 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
    if request.user.is_staff:
        groups = request.user.groups.values_list('name', flat=True)
    else:
        groups = request.user.groups.values_list('name', flat=True)[1:]

    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 len(Document.objects.filter(code=code)) == 0:
            return render(request, 'server/download_doc.html', context)
        if Document.objects.filter(code=code)[0].group.name not in groups:
            return render(request, 'server/download_doc.html', context)

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

        # 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)