diff options
author | adityacp | 2016-09-20 14:56:55 +0530 |
---|---|---|
committer | adityacp | 2016-09-20 14:56:55 +0530 |
commit | 4d8a0b41935ecc182c90acf34e7696a207a664f6 (patch) | |
tree | 152a6ca5ae85169176a41c1221570217d11f3896 | |
parent | 2ac8383ceb3f8850043ab317a7a7ebd2bfcf3921 (diff) | |
download | online_test-4d8a0b41935ecc182c90acf34e7696a207a664f6.tar.gz online_test-4d8a0b41935ecc182c90acf34e7696a207a664f6.tar.bz2 online_test-4d8a0b41935ecc182c90acf34e7696a207a664f6.zip |
Demo course creation without initial fixtures
-rw-r--r-- | yaksh/models.py | 47 | ||||
-rw-r--r-- | yaksh/views.py | 38 |
2 files changed, 67 insertions, 18 deletions
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 diff --git a/yaksh/views.py b/yaksh/views.py index 3e42b64..959d323 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -256,15 +256,8 @@ def add_quiz(request, course_id, quiz_id=None): user = request.user course = get_object_or_404(Course, pk=course_id) ci = RequestContext(request) - if not is_moderator(user): - raise Http404('You are not allowed to view this page!') - try: - demo_user = User.objects.get(username="yaksh_demo_user") - except User.DoesNotExist: - demo_user = None - if course.creator != demo_user: - if (user != course.creator and user not in course.teachers.all()): - raise Http404('You are not allowed to view this course !') + if not is_moderator(user) or (user != course.creator and user not in course.teachers.all()): + raise Http404('You are not allowed to view this course !') context = {} if request.method == "POST": if quiz_id is None: @@ -688,18 +681,10 @@ def courses(request): ci = RequestContext(request) if not is_moderator(user): raise Http404('You are not allowed to view this page') - try: - demo_user = User.objects.get(username="yaksh_demo_user") - demo_course = Course.objects.get(creator=demo_user) - except User.DoesNotExist, Course.DoesNotExist: - demo_user = None - demo_course = None - courses = Course.objects.filter(creator=user, is_trial=False) allotted_courses = Course.objects.filter(teachers=user, is_trial=False) - context = {'courses': courses, "allotted_courses": allotted_courses, - 'demo_course': demo_course} + context = {'courses': courses, "allotted_courses": allotted_courses} return my_render_to_response('yaksh/courses.html', context, context_instance=ci) @@ -1309,3 +1294,20 @@ def view_answerpaper(request, questionpaper_id): return my_render_to_response('yaksh/view_answerpaper.html', context) else: return my_redirect('/exam/quizzes/') + + +@login_required +def create_demo_course(request): + """ creates a demo course for user """ + user = request.user + ci = RequestContext(request) + if not is_moderator(user): + raise("You are not allowed to view this page") + demo_course = Course() + success = demo_course.create_demo(user) + if success: + msg = "Created Demo course successfully" + else: + msg = "Demo course already created" + context = {'msg': msg} + return my_render_to_response('manage.html', context, context_instance=ci) |