summaryrefslogtreecommitdiff
path: root/yaksh
diff options
context:
space:
mode:
authoradityacp2016-08-26 11:31:33 +0530
committeradityacp2016-08-26 11:31:33 +0530
commit74745309a065399f6bd33544de8705f0004edf30 (patch)
treef3e72c2dc8c752efe31c7393a158bb995328d9f7 /yaksh
parentc231dcb006c7dc6db4dc0312f53447971776635f (diff)
downloadonline_test-74745309a065399f6bd33544de8705f0004edf30.tar.gz
online_test-74745309a065399f6bd33544de8705f0004edf30.tar.bz2
online_test-74745309a065399f6bd33544de8705f0004edf30.zip
changed models and views questions dump and load
Diffstat (limited to 'yaksh')
-rw-r--r--yaksh/models.py35
-rw-r--r--yaksh/views.py4
2 files changed, 20 insertions, 19 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index d626b26..5bb44fa 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -232,14 +232,14 @@ class Question(models.Model):
return json.dumps(question_data)
- def dump_into_json(self, question_ids, user):
+ def dump_questions(self, question_ids, user):
questions = Question.objects.filter(id__in=question_ids, user_id=user.id)
questions_dict = []
zip_file_name = StringIO()
zip_file = zipfile.ZipFile(zip_file_name, "a")
for question in questions:
test_case = question.get_test_cases()
- files = question._add_and_get_files(zip_file)
+ file_names = question._add_and_get_files(zip_file)
q_dict = {'summary': question.summary,
'description': question.description,
'points': question.points, 'language': question.language,
@@ -247,20 +247,20 @@ class Question(models.Model):
'test_case_type': question.test_case_type,
'snippet': question.snippet,
'testcase': [case.get_field_value() for case in test_case],
- 'files': files}
+ 'files': file_names}
questions_dict.append(q_dict)
question._add_json_to_zip(zip_file, questions_dict)
return zip_file_name
- def load_from_json(self, questions_list, user):
+ def load_questions(self, questions_list, user):
questions = json.loads(questions_list)
- ques = Question()
for question in questions:
question['user'] = user
- files = question.pop('files')
+ file_names = question.pop('files')
test_cases = question.pop('testcase')
que, result = Question.objects.get_or_create(**question)
- que._add_files_to_db(files)
+ if file_names:
+ que._add_files_to_db(file_names)
model_class = get_model_class(que.test_case_type)
for test_case in test_cases:
model_class.objects.get_or_create(question=que, **test_case)
@@ -289,18 +289,19 @@ class Question(models.Model):
def _add_and_get_files(self, zip_file):
files = FileUpload.objects.filter(question=self)
- for file in files:
- zip_file.write(file.file.path, (os.path.basename(file.file.path)))
- files_list = [os.path.basename(file.file.name) for file in files]
+ files_list = []
+ for f in files:
+ zip_file.write(f.file.path, (os.path.basename(f.file.path)))
+ files_list = os.path.basename(f.file.path)
return files_list
- def _add_files_to_db(self, files):
- if files:
- for file_name in files:
- file = open(file_name, 'r')
- django_file = File(file)
- f = FileUpload.objects.get_or_create(file=django_file, question=self)
- os.remove(file_name)
+ def _add_files_to_db(self, file_names):
+ for file_name in file_names:
+ que_file = open(file_name, 'r')
+ #Converting to Python file object with some Django-specific additions
+ django_file = File(que_file)
+ f = FileUpload.objects.get_or_create(file=django_file, question=self)
+ os.remove(file_name)
def _add_json_to_zip(self, zip_file, q_dict):
json_data = json.dumps(q_dict, indent=2)
diff --git a/yaksh/views.py b/yaksh/views.py
index 87e6005..616c3cc 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -94,7 +94,7 @@ def read_json(json_file, user):
if os.path.exists(json_file):
with open(json_file, 'r') as q_file:
questions_list = q_file.read()
- question.load_from_json(questions_list, user)
+ question.load_questions(questions_list, user)
os.remove(json_file)
@@ -901,7 +901,7 @@ def show_all_questions(request):
question_ids = request.POST.getlist('question')
if question_ids:
question = Question()
- zip_file = question.dump_into_json(question_ids, user)
+ zip_file = question.dump_questions(question_ids, user)
response = HttpResponse(content_type='application/zip')
response['Content-Disposition'] = '''attachment;\
filename={0}_questions.zip'''.format(user)