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