1+ import datetime # for mass form
2+
13from django import forms
24
35from .models import EnrichmentActivity
@@ -13,3 +15,60 @@ class Meta:
1315 model = EnrichmentActivity
1416
1517 fields = ["title" , "description" , "time" , "location" , "capacity" , "presign" , "groups_allowed" , "groups_blacklisted" ]
18+
19+
20+ WEEKDAY_FIELDS = [
21+ ("monday" , "Monday" ),
22+ ("tuesday" , "Tuesday" ),
23+ ("wednesday" , "Wednesday" ),
24+ ("thursday" , "Thursday" ),
25+ ("friday" , "Friday" ),
26+ ]
27+
28+
29+ class EnrichmentActivityBulkForm (EnrichmentActivityForm ):
30+ """A way to create multiple enrichment activities at once.
31+
32+ A user would check off boxes given a date in the selected week.
33+ """
34+
35+ week_of = forms .DateField (
36+ label = "Target Week" ,
37+ widget = forms .DateInput (attrs = {"type" : "date" }),
38+ )
39+ monday = forms .BooleanField (label = "Monday" , required = False )
40+ tuesday = forms .BooleanField (label = "Tuesday" , required = False )
41+ wednesday = forms .BooleanField (label = "Wednesday" , required = False )
42+ thursday = forms .BooleanField (label = "Thursday" , required = False )
43+ friday = forms .BooleanField (label = "Friday" , required = False )
44+ activity_time = forms .TimeField (label = "Time of day" , widget = forms .TimeInput (attrs = {"type" : "time" }), initial = datetime .time (12 , 0 ))
45+
46+ def __init__ (self , * args , ** kwargs ):
47+ super ().__init__ (* args , ** kwargs )
48+ # remove time so it doesn't appear in the form
49+ self .fields .pop ("time" )
50+
51+ def clean (self ):
52+ cleaned = super ().clean ()
53+ # Require at least one day to be checked
54+ days_checked = any (cleaned .get (day ) for day , _ in WEEKDAY_FIELDS )
55+ if not days_checked :
56+ raise forms .ValidationError ("Please select at least one day of the week." )
57+ return cleaned
58+
59+ def get_selected_dates (self ): # returns in the form of a list.
60+ week_of = self .cleaned_data ["week_of" ]
61+ activity_time = self .cleaned_data ["activity_time" ]
62+ monday = week_of - datetime .timedelta (days = week_of .weekday ())
63+ offsets = {
64+ "monday" : 0 ,
65+ "tuesday" : 1 ,
66+ "wednesday" : 2 ,
67+ "thursday" : 3 ,
68+ "friday" : 4 ,
69+ }
70+ return [
71+ datetime .datetime .combine (monday + datetime .timedelta (days = offset ), activity_time )
72+ for day_name , offset in offsets .items ()
73+ if self .cleaned_data .get (day_name )
74+ ]
0 commit comments