summaryrefslogtreecommitdiff
path: root/yaksh/forms.py
diff options
context:
space:
mode:
authorPalaparthy Adityachandra2020-11-17 15:49:01 +0530
committerGitHub2020-11-17 15:49:01 +0530
commit5f92f4db6e108b338ea6509052f0990f1f94c6cb (patch)
tree13e55275326aa2d533d36ae804d36ba833f0bb41 /yaksh/forms.py
parentd1de6058253c5df8984ac36d9de33396a368edce (diff)
parent6aef69d6e5a3eb3dde2d39e0bb9e1dd5b05a8b3c (diff)
downloadonline_test-5f92f4db6e108b338ea6509052f0990f1f94c6cb.tar.gz
online_test-5f92f4db6e108b338ea6509052f0990f1f94c6cb.tar.bz2
online_test-5f92f4db6e108b338ea6509052f0990f1f94c6cb.zip
Merge branch 'master' into tinymce
Diffstat (limited to 'yaksh/forms.py')
-rw-r--r--yaksh/forms.py133
1 files changed, 128 insertions, 5 deletions
diff --git a/yaksh/forms.py b/yaksh/forms.py
index 3c4d664..d57d388 100644
--- a/yaksh/forms.py
+++ b/yaksh/forms.py
@@ -1,13 +1,15 @@
from django import forms
from yaksh.models import (
get_model_class, Profile, Quiz, Question, Course, QuestionPaper, Lesson,
- LearningModule, TestCase, languages, question_types, Post, Comment
+ LearningModule, TestCase, languages, question_types, Post, Comment,
+ Topic
)
from grades.models import GradingSystem
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.conf import settings
from django.utils import timezone
+from django.template.defaultfilters import filesizeformat
from textwrap import dedent
try:
from string import letters
@@ -15,6 +17,8 @@ except ImportError:
from string import ascii_letters as letters
from string import punctuation, digits
import pytz
+from ast import literal_eval
+
from .send_emails import generate_activation_key
languages = (("", "Select Language"),) + languages
@@ -520,8 +524,12 @@ class LessonForm(forms.ModelForm):
self.fields['description'].widget.attrs.update(
{'class': form_input_class, 'placeholder': des_msg}
)
- self.fields['video_file'].widget.attrs.update(
- {'class': "custom-file-input"}
+ self.fields['video_path'].widget.attrs.update(
+ {'class': form_input_class,
+ 'placeholder': dedent("""\
+ {'youtube': '', 'vimeo': '', 'others': ''}
+ """),
+ }
)
class Meta:
@@ -538,8 +546,34 @@ class LessonForm(forms.ModelForm):
"Please upload video files in {0} format".format(
", ".join(actual_extension))
)
+ if file.size > settings.MAX_UPLOAD_SIZE:
+ raise forms.ValidationError(
+ f"Video file size must be less than "\
+ f"{filesizeformat(settings.MAX_UPLOAD_SIZE)}. "
+ f"Current size is {filesizeformat(file.size)}"
+ )
return file
+ def clean_video_path(self):
+ path = self.cleaned_data.get("video_path")
+ if path:
+ try:
+ value = literal_eval(path)
+ if not isinstance(value, dict):
+ raise forms.ValidationError(
+ "Value must be dictionary e.g {'youtube': 'video-id'}"
+ )
+ else:
+ if len(value) > 1:
+ raise forms.ValidationError(
+ "Only one type of video path is allowed"
+ )
+ except ValueError:
+ raise forms.ValidationError(
+ "Value must be dictionary e.g {'youtube': 'video-id'}"
+ )
+ return path
+
class LessonFileForm(forms.Form):
Lesson_files = forms.FileField(
@@ -579,7 +613,7 @@ class TestcaseForm(forms.ModelForm):
class PostForm(forms.ModelForm):
class Meta:
model = Post
- fields = ["title", "description", "image"]
+ fields = ["title", "description", "image", "anonymous"]
widgets = {
'title': forms.TextInput(
attrs={
@@ -602,7 +636,7 @@ class PostForm(forms.ModelForm):
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
- fields = ["description", "image"]
+ fields = ["description", "image", "anonymous"]
widgets = {
'description': forms.Textarea(
attrs={
@@ -615,3 +649,92 @@ class CommentForm(forms.ModelForm):
}
)
}
+
+
+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': 'Name'}
+ )
+ self.fields['timer'].widget.attrs.update(
+ {'class': form_input_class, 'placeholder': 'Time'}
+ )
+ self.fields['description'].widget.attrs.update(
+ {'class': form_input_class, 'placeholder': 'Description'}
+ )
+ 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):
+
+ type = forms.CharField()
+
+ timer = forms.CharField()
+
+ def __init__(self, *args, **kwargs):
+ if 'question_type' in kwargs:
+ 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'}
+ )
+ self.fields['language'].widget.attrs.update(
+ {'class': 'custom-select'}
+ )
+ self.fields['topic'].widget.attrs.update(
+ {'class': form_input_class, 'placeholder': 'Question topic name'}
+ )
+ self.fields['points'].widget.attrs.update(
+ {'class': form_input_class, 'placeholder': 'Points'}
+ )
+ self.fields['type'].widget.attrs.update(
+ {'class': form_input_class, 'readonly': True}
+ )
+ self.fields['type'].initial = question_type
+ self.fields['description'].widget.attrs.update(
+ {'class': form_input_class, 'placeholder': 'Description',
+ 'id': 'que_description'}
+ )
+ 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