diff options
Diffstat (limited to 'yaksh/views.py')
-rw-r--r-- | yaksh/views.py | 193 |
1 files changed, 73 insertions, 120 deletions
diff --git a/yaksh/views.py b/yaksh/views.py index b54461f..51f6e54 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -176,15 +176,14 @@ def quizlist_user(request, enrolled=None, msg=None): courses = hidden_courses title = 'Search Results' else: - courses = list(Course.objects.filter( - active=True, is_trial=False, + enrolled_courses = user.students.filter(is_trial=False).order_by('-id') + remaining_courses = list(Course.objects.filter( + active=True, is_trial=False, hidden=False ).exclude( - ~Q(requests=user), ~Q(rejected=user), hidden=True - ).order_by('-id')) - enrolled_course = list( - user.students.filter(is_trial=False).order_by('-id') - ) - courses.extend(enrolled_course) + id__in=enrolled_courses.values_list("id", flat=True) + ).order_by('-id')) + courses = list(enrolled_courses) + courses.extend(remaining_courses) title = 'All Courses' for course in courses: @@ -319,7 +318,7 @@ def add_question(request, question_id=None): @login_required @email_verified -def add_quiz(request, quiz_id=None, course_id=None): +def add_quiz(request, course_id=None, module_id=None, quiz_id=None): """To add a new quiz in the database. Create a new quiz and store it.""" user = request.user @@ -331,6 +330,8 @@ def add_quiz(request, quiz_id=None, course_id=None): raise Http404('This quiz does not belong to you') else: quiz = None + if module_id: + module = get_object_or_404(LearningModule, id=module_id) if course_id: course = get_object_or_404(Course, pk=course_id) if not course.is_creator(user) and not course.is_teacher(user): @@ -341,9 +342,22 @@ def add_quiz(request, quiz_id=None, course_id=None): form = QuizForm(request.POST, instance=quiz) if form.is_valid(): if quiz is None: + last_unit = module.get_learning_units().last() + order = last_unit.order + 1 if last_unit else 1 form.instance.creator = user - form.save() + else: + order = module.get_unit_order("quiz", quiz) + added_quiz = form.save() + unit, created = LearningUnit.objects.get_or_create( + type="quiz", quiz=added_quiz, order=order + ) + if created: + module.learning_unit.add(unit.id) messages.success(request, "Quiz saved successfully") + return redirect( + reverse("yaksh:edit_quiz", + args=[course_id, module_id, added_quiz.id]) + ) else: form = QuizForm(instance=quiz) context["course_id"] = course_id @@ -354,7 +368,7 @@ def add_quiz(request, quiz_id=None, course_id=None): @login_required @email_verified -def add_exercise(request, quiz_id=None, course_id=None): +def add_exercise(request, course_id=None, module_id=None, quiz_id=None): user = request.user if not is_moderator(user): raise Http404('You are not allowed to view this course !') @@ -364,6 +378,8 @@ def add_exercise(request, quiz_id=None, course_id=None): raise Http404('This quiz does not belong to you') else: quiz = None + if module_id: + module = get_object_or_404(LearningModule, id=module_id) if course_id: course = get_object_or_404(Course, pk=course_id) if not course.is_creator(user) and not course.is_teacher(user): @@ -374,7 +390,11 @@ def add_exercise(request, quiz_id=None, course_id=None): form = ExerciseForm(request.POST, instance=quiz) if form.is_valid(): if quiz is None: + last_unit = module.get_learning_units().last() + order = last_unit.order + 1 if last_unit else 1 form.instance.creator = user + else: + order = module.get_unit_order("quiz", quiz) quiz = form.save(commit=False) quiz.is_exercise = True quiz.time_between_attempts = 0 @@ -384,9 +404,18 @@ def add_exercise(request, quiz_id=None, course_id=None): quiz.duration = 1000 quiz.pass_criteria = 0 quiz.save() + unit, created = LearningUnit.objects.get_or_create( + type="quiz", quiz=quiz, order=order + ) + if created: + module.learning_unit.add(unit.id) messages.success( request, "{0} saved successfully".format(quiz.description) ) + return redirect( + reverse("yaksh:edit_exercise", + args=[course_id, module_id, quiz.id]) + ) else: form = ExerciseForm(instance=quiz) context["exercise"] = quiz @@ -1365,8 +1394,7 @@ def _get_questions_from_tags(question_tags, user, active=True): @login_required @email_verified -def design_questionpaper(request, quiz_id, questionpaper_id=None, - course_id=None): +def design_questionpaper(request, course_id, quiz_id, questionpaper_id=None): user = request.user que_tags = Question.objects.filter( active=True, user=user).values_list('tags', flat=True).distinct() @@ -2444,7 +2472,7 @@ def download_yaml_template(request): @login_required @email_verified -def edit_lesson(request, lesson_id=None, course_id=None): +def edit_lesson(request, course_id=None, module_id=None, lesson_id=None): user = request.user if not is_moderator(user): raise Http404('You are not allowed to view this page!') @@ -2454,13 +2482,13 @@ def edit_lesson(request, lesson_id=None, course_id=None): raise Http404('This Lesson does not belong to you') else: lesson = None + if module_id: + module = get_object_or_404(LearningModule, id=module_id) if course_id: course = get_object_or_404(Course, id=course_id) if not course.is_creator(user) and not course.is_teacher(user): raise Http404('This Lesson does not belong to you') - redirect_url = reverse("yaksh:get_course_modules", args=[course_id]) - else: - redirect_url = reverse("yaksh:show_all_lessons") + context = {} if request.method == "POST": if "Save" in request.POST: @@ -2476,7 +2504,11 @@ def edit_lesson(request, lesson_id=None, course_id=None): lesson.remove_file() if lesson_form.is_valid(): if lesson is None: + last_unit = module.get_learning_units().last() + order = last_unit.order + 1 if last_unit else 1 lesson_form.instance.creator = user + else: + order = module.get_unit_order("lesson", lesson) lesson = lesson_form.save() lesson.html_data = get_html_text(lesson.description) lesson.save() @@ -2485,9 +2517,18 @@ def edit_lesson(request, lesson_id=None, course_id=None): LessonFile.objects.get_or_create( lesson=lesson, file=les_file ) + unit, created = LearningUnit.objects.get_or_create( + type="lesson", lesson=lesson, order=order + ) + if created: + module.learning_unit.add(unit.id) messages.success( request, "Saved {0} successfully".format(lesson.name) ) + return redirect( + reverse("yaksh:edit_lesson", + args=[course_id, module_id, lesson.id]) + ) else: context['lesson_form'] = lesson_form context['error'] = lesson_form["video_file"].errors @@ -2591,14 +2632,14 @@ def design_module(request, module_id, course_id=None): for order, value in enumerate(add_values, start_val): learning_id, type = value.split(":") if type == "quiz": - learning_unit = LearningUnit.objects.create( + unit, status = LearningUnit.objects.get_or_create( order=order, quiz_id=learning_id, type=type) else: - learning_unit = LearningUnit.objects.create( + unit, status = LearningUnit.objects.get_or_create( order=order, lesson_id=learning_id, type=type) - to_add_list.append(learning_unit) + to_add_list.append(unit) learning_module.learning_unit.add(*to_add_list) messages.success(request, "Lesson/Quiz added successfully") else: @@ -2661,21 +2702,20 @@ def design_module(request, module_id, course_id=None): context['status'] = 'design' context['module_id'] = module_id context['course_id'] = course_id + context['module'] = learning_module return my_render_to_response(request, 'yaksh/add_module.html', context) @login_required @email_verified -def add_module(request, module_id=None, course_id=None): +def add_module(request, course_id=None, module_id=None): user = request.user if not is_moderator(user): raise Http404('You are not allowed to view this page!') - redirect_url = reverse("yaksh:show_all_modules") if course_id: course = Course.objects.get(id=course_id) if not course.is_creator(user) and not course.is_teacher(user): raise Http404('This course does not belong to you') - redirect_url = reverse("yaksh:get_course_modules", args=[course_id]) if module_id: module = LearningModule.objects.get(id=module_id) if not module.creator == user and not course_id: @@ -2688,10 +2728,14 @@ def add_module(request, module_id=None, course_id=None): module_form = LearningModuleForm(request.POST, instance=module) if module_form.is_valid(): if module is None: + last_module = course.get_learning_modules().last() module_form.instance.creator = user + if last_module: + module_form.instance.order = last_module.order + 1 module = module_form.save() module.html_data = get_html_text(module.description) module.save() + course.learning_module.add(module.id) messages.success( request, "Saved {0} successfully".format(module.name) @@ -2708,99 +2752,6 @@ def add_module(request, module_id=None, course_id=None): @login_required @email_verified -def show_all_quizzes(request): - user = request.user - if not is_moderator(user): - raise Http404('You are not allowed to view this page!') - quizzes = Quiz.objects.filter(creator=user, is_trial=False) - - form = SearchFilterForm() - - if request.method == 'POST': - quiz_tags = request.POST.get('search_tags') - quiz_status = request.POST.get('search_status') - - if quiz_status == 'select' : - quizzes = quizzes.filter( - description__contains=quiz_tags) - elif quiz_status == 'active' : - quizzes = quizzes.filter( - description__contains=quiz_tags, active=True) - elif quiz_status == 'closed': - quizzes = quizzes.filter( - description__contains=quiz_tags, active=False) - quizzes_found = quizzes.count() - - context = {"quizzes": quizzes, "form": form, - "quizzes_found": quizzes_found} - return my_render_to_response(request, 'yaksh/quizzes.html', context) - - -@login_required -@email_verified -def show_all_lessons(request): - user = request.user - if not is_moderator(user): - raise Http404('You are not allowed to view this page!') - lessons = Lesson.objects.filter(creator=user) - - form = SearchFilterForm() - - if request.method == 'POST': - lesson_tags = request.POST.get('search_tags') - lesson_status = request.POST.get('search_status') - - if lesson_status == 'select' : - lessons = lessons.filter( - description__contains=lesson_tags) - elif lesson_status == 'active' : - lessons = lessons.filter( - description__contains=lesson_tags, active=True) - elif lesson_status == 'closed': - lessons = lessons.filter( - description__contains=lesson_tags, active=False) - lessons_found = lessons.count() - - context = {"lessons": lessons, "form": form, - "lessons_found": lessons_found} - return my_render_to_response(request, 'yaksh/lessons.html', context) - - -@login_required -@email_verified -def show_all_modules(request): - user = request.user - if not is_moderator(user): - raise Http404('You are not allowed to view this page!') - learning_modules = LearningModule.objects.filter( - creator=user, is_trial=False) - - form = SearchFilterForm() - - if request.method == 'POST': - module_tags = request.POST.get('search_tags') - module_status = request.POST.get('search_status') - - if module_status == 'select' : - learning_modules = learning_modules.filter( - name__contains=module_tags) - elif module_status == 'active' : - learning_modules = learning_modules.filter( - name__contains=module_tags, active=True) - elif module_status == 'closed': - learning_modules = learning_modules.filter( - name__contains=module_tags, active=False) - learning_modules_found = learning_modules.count() - - context = {"modules": learning_modules, "form": form, - "modules_found": learning_modules_found} - return my_render_to_response( - request, 'yaksh/modules.html', context - ) - - -@login_required -@email_verified def preview_html_text(request): user = request.user if not is_moderator(user): @@ -2885,10 +2836,12 @@ def design_course(request, course_id): else: start_val = 1 for order, value in enumerate(add_values, start_val): - learning_module = LearningModule.objects.get(id=int(value)) - learning_module.order = order - learning_module.save() - to_add_list.append(learning_module) + module, created = LearningModule.objects.get_or_create( + id=int(value) + ) + module.order = order + module.save() + to_add_list.append(module) course.learning_module.add(*to_add_list) messages.success(request, "Modules added successfully") else: |