diff options
author | prathamesh | 2021-03-24 12:56:33 +0530 |
---|---|---|
committer | prathamesh | 2021-03-24 12:56:33 +0530 |
commit | f38664721ed117f445b27af839fe41898ed90a47 (patch) | |
tree | 7201135da9b74729dce26747a0a7cee05a760a23 | |
parent | a43a152bc36717fd85493d91e6fa5d67c3ce4e5d (diff) | |
download | online_test-f38664721ed117f445b27af839fe41898ed90a47.tar.gz online_test-f38664721ed117f445b27af839fe41898ed90a47.tar.bz2 online_test-f38664721ed117f445b27af839fe41898ed90a47.zip |
Modify QR code functionality, courtesy @adityacp
-rw-r--r-- | yaksh/models.py | 6 | ||||
-rw-r--r-- | yaksh/templates/yaksh/question.html | 9 | ||||
-rw-r--r-- | yaksh/templates/yaksh/upload_file.html | 72 | ||||
-rw-r--r-- | yaksh/views.py | 58 |
4 files changed, 98 insertions, 47 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index d792205..efe772c 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -3360,8 +3360,10 @@ class QRcode(models.Model): def generate_image(self, content): img = qrcode.make(content) - path = os.path.join(settings.MEDIA_ROOT, 'qrcode', - '{0}.png'.format(self.short_key)) + qr_dir = os.path.join(settings.MEDIA_ROOT, 'qrcode') + if not os.path.exists(qr_dir): + os.makedirs(qr_dir) + path = os.path.join(qr_dir, f'{self.short_key}.png') img.save(path) self.image = os.path.join('qrcode', '{0}.png'.format(self.short_key)) self.activate() diff --git a/yaksh/templates/yaksh/question.html b/yaksh/templates/yaksh/question.html index 3b73781..17305b6 100644 --- a/yaksh/templates/yaksh/question.html +++ b/yaksh/templates/yaksh/question.html @@ -290,12 +290,13 @@ question_type = "{{ question.type }}"; </ul> </div> {% endif %} + <br> {% if qrcode %} - <img src="{{ qrcode.image.url }}"> + <img src="{{ qrcode.image.url }}" width="200" height="200"> {% else %} - <a class="active btn btn-outline-primary " href="#"data-toggle="tooltip" - title="Upload from any device using the QR Code" - onclick="call_skip('{% url 'yaksh:generate_qrcode' paper.id question.id module.id %}')">Generate QR Code</a> + <br> + <a class="active btn btn-outline-primary " href="{% url 'yaksh:generate_qrcode' paper.id question.id module.id %}" data-toggle="tooltip" + title="Upload from any device using the QR Code">Generate QR Code</a> {% endif %} <p></p> {% endif %} diff --git a/yaksh/templates/yaksh/upload_file.html b/yaksh/templates/yaksh/upload_file.html index 04d8b09..e25e8e7 100644 --- a/yaksh/templates/yaksh/upload_file.html +++ b/yaksh/templates/yaksh/upload_file.html @@ -1,16 +1,10 @@ +{% load static %} <html> <title> Upload File </title> - -<script> -function validate(){ - uploaded_file = document.getElementById("assignment").value; - if(uploaded_file == ""){ - $("#upload_alert").modal("show"); - return false; - } - return true; -} -</script> +<script language="JavaScript" type="text/javascript" src="{% static 'yaksh/js/jquery-3.3.1.min.js' %}"></script> +<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.8.1/dropzone.min.css"> +<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.8.1/basic.min.css"> +<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.8.1/min/dropzone.min.js"></script> <style> div, input, button { font-size: x-large; @@ -24,9 +18,61 @@ div, input, button { <form id="code" action="{% url 'yaksh:upload_file' key %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <h3>Upload assignment file for {{ question.summary }}</h3> - <input type=file id="assignment" name="assignment" multiple=""> - <button class="btn btn-success" type="submit" name="check" id="check" onClick="return validate();">Upload</button> + <div class="dropzone needsclick dz-clickable" id="dropzone_file"> + <div class="dz-message needsclick"> + <button type="button" class="dz-button"> + Drop files here or click to upload. + </button> + </div> + </div> + <br> + <button class="btn btn-success" type="submit" name="check" id="check">Upload</button> </form> {% endif %} </div> </html> +<script> + Dropzone.autoDiscover = false; + var submitfiles; + $(document).ready(function(){ + var filezone = $("div#dropzone_file").dropzone({ + url: $("#code").attr("action"), + parallelUploads: 10, + uploadMultiple: true, + maxFiles:20, + paramName: "assignment", + autoProcessQueue: false, + init: function() { + var submitButton = document.querySelector("#check"); + myDropzone = this; + submitButton.addEventListener("click", function(e) { + if (myDropzone.getQueuedFiles().length === 0) { + alert("Please select a file and upload"); + e.preventDefault(); + return; + } + if (myDropzone.getAcceptedFiles().length > 0) { + if (submitfiles === true) { + submitfiles = false; + return; + } + e.preventDefault(); + myDropzone.processQueue(); + myDropzone.on("complete", function () { + submitfiles = true; + $('#check').trigger('click'); + }); + } + }); + }, + success: function (file, response) { + document.open(); + document.write(response); + document.close(); + }, + headers: { + "X-CSRFToken": document.getElementById("code").elements[0].value + } + }); + }); +</script> diff --git a/yaksh/views.py b/yaksh/views.py index f8af247..a93f6e0 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -4158,11 +4158,13 @@ def generate_qrcode(request, answerpaper_id, question_id, module_id): handler = QRcodeHandler.objects.get_or_create(user=user, question=question, answerpaper=answerpaper)[0] qrcode = handler.get_qrcode() - content = '{0}/exam/upload_file/{1}'.format(URL_ROOT, qrcode.short_key) + content = request.build_absolute_uri( + reverse("yaksh:upload_file", args=[qrcode.short_key]) + ) qrcode.generate_image(content) qrcode.save() return show_question(request, question, answerpaper, - course_id=answerpaper.course.id, module_id=module_id, + course_id=answerpaper.course_id, module_id=module_id, previous_question=question) @@ -4175,36 +4177,36 @@ def upload_file(request, key): context['msg'] = 'Sorry, test time up!' return render(request, 'yaksh/upload_file.html', context) if request.method == 'POST': - assignment_filename = request.FILES.getlist('assignment') - if not assignment_filename: + assign_files = [] + assignments = request.FILES + for i in range(len(assignments)): + assign_files.append(assignments[f"assignment[{i}]"]) + if not assign_files: msg = 'Please upload assignment file' context['msg'] = msg return render(request, 'yaksh/upload_file.html', context) - for fname in assignment_filename: + AssignmentUpload.objects.filter( + assignmentQuestion_id=handler.question_id, + answer_paper_id=handler.answerpaper_id + ).delete() + uploaded_files = [] + for fname in assign_files: fname._name = fname._name.replace(" ", "_") - assignment_files = AssignmentUpload.objects.filter( - assignmentQuestion=handler.question, - course_id=handler.answerpaper.course.id, - assignmentFile__icontains=fname, user=handler.user, - question_paper=handler.answerpaper.question_paper) - if assignment_files.exists(): - assign_file = assignment_files.first() - if os.path.exists(assign_file.assignmentFile.path): - os.remove(assign_file.assignmentFile.path) - assign_file.delete() - AssignmentUpload.objects.create( - user=handler.user, - assignmentQuestion=handler.question, - course_id=handler.answerpaper.course.id, assignmentFile=fname, - question_paper=handler.answerpaper.question_paper) - qrcode.set_used() - qrcode.deactivate() - qrcode.save() - context['success'] = True - msg = "File Uploaded Successfully! Reload the (test)question "\ - "page to see the uploaded file" - context['msg'] = msg - return render(request, 'yaksh/upload_file.html', context) + uploaded_files.append( + AssignmentUpload( + assignmentQuestion_id=handler.question_id, + assignmentFile=fname, + answer_paper_id=handler.answerpaper_id) + ) + AssignmentUpload.objects.bulk_create(uploaded_files) + qrcode.set_used() + qrcode.deactivate() + qrcode.save() + context['success'] = True + msg = "File Uploaded Successfully! Reload the (test)question "\ + "page to see the uploaded file" + context['msg'] = msg + return render(request, 'yaksh/upload_file.html', context) return render(request, 'yaksh/upload_file.html', context) |