forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourse_forms.py
131 lines (103 loc) · 3.67 KB
/
course_forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from django import forms
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.utils.translation import ugettext_lazy as _
from course.models import LearningObjectCategory, CourseModule, CourseInstance
class FieldsetModelForm(forms.ModelForm):
class Meta:
legend = ""
fields = []
def get_fieldsets(self):
return [
{ "legend": self.Meta.legend, "fields": self.get_fields(*self.Meta.fields) },
]
def get_fields(self, *names):
return [self[name] for name in names]
class LearningObjectCategoryForm(FieldsetModelForm):
class Meta:
model = LearningObjectCategory
legend = _("Category")
fields = [
'status',
'name',
'points_to_pass',
'description'
]
class CourseModuleForm(FieldsetModelForm):
class Meta:
model = CourseModule
fields = [
'status',
'order',
'name',
'url',
'introduction',
'points_to_pass',
'opening_time',
'closing_time',
'late_submissions_allowed',
'late_submission_deadline',
'late_submission_penalty'
]
def get_fieldsets(self):
return [
{ 'legend':_('Hierarchy'), 'fields':self.get_fields('status','order','url') },
{ 'legend':_('Content'), 'fields':self.get_fields('name','introduction','points_to_pass') },
{ 'legend':_('Schedule'), 'fields':self.get_fields('opening_time','closing_time',
'late_submissions_allowed','late_submission_deadline', 'late_submission_penalty') },
]
class CourseInstanceForm(forms.ModelForm):
class Meta:
model = CourseInstance
fields = [
'visible_to_students',
'instance_name',
'url',
'image',
'language',
'starting_time',
'ending_time',
'enrollment_audience',
'view_content_to',
'assistants',
'technical_error_emails',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["assistants"].widget.attrs["class"] = "search-select"
self.fields["assistants"].help_text = ""
if self.instance and self.instance.visible_to_students:
self.fields["url"].widget.attrs["readonly"] = "true"
self.fields["url"].help_text = _("The URL identifier is locked "
"while the course is visible to students.")
def clean_url(self):
if self.instance and self.instance.visible_to_students:
return self.instance.url
return self.cleaned_data["url"]
class CourseIndexForm(forms.ModelForm):
class Meta:
model = CourseInstance
fields = [
'index_mode',
'description',
'footer',
]
class CourseContentForm(forms.ModelForm):
class Meta:
model = CourseInstance
fields = [
'module_numbering',
'content_numbering',
]
class CloneInstanceForm(forms.Form):
url = forms.CharField(label=_("New URL identifier for the course instance"),
validators=[RegexValidator(regex="^[\w\-\.]+$")])
def __init__(self, *args, **kwargs):
self.instance = kwargs.pop('instance')
super().__init__(*args, **kwargs)
def clean_url(self):
url = self.cleaned_data['url']
if CourseInstance.objects.filter(
course=self.instance.course, url=url).exists():
raise ValidationError(_("The URL is already taken."))
return url