0001-manager-add-widget-to-define-time-periods-for-multip.patch
chrono/manager/forms.py | ||
---|---|---|
28 | 28 |
from django.utils.translation import ugettext_lazy as _ |
29 | 29 | |
30 | 30 |
from chrono.agendas.models import (Agenda, Event, MeetingType, TimePeriod, Desk, |
31 |
TimePeriodException) |
|
31 |
TimePeriodException, WEEKDAYS_LIST)
|
|
32 | 32 | |
33 | 33 |
from . import widgets |
34 | 34 | |
... | ... | |
91 | 91 |
exclude = [] |
92 | 92 | |
93 | 93 | |
94 |
class TimePeriodAddForm(forms.Form): |
|
95 |
weekdays = forms.MultipleChoiceField( |
|
96 |
label=_('Days'), |
|
97 |
widget=widgets.WeekdaysWidget(), |
|
98 |
choices=WEEKDAYS_LIST) |
|
99 |
start_time = forms.TimeField(label=_('Start Time'), widget=widgets.TimeWidget()) |
|
100 |
end_time = forms.TimeField(label=_('End Time'), widget=widgets.TimeWidget()) |
|
101 | ||
102 | ||
94 | 103 |
class TimePeriodForm(forms.ModelForm): |
95 | 104 |
class Meta: |
96 | 105 |
model = TimePeriod |
chrono/manager/static/css/style.scss | ||
---|---|---|
246 | 246 |
top: 3ex; |
247 | 247 |
z-index: 100; |
248 | 248 |
} |
249 | ||
250 |
ul#id_weekdays { |
|
251 |
margin: 0; |
|
252 |
padding: 0; |
|
253 |
list-style: none; |
|
254 |
max-width: 42em; |
|
255 |
li { |
|
256 |
margin: 0; |
|
257 |
padding: 0; |
|
258 |
display: inline-block; |
|
259 |
width: 10em; |
|
260 |
} |
|
261 |
} |
chrono/manager/views.py | ||
---|---|---|
37 | 37 | |
38 | 38 |
from .forms import (AgendaAddForm, AgendaEditForm, EventForm, NewMeetingTypeForm, MeetingTypeForm, |
39 | 39 |
TimePeriodForm, ImportEventsForm, NewDeskForm, DeskForm, TimePeriodExceptionForm, |
40 |
ExceptionsImportForm, AgendasImportForm) |
|
40 |
ExceptionsImportForm, AgendasImportForm, TimePeriodAddForm)
|
|
41 | 41 |
from .utils import import_site |
42 | 42 | |
43 | 43 | |
... | ... | |
621 | 621 |
meeting_type_delete = MeetingTypeDeleteView.as_view() |
622 | 622 | |
623 | 623 | |
624 |
class AgendaAddTimePeriodView(ManagedDeskMixin, CreateView):
|
|
624 |
class AgendaAddTimePeriodView(ManagedDeskMixin, FormView):
|
|
625 | 625 |
template_name = 'chrono/manager_time_period_form.html' |
626 |
model = TimePeriod |
|
627 |
form_class = TimePeriodForm |
|
626 |
form_class = TimePeriodAddForm |
|
627 | ||
628 |
def form_valid(self, form): |
|
629 |
for weekday in form.cleaned_data.get('weekdays'): |
|
630 |
period = TimePeriod( |
|
631 |
weekday=weekday, |
|
632 |
start_time=form.cleaned_data['start_time'], |
|
633 |
end_time=form.cleaned_data['end_time'], |
|
634 |
desk=self.desk) |
|
635 |
period.save() |
|
636 |
return super(AgendaAddTimePeriodView, self).form_valid(form) |
|
628 | 637 | |
629 | 638 |
agenda_add_time_period = AgendaAddTimePeriodView.as_view() |
630 | 639 |
chrono/manager/widgets.py | ||
---|---|---|
11 | 11 |
import re |
12 | 12 |
import uuid |
13 | 13 | |
14 |
from django.forms.widgets import DateTimeInput, DateInput, TimeInput |
|
14 |
from django.forms.widgets import DateTimeInput, DateInput, TimeInput, SelectMultiple
|
|
15 | 15 |
from django.utils.formats import get_language |
16 | 16 |
from django.utils.safestring import mark_safe |
17 | 17 | |
... | ... | |
173 | 173 |
options['format'] = options.get('format', 'hh:ii') |
174 | 174 | |
175 | 175 |
super(TimeWidget, self).__init__(attrs, options, usel10n) |
176 | ||
177 | ||
178 |
class WeekdaysWidget(SelectMultiple): |
|
179 |
def render(self, name, value, attrs=None, choices=()): |
|
180 |
s = [] |
|
181 |
value = value or [] |
|
182 |
for choice_id, choice_label in self.choices: |
|
183 |
s.append('<li><label><input type="checkbox" ' |
|
184 |
' name="%(name)s-%(choice_id)s" %(checked)s>' |
|
185 |
'<span>%(choice_label)s</span></label></li>' % { |
|
186 |
'name': name, |
|
187 |
'checked': 'checked' if choice_id in value else '', |
|
188 |
'choice_id': choice_id, |
|
189 |
'choice_label': choice_label}) |
|
190 |
return mark_safe('<ul id="%(id)s">' % attrs + '\n'.join(s) + '</ul>') |
|
191 | ||
192 |
def value_from_datadict(self, data, files, name): |
|
193 |
choices = [] |
|
194 |
for choice_id, choice_label in self.choices: |
|
195 |
if data.get('%s-%s' % (name, choice_id)): |
|
196 |
choices.append(choice_id) |
|
197 |
return choices |
tests/test_manager.py | ||
---|---|---|
650 | 650 |
resp = app.get('/manage/agendas/%s/' % agenda.id, status=302).follow() |
651 | 651 |
resp = resp.click('Settings') |
652 | 652 |
resp = resp.click('Add a time period') |
653 |
resp.form['weekday'].select(text='Wednesday')
|
|
653 |
resp.form['weekdays-2'].checked = True
|
|
654 | 654 |
resp.form['start_time'] = '10:00' |
655 | 655 |
resp.form['end_time'] = '17:00' |
656 | 656 |
resp = resp.form.submit() |
... | ... | |
663 | 663 | |
664 | 664 |
# add a second time period |
665 | 665 |
resp = resp.click('Add a time period', index=0) |
666 |
resp.form['weekday'].select(text='Monday')
|
|
666 |
resp.form['weekdays-0'].checked = True
|
|
667 | 667 |
resp.form['start_time'] = '10:00' |
668 | 668 |
resp.form['end_time'] = '13:00' |
669 | 669 |
resp = resp.form.submit() |
... | ... | |
677 | 677 |
assert 'Edit Time Period' in resp.text |
678 | 678 |
resp.form['start_time'] = '9:00' |
679 | 679 |
resp = resp.form.submit() |
680 |
resp = resp.follow() |
|
680 | 681 |
assert TimePeriod.objects.get(desk=desk, weekday=2).start_time.hour == 9 |
681 | 682 | |
683 |
# and add same time periods on multiple days |
|
684 |
resp = resp.click('Add a time period', index=0) |
|
685 |
resp.form['weekdays-4'].checked = True |
|
686 |
resp.form['weekdays-5'].checked = True |
|
687 |
resp.form['start_time'] = '10:00' |
|
688 |
resp.form['end_time'] = '13:00' |
|
689 |
resp = resp.form.submit() |
|
690 |
assert TimePeriod.objects.filter(desk=desk).count() == 4 |
|
682 | 691 | |
683 | 692 |
def test_meetings_agenda_delete_time_period(app, admin_user): |
684 | 693 |
agenda = Agenda(label=u'Foo bar', kind='meetings') |
... | ... | |
810 | 819 |
resp = resp.form.submit().follow() |
811 | 820 |
# adding a new time period |
812 | 821 |
resp = resp.click('Add a time period') |
813 |
resp.form['weekday'].select(text='Wednesday')
|
|
822 |
resp.form['weekdays-2'].checked = True
|
|
814 | 823 |
resp.form['start_time'] = '10:00' |
815 | 824 |
resp.form['end_time'] = '17:00' |
816 | 825 |
resp = resp.form.submit().follow() |
817 |
- |