File tree Expand file tree Collapse file tree 2 files changed +18
-13
lines changed Expand file tree Collapse file tree 2 files changed +18
-13
lines changed Original file line number Diff line number Diff line change 11from competitions .models import Competition
22from django import forms
33from django .core .validators import FileExtensionValidator
4- from users .models import User
4+ from problems .models import Problem
55
66
77class CompetitionForm (forms .ModelForm ):
@@ -38,8 +38,20 @@ class Meta:
3838 "competition_start" : forms .DateTimeInput (attrs = {"type" : "datetime" }),
3939 }
4040
41- def __init__ (self , user : User , ** kwargs ):
42- super ().__init__ (** kwargs )
41+ def save (self , commit : bool = True ):
42+ obj = super ().save (commit = True )
43+
44+ current_problems = Problem .objects .filter (competition = obj )
45+ current_count = current_problems .count ()
46+ requested_count = self .cleaned_data ["problem_count" ]
47+ if current_count != requested_count :
48+ if current_count > requested_count :
49+ current_problems .filter (number__gt = requested_count ).delete ()
50+ else :
51+ for i in range (current_count + 1 , requested_count + 1 ):
52+ Problem .objects .create (competition = obj , number = i )
53+
54+ return obj
4355
4456
4557class TearoffUploadForm (forms .Form ):
Original file line number Diff line number Diff line change 2020from bullet_admin .views import GenericForm
2121
2222
23- class CompetitionFormMixin ( GenericForm ):
23+ class CompetitionUpdateView ( BranchAdminAccess , UpdateView ):
2424 form_class = CompetitionForm
25-
26- def get_form_kwargs (self ):
27- kw = super ().get_form_kwargs ()
28- kw ["user" ] = self .request .user
29- return kw
30-
31-
32- class CompetitionUpdateView (BranchAdminAccess , CompetitionFormMixin , UpdateView ):
3325 form_title = "Edit competition"
3426 template_name = "bullet_admin/competition/form.html"
3527
@@ -40,7 +32,8 @@ def get_success_url(self):
4032 return reverse ("badmin:competition_switch" )
4133
4234
43- class CompetitionCreateView (BranchAdminAccess , CompetitionFormMixin , CreateView ):
35+ class CompetitionCreateView (BranchAdminAccess , CreateView ):
36+ form_class = CompetitionForm
4437 form_title = "New competition"
4538
4639 def form_valid (self , form ):
You can’t perform that action at this time.
0 commit comments