-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathforms.py
More file actions
880 lines (725 loc) · 32 KB
/
forms.py
File metadata and controls
880 lines (725 loc) · 32 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
from decimal import Decimal
import os
import itertools
import pprint
from django.db import transaction
from django import forms
from django.core.exceptions import ValidationError
from mittab.apps.tab.models import *
from mittab.libs import errors
from mittab import settings
from mittab.libs.cacheing import cache_logic
class UploadBackupForm(forms.Form):
file = forms.FileField(label="Your Backup File")
class UploadDataForm(forms.Form):
team_file = forms.FileField(label="Teams Data File", required=False)
judge_file = forms.FileField(label="Judge Data File", required=False)
room_file = forms.FileField(label="Room Data File", required=False)
scratch_file = forms.FileField(label="Scratch Data File", required=False)
class SchoolForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(SchoolForm, self).__init__(*args, **kwargs)
class Meta:
model = School
fields = "__all__"
class SchoolApdaIdForm(forms.ModelForm):
class Meta:
model = School
fields = ["apda_id"]
class RoomForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
entry = "first_entry" in kwargs
if entry:
kwargs.pop("first_entry")
super(RoomForm, self).__init__(*args, **kwargs)
if not entry:
num_rounds = TabSettings.objects.get(key="tot_rounds").value
try:
room = kwargs["instance"]
checkins = [
c.round_number for c in RoomCheckIn.objects.filter(room=room)
]
for i in range(-1, num_rounds):
# 0 is included as zero represents outrounds
label = f"Checked in for round {i + 1}?"
if i == -1:
label = "Checked in for outrounds?"
field_name = f"checkin_{i}"
self.fields[field_name] = forms.BooleanField(
label=label,
initial=i + 1 in checkins,
required=False)
except Exception:
pass
def save(self, commit=True):
room = super(RoomForm, self).save(commit)
num_rounds = TabSettings.objects.get(key="tot_rounds").value
for i in range(-1, num_rounds):
field_name = f"checkin_{i}"
if field_name in self.cleaned_data:
should_be_checked_in = self.cleaned_data[field_name]
checked_in = RoomCheckIn.objects.filter(room=room,
round_number=i + 1)
# Two cases, either the room is not checked in and the user says he is,
# or the room is checked in and the user says he is not
if not checked_in and should_be_checked_in:
checked_in = RoomCheckIn(room=room, round_number=i + 1)
checked_in.save()
elif checked_in and not should_be_checked_in:
checked_in.delete()
return room
class Meta:
model = Room
fields = "__all__"
class JudgeForm(forms.ModelForm):
schools = forms.ModelMultipleChoiceField(queryset=School.objects.all(),
required=False)
def __init__(self, *args, **kwargs):
entry = "first_entry" in kwargs
if entry:
kwargs.pop("first_entry")
super(JudgeForm, self).__init__(*args, **kwargs)
if not entry:
num_rounds = TabSettings.objects.get(key="tot_rounds").value
try:
judge = kwargs["instance"]
checkins = [
c.round_number for c in CheckIn.objects.filter(judge=judge)
]
for i in range(-1, num_rounds):
# 0 is included as zero represents outrounds
label = f"Checked in for round {i + 1}?"
if i == -1:
label = "Checked in for outrounds?"
field_name = f"checkin_{i}"
self.fields[field_name] = forms.BooleanField(
label=label,
initial=i + 1 in checkins,
required=False)
except Exception:
pass
def save(self, commit=True):
judge = super(JudgeForm, self).save(commit)
num_rounds = TabSettings.objects.get(key="tot_rounds").value
for i in range(-1, num_rounds):
field_name = f"checkin_{i}"
if field_name in self.cleaned_data:
should_be_checked_in = self.cleaned_data[field_name]
checked_in = CheckIn.objects.filter(judge=judge,
round_number=i + 1)
# Two cases, either the judge is not checked in and the user says he is,
# or the judge is checked in and the user says he is not
if not checked_in and should_be_checked_in:
checked_in = CheckIn(judge=judge, round_number=i + 1)
checked_in.save()
elif checked_in and not should_be_checked_in:
checked_in.delete()
return judge
class Meta:
model = Judge
fields = "__all__"
class Media:
css = {
"all": (os.path.join(settings.BASE_DIR,
"/static/admin/css/widgets.css"), ),
}
js = ("/admin/jsi18n"),
class TeamForm(forms.ModelForm):
debaters = forms.ModelMultipleChoiceField(
queryset=Debater.objects.all(),
required=False
)
def clean_debaters(self):
data = self.cleaned_data["debaters"]
if len(data) not in [1, 2]:
raise forms.ValidationError("You must select 1 or 2 debaters!")
for debater in data:
if debater.team_set.count() > 1 or \
(debater.team_set.count() == 1 and
debater.team_set.first().id != self.instance.id):
raise forms.ValidationError(
"""A debater cannot already be on a different team!
Consider editing the debaters' existing team,
or removing them from it before creating this one."""
)
return data
class Meta:
model = Team
exclude = ["tiebreaker"]
class Media:
css = {
"all": (os.path.join(settings.BASE_DIR,
"/static/admin/css/widgets.css"), ),
}
js = ("/admin/jsi18n"),
class TeamEntryForm(TeamForm):
number_scratches = forms.IntegerField(label="How many initial scratches?",
initial=0)
def clean_debaters(self):
data = self.cleaned_data["debaters"]
for debater in data:
if debater.team_set.count() > 0:
raise forms.ValidationError(
"""A debater cannot already be on a different team!
Consider editing the debaters' existing team,
or removing them from it before creating this one."""
)
return data
class Meta:
model = Team
exclude = ["tiebreaker"]
class ScratchForm(forms.ModelForm):
team = forms.ChoiceField(choices=[])
judge = forms.ChoiceField(choices=[])
scratch_type = forms.ChoiceField(choices=Scratch.TYPE_CHOICES)
def __init__(self, *args, **kwargs):
team_queryset = kwargs.pop("team_queryset", Team.objects.all())
judge_queryset = kwargs.pop("judge_queryset", Judge.objects.all())
super(ScratchForm, self).__init__(*args, **kwargs)
self.fields["team"].choices = [
(str(team.id), team.name) for team in team_queryset
]
self.fields["judge"].choices = [
(str(judge.id), judge.name) for judge in judge_queryset
]
# If we're editing an existing scratch, set initial values
if self.instance and self.instance.pk:
self.fields["team"].initial = str(self.instance.team.id)
self.fields["judge"].initial = str(self.instance.judge.id)
def save(self, commit=True):
instance = super(ScratchForm, self).save(commit=False)
# Convert string IDs to actual model instances
instance.team = Team.objects.get(pk=int(self.cleaned_data["team"]))
instance.judge = Judge.objects.get(pk=int(self.cleaned_data["judge"]))
if commit:
instance.save()
return instance
class Meta:
model = Scratch
fields = "__all__"
exclude = ["team", "judge"]
class JudgeJudgeScratchForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
judge_queryset = kwargs.pop("judge_queryset", Judge.objects.all())
super().__init__(*args, **kwargs)
self.fields["judge_one"].queryset = judge_queryset
self.fields["judge_two"].queryset = judge_queryset
def clean(self):
cleaned_data = super().clean()
judge_one = cleaned_data.get("judge_one")
judge_two = cleaned_data.get("judge_two")
if judge_one and judge_two and judge_one == judge_two:
raise forms.ValidationError("Pick two different judges")
return cleaned_data
class Meta:
model = JudgeJudgeScratch
fields = "__all__"
class TeamTeamScratchForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
team_queryset = kwargs.pop("team_queryset", Team.objects.all())
super().__init__(*args, **kwargs)
self.fields["team_one"].queryset = team_queryset
self.fields["team_two"].queryset = team_queryset
def clean(self):
cleaned_data = super().clean()
team_one = cleaned_data.get("team_one")
team_two = cleaned_data.get("team_two")
if team_one and team_two and team_one == team_two:
raise forms.ValidationError("Pick two different teams")
return cleaned_data
class Meta:
model = TeamTeamScratch
fields = "__all__"
class DebaterForm(forms.ModelForm):
class Meta:
model = Debater
exclude = ["tiebreaker"]
class DebaterApdaIdForm(forms.ModelForm):
class Meta:
model = Debater
fields = ["apda_id"]
def validate_speaks(value):
if not (TabSettings.get("min_speak", 0) <= value <= TabSettings.get(
"max_speak", 50)):
raise ValidationError(
f"{value} is an entirely invalid speaker score, try again.")
class ResultEntryForm(forms.Form):
NAMES = {
"pm": "Prime Minister",
"mg": "Member of Government",
"lo": "Leader of the Opposition",
"mo": "Member of the Opposition"
}
GOV = ["pm", "mg"]
OPP = ["lo", "mo"]
DEBATERS = GOV + OPP
RANKS = (
(1, 1),
(2, 2),
(3, 3),
(4, 4),
)
winner = forms.ChoiceField(label="Which team won the round?",
choices=Round.VICTOR_CHOICES)
def __init__(self, *args, **kwargs):
# Have to pop these off before sending to the super constructor
round_object = kwargs.pop("round_instance")
no_fill = False
if "no_fill" in kwargs:
kwargs.pop("no_fill")
no_fill = True
super(ResultEntryForm, self).__init__(*args, **kwargs)
# If we already have information, fill that into the form
if round_object.victor != 0 and not no_fill:
self.fields["winner"].initial = round_object.victor
self.fields["round_instance"] = forms.IntegerField(
initial=round_object.pk, widget=forms.HiddenInput())
gov_team, opp_team = round_object.gov_team, round_object.opp_team
gov_debaters = [(-1, "---")] + [(d.id, d.name)
for d in gov_team.debaters.all()]
opp_debaters = [(-1, "---")] + [(d.id, d.name)
for d in opp_team.debaters.all()]
for deb in self.DEBATERS:
debater_choices = gov_debaters if deb in self.GOV else opp_debaters
self.fields[self.deb_attr_name(
deb, "debater")] = forms.ChoiceField(
label=f"Who was {self.NAMES[deb]}?",
choices=debater_choices
)
self.fields[self.deb_attr_name(
deb, "speaks")] = forms.DecimalField(
label=f"{self.NAMES[deb]} Speaks",
validators=[validate_speaks])
self.fields[self.deb_attr_name(deb, "ranks")] = forms.ChoiceField(
label=f"{self.NAMES[deb]} Rank", choices=self.RANKS)
if round_object.victor == 0 or no_fill:
return
for stats in round_object.roundstats_set.all():
deb = stats.debater_role
self.fields[self.deb_attr_name(
deb, "debater")].initial = stats.debater.id
self.fields[self.deb_attr_name(
deb, "speaks")].initial = stats.speaks
self.fields[self.deb_attr_name(deb, "ranks")].initial = int(
round(stats.ranks))
def clean(self):
cleaned_data = self.cleaned_data
try:
speak_ranks = [(self.deb_attr_val(d, "speaks"),
self.deb_attr_val(d, "ranks"), d)
for d in self.DEBATERS]
sorted_by_ranks = sorted(speak_ranks, key=lambda x: x[1])
# Check to make sure everyone has different ranks
if self.has_invalid_ranks():
for deb in self.DEBATERS:
self.add_error(
self.deb_attr_name(deb, "ranks"),
self.error_class(["Ranks must be different"]))
# Check to make sure that the lowest ranks have the highest scores
high_score = sorted_by_ranks[0][0]
for (speaks, _rank, deb) in sorted_by_ranks:
if speaks > high_score:
self.add_error(
self.deb_attr_name(deb, "speaks"),
self.error_class(
["These speaks are too high for the rank"]))
high_score = speaks
# Make sure that all debaters were selected
for deb in self.DEBATERS:
if self.deb_attr_val(deb, "debater", int) == -1:
self.add_error(
self.deb_attr_name(deb, "debater"),
self.error_class(["You need to pick a debater"]))
cleaned_data["winner"] = int(cleaned_data["winner"])
if cleaned_data["winner"] == Round.NONE:
self.add_error("winner",
self.error_class(["Someone has to win!"]))
# If we already have errors, don't bother with the other validations
if self.errors:
return
# Check to make sure that the team with most speaks and the least
# ranks win the round
gov_speaks = sum(
[self.deb_attr_val(d, "speaks", float) for d in self.GOV])
opp_speaks = sum(
[self.deb_attr_val(d, "speaks", float) for d in self.OPP])
gov_ranks = sum(
[self.deb_attr_val(d, "ranks", int) for d in self.GOV])
opp_ranks = sum(
[self.deb_attr_val(d, "ranks", int) for d in self.OPP])
gov_points = (gov_speaks, -gov_ranks)
opp_points = (opp_speaks, -opp_ranks)
if cleaned_data["winner"] == Round.GOV and opp_points > gov_points:
self.add_error("winner", self.error_class(["Low Point Win!!"]))
elif cleaned_data[
"winner"] == Round.OPP and gov_points > opp_points:
self.add_error("winner", self.error_class(["Low Point Win!!"]))
except Exception:
errors.emit_current_exception()
self.add_error(
"winner",
self.error_class(
["Non handled error, preventing data contamination"]))
return cleaned_data
def save(self, _commit=True):
cleaned_data = self.cleaned_data
round_obj = Round.objects.get(pk=cleaned_data["round_instance"])
if not round_obj.victor == cleaned_data["winner"]:
cache_logic.invalidate_cache("team_rankings")
cache_logic.invalidate_cache("speaker_rankings")
round_obj.victor = cleaned_data["winner"]
with transaction.atomic():
for debater in self.DEBATERS:
old_stats = RoundStats.objects.filter(round=round_obj,
debater_role=debater)
debater_obj = Debater.objects.get(
pk=self.deb_attr_val(debater, "debater"))
stats = RoundStats(
debater=debater_obj,
round=round_obj,
speaks=self.deb_attr_val(debater, "speaks", float),
ranks=self.deb_attr_val(debater, "ranks", int),
debater_role=debater)
if old_stats.exists():
for old_stat in old_stats:
if (old_stat.debater != stats.debater) or \
(old_stat.speaks != stats.speaks) or \
(old_stat.ranks != stats.ranks) or \
(old_stat.debater_role != stats.debater_role) or \
(old_stat.round != stats.round):
cache_logic.invalidate_cache("team_rankings")
cache_logic.invalidate_cache("speaker_rankings")
old_stats.delete()
stats.save()
round_obj.save()
return round_obj
def deb_attr_val(self, position, attr, cast=None):
val = self.cleaned_data[self.deb_attr_name(position, attr)]
if cast:
return cast(val)
else:
return val
def deb_attr_name(self, position, attr):
return f"{position}_{attr}"
def has_invalid_ranks(self):
ranks = [int(self.deb_attr_val(d, "ranks")) for d in self.DEBATERS]
return sorted(ranks) != [1, 2, 3, 4]
class EBallotForm(ResultEntryForm):
ballot_code = forms.CharField(max_length=30, min_length=0)
def __init__(self, *args, **kwargs):
ballot_code = ""
if "ballot_code" in kwargs:
ballot_code = kwargs.pop("ballot_code")
super(EBallotForm, self).__init__(*args, **kwargs)
self.fields["ballot_code"].initial = ballot_code
def clean(self):
cleaned_data = self.cleaned_data
round_obj = Round.objects.get(pk=cleaned_data["round_instance"])
cur_round = TabSettings.get("cur_round", 0) - 1
try:
ballot_code = cleaned_data.get("ballot_code")
judge = Judge.objects.filter(ballot_code=ballot_code).first()
if not judge:
msg = "Incorrect ballot code. Enter again."
self._errors["ballot_code"] = self.error_class([msg])
elif round_obj.round_number != cur_round:
msg = (
f"This ballot is for round {round_obj.round_number}, "
f"but the current round is {cur_round}. "
"Go to tab to submit this result."
)
self._errors["winner"] = self.error_class([msg])
else:
if round_obj.chair.ballot_code != judge.ballot_code:
msg = "You are not judging the round, or you are not the chair"
self._errors["ballot_code"] = self.error_class([msg])
elif RoundStats.objects.filter(round=round_obj).first():
msg = (
"A ballot has already been completed for this round. "
"Go to tab if you need to change the results."
)
self._errors["ballot_code"] = self.error_class([msg])
if int(cleaned_data["winner"]) not in [Round.GOV, Round.OPP]:
msg = "Go to tab to submit a result other than a win or loss."
self._errors["winner"] = self.error_class([msg])
for deb in self.DEBATERS:
speaks = self.deb_attr_val(deb, "speaks", float)
_, decimal_val = str(speaks).split(".")
key = self.deb_attr_name(deb, "speaks")
if int(decimal_val) != 0:
msg = "Speaks must be whole numbers"
self._errors[key] = self.error_class([msg])
if speaks > float(TabSettings.get("max_eballot_speak", 35)) or \
speaks < float(TabSettings.get("min_eballot_speak", 15)):
msg = "Speaks must be justified to tab."
self._errors[key] = self.error_class([msg])
except Exception as e:
print(f"Caught error {e}")
self._errors["winner"] = self.error_class(
["Non handled error, preventing data contamination"])
return super(EBallotForm, self).clean()
class SettingsForm(forms.Form):
def __init__(self, *args, **kwargs):
settings_to_import = kwargs.pop("settings")
self.settings = settings_to_import
super(SettingsForm, self).__init__(*args, **kwargs)
for setting in self.settings:
field_name = f"setting_{setting['name']}"
label = setting["name"].replace("_", " ").title()
if setting.get("type") == "boolean":
self.fields[field_name] = forms.BooleanField(
label=label,
help_text=setting["description"],
initial=setting["value"],
required=False,
widget=forms.CheckboxInput(attrs={
"class": "form-check-input"
})
)
elif setting.get("type") == "choice":
choices = [(c[0], c[1]) for c in setting.get("choices", [])]
self.fields[field_name] = forms.TypedChoiceField(
label=label,
help_text=setting["description"],
choices=choices,
initial=setting["value"],
coerce=int,
widget=forms.Select(attrs={
"class": "form-control"
})
)
elif setting.get("type") == "text":
self.fields[field_name] = forms.CharField(
label=label,
help_text=setting["description"],
initial=setting["value"],
required=False,
widget=forms.TextInput(attrs={
"class": "form-control",
"style": "min-width: 300px;"
})
)
else:
self.fields[field_name] = forms.IntegerField(
label=label,
help_text=setting["description"],
initial=setting["value"],
widget=forms.NumberInput(attrs={
"class": "form-control"
})
)
def save(self):
for setting in self.settings:
field = f"setting_{setting['name']}"
key = setting["name"]
if "type" in setting and setting["type"] == "boolean":
value_to_set = 1 if self.cleaned_data[field] else 0
else:
value_to_set = self.cleaned_data[field]
TabSettings.set(key, value_to_set)
def validate_panel(result):
all_good = True
all_results = list(itertools.chain(*list(result.values())))
debater_roles = list(zip(*all_results))
# Check everyone is in the same position
for debater in debater_roles:
debs = [(deb, role) for (deb, role, _speak, _rank) in debater]
if not all(deb == debs[0] for deb in debs):
all_good = False
break
# Check the winner makes sense
final_winner = max([(len(v), k) for k, v in result.items()])[1]
debater_roles = list(zip(*result[final_winner]))
for debater in debater_roles:
debs = [(deb, role) for (deb, role, _speak, _rank) in debater]
if ((len(debs) != len(result[final_winner])) or (len(debs) < 2)):
all_good = False
break
return all_good, "Inconsistent Panel results, please check yourself"
def score_panel(result, discard_minority):
final_winner = max([(len(v), k) for k, v in result.items()])[1]
debater_roles = list(zip(*result[final_winner]))
# Take all speaks and ranks even if they are a minority judge
if not discard_minority:
all_results = list(itertools.chain(*list(result.values())))
debater_roles = list(zip(*all_results))
final_scores = []
for debater in debater_roles:
debs = [(deb, role) for (deb, role, _speak, _rank) in debater]
deb, role = debs[0]
speaks = [speak for (_deb, _role, speak, _rank) in debater]
avg_speaks = sum(speaks) / float(len(speaks))
ranks = [rank for (_deb, _role, _speak, rank) in debater]
avg_ranks = sum(ranks) / float(len(ranks))
final_scores.append((deb, role, avg_speaks, avg_ranks))
# Rank by resulting average speaks
ranked = sorted([score for score in final_scores],
key=lambda x: Decimal(x[3]).quantize(Decimal("1.00")))
ranked = sorted([score for score in ranked],
key=lambda x: Decimal(x[2]).quantize(Decimal("1.00")),
reverse=True)
ranked = [(deb, role, speak, rank + 1)
for (rank, (deb, role, speak, _)) in enumerate(ranked)]
print("Ranked Debaters")
pprint.pprint(ranked)
# Break any ties by taking the average of the tied ranks
ties = {}
for (score_i, score) in enumerate(ranked):
# For floating point roundoff errors
d_score = Decimal(score[2]).quantize(Decimal("1.00"))
d_rank = Decimal(score[3]).quantize(Decimal("1.00"))
tie_key = (d_score, d_rank)
if tie_key in ties:
ties[tie_key].append((score_i, score[3]))
else:
ties[tie_key] = [(score_i, score[3])]
print("Ties")
pprint.pprint(ties)
# Average over the tied ranks
for val in ties.values():
if len(val) > 1:
tied_ranks = [rank for _score_i, rank in val]
avg = sum(tied_ranks) / float(len(tied_ranks))
for i, _rank in val:
final_score = ranked[i]
ranked[i] = (final_score[0], final_score[1], final_score[2],
avg)
print("Final scores")
pprint.pprint(ranked)
return ranked, final_winner
class OutroundResultEntryForm(forms.Form):
winner = forms.ChoiceField(label="Which team won the round?",
choices=Outround.VICTOR_CHOICES)
def __init__(self, *args, **kwargs):
# Have to pop these off before sending to the super constructor
round_object = kwargs.pop("round_instance")
no_fill = False
if "no_fill" in kwargs:
kwargs.pop("no_fill")
no_fill = True
super(OutroundResultEntryForm, self).__init__(*args, **kwargs)
# If we already have information, fill that into the form
if round_object.victor != 0 and not no_fill:
self.fields["winner"].initial = round_object.victor
self.fields["round_instance"] = forms.IntegerField(
initial=round_object.pk, widget=forms.HiddenInput())
if round_object.victor == 0 or no_fill:
return
def clean(self):
cleaned_data = self.cleaned_data
try:
if cleaned_data["winner"] == Round.NONE:
self.add_error("winner",
self.error_class(["Someone has to win!"]))
if self.errors:
return
except Exception:
errors.emit_current_exception()
self.add_error(
"winner",
self.error_class(
["Non handled error, preventing data contamination"]))
return cleaned_data
def save(self, _commit=True):
cleaned_data = self.cleaned_data
round_obj = Outround.objects.get(pk=cleaned_data["round_instance"])
round_obj.victor = cleaned_data["winner"]
round_obj.save()
round_obj = Outround.objects.get(pk=cleaned_data["round_instance"])
if round_obj.victor > 0:
winning_team_seed = round_obj.winner.breaking_team.effective_seed
losing_team_seed = round_obj.loser.breaking_team.effective_seed
if losing_team_seed < winning_team_seed:
round_obj.winner.breaking_team.effective_seed = losing_team_seed
round_obj.winner.breaking_team.save()
breaking_team = round_obj.loser.breaking_team
breaking_team.effective_seed = breaking_team.seed
breaking_team.save()
return round_obj
class RoomTagForm(forms.ModelForm):
teams = forms.ModelMultipleChoiceField(
queryset=Team.objects.all(),
required=False,
)
judges = forms.ModelMultipleChoiceField(
queryset=Judge.objects.all(),
required=False,
)
rooms = forms.ModelMultipleChoiceField(
queryset=Room.objects.all(),
required=False,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk:
self.fields["teams"].initial = self.instance.team_set.all()
self.fields["judges"].initial = self.instance.judge_set.all()
self.fields["rooms"].initial = self.instance.room_set.all()
def save(self, commit=True):
room_tag = super().save(commit=commit)
room_tag.team_set.set(self.cleaned_data.get("teams", []))
room_tag.judge_set.set(self.cleaned_data.get("judges", []))
room_tag.room_set.set(self.cleaned_data.get("rooms", []))
return room_tag
class Meta:
model = RoomTag
fields = ("tag", "priority", "teams", "judges", "rooms")
class MiniRoomTagForm(RoomTagForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields.pop("teams")
self.fields.pop("judges")
self.fields.pop("rooms")
class RankingGroupForm(forms.ModelForm):
teams = forms.ModelMultipleChoiceField(
queryset=Team.objects.all(),
required=False,
)
debaters = forms.ModelMultipleChoiceField(
queryset=Debater.objects.all(),
required=False,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk:
self.fields["teams"].initial = self.instance.teams.all()
self.fields["debaters"].initial = self.instance.debaters.all()
def save(self, commit=True):
ranking_group = super().save(commit=False)
def save_m2m():
ranking_group.teams.set(self.cleaned_data.get("teams", []))
ranking_group.debaters.set(self.cleaned_data.get("debaters", []))
if commit:
ranking_group.save()
save_m2m()
else:
self._save_m2m = save_m2m
return ranking_group
class Meta:
model = RankingGroup
fields = ("name", "teams", "debaters")
class MiniRankingGroupForm(RankingGroupForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields.pop("teams")
self.fields.pop("debaters")
class BackupForm(forms.Form):
backup_name = forms.CharField(
max_length=255,
label="Backup name",
widget=forms.TextInput(
attrs={"placeholder": "Enter backup name", "pattern": r"[^_]*"}
)
)
include_scratches = forms.BooleanField(
required=False,
initial=True,
label="Include scratches"
)
def clean_backup_name(self):
backup_name = self.cleaned_data["backup_name"]
if "_" in backup_name:
raise forms.ValidationError(
"Backup name cannot contain underscores (_)."
)
return backup_name