summaryrefslogtreecommitdiff
path: root/yaksh/models.py
diff options
context:
space:
mode:
authorKing2016-07-28 17:39:44 +0530
committerGitHub2016-07-28 17:39:44 +0530
commit2b03aeb36fa333ea1644a248c742cf0c1df12a5f (patch)
tree4c753ba9a1fbccf3e06d1ed1d998137c88a73b88 /yaksh/models.py
parent344e1e804cee214c0d0f5c41ca16d871e786d4c3 (diff)
parent83bee9d89e163e98504c8aa210ce60200bd1cd1d (diff)
downloadonline_test-2b03aeb36fa333ea1644a248c742cf0c1df12a5f.tar.gz
online_test-2b03aeb36fa333ea1644a248c742cf0c1df12a5f.tar.bz2
online_test-2b03aeb36fa333ea1644a248c742cf0c1df12a5f.zip
Merge pull request #113 from adityacp/file_based_questions
File based questions
Diffstat (limited to 'yaksh/models.py')
-rw-r--r--yaksh/models.py34
1 files changed, 32 insertions, 2 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index 4ee6141..73d4b27 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -11,6 +11,8 @@ from django.contrib.contenttypes.models import ContentType
from taggit.managers import TaggableManager
from django.utils import timezone
import pytz
+import os
+import shutil
languages = (
("python", "Python"),
@@ -62,6 +64,9 @@ def has_profile(user):
""" check if user has profile """
return True if hasattr(user, 'profile') else False
+def get_upload_dir(instance, filename):
+ return "question_%s/%s" % (instance.question.id, filename)
+
###############################################################################
class CourseManager(models.Manager):
@@ -216,6 +221,10 @@ class Question(models.Model):
question_data['test_case_data'] = test_case_data
question_data['user_answer'] = user_answer
+ files = FileUpload.objects.filter(question=self)
+ if files:
+ question_data['file_paths'] = [(file.file.path, file.extract)
+ for file in files]
return json.dumps(question_data)
@@ -252,7 +261,7 @@ class Question(models.Model):
model=self.test_case_type
)
test_cases = test_case_ctype.get_all_objects_for_this_type(
- question=self,
+ question=self,
**kwargs
)
@@ -263,7 +272,7 @@ class Question(models.Model):
model=self.test_case_type
)
test_case = test_case_ctype.get_object_for_this_type(
- question=self,
+ question=self,
**kwargs
)
@@ -274,6 +283,27 @@ class Question(models.Model):
###############################################################################
+class FileUpload(models.Model):
+ file = models.FileField(upload_to=get_upload_dir, blank=True)
+ question = models.ForeignKey(Question, related_name="question")
+ extract = models.BooleanField(default=False)
+
+ def remove(self):
+ if os.path.exists(self.file.path):
+ os.remove(self.file.path)
+ if os.listdir(os.path.dirname(self.file.path)) == []:
+ os.rmdir(os.path.dirname(self.file.path))
+ self.delete()
+
+ def set_extract_status(self):
+ if self.extract:
+ self.extract = False
+ else:
+ self.extract = True
+ self.save()
+
+
+###############################################################################
class Answer(models.Model):
"""Answers submitted by the users."""