diff options
Diffstat (limited to 'yaksh/views.py')
-rw-r--r-- | yaksh/views.py | 240 |
1 files changed, 231 insertions, 9 deletions
diff --git a/yaksh/views.py b/yaksh/views.py index 01f5e4e..652e08c 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -38,14 +38,18 @@ from yaksh.models import ( StdIOBasedTestCase, StringTestCase, TestCase, User, get_model_class, FIXTURES_DIR_PATH, MOD_GROUP_NAME, Lesson, LessonFile, LearningUnit, LearningModule, CourseStatus, question_types, Post, Comment, +<<<<<<< HEAD + Topic, TableOfContents +======= MicroManager +>>>>>>> 23bf46ac2e262ceb373388196962a0bec048c6cb ) from yaksh.forms import ( UserRegisterForm, UserLoginForm, QuizForm, QuestionForm, QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm, FileForm, QuestionPaperForm, LessonForm, LessonFileForm, LearningModuleForm, ExerciseForm, TestcaseForm, - SearchFilterForm, PostForm, CommentForm + SearchFilterForm, PostForm, CommentForm, TopicForm, VideoQuizForm ) from yaksh.settings import SERVER_POOL_PORT, SERVER_HOST_NAME from .settings import URL_ROOT @@ -2646,6 +2650,8 @@ def edit_lesson(request, course_id=None, module_id=None, lesson_id=None): raise Http404('This Lesson does not belong to you') context = {} + lesson_form = LessonForm(instance=lesson) + lesson_files_form = LessonFileForm() if request.method == "POST": if "Save" in request.POST: lesson_form = LessonForm(request.POST, request.FILES, @@ -2685,10 +2691,6 @@ def edit_lesson(request, course_id=None, module_id=None, lesson_id=None): reverse("yaksh:edit_lesson", args=[course_id, module_id, lesson.id]) ) - else: - context['lesson_form'] = lesson_form - context['error'] = lesson_form["video_file"].errors - context['lesson_file_form'] = lesson_file_form if 'Delete' in request.POST: remove_files_id = request.POST.getlist('delete_files') @@ -2704,9 +2706,15 @@ def edit_lesson(request, course_id=None, module_id=None, lesson_id=None): request, "Please select atleast one file to delete" ) + contents = TableOfContents.objects.filter( + course_id=course_id, lesson_id=lesson_id + ) + data = loader.render_to_string( + "yaksh/show_toc.html", context={'contents': contents}, + request=request + ) + context['toc'] = data lesson_files = LessonFile.objects.filter(lesson=lesson) - lesson_files_form = LessonFileForm() - lesson_form = LessonForm(instance=lesson) context['lesson_form'] = lesson_form context['lesson_file_form'] = lesson_files_form context['lesson_files'] = lesson_files @@ -2749,7 +2757,9 @@ def show_lesson(request, lesson_id, module_id, course_id): # update course status with current unit _update_unit_status(course_id, user, learn_unit) - + toc = TableOfContents.objects.filter( + course_id=course_id, lesson_id=lesson_id + ) all_modules = course.get_learning_modules() if learn_unit.has_prerequisite(): if not learn_unit.is_prerequisite_complete(user, learn_module, course): @@ -2758,7 +2768,7 @@ def show_lesson(request, lesson_id, module_id, course_id): context = {'lesson': learn_unit.lesson, 'user': user, 'course': course, 'state': "lesson", "all_modules": all_modules, 'learning_units': learning_units, "current_unit": learn_unit, - 'learning_module': learn_module} + 'learning_module': learn_module, 'toc': toc} return my_render_to_response(request, 'yaksh/show_video.html', context) @@ -3494,6 +3504,72 @@ def hide_comment(request, course_id, uuid): @login_required @email_verified +<<<<<<< HEAD +def add_marker(request, course_id, lesson_id): + user = request.user + course = get_object_or_404(Course, pk=course_id) + if (not is_moderator(user) or + not course.is_creator(user) or not course.is_creator(user)): + raise Http404("You are not allowed to view this page") + content_type = request.POST.get("content") + question_type = request.POST.get("type") + if content_type == '1': + form = TopicForm() + template_name = 'yaksh/add_topic.html' + status = 1 + formset = None + tc_class = None + else: + if not question_type: + question_type = "mcq" + form = VideoQuizForm(question_type=question_type) + formset, tc_class = get_tc_formset(question_type) + template_name = 'yaksh/add_video_quiz.html' + status = 2 + context = {'form': form, 'course_id': course.id, 'lesson_id': lesson_id, + 'formset': formset, 'tc_class': tc_class, + 'content_type': content_type} + data = loader.render_to_string( + template_name, context=context, request=request + ) + return JsonResponse( + {'success': True, 'data': data, 'content_type': content_type, + 'status': status} + ) + + +def get_tc_formset(question_type, post=None, question=None): + tc, tc_class = McqTestCase, 'mcqtestcase' + if question_type == 'mcq' or question_type == 'mcc': + tc, tc_class = McqTestCase, 'mcqtestcase' + elif question_type == 'integer': + tc, tc_class = IntegerTestCase, 'integertestcase' + elif question_type == 'float': + tc, tc_class = FloatTestCase, 'floattestcase' + elif question_type == 'string': + tc, tc_class = StringTestCase, 'stringtestcase' + TestcaseFormset = inlineformset_factory( + Question, tc, form=TestcaseForm, extra=1, fields="__all__", + ) + formset = TestcaseFormset( + post, initial=[{'type': tc_class}], instance=question + ) + return formset, tc_class + + +def get_toc_contents(request, course_id, lesson_id): + contents = TableOfContents.objects.filter( + course_id=course_id, lesson_id=lesson_id + ) + data = loader.render_to_string( + "yaksh/show_toc.html", context={'contents': contents}, + request=request + ) + return data + + +@login_required +@email_verified def allow_special_attempt(request, user_id, course_id, quiz_id): user = request.user @@ -3536,6 +3612,135 @@ def allow_special_attempt(request, user_id, course_id, quiz_id): @login_required @email_verified +def add_topic(request, content_type, course_id, lesson_id, toc_id=None, + topic_id=None): + user = request.user + course = get_object_or_404(Course, pk=course_id) + if (not is_moderator(user) or + not course.is_creator(user) or not course.is_creator(user)): + raise Http404("You are not allowed to view this page") + if topic_id: + topic = get_object_or_404(Topic, pk=topic_id) + else: + topic = None + if toc_id: + toc = get_object_or_404(TableOfContents, pk=toc_id) + else: + toc = None + context = {} + if request.method == "POST": + form = TopicForm(request.POST, instance=topic) + if form.is_valid(): + form.save() + time = request.POST.get("timer") + if not topic: + TableOfContents.objects.create( + content_object=form.instance, course_id=course_id, + lesson_id=lesson_id, content=content_type, + time=time + ) + context['toc'] = get_toc_contents(request, course_id, lesson_id) + if toc: + toc.time = time + toc.save() + status_code = 200 + context['success'] = True + context['message'] = 'Added topic successfully' + else: + status_code = 400 + context['success'] = False + context['message'] = form.errors.as_json() + else: + form = TopicForm(instance=topic, time=toc.time) + template_context = {'form': form, 'course_id': course.id, + 'lesson_id': lesson_id, 'content_type': content_type, + 'topic_id': topic_id, 'toc_id': toc_id} + data = loader.render_to_string( + "yaksh/add_topic.html", context=template_context, request=request + ) + context['success'] = True + context['data'] = data + context['content_type'] = content_type + context['status'] = 1 + status_code = 200 + return JsonResponse(context, status=status_code) + + +@login_required +@email_verified +def add_marker_quiz(request, content_type, course_id, lesson_id, + toc_id=None, question_id=None): + user = request.user + course = get_object_or_404(Course, pk=course_id) + if (not is_moderator(user) or + not course.is_creator(user) or not course.is_creator(user)): + raise Http404("You are not allowed to view this page") + if question_id: + question = get_object_or_404(Question, pk=question_id) + else: + question = None + if toc_id: + toc = get_object_or_404(TableOfContents, pk=toc_id) + else: + toc = None + context = {} + if request.method == "POST": + qform = VideoQuizForm(request.POST, instance=question) + if qform.is_valid(): + if not question_id: + qform.save(commit=False) + qform.instance.user = user + qform.save() + formset, tc_class = get_tc_formset( + qform.instance.type, request.POST, qform.instance + ) + if formset.is_valid(): + formset.save() + time = request.POST.get("timer") + if not question: + TableOfContents.objects.create( + content_object=qform.instance, course_id=course_id, + lesson_id=lesson_id, content=content_type, + time=time + ) + context['toc'] = get_toc_contents(request, course_id, lesson_id) + if toc: + toc.time = time + toc.save() + status_code = 200 + context['success'] = True + context['message'] = 'Added question successfully' + context['content_type'] = content_type + else: + status_code = 400 + context['success'] = False + context['message'] = "Error in saving form" + else: + status_code = 400 + context['success'] = False + context['message'] = qform.errors.as_json() + else: + form = VideoQuizForm(instance=question, time=toc.time) + formset, tc_class = get_tc_formset(question.type, question=question) + template_context = { + 'form': form, 'course_id': course.id, 'lesson_id': lesson_id, + 'formset': formset, 'tc_class': tc_class, 'toc_id': toc_id, + 'content_type': content_type, 'question_id': question_id + } + data = loader.render_to_string( + "yaksh/add_video_quiz.html", context=template_context, + request=request + ) + context['success'] = True + context['data'] = data + context['content_type'] = content_type + context['status'] = 2 + status_code = 200 + return JsonResponse(context, status=status_code) + + +@login_required +@email_verified def revoke_special_attempt(request, micromanager_id): user = request.user @@ -3556,6 +3761,23 @@ def revoke_special_attempt(request, micromanager_id): @login_required @email_verified +def delete_toc(request, course_id, toc_id): + user = request.user + course = get_object_or_404(Course, pk=course_id) + if (not is_moderator(user) or + not course.is_creator(user) or not course.is_creator(user)): + raise Http404("You are not allowed to view this page") + toc = get_object_or_404(TableOfContents, pk=toc_id) + redirect_url = request.POST.get("redirect_url") + if toc.content == 1: + get_object_or_404(Topic, pk=toc.object_id).delete() + else: + get_object_or_404(Question, id=toc.object_id).delete() + return redirect(redirect_url) + + +@login_required +@email_verified def extend_time(request, paper_id): user = request.user |