-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforms.py
More file actions
227 lines (172 loc) · 5.83 KB
/
forms.py
File metadata and controls
227 lines (172 loc) · 5.83 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from pprint import pprint
from django.contrib.auth.password_validation import validate_password
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
from .models import Professor, Student, Classroom, Assignment, Question
class SignupForm(UserCreationForm):
PROFESSOR = 'professor'
STUDENT = 'student'
USER_TYPE_CHOICE = (
(PROFESSOR, 'Professor',),
(STUDENT, 'Student',),
)
user_type = forms.ChoiceField(
required=True, choices=USER_TYPE_CHOICE, widget=forms.RadioSelect)
def save(self, commit=True):
# save user and get the type of user
user = super().save()
user_type = self.cleaned_data.get('user_type')
# save either as professor or student and return the instance
if user_type == 'professor':
professor = Professor.objects.create(user=user)
if commit:
professor.save()
return professor
elif user_type == 'student':
student = Student.objects.create(user=user)
if commit:
student.save()
return student
class Meta(UserCreationForm.Meta):
fields = UserCreationForm.Meta.fields + (
'email',
'user_type',
)
class ClassroomCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
# getting the professor to be associated with the classroom
self.professor = kwargs.pop('professor')
institution = self.professor.institution
super().__init__(*args, **kwargs)
# adding professor field with initial value as the professor passed to
# constructor
self.fields['professor'] = forms.ModelChoiceField(
queryset=Professor.objects.filter(
institution=institution,
),
initial=self.professor,
widget=forms.HiddenInput,
)
# adding students field that has the students of the same institute as that
# of the professor
self.fields['students'] = forms.ModelMultipleChoiceField(
queryset=Student.objects.filter(
institution=institution,
),
required=False,
)
def save(self, commit=True):
classroom = super().save(commit=False)
classroom.professor = self.professor # setting classroom's professor
# saving the classroom, setting the students and returning the
# classroom instance
if commit:
classroom.save()
classroom.students.set(self.cleaned_data['students'])
return classroom
class Meta:
model = Classroom
fields = (
'title',
)
class ClassroomJoinForm(forms.Form):
join_code = forms.CharField(max_length=5)
class ClassroomEditForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.professor = kwargs.pop('professor')
# getting the classroom instance passed to the constructor
self.classroom = kwargs['instance']
institution = self.professor.institution
students = self.classroom.students.all()
super().__init__(*args, **kwargs)
self.fields['students'] = forms.ModelMultipleChoiceField(
queryset=Student.objects.filter(
institution=institution,
),
initial=students,
required=False,
)
def save(self, commit=True):
# updating the classroom's title and student set
self.classroom.title = self.cleaned_data['title']
self.classroom.students.set(self.cleaned_data['students'])
# saving and returning the classroom instance
if commit:
self.classroom.save()
return self.classroom
class Meta:
model = Classroom
fields = (
'title',
)
class AssignmentCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
# getting the classroom to be associated with the assignment
self.classroom = kwargs.pop('classroom')
super().__init__(*args, **kwargs)
def save(self, commit=True):
assignment = super().save(commit=False)
# setting the assignment's classroom
assignment.classroom = self.classroom
# saving and returning the assignment instance
if commit:
assignment.save()
return assignment
class Meta:
model = Assignment
fields = (
'title',
'deadline',
'language',
)
class NewAssignmentCreateForm(forms.ModelForm):
class Meta:
model = Assignment
fields = (
'title',
'classroom',
'deadline',
'language',
)
class AssignmentEditForm(forms.ModelForm):
class Meta:
model = Assignment
fields = (
'title',
'deadline',
'language',
)
class QuestionCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.assignment = kwargs.pop('assignment')
super().__init__(*args, **kwargs)
def save(self, commit=True):
question = super().save(commit=False)
question.assignment = self.assignment
if commit:
question.save()
return question
class Meta:
model = Question
fields = (
'title',
'description',
'sample_input',
'sample_output',
'marks',
'draft',
'check_plagiarism',
)
class QuestionEditForm(forms.ModelForm):
class Meta:
model = Question
fields = (
'title',
'description',
'sample_input',
'sample_output',
'marks',
'draft',
'check_plagiarism',
)