summaryrefslogtreecommitdiff
path: root/yaksh/models.py
diff options
context:
space:
mode:
authoradityacp2016-06-20 14:50:58 +0530
committeradityacp2016-07-28 15:56:06 +0530
commitd27b168e6fa0789891acf8952a60a7c200caef44 (patch)
treeb99332a7937a14789e5a3e4ba556e840898bf51f /yaksh/models.py
parent542f862445b65f56704009e324ed39a7eec23dad (diff)
downloadonline_test-d27b168e6fa0789891acf8952a60a7c200caef44.tar.gz
online_test-d27b168e6fa0789891acf8952a60a7c200caef44.tar.bz2
online_test-d27b168e6fa0789891acf8952a60a7c200caef44.zip
added files upload for questions
Diffstat (limited to 'yaksh/models.py')
-rw-r--r--yaksh/models.py42
1 files changed, 40 insertions, 2 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index 4ee6141..20d8716 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 = QuestionsFileUpload.objects.filter(question=self)
+ if files:
+ question_data['file_paths'] = [(file.files.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,35 @@ class Question(models.Model):
###############################################################################
+class QuestionsFileUpload(models.Model):
+ files = models.FileField(upload_to=get_upload_dir, blank=True)
+ question = models.ForeignKey(Question, related_name="question")
+ extract = models.BooleanField(default=False)
+
+ def delete_all_files(self, files):
+ for file in files:
+ if os.path.exists(file.files.path):
+ shutil.rmtree(os.path.dirname(file.files.path))
+ file.delete()
+
+ def delete_selected_files(self, files):
+ for file in files:
+ if os.path.exists(file.files.path):
+ os.remove(file.files.path)
+ if os.listdir(os.path.dirname(file.files.path)) == []:
+ os.rmdir(os.path.dirname(file.files.path))
+ file.delete()
+
+ def extract_files(self, files):
+ for file in files:
+ if file.extract:
+ file.extract = False
+ else:
+ file.extract = True
+ file.save()
+
+
+###############################################################################
class Answer(models.Model):
"""Answers submitted by the users."""