1
|
|
|
from django import forms |
2
|
|
|
from django.contrib.auth.models import User |
3
|
|
|
from .models import Submission, StudyProgram |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
def validate_authors(authors, assignment): |
7
|
|
|
if assignment.max_authors < len(authors): |
8
|
|
|
raise forms.ValidationError( |
9
|
|
|
"At most {0} authors are allowed.".format(assignment.max_authors)) |
10
|
|
|
for author in authors: |
11
|
|
|
if not assignment.can_create_submission(author): |
12
|
|
|
raise forms.ValidationError( |
13
|
|
|
"{0} is not allowed to submit solutions for this assignment.".format(author)) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class SubmissionWithGroups(forms.ModelForm): |
17
|
|
|
|
18
|
|
|
class Meta: |
19
|
|
|
model = Submission |
20
|
|
|
fields = ('authors', 'notes') |
21
|
|
|
|
22
|
|
|
def __init__(self, current_user, ass, *args, **kwargs): |
23
|
|
|
''' |
24
|
|
|
Adjusts the list of choices for the authors.''' |
25
|
|
|
super(SubmissionWithGroups, self).__init__(*args, **kwargs) |
26
|
|
|
|
27
|
|
|
self.assignment = ass |
28
|
|
|
|
29
|
|
|
# remove all users already having a submission for the assignment |
30
|
|
|
# + the current user |
31
|
|
|
havingSubmissions = [] |
32
|
|
|
for submission in ass.submissions.all().exclude(state=Submission.WITHDRAWN): |
33
|
|
|
for author in submission.authors.all(): |
34
|
|
|
havingSubmissions.append(author.pk) |
35
|
|
|
# The submitter should still be in the list (see #13), |
36
|
|
|
# but the course owner should not (see #56) |
37
|
|
|
allowed_authors = User.objects.exclude( |
38
|
|
|
pk__in=havingSubmissions).exclude(is_active=False) |
39
|
|
|
# But the course owner must be in the list when creating |
40
|
|
|
# a test submission (see #203) |
41
|
|
|
if current_user.pk != ass.course.owner.pk: |
42
|
|
|
allowed_authors = allowed_authors.exclude(pk=ass.course.owner.pk) |
43
|
|
|
self.fields['authors'].queryset = allowed_authors |
44
|
|
|
|
45
|
|
|
def clean_authors(self): |
46
|
|
|
data = self.cleaned_data['authors'] |
47
|
|
|
validate_authors(data, self.assignment) |
48
|
|
|
return data |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
class SubmissionWithoutGroups(forms.ModelForm): |
52
|
|
|
|
53
|
|
|
class Meta: |
54
|
|
|
model = Submission |
55
|
|
|
fields = ('notes',) |
56
|
|
|
|
57
|
|
|
def __init__(self, current_user, ass, *args, **kwargs): |
58
|
|
|
super(SubmissionWithoutGroups, self).__init__(*args, **kwargs) |
59
|
|
|
|
60
|
|
|
self.assignment = ass |
61
|
|
|
|
62
|
|
|
def clean_authors(self): |
63
|
|
|
data = self.cleaned_data['authors'] |
64
|
|
|
validate_authors(data, self.assignment) |
65
|
|
|
return data |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
class SubmissionWithoutGroupsWithFileForm(SubmissionWithoutGroups): |
69
|
|
|
attachment = forms.FileField() |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
class SubmissionWithoutGroupsWithoutFileForm(SubmissionWithoutGroups): |
73
|
|
|
pass |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
class SubmissionWithGroupsWithFileForm(SubmissionWithGroups): |
77
|
|
|
attachment = forms.FileField() |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
class SubmissionWithGroupsWithoutFileForm(SubmissionWithGroups): |
81
|
|
|
pass |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
class SubmissionFileUpdateForm(forms.ModelForm): |
85
|
|
|
|
86
|
|
|
attachment = forms.FileField() |
87
|
|
|
|
88
|
|
|
class Meta: |
89
|
|
|
model = Submission |
90
|
|
|
fields = ('notes',) |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
def getSubmissionForm(assignment): |
94
|
|
|
if assignment.max_authors > 1: |
95
|
|
|
if assignment.has_attachment: |
96
|
|
|
return SubmissionWithGroupsWithFileForm |
97
|
|
|
else: |
98
|
|
|
return SubmissionWithGroupsWithoutFileForm |
99
|
|
|
else: |
100
|
|
|
if assignment.has_attachment: |
101
|
|
|
return SubmissionWithoutGroupsWithFileForm |
102
|
|
|
else: |
103
|
|
|
return SubmissionWithoutGroupsWithoutFileForm |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
class SettingsForm(forms.ModelForm): |
107
|
|
|
email = forms.CharField(max_length=75, required=True) |
108
|
|
|
first_name = forms.CharField(max_length=30, required=True) |
109
|
|
|
last_name = forms.CharField(max_length=30, required=True) |
110
|
|
|
username = forms.CharField(max_length=30, required=True) |
111
|
|
|
student_id = forms.CharField( |
112
|
|
|
max_length=30, required=False, label="Student ID (optional)") |
113
|
|
|
study_program = forms.ModelChoiceField( |
114
|
|
|
queryset=StudyProgram.objects, required=False) |
115
|
|
|
|
116
|
|
|
class Meta: |
117
|
|
|
model = User |
118
|
|
|
fields = ('username', 'first_name', 'last_name', 'email') |
119
|
|
|
|
120
|
|
|
def save(self, commit=True): |
121
|
|
|
self.instance.profile.student_id = self.cleaned_data['student_id'] |
122
|
|
|
self.instance.profile.study_program = self.cleaned_data['study_program'] |
123
|
|
|
self.instance.profile.save() |
124
|
|
|
return super(SettingsForm, self).save(commit=commit) |
125
|
|
|
|
126
|
|
|
def __init__(self, *args, **kwargs): |
127
|
|
|
super(SettingsForm, self).__init__(*args, **kwargs) |
128
|
|
|
self.initial['student_id'] = self.instance.profile.student_id |
129
|
|
|
self.initial['study_program'] = self.instance.profile.study_program |
130
|
|
|
|
131
|
|
|
def clean_study_program(self): |
132
|
|
|
data = self.cleaned_data['study_program'] |
133
|
|
|
if data is None and self.instance.profile.study_program is None and StudyProgram.objects.count() > 1: |
134
|
|
|
raise forms.ValidationError( |
135
|
|
|
"Please select your study program.") |
136
|
|
|
return data |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
class MailForm(forms.Form): |
140
|
|
|
subject = forms.CharField(max_length=50, required=True) |
141
|
|
|
message = forms.CharField( |
142
|
|
|
widget=forms.Textarea, required=True, initial="Dear #FIRSTNAME# #LASTNAME#, ") |
143
|
|
|
|
144
|
|
|
|