diff options
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/forms.py | 5 | ||||
-rw-r--r-- | yaksh/models.py | 20 | ||||
-rw-r--r-- | yaksh/urls.py | 22 | ||||
-rw-r--r-- | yaksh/views.py | 121 |
4 files changed, 128 insertions, 40 deletions
diff --git a/yaksh/forms.py b/yaksh/forms.py index 797f54e..eedd809 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -650,6 +650,7 @@ class CommentForm(forms.ModelForm): class TopicForm(forms.ModelForm): timer = forms.CharField() + def __init__(self, *args, **kwargs): super(TopicForm, self).__init__(*args, **kwargs) self.fields['name'].widget.attrs.update( @@ -666,8 +667,6 @@ class TopicForm(forms.ModelForm): class VideoQuizForm(forms.ModelForm): - _types = dict(question_types) - type = forms.CharField() timer = forms.CharField() @@ -693,7 +692,7 @@ class VideoQuizForm(forms.ModelForm): self.fields['type'].widget.attrs.update( {'class': form_input_class, 'readonly': True} ) - self.fields['type'].initial = self._types.get(question_type) + self.fields['type'].initial = question_type self.fields['description'].widget.attrs.update( {'class': form_input_class, 'placeholder': 'Description'} ) diff --git a/yaksh/models.py b/yaksh/models.py index 05bb459..f8b17d5 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -511,8 +511,6 @@ class Quiz(models.Model): objects = QuizManager() - content = GenericRelation("TableOfContents") - class Meta: verbose_name_plural = "Quizzes" @@ -1344,6 +1342,8 @@ class Question(models.Model): # Solution for the question. solution = models.TextField(blank=True) + content = GenericRelation("TableOfContents") + tc_code_types = { "python": [ ("standardtestcase", "Standard TestCase"), @@ -2730,17 +2730,29 @@ class Comment(ForumBase): class TableOfContents(models.Model): + toc_types = ((1, "Topic"), (2, "Graded Quiz"), (3, "Exercise"), (4, "Poll")) course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='course') - Lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, + lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, related_name='contents') time = models.CharField(max_length=100, default=0) + content = models.IntegerField(choices=toc_types) content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) object_id = models.PositiveIntegerField() content_object = GenericForeignKey() + class Meta: + verbose_name_plural = "Table Of Contents" + + def get_toc_text(self): + if self.content == 1: + content_name = Topic.objects.get(id=self.object_id).name + else: + content_name = Question.objects.get(id=self.object_id).summary + return content_name + def __str__(self): - return f"Contents in {self.lesson.name}" + return f"TOC for {self.lesson.name} with {self.get_content_display()}" class Topic(models.Model): diff --git a/yaksh/urls.py b/yaksh/urls.py index 7cf3bf7..2c462b3 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -239,16 +239,20 @@ urlpatterns = [ name="mark_notification"), path('manage/add/marker/<int:course_id>/<int:lesson_id>', views.add_marker, name='add_marker'), - path('manage/add/topic/<int:course_id>/<int:lesson_id>', views.add_topic, - name='add_topic'), - path('manage/edit/topic/<int:course_id>/<int:lesson_id>/<int:topic_id>', + path('manage/add/lesson/topic/<int:content_type>/<int:course_id>/<int:lesson_id>', + views.add_topic, name='add_topic'), + path('manage/edit/lesson/topic/<int:content_type>/<int:course_id>/<int:lesson_id>/<int:topic_id>', views.add_topic, name='edit_topic'), - path('manage/add/quiz/<int:course_id>/<int:lesson_id>', + path('manage/add/lesson/quiz/<int:content_type>/<int:course_id>/<int:lesson_id>', views.add_marker_quiz, name='add_marker_quiz'), - path('manage/edit/quiz/<int:course_id>/<int:lesson_id>/<int:quiz_id>/<int:question_id>', + path('manage/edit/lesson/quiz/<int:content_type>/<int:course_id>/<int:lesson_id>/<int:question_id>', views.add_marker_quiz, name='edit_marker_quiz'), - path('manage/add/exercise/<int:course_id>/<int:lesson_id>', - views.add_marker_quiz, name='add_marker_exercise'), - path('manage/edit/exercise/<int:course_id>/<int:lesson_id>/<int:quiz_id>/<int:question_id>', - views.add_marker_quiz, name='edit_marker_exercise') + path('manage/add/lesson/exercise/<int:content_type>/<int:course_id>/<int:lesson_id>', + views.add_marker_quiz, name='add_marker_quiz'), + path('manage/edit/lesson/exercise/<int:content_type>/<int:course_id>/<int:lesson_id>/<int:question_id>', + views.add_marker_quiz, name='edit_marker_quiz'), + path('manage/add/lesson/poll/<int:content_type>/<int:course_id>/<int:lesson_id>', + views.add_marker_quiz, name='add_marker_quiz'), + path('manage/edit/lesson/poll/<int:content_type>/<int:course_id>/<int:lesson_id>/<int:question_id>', + views.add_marker_quiz, name='edit_marker_quiz') ] diff --git a/yaksh/views.py b/yaksh/views.py index 668a88c..db46d90 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -37,7 +37,8 @@ from yaksh.models import ( QuestionPaper, QuestionSet, Quiz, Question, StandardTestCase, StdIOBasedTestCase, StringTestCase, TestCase, User, get_model_class, FIXTURES_DIR_PATH, MOD_GROUP_NAME, Lesson, LessonFile, - LearningUnit, LearningModule, CourseStatus, question_types, Post, Comment + LearningUnit, LearningModule, CourseStatus, question_types, Post, Comment, + Topic, TableOfContents ) from yaksh.forms import ( UserRegisterForm, UserLoginForm, QuizForm, QuestionForm, @@ -2634,6 +2635,14 @@ 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) context['lesson_form'] = lesson_form context['lesson_file_form'] = lesson_files_form @@ -3428,9 +3437,8 @@ def add_marker(request, course_id, lesson_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") - data = json.loads(request.body.decode("utf-8")) - content_type = data[-2].get("value") - print(content_type) + content_type = request.POST.get("content") + question_type = request.POST.get("type") if content_type == '1': form = TopicForm() template_name = 'yaksh/add_topic.html' @@ -3438,16 +3446,15 @@ def add_marker(request, course_id, lesson_id): formset = None tc_class = None else: - try: - question_type = data[-1].get('value') - except IndexError: + 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} + 'formset': formset, 'tc_class': tc_class, + 'content_type': content_type} data = loader.render_to_string( template_name, context=context, request=request ) @@ -3456,7 +3463,8 @@ def add_marker(request, course_id, lesson_id): 'status': status} ) -def get_tc_formset(question_type): + +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' @@ -3469,37 +3477,102 @@ def get_tc_formset(question_type): TestcaseFormset = inlineformset_factory( Question, tc, form=TestcaseForm, extra=1, fields="__all__", ) - formset = TestcaseFormset(initial=[{'type': tc_class}]) + formset = TestcaseFormset( + post, initial=[{'type': tc_class}], instance=question + ) return formset, tc_class @login_required @email_verified -def add_topic(request, course_id, lesson_id, topic_id=None): +def add_topic(request, content_type, course_id, lesson_id, 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") - print(request.method) - data = json.loads(request.body.decode("utf-8")) - print(data) - return JsonResponse( - {'success': True, 'data': data, 'message': 'Added successfully'} - ) + if topic_id: + topic = get_object_or_404(Topic, pk=topic_id) + else: + topic = None + context = {} + if request.method == "POST": + form = TopicForm(request.POST, instance=topic) + if form.is_valid(): + form.save() + if not topic: + TableOfContents.objects.create( + content_object=form.instance, course_id=course_id, + lesson_id=lesson_id, content=content_type, + time=request.POST.get("timer") + ) + 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 + status_code = 200 + context['success'] = True + context['message'] = 'Added topic successfully' + else: + status_code = 400 + context['success'] = False + context['message'] = form.errors.as_json() + return JsonResponse(context, status=status_code) @login_required @email_verified -def add_marker_quiz(request, course_id, lesson_id, question_id=None): +def add_marker_quiz(request, content_type, course_id, lesson_id, + 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") - print(request.method) - data = json.loads(request.body.decode("utf-8")) - print(data) - return JsonResponse( - {'success': True, 'data': data, 'message': 'Added successfully'} - ) + if question_id: + question = get_object_or_404(Question, pk=question_id) + else: + question = None + context = {} + if request.method == "POST": + qform = VideoQuizForm(request.POST, instance=question) + if qform.is_valid(): + 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() + if not question: + TableOfContents.objects.create( + content_object=qform.instance, course_id=course_id, + lesson_id=lesson_id, content=content_type, + time=request.POST.get("timer") + ) + 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 + 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'] = formset.errors.as_json() + else: + status_code = 400 + context['success'] = False + context['message'] = qform.errors.as_json() + return JsonResponse(context, status=status_code) |