From 856ad4734e168c1b56f349244acd0cc14fcd8d78 Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Thu, 10 Dec 2020 14:01:22 +0100 Subject: lazyload images --- server/templates/server/doc_list.html | 10 +++++--- server/urls.py | 1 + server/views.py | 45 ++++++++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 11 deletions(-) (limited to 'server') diff --git a/server/templates/server/doc_list.html b/server/templates/server/doc_list.html index 2499245..f98cbd5 100644 --- a/server/templates/server/doc_list.html +++ b/server/templates/server/doc_list.html @@ -160,6 +160,9 @@ {% elif doc.0.status == "autosign" %} assignment_turned_in {% endif %} + {% if settings.DEBUG %} + {{doc.0.id}}: + {% endif %} {{doc.0.document_type.name}} {{doc.0.user.first_name}} {{doc.0.user.last_name}} @@ -416,7 +419,7 @@
- {% if doc.5 %}{% endif %} + {% if doc.5 %}{% endif %}
@@ -425,7 +428,7 @@
- {% if doc.6 %}{% endif %} + {% if doc.6 %}{% endif %}
@@ -434,7 +437,7 @@
- {% if doc.7 %}{% endif %} + {% if doc.7 %}{% endif %}
@@ -465,6 +468,7 @@ $(document).ready(function(){ update(); document.getElementById('progress_bar').className = "determinate"; {% endif %} + lazyload(); }); function resetModal() { diff --git a/server/urls.py b/server/urls.py index 3c4f66f..087e7ad 100644 --- a/server/urls.py +++ b/server/urls.py @@ -16,4 +16,5 @@ urlpatterns = [ path('docpreview', views.docpreview, name='docpreview'), path('progress', views.get_progress, name='progress'), path('request', views.data_request, name='request'), + path('media//', views.media_request, name='media'), ] diff --git a/server/views.py b/server/views.py index 9ea1d3f..c7983e2 100644 --- a/server/views.py +++ b/server/views.py @@ -915,17 +915,13 @@ def doclist(request): 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() + vac_file = "/server/media/" + str(i.id) + "/vac_certificate" 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() + health_file = "/server/media/" + str(i.id) + "/health_care_certificate" if i.signed_doc: - with open(i.signed_doc.name, 'rb') as image_file: - sign_doc_file = base64.b64encode( - image_file.read()).decode() + sign_doc_file = "/server/media/" + str(i.id) + "/signed_doc" doc_group = i.user.groups.values_list('name', flat=True)[0] @@ -1139,6 +1135,10 @@ def docpreview(request): document = Document.objects.filter(code=code)[0] parent_group = document.user.groups.values_list('name', flat=True)[0] + # user has not permission to view document + if parent_group not in groups: + return + # prepare images in base64 vac_file = "" health_file = "" @@ -1183,4 +1183,33 @@ def data_request(request): data += user.email + ", " data = data[:-2] context["data"] = data - return render(request, 'server/data_request.html', context) \ No newline at end of file + return render(request, 'server/data_request.html', context) + +def media_request(request, id=0, t=""): + doc = Document.objects.get(id=id) + doc_group = doc.user.groups.values_list('name', flat=True)[0] + + # check if user can view media + if request.user.is_staff: + # user is staff + groups = request.user.groups.values_list('name', flat=True) + if doc_group not in groups: + return + elif request.user.has_perm("client.staff"): + # user is psudo-staff + groups = request.user.groups.values_list('name', flat=True)[1:] + if doc_group not in groups: + return + else: + # is normal user + if doc.user != request.user: + 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) \ No newline at end of file -- cgit v1.2.1