1
+ from __future__ import annotations
2
+
1
3
import collections
4
+
2
5
from django import forms
3
- from django .utils import timezone
4
6
from django .http import Http404
5
- from django .shortcuts import render , redirect
6
- from django .views .generic import View , TemplateView , ListView , FormView , UpdateView
7
- from ..models import Program , Question , Answer , Applicant , Score , Resource
8
- from ..forms import QuestionForm , BaseApplyForm , ScoreForm , ResourceForm , AllocationForm
9
-
7
+ from django .shortcuts import redirect , render
8
+ from django .utils import timezone
9
+ from django .views .generic import FormView , ListView , TemplateView , UpdateView , View
10
10
11
- def index (request ):
12
- return render (request , "index.html" , {
13
- "accessible_programs" : Program .objects .filter (users__pk = request .user .pk ).order_by ("name" ),
14
- })
11
+ from ..forms import AllocationForm , BaseApplyForm , QuestionForm , ResourceForm , ScoreForm
12
+ from ..models import Answer , Applicant , Program , Question , Resource , Score
15
13
16
14
17
- class ProgramMixin (object ):
15
+ def index (request ):
16
+ return render (
17
+ request ,
18
+ "index.html" ,
19
+ {
20
+ "accessible_programs" : Program .objects .filter (
21
+ users__pk = request .user .pk
22
+ ).order_by ("name" ),
23
+ },
24
+ )
25
+
26
+
27
+ class ProgramMixin :
18
28
"""
19
29
Generic view base class which does things in context of a Program.
20
30
"""
@@ -28,12 +38,12 @@ def dispatch(self, *args, **kwargs):
28
38
return redirect ("login" )
29
39
if self .login_required and not self .program .user_allowed (self .request .user ):
30
40
raise Http404 ("Not logged in" )
31
- return super (ProgramMixin , self ).dispatch (* args , ** kwargs )
41
+ return super ().dispatch (* args , ** kwargs )
32
42
33
43
def render_to_response (self , context , ** kwargs ):
34
- context [' program' ] = self .program
35
- context [' user_allowed_program' ] = self .program .user_allowed (self .request .user )
36
- return super (ProgramMixin , self ).render_to_response (context , ** kwargs )
44
+ context [" program" ] = self .program
45
+ context [" user_allowed_program" ] = self .program .user_allowed (self .request .user )
46
+ return super ().render_to_response (context , ** kwargs )
37
47
38
48
39
49
class ProgramHome (ProgramMixin , TemplateView ):
@@ -49,7 +59,9 @@ def get_context_data(self):
49
59
user .num_votes = user .scores .filter (applicant__program = self .program ).count ()
50
60
return {
51
61
"num_applicants" : self .program .applicants .count (),
52
- "num_scored" : self .request .user .scores .filter (applicant__program = self .program ).count (),
62
+ "num_scored" : self .request .user .scores .filter (
63
+ applicant__program = self .program
64
+ ).count (),
53
65
"users" : users ,
54
66
}
55
67
@@ -63,8 +75,8 @@ class ProgramQuestions(ProgramMixin, FormView):
63
75
form_class = QuestionForm
64
76
65
77
def get_context_data (self , ** kwargs ):
66
- context = super (ProgramQuestions , self ).get_context_data (** kwargs )
67
- context [' questions' ] = self .program .questions .order_by ("order" )
78
+ context = super ().get_context_data (** kwargs )
79
+ context [" questions" ] = self .program .questions .order_by ("order" )
68
80
return context
69
81
70
82
def form_valid (self , form ):
@@ -104,8 +116,8 @@ class ProgramApply(ProgramMixin, FormView):
104
116
template_name = "program-apply.html"
105
117
106
118
def get_form_kwargs (self ):
107
- kwargs = super (ProgramApply , self ).get_form_kwargs ()
108
- kwargs [' program' ] = self .program
119
+ kwargs = super ().get_form_kwargs ()
120
+ kwargs [" program" ] = self .program
109
121
return kwargs
110
122
111
123
def get_form_class (self ):
@@ -117,23 +129,25 @@ def get_form_class(self):
117
129
"text" : forms .CharField ,
118
130
"textarea" : forms .CharField ,
119
131
"integer" : forms .IntegerField ,
120
- }[question .type ](required = question .required , widget = widget , label = question .question )
121
- return type ("ApplicationForm" , (BaseApplyForm , ), fields )
132
+ }[question .type ](
133
+ required = question .required , widget = widget , label = question .question
134
+ )
135
+ return type ("ApplicationForm" , (BaseApplyForm ,), fields )
122
136
123
137
def form_valid (self , form ):
124
138
applicant = Applicant .objects .create (
125
- program = self .program ,
126
- name = form .cleaned_data ["name" ],
127
- email = form .cleaned_data ["email" ],
128
- applied = timezone .now (),
139
+ program = self .program ,
140
+ name = form .cleaned_data ["name" ],
141
+ email = form .cleaned_data ["email" ],
142
+ applied = timezone .now (),
129
143
)
130
144
for question in self .program .questions .order_by ("order" ):
131
145
value = form .cleaned_data .get ("question-%s" % question .id , None ) or None
132
146
if value :
133
147
Answer .objects .create (
134
- applicant = applicant ,
135
- question = question ,
136
- answer = value ,
148
+ applicant = applicant ,
149
+ question = question ,
150
+ answer = value ,
137
151
)
138
152
return redirect (self .program .urls .apply_success )
139
153
@@ -162,20 +176,24 @@ def get_queryset(self):
162
176
else :
163
177
self .sort = "applied"
164
178
# Fetch applicants
165
- applicants = list (self .program .applicants .prefetch_related ("scores" ).order_by ("-applied" ))
179
+ applicants = list (
180
+ self .program .applicants .prefetch_related ("scores" ).order_by ("-applied" )
181
+ )
166
182
for applicant in applicants :
167
- applicant .has_scored = applicant .scores .filter (user = self .request .user ).exists ()
183
+ applicant .has_scored = applicant .scores .filter (
184
+ user = self .request .user
185
+ ).exists ()
168
186
if applicant .has_scored :
169
187
applicant .average_score = applicant .average_score ()
170
188
else :
171
- applicant .average_score = - 1
189
+ applicant .average_score = - 1
172
190
if self .sort == "score" :
173
191
applicants .sort (key = lambda a : a .average_score , reverse = True )
174
192
return applicants
175
193
176
194
def get_context_data (self ):
177
- context = super (ProgramApplicants , self ).get_context_data ()
178
- context [' sort' ] = self .sort
195
+ context = super ().get_context_data ()
196
+ context [" sort" ] = self .sort
179
197
return context
180
198
181
199
@@ -192,7 +210,9 @@ def get(self, request, applicant_id):
192
210
for question in questions :
193
211
question .answer = question .answers .filter (applicant = applicant ).first ()
194
212
# See if we already scored this one
195
- score = Score .objects .filter (applicant = applicant , user = self .request .user ).first ()
213
+ score = Score .objects .filter (
214
+ applicant = applicant , user = self .request .user
215
+ ).first ()
196
216
old_score = score .score if score else None
197
217
if score :
198
218
all_scores = Score .objects .filter (applicant = applicant )
@@ -207,7 +227,11 @@ def get(self, request, applicant_id):
207
227
new_score .user = self .request .user
208
228
if old_score and new_score .score != old_score :
209
229
new_score .score_history = "," .join (
210
- [x .strip () for x in (new_score .score_history or "" ).split ("," ) if x .strip ()]
230
+ [
231
+ x .strip ()
232
+ for x in (new_score .score_history or "" ).split ("," )
233
+ if x .strip ()
234
+ ]
211
235
+ ["%.1f" % old_score ]
212
236
)
213
237
new_score .save ()
@@ -217,12 +241,14 @@ def get(self, request, applicant_id):
217
241
return redirect ("." )
218
242
else :
219
243
form = ScoreForm (instance = score )
220
- return self .render_to_response ({
221
- "applicant" : applicant ,
222
- "questions" : questions ,
223
- "all_scores" : all_scores ,
224
- "form" : form ,
225
- })
244
+ return self .render_to_response (
245
+ {
246
+ "applicant" : applicant ,
247
+ "questions" : questions ,
248
+ "all_scores" : all_scores ,
249
+ "form" : form ,
250
+ }
251
+ )
226
252
227
253
post = get
228
254
@@ -234,7 +260,11 @@ class RandomUnscoredApplicant(ProgramMixin, View):
234
260
"""
235
261
236
262
def get (self , request ):
237
- applicant = self .program .applicants .exclude (scores__user = self .request .user ).order_by ("?" ).first ()
263
+ applicant = (
264
+ self .program .applicants .exclude (scores__user = self .request .user )
265
+ .order_by ("?" )
266
+ .first ()
267
+ )
238
268
if applicant :
239
269
return redirect (applicant .urls .view )
240
270
else :
@@ -251,17 +281,17 @@ class ApplicantAllocations(ProgramMixin, FormView):
251
281
252
282
def dispatch (self , * args , ** kwargs ):
253
283
self .applicant = Applicant .objects .get (pk = kwargs .pop ("applicant_id" ))
254
- return super (ApplicantAllocations , self ).dispatch (* args , ** kwargs )
284
+ return super ().dispatch (* args , ** kwargs )
255
285
256
286
def get_form_kwargs (self ):
257
- kwargs = super (ApplicantAllocations , self ).get_form_kwargs ()
258
- kwargs [' applicant' ] = self .applicant
287
+ kwargs = super ().get_form_kwargs ()
288
+ kwargs [" applicant" ] = self .applicant
259
289
return kwargs
260
290
261
291
def get_context_data (self , ** kwargs ):
262
- context = super (ApplicantAllocations , self ).get_context_data (** kwargs )
263
- context [' applicant' ] = self .applicant
264
- context [' allocations' ] = self .applicant .allocations .order_by ("resource__name" )
292
+ context = super ().get_context_data (** kwargs )
293
+ context [" applicant" ] = self .applicant
294
+ context [" allocations" ] = self .applicant .allocations .order_by ("resource__name" )
265
295
return context
266
296
267
297
def form_valid (self , form ):
@@ -275,8 +305,7 @@ def post(self, request, *args, **kwargs):
275
305
if "delete" in request .POST :
276
306
self .applicant .allocations .filter (pk = request .POST ["delete_id" ]).delete ()
277
307
return redirect (self .applicant .urls .allocations )
278
- return super (ApplicantAllocations , self ).post (request , * args , ** kwargs )
279
-
308
+ return super ().post (request , * args , ** kwargs )
280
309
281
310
282
311
class ProgramResources (ProgramMixin , FormView ):
@@ -288,8 +317,8 @@ class ProgramResources(ProgramMixin, FormView):
288
317
template_name = "program-resources.html"
289
318
290
319
def get_context_data (self , ** kwargs ):
291
- context = super (ProgramResources , self ).get_context_data (** kwargs )
292
- context [' resources' ] = self .program .resources .order_by ("name" )
320
+ context = super ().get_context_data (** kwargs )
321
+ context [" resources" ] = self .program .resources .order_by ("name" )
293
322
return context
294
323
295
324
def form_valid (self , form ):
0 commit comments