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
|
# The Summit Scheduler web application
# Copyright (C) 2008 - 2013 Ubuntu Community, Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.conf import settings
from django import forms
from summit.schedule.models.meetingmodel import Meeting
from summit.schedule.models.attendeemodel import Attendee
from summit.schedule.models.participantmodel import Participant
from common.forms import RenderableMixin
from common.widgets import DateTimeWidget
class MultipleAttendeeField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
return u"%s %s (%s)" % (
obj.user.first_name,
obj.user.last_name,
obj.user.username
)
class MeetingFormBase(forms.ModelForm):
participants = MultipleAttendeeField(
queryset=Attendee.objects.all,
widget=forms.CheckboxSelectMultiple,
label='Participants',
required=False
)
def __init__(self, *args, **kwargs):
if 'instance' in kwargs:
if kwargs['instance'].pk is not None:
# We get the 'initial' keyword argument or initialize it
# as a dict if it didn't exist.
initial = kwargs.setdefault('initial', {})
# The widget for a ModelMultipleChoiceField expects
# a list of primary key for the selected data.
initial['participants'] = [
attendee.pk
for attendee in kwargs['instance'].participants.all()]
super(MeetingFormBase, self).__init__(*args, **kwargs)
self.fields['tracks'].queryset = self.instance.summit.track_set.all()
summit = self.instance.summit
self.fields['participants'].queryset = Attendee.objects.filter(
summit=summit
).order_by(
'user__first_name',
'user__last_name',
'user__username'
)
def save(self, commit=True):
instance = super(MeetingFormBase, self).save(commit)
# Prepare a 'save_m2m' method for the form,
if 'save_m2m' in dir(self):
old_save_m2m = self.save_m2m
else:
old_save_m2m = None
def save_m2m():
if old_save_m2m is not None:
old_save_m2m()
# This is where we actually link the pizza with toppings
instance.participants.clear()
for participant in self.cleaned_data['participants']:
record = Participant(
meeting=instance,
attendee=participant,
participation='INTERESTED'
)
record.save()
self.save_m2m = save_m2m
# Do we need to save all changes now?
if commit:
instance.save()
self.save_m2m()
return instance
class CreateMeeting(MeetingFormBase, RenderableMixin):
class Meta:
model = Meeting
fields = (
'title',
'description',
'tracks',
'type',
'private',
'spec_url',
'wiki_url',
'pad_url',
'requires_dial_in',
'slots',
'approved',
'video',
'override_break'
)
class OrganizerEditMeeting(MeetingFormBase, RenderableMixin):
class Meta:
model = Meeting
fields = (
'title',
'description',
'tracks',
'type',
'private',
'spec_url',
'wiki_url',
'pad_url',
'requires_dial_in',
'slots',
'approved',
'video',
'override_break'
)
class ProposeMeeting(MeetingFormBase, RenderableMixin):
class Meta:
model = Meeting
fields = (
'title',
'description',
'tracks',
'spec_url',
'wiki_url',
'pad_url'
)
class EditMeeting(MeetingFormBase, RenderableMixin):
class Meta:
model = Meeting
fields = (
'title',
'description',
'tracks',
'spec_url',
'wiki_url',
'pad_url'
)
class MeetingReview(forms.ModelForm, RenderableMixin):
class Meta:
model = Meeting
fields = ('approved',)
class AttendMeeting(forms.ModelForm, RenderableMixin):
class Meta:
model = Participant
fields = ('participation',)
def __init__(self, *args, **kwargs):
super(AttendMeeting, self).__init__(*args, **kwargs)
self.fields['participation'].choices = (
(u'ATTENDING', u'Attending'),
(u'INTERESTED', u'Very interested in attending')
)
class OrganizerChangeAttend(forms.ModelForm, RenderableMixin):
class Meta:
model = Participant
fields = ('participation',)
def __init__(self, *args, **kwargs):
super(OrganizerChangeAttend, self).__init__(*args, **kwargs)
self.fields['participation'].choices = (
(u'INTERESTED', u'Should Attend'),
(u'REQUIRED', u'Required to Attend')
)
import re
YOUTUBE_EMBED_HTML = re.compile(r'\<iframe width="\d+" height="\d+" src="https?:\/\/www.youtube.com\/embed\/(?P<youtube_id>.*)" frameborder="0" allowfullscreen\>\<\/iframe\>')
YOUTUBE_PAGE_URL = re.compile(r'https?:\/\/www.youtube.com\/watch\?v=(?P<youtube_id>[^&]*).*')
YOUTUBE_SHORT_URL = re.compile(r'https?:\/\/youtu.be\/(?P<youtube_id>[^\?]*).*')
class YouTubeEmbedURL(forms.URLField):
def clean(self, value):
match_html = YOUTUBE_EMBED_HTML.match(value)
match_page = YOUTUBE_PAGE_URL.match(value)
match_short = YOUTUBE_SHORT_URL.match(value)
if match_html:
value = 'http://www.youtube.com/embed/' + match_html.group('youtube_id')
elif match_page:
value = 'http://www.youtube.com/embed/' + match_page.group('youtube_id')
elif match_short:
value = 'http://www.youtube.com/embed/' + match_short.group('youtube_id')
else:
pass
return value
class EditMeetingHangout(forms.ModelForm, RenderableMixin):
class Meta:
model = Meeting
fields = ('hangout_url', 'broadcast_url')
broadcast_url = YouTubeEmbedURL(label='Broadcast URL')
class Registration(forms.ModelForm, RenderableMixin):
class Meta:
model = Attendee
fields = (
'start_utc',
'end_utc',
'crew',
)
def __init__(self, *args, **kargs):
super(Registration, self).__init__(*args, **kargs)
self.fields['start_utc'].widget = DateTimeWidget()
self.fields['end_utc'].widget = DateTimeWidget()
def clean(self):
begin = self.cleaned_data.get('start_utc')
end = self.cleaned_data.get('end_utc')
if begin and end and begin > end:
raise forms.ValidationError(
"Availability can not end before it starts."
)
return self.cleaned_data
def save(self, commit=True):
instance = super(Registration, self).save(commit)
instance.from_launchpad = False
instance.save()
|