From 7e5608d0853d69358c14f9fb8fbd6465e21b8962 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 7 Sep 2020 12:53:08 +0530 Subject: Add edit and delete table of contents options --- yaksh/forms.py | 28 ++++- yaksh/static/yaksh/js/lesson.js | 198 ++++++++++++++++-------------- yaksh/templates/yaksh/add_lesson.html | 6 + yaksh/templates/yaksh/add_topic.html | 2 +- yaksh/templates/yaksh/add_video_quiz.html | 2 +- yaksh/templates/yaksh/show_toc.html | 39 ++++-- yaksh/templatetags/custom_filters.py | 3 +- yaksh/urls.py | 19 ++- yaksh/views.py | 115 +++++++++++++---- 9 files changed, 269 insertions(+), 143 deletions(-) diff --git a/yaksh/forms.py b/yaksh/forms.py index eedd809..ba8b7d5 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -652,6 +652,7 @@ class TopicForm(forms.ModelForm): timer = forms.CharField() def __init__(self, *args, **kwargs): + time = kwargs.pop("time") if "time" in kwargs else None super(TopicForm, self).__init__(*args, **kwargs) self.fields['name'].widget.attrs.update( {'class': form_input_class, 'placeholder': 'Topic Name'} @@ -659,11 +660,23 @@ class TopicForm(forms.ModelForm): self.fields['timer'].widget.attrs.update( {'class': form_input_class, 'placeholder': 'Topic Time'} ) + self.fields['timer'].initial = time class Meta: model = Topic fields = "__all__" + def clean_timer(self): + timer = self.cleaned_data.get("timer") + if timer: + try: + hh, mm, ss = timer.split(":") + except ValueError: + raise forms.ValidationError( + "Marker time should be in the format hh:mm:ss" + ) + return timer + class VideoQuizForm(forms.ModelForm): @@ -676,6 +689,7 @@ class VideoQuizForm(forms.ModelForm): question_type = kwargs.pop('question_type') else: question_type = "mcq" + time = kwargs.pop("time") if "time" in kwargs else None super(VideoQuizForm, self).__init__(*args, **kwargs) self.fields['summary'].widget.attrs.update( {'class': form_input_class, 'placeholder': 'Summary'} @@ -690,7 +704,7 @@ class VideoQuizForm(forms.ModelForm): {'class': form_input_class, 'placeholder': 'Points'} ) self.fields['type'].widget.attrs.update( - {'class': form_input_class, 'readonly': True} + {'class': form_input_class} ) self.fields['type'].initial = question_type self.fields['description'].widget.attrs.update( @@ -699,8 +713,20 @@ class VideoQuizForm(forms.ModelForm): self.fields['timer'].widget.attrs.update( {'class': form_input_class, 'placeholder': 'Quiz Time'} ) + self.fields['timer'].initial = time class Meta: model = Question fields = ['summary', 'description', 'points', 'language', 'type', 'topic'] + + def clean_timer(self): + timer = self.cleaned_data.get("timer") + if timer: + try: + hh, mm, ss = timer.split(":") + except ValueError: + raise forms.ValidationError( + "Marker time should be in the format hh:mm:ss" + ) + return timer \ No newline at end of file diff --git a/yaksh/static/yaksh/js/lesson.js b/yaksh/static/yaksh/js/lesson.js index 38db7d2..92038c9 100644 --- a/yaksh/static/yaksh/js/lesson.js +++ b/yaksh/static/yaksh/js/lesson.js @@ -16,10 +16,6 @@ $(document).ready(function() { seconds = seconds < 10 ? "0" + seconds : seconds; timer.val(hours + ":" + minutes + ":" + seconds); }); - function csrfSafeMethod(method) { - // these HTTP methods do not require CSRF protection - return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); - } $("#vtimer").on("change keyup paste", function() { player.pause(); @@ -50,95 +46,7 @@ $(document).ready(function() { ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf); }); - function ajax_call(url, method, data, csrf) { - $.ajax({ - url: url, - timeout: 15000, - method: method, - data: data, - beforeSend: function(xhr, settings) { - if (!csrfSafeMethod(settings.type) && !this.crossDomain) { - xhr.setRequestHeader("X-CSRFToken", csrf); - } - }, - success: function(msg) { - unlock_screen(); - if (msg.success) { - if (msg.status) $("#lesson-content").html(msg.data); - if (msg.content_type === '1') { - add_topic(); - } - else { - add_question(); - } - } - if (msg.toc) show_toc(msg.toc); - if (msg.message) alert(msg.message) - }, - error: function(xhr, data) { - switch(xhr.status) { - case 400: { - unlock_screen(); - console.log(data.responseJSON); - break; - } - case 500: { - unlock_screen(); - alert('500 status code! server error'); - break; - } - case 404: { - unlock_screen(); - alert('404 status code! server error'); - break; - } - } - } - }); - } - - function add_topic() { - if (!$("#id_timer").val()) { - $("#id_timer").val($("#vtimer").val()); - } - $("#topic-form").submit(function(e) { - e.preventDefault(); - lock_screen(); - var csrf = document.getElementById("topic-form").elements[0].value; - ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf); - }); - } - - function add_question() { - if (!$("#id_timer").val()) { - $("#id_timer").val($("#vtimer").val()); - } - $("#question-form").submit(function(e) { - e.preventDefault(); - lock_screen(); - var csrf = document.getElementById("question-form").elements[0].value; - ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf); - }); - } - - function lock_screen() { - document.getElementById("ontop").style.display = "block"; - } - - function unlock_screen() { - document.getElementById("ontop").style.display = "none"; - } - - function show_error() { - - } - - function show_toc(toc) { - $("#lesson-content").empty(); - $("#toc").html(toc); - } - - $('#id_video_file').on('change',function(){ + $('#id_video_file').on('change',function() { //get the file name var files = []; for (var i = 0; i < $(this)[0].files.length; i++) { @@ -147,7 +55,7 @@ $(document).ready(function() { $(this).next('.custom-file-label').html(files.join(', ')); }); - $('#id_Lesson_files').on('change',function(){ + $('#id_Lesson_files').on('change',function() { //get the file name var files = []; for (var i = 0; i < $(this)[0].files.length; i++) { @@ -156,3 +64,105 @@ $(document).ready(function() { $(this).next('.custom-file-label').html(files.join(', ')); }); }); + + +function add_topic() { + if (!$("#id_timer").val()) { + $("#id_timer").val($("#vtimer").val()); + } + $("#topic-form").submit(function(e) { + e.preventDefault(); + lock_screen(); + var csrf = document.getElementById("topic-form").elements[0].value; + ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf); + }); +} + +function add_question() { + if (!$("#id_timer").val()) { + $("#id_timer").val($("#vtimer").val()); + } + $("#question-form").submit(function(e) { + e.preventDefault(); + lock_screen(); + var csrf = document.getElementById("question-form").elements[0].value; + ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf); + }); +} + +function lock_screen() { + document.getElementById("ontop").style.display = "block"; +} + +function unlock_screen() { + document.getElementById("ontop").style.display = "none"; +} + +function show_error(error) { + var err_msg = "\n"; + Object.keys(err).forEach(function(key) { + var value = err[key]; + err_msg = err_msg + key + " : " + value[0].message + "\n"; + }); + alert(err_msg); +} + +function show_toc(toc) { + $("#lesson-content").empty(); + $("#toc").html(toc); +} + +function csrfSafeMethod(method) { + // these HTTP methods do not require CSRF protection + return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); +} + +function ajax_call(url, method, data, csrf) { + $.ajax({ + url: url, + timeout: 15000, + method: method, + data: data, + beforeSend: function(xhr, settings) { + if (!csrfSafeMethod(settings.type) && !this.crossDomain) { + xhr.setRequestHeader("X-CSRFToken", csrf); + } + }, + success: function(msg) { + unlock_screen(); + if (msg.success) { + if (msg.status) $("#lesson-content").html(msg.data); + if (parseInt(msg.content_type) === 1) { + add_topic(); + } + else { + add_question(); + } + } + if (msg.toc) show_toc(msg.toc); + if (msg.message) alert(msg.message) + }, + error: function(xhr, data) { + unlock_screen(); + switch(xhr.status) { + case 400: { + err = JSON.parse(xhr.responseJSON.message); + show_error(err); + break; + } + case 500: { + alert('500 status code! server error'); + break; + } + case 404: { + alert('404 status code! server error'); + break; + } + default: { + alert('Unable to perform action. Please try again'); + break; + } + } + } + }); +} diff --git a/yaksh/templates/yaksh/add_lesson.html b/yaksh/templates/yaksh/add_lesson.html index 62aa881..6018e54 100644 --- a/yaksh/templates/yaksh/add_lesson.html +++ b/yaksh/templates/yaksh/add_lesson.html @@ -158,7 +158,13 @@