summaryrefslogtreecommitdiff
path: root/yaksh/models.py
diff options
context:
space:
mode:
authorprathamesh2020-11-13 15:34:22 +0530
committerprathamesh2020-11-13 15:34:22 +0530
commitb0ab5d93393d191ffbc6e0c4b0cc3ebc2624fc27 (patch)
treecd2d5ee1b055dbeb3ad6b7b1b021d460db3dc20e /yaksh/models.py
parent1ad9f3176c2df25df1f8fae601eef657e39f8b46 (diff)
downloadonline_test-b0ab5d93393d191ffbc6e0c4b0cc3ebc2624fc27.tar.gz
online_test-b0ab5d93393d191ffbc6e0c4b0cc3ebc2624fc27.tar.bz2
online_test-b0ab5d93393d191ffbc6e0c4b0cc3ebc2624fc27.zip
Use QRcode to upload a file from any device
Diffstat (limited to 'yaksh/models.py')
-rw-r--r--yaksh/models.py81
1 files changed, 80 insertions, 1 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index e2b9952..e9b2c1b 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -26,6 +26,8 @@ import tempfile
from textwrap import dedent
from ast import literal_eval
import pandas as pd
+import qrcode
+import hashlib
# Django Imports
from django.db import models
@@ -2923,7 +2925,7 @@ class TOCManager(models.Manager):
topic = Topic.objects.create(**content)
toc.append(TableOfContents(
course_id=course_id, lesson_id=lesson_id, time=time,
- content_object=topic, content=content_type
+ content_object=topic, content=content_type
))
messages.append((True, f"{topic.name} added successfully"))
else:
@@ -3175,3 +3177,80 @@ class MicroManager(models.Model):
def __str__(self):
return 'MicroManager for {0} - {1}'.format(self.student.username,
self.course.name)
+
+
+class QRcode(models.Model):
+ random_key = models.CharField(max_length=128, blank=True)
+ short_key = models.CharField(max_length=128, null=True, unique=True)
+ image = models.ImageField(upload_to='qrcode', blank=True)
+ used = models.BooleanField(default=False)
+ active = models.BooleanField(default=False)
+ handler = models.ForeignKey('QRcodeHandler', on_delete=models.CASCADE)
+
+ def __str__(self):
+ return 'QRcode {0}'.format(self.short_key)
+
+ def is_active(self):
+ return self.active
+
+ def is_used(self):
+ return self.used
+
+ def deactivate(self):
+ self.active = False
+
+ def activate(self):
+ self.active = True
+
+ def set_used(self):
+ self.used = True
+
+ def set_random_key(self):
+ key = hashlib.sha1('{0}'.format(self.id).encode()).hexdigest()
+ self.random_key = key
+
+ def set_short_key(self):
+ key = self.random_key
+ if key:
+ num = 5
+ for i in range(40):
+ try:
+ self.short_key = key[0 : num]
+ break
+ except django.db.IntegrityError:
+ num = num + 1
+
+ def generate_image(self, content):
+ img = qrcode.make(content)
+ path = os.path.join(settings.MEDIA_ROOT, 'qrcode',
+ '{0}.png'.format(self.short_key))
+ img.save(path)
+ self.image = os.path.join('qrcode', '{0}.png'.format(self.short_key))
+ self.activate()
+
+
+class QRcodeHandler(models.Model):
+ user = models.ForeignKey(User, on_delete=models.CASCADE)
+ answerpaper = models.ForeignKey(AnswerPaper, on_delete=models.CASCADE)
+ question = models.ForeignKey(Question, on_delete=models.CASCADE)
+
+
+ def __str__(self):
+ return 'QRcode Handler for {0}'.format(self.user.username)
+
+ def get_qrcode(self):
+ qrcodes = self.qrcode_set.filter(active=True, used=False)
+ if qrcodes.exists():
+ return qrcodes.last()
+ else:
+ return self._create_qrcode()
+
+ def _create_qrcode(self):
+ qrcode = QRcode.objects.create(handler=self)
+ qrcode.set_random_key()
+ qrcode.set_short_key()
+ return qrcode
+
+ def can_use(self):
+ return self.answerpaper.is_attempt_inprogress()
+