From 2a0909ef9d1107ec09a870947946cb336f0373c3 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 25 Aug 2016 16:23:43 +0530 Subject: added upload and download questions with files --- yaksh/models.py | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index 73d4b27..d626b26 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -10,9 +10,13 @@ from django.forms.models import model_to_dict from django.contrib.contenttypes.models import ContentType from taggit.managers import TaggableManager from django.utils import timezone +from django.core.files import File +from StringIO import StringIO import pytz import os import shutil +import zipfile + languages = ( ("python", "Python"), @@ -231,27 +235,32 @@ class Question(models.Model): def dump_into_json(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) q_dict = {'summary': question.summary, 'description': question.description, - 'points': question.points, - 'language': question.language, - 'type': question.type, - 'active': question.active, + 'points': question.points, 'language': question.language, + 'type': question.type, 'active': question.active, 'test_case_type': question.test_case_type, 'snippet': question.snippet, - 'testcase': [case.get_field_value() for case in test_case]} + 'testcase': [case.get_field_value() for case in test_case], + 'files': files} questions_dict.append(q_dict) - - return json.dumps(questions_dict, indent=2) + question._add_json_to_zip(zip_file, questions_dict) + return zip_file_name def load_from_json(self, questions_list, user): questions = json.loads(questions_list) + ques = Question() for question in questions: question['user'] = user + files = question.pop('files') test_cases = question.pop('testcase') que, result = Question.objects.get_or_create(**question) + que._add_files_to_db(files) 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) @@ -278,6 +287,29 @@ class Question(models.Model): return test_case + 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] + 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_json_to_zip(self, zip_file, q_dict): + json_data = json.dumps(q_dict, indent=2) + with open("questions_dump.json", "w") as json_file: + json_file.write(json_data) + zip_file.write(json_file.name) + zip_file.close() + os.remove(json_file.name) + def __unicode__(self): return self.summary -- cgit From 74745309a065399f6bd33544de8705f0004edf30 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 26 Aug 2016 11:31:33 +0530 Subject: changed models and views questions dump and load --- yaksh/models.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'yaksh/models.py') 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) -- cgit From 4eb9778454dc5ecec99ccf951f012a69582b0ca3 Mon Sep 17 00:00:00 2001 From: adityacp Date: Tue, 30 Aug 2016 22:44:16 +0530 Subject: added read_json func in Question model --- yaksh/models.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index e296524..4fb77fd 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -293,15 +293,17 @@ class Question(models.Model): files_list = [] for f in files: zip_file.write(f.file.path, (os.path.basename(f.file.path))) - files_list.append(os.path.basename(f.file.path)) + files_list.append(((os.path.basename(f.file.path)), f.extract)) return files_list def _add_files_to_db(self, file_names): - for file_name in file_names: + for file_name, extract 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) + f = FileUpload.objects.get_or_create(file=django_file, + question=self, + extract=extract) os.remove(file_name) def _add_json_to_zip(self, zip_file, q_dict): @@ -314,6 +316,13 @@ class Question(models.Model): zip_file.close() shutil.rmtree(tmp_file_path) + def read_json(self, json_file, user): + if os.path.exists(json_file): + with open(json_file, 'r') as q_file: + questions_list = q_file.read() + self.load_questions(questions_list, user) + os.remove(json_file) + def __unicode__(self): return self.summary -- cgit From 4d8a0b41935ecc182c90acf34e7696a207a664f6 Mon Sep 17 00:00:00 2001 From: adityacp Date: Tue, 20 Sep 2016 14:56:55 +0530 Subject: Demo course creation without initial fixtures --- yaksh/models.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index e7a60df..870d8b9 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -17,6 +17,7 @@ import os import shutil import zipfile import tempfile +from file_utils import extract_files languages = ( @@ -155,6 +156,23 @@ class Course(models.Model): def remove_teachers(self, *teachers): self.teachers.remove(*teachers) + def create_demo(self, user): + course = Course.objects.filter(creator=user, name="Yaksh Demo course") + if not course: + course, c_status= Course.objects.get_or_create(name="Yaksh Demo course", + enrollment="open", + creator=user) + quiz = Quiz() + demo_quiz = quiz.create_demo_quiz(course) + demo_ques = Question() + demo_ques.create_demo_questions(user) + demo_que_ppr = QuestionPaper() + demo_que_ppr.create_demo_que_ppr(demo_quiz) + success = True + else: + success = False + return success + def __unicode__(self): return self.name @@ -323,6 +341,13 @@ class Question(models.Model): self.load_questions(questions_list, user) os.remove(json_file) + def create_demo_questions(self, user): + zip_file_path = os.path.join(os.getcwd(), 'yaksh', + 'fixtures', 'demo_questions.zip') + extract_files(zip_file_path) + self.read_json("questions_dump.json", user) + + def __unicode__(self): return self.summary @@ -487,6 +512,17 @@ class Quiz(models.Model): def has_prerequisite(self): return True if self.prerequisite else False + + def create_demo_quiz(self, course): + demo_quiz = Quiz.objects.get_or_create(start_date_time=timezone.now(), + end_date_time=timezone.now() + timedelta(176590), + duration=30, active=True, + attempts_allowed=-1, + time_between_attempts=0, + description='Yaksh Demo quiz', pass_criteria=0, + language='Python', prerequisite=None, + course=course) + return demo_quiz def __unicode__(self): desc = self.description or 'Quiz' @@ -618,6 +654,17 @@ class QuestionPaper(models.Model): if self.quiz.has_prerequisite(): prerequisite = self._get_prequisite_paper() return prerequisite._is_questionpaper_passed(user) + + def create_demo_que_ppr(self, demo_quiz): + question_paper = QuestionPaper.objects.get_or_create(quiz=demo_quiz[0], + total_marks=5.0, + shuffle_questions=True + ) + questions = Question.objects.filter(active=True, + summary="Yaksh Demo Question") + # add fixed set of questions to the question paper + for question in questions: + question_paper[0].fixed_questions.add(question) def __unicode__(self): return "Question Paper for " + self.quiz.description -- cgit From a689afd85ef3f0e4ddb8255e06c50f400bebd165 Mon Sep 17 00:00:00 2001 From: adityacp Date: Tue, 20 Sep 2016 16:09:54 +0530 Subject: used create method instead of get_or_create to create course --- yaksh/models.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index 870d8b9..d176c57 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -159,9 +159,9 @@ class Course(models.Model): def create_demo(self, user): course = Course.objects.filter(creator=user, name="Yaksh Demo course") if not course: - course, c_status= Course.objects.get_or_create(name="Yaksh Demo course", - enrollment="open", - creator=user) + course = Course.objects.create(name="Yaksh Demo course", + enrollment="open", + creator=user) quiz = Quiz() demo_quiz = quiz.create_demo_quiz(course) demo_ques = Question() @@ -514,7 +514,7 @@ class Quiz(models.Model): return True if self.prerequisite else False def create_demo_quiz(self, course): - demo_quiz = Quiz.objects.get_or_create(start_date_time=timezone.now(), + demo_quiz = Quiz.objects.create(start_date_time=timezone.now(), end_date_time=timezone.now() + timedelta(176590), duration=30, active=True, attempts_allowed=-1, @@ -656,15 +656,15 @@ class QuestionPaper(models.Model): return prerequisite._is_questionpaper_passed(user) def create_demo_que_ppr(self, demo_quiz): - question_paper = QuestionPaper.objects.get_or_create(quiz=demo_quiz[0], - total_marks=5.0, - shuffle_questions=True - ) + question_paper = QuestionPaper.objects.create(quiz=demo_quiz, + total_marks=5.0, + shuffle_questions=True + ) questions = Question.objects.filter(active=True, summary="Yaksh Demo Question") # add fixed set of questions to the question paper for question in questions: - question_paper[0].fixed_questions.add(question) + question_paper.fixed_questions.add(question) def __unicode__(self): return "Question Paper for " + self.quiz.description -- cgit From e1811e3560742a4070f3e92e059a7cd1c50f1f36 Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 21 Sep 2016 14:38:00 +0530 Subject: changed func name from create_demo_que_ppr to create_demo_quiz_paper --- yaksh/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index d176c57..71cd258 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -167,7 +167,7 @@ class Course(models.Model): demo_ques = Question() demo_ques.create_demo_questions(user) demo_que_ppr = QuestionPaper() - demo_que_ppr.create_demo_que_ppr(demo_quiz) + demo_que_ppr.create_demo_quiz_ppr(demo_quiz) success = True else: success = False @@ -655,7 +655,7 @@ class QuestionPaper(models.Model): prerequisite = self._get_prequisite_paper() return prerequisite._is_questionpaper_passed(user) - def create_demo_que_ppr(self, demo_quiz): + def create_demo_quiz_ppr(self, demo_quiz): question_paper = QuestionPaper.objects.create(quiz=demo_quiz, total_marks=5.0, shuffle_questions=True -- cgit