1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """html calendar views""" |
|
19 |
|
20 __docformat__ = "restructuredtext en" |
|
21 _ = unicode |
|
22 |
|
23 from datetime import date, time, timedelta |
|
24 |
|
25 from logilab.mtconverter import xml_escape |
|
26 from logilab.common.date import (ONEDAY, ONEWEEK, days_in_month, previous_month, |
|
27 next_month, first_day, last_day, date_range) |
|
28 |
|
29 from cubicweb.interfaces import ICalendarViews |
|
30 from cubicweb.selectors import implements, adaptable |
|
31 from cubicweb.view import EntityView, EntityAdapter, implements_adapter_compat |
|
32 |
|
33 class ICalendarViewsAdapter(EntityAdapter): |
|
34 """calendar views interface""" |
|
35 __regid__ = 'ICalendarViews' |
|
36 __select__ = implements(ICalendarViews, warn=False) # XXX for bw compat, should be abstract |
|
37 |
|
38 @implements_adapter_compat('ICalendarViews') |
|
39 def matching_dates(self, begin, end): |
|
40 """ |
|
41 :param begin: day considered as begin of the range (`DateTime`) |
|
42 :param end: day considered as end of the range (`DateTime`) |
|
43 |
|
44 :return: |
|
45 a list of dates (`DateTime`) in the range [`begin`, `end`] on which |
|
46 this entity apply |
|
47 """ |
|
48 raise NotImplementedError |
|
49 |
|
50 |
|
51 # used by i18n tools |
|
52 WEEKDAYS = [_("monday"), _("tuesday"), _("wednesday"), _("thursday"), |
|
53 _("friday"), _("saturday"), _("sunday")] |
|
54 MONTHNAMES = [ _('january'), _('february'), _('march'), _('april'), _('may'), |
|
55 _('june'), _('july'), _('august'), _('september'), _('october'), |
|
56 _('november'), _('december') |
|
57 ] |
|
58 |
|
59 class _CalendarView(EntityView): |
|
60 """base calendar view containing helpful methods to build calendar views""" |
|
61 __select__ = adaptable('ICalendarViews') |
|
62 paginable = False |
|
63 |
|
64 # Navigation building methods / views #################################### |
|
65 |
|
66 PREV = u'<a href="%s"><<</a>  <a href="%s"><</a>' |
|
67 NEXT = u'<a href="%s">></a>  <a href="%s">>></a>' |
|
68 NAV_HEADER = u"""<table class="calendarPageHeader"> |
|
69 <tr><td class="prev">%s</td><td class="next">%s</td></tr> |
|
70 </table> |
|
71 """ % (PREV, NEXT) |
|
72 |
|
73 def nav_header(self, date, smallshift=3, bigshift=9): |
|
74 """prints shortcut links to go to previous/next steps (month|week)""" |
|
75 prev1 = previous_month(date, smallshift) |
|
76 next1 = next_month(date, smallshift) |
|
77 prev2 = previous_month(date, bigshift) |
|
78 next2 = next_month(date, bigshift) |
|
79 rql = self.cw_rset.printable_rql() |
|
80 return self.NAV_HEADER % ( |
|
81 xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=prev2.year, |
|
82 month=prev2.month)), |
|
83 xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=prev1.year, |
|
84 month=prev1.month)), |
|
85 xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=next1.year, |
|
86 month=next1.month)), |
|
87 xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=next2.year, |
|
88 month=next2.month))) |
|
89 |
|
90 |
|
91 # Calendar building methods ############################################## |
|
92 |
|
93 def build_calendars(self, schedule, begin, end): |
|
94 """build several HTML calendars at once, one for each month |
|
95 between begin and end |
|
96 """ |
|
97 return [self.build_calendar(schedule, date) |
|
98 for date in date_range(begin, end, incmonth=1)] |
|
99 |
|
100 def build_calendar(self, schedule, first_day): |
|
101 """method responsible for building *one* HTML calendar""" |
|
102 # FIXME iterates between [first_day-first_day.day_of_week ; |
|
103 # last_day+6-last_day.day_of_week] |
|
104 umonth = self._cw.format_date(first_day, '%B %Y') # localized month name |
|
105 rows = [] |
|
106 current_row = [NO_CELL] * first_day.weekday() |
|
107 for daynum in xrange(0, days_in_month(first_day)): |
|
108 # build cell day |
|
109 day = first_day + timedelta(daynum) |
|
110 events = schedule.get(day) |
|
111 if events: |
|
112 events = [u'\n'.join(event) for event in events.values()] |
|
113 current_row.append(CELL % (daynum+1, '\n'.join(events))) |
|
114 else: |
|
115 current_row.append(EMPTY_CELL % (daynum+1)) |
|
116 # store & reset current row on Sundays |
|
117 if day.weekday() == 6: |
|
118 rows.append(u'<tr>%s%s</tr>' % (WEEKNUM_CELL % day.isocalendar()[1], ''.join(current_row))) |
|
119 current_row = [] |
|
120 current_row.extend([NO_CELL] * (6-day.weekday())) |
|
121 rql = self.cw_rset.printable_rql() |
|
122 if day.weekday() != 6: |
|
123 rows.append(u'<tr>%s%s</tr>' % (WEEKNUM_CELL % day.isocalendar()[1], ''.join(current_row))) |
|
124 url = self._cw.build_url(rql=rql, vid='calendarmonth', |
|
125 year=first_day.year, month=first_day.month) |
|
126 monthlink = u'<a href="%s">%s</a>' % (xml_escape(url), umonth) |
|
127 return CALENDAR(self._cw) % (monthlink, '\n'.join(rows)) |
|
128 |
|
129 def _mk_schedule(self, begin, end, itemvid='calendaritem'): |
|
130 """private method that gathers information from resultset |
|
131 and builds calendars according to it |
|
132 |
|
133 :param begin: begin of date range |
|
134 :param end: end of date rangs |
|
135 :param itemvid: which view to call to render elements in cells |
|
136 |
|
137 returns { day1 : { hour : [views] }, |
|
138 day2 : { hour : [views] } ... } |
|
139 """ |
|
140 # put this here since all sub views are calling this method |
|
141 self._cw.add_css('cubicweb.calendar.css') |
|
142 schedule = {} |
|
143 for row in xrange(len(self.cw_rset.rows)): |
|
144 entity = self.cw_rset.get_entity(row, 0) |
|
145 infos = u'<div class="event">' |
|
146 infos += self._cw.view(itemvid, self.cw_rset, row=row) |
|
147 infos += u'</div>' |
|
148 for date_ in entity.cw_adapt_to('ICalendarViews').matching_dates(begin, end): |
|
149 day = date(date_.year, date_.month, date_.day) |
|
150 try: |
|
151 dt = time(date_.hour, date_.minute, date_.second) |
|
152 except AttributeError: |
|
153 # date instance |
|
154 dt = time(0, 0, 0) |
|
155 schedule.setdefault(day, {}) |
|
156 schedule[day].setdefault(dt, []).append(infos) |
|
157 return schedule |
|
158 |
|
159 |
|
160 @staticmethod |
|
161 def get_date_range(day, shift=4): |
|
162 """returns a couple (begin, end) |
|
163 |
|
164 <begin> is the first day of current_month - shift |
|
165 <end> is the last day of current_month + (shift+1) |
|
166 """ |
|
167 begin = first_day(previous_month(day, shift)) |
|
168 end = last_day(next_month(day, shift)) |
|
169 return begin, end |
|
170 |
|
171 def _build_ampm_cells(self, events): |
|
172 """create a view without any hourly details. |
|
173 |
|
174 :param events: dictionnary with all events classified by hours |
|
175 """ |
|
176 # split events according am/pm |
|
177 am_events = [event for e_time, e_list in events.iteritems() |
|
178 if 0 <= e_time.hour < 12 |
|
179 for event in e_list] |
|
180 pm_events = [event for e_time, e_list in events.iteritems() |
|
181 if 12 <= e_time.hour < 24 |
|
182 for event in e_list] |
|
183 # format each am/pm cell |
|
184 if am_events: |
|
185 am_content = AMPM_CONTENT % ("amCell", "am", '\n'.join(am_events)) |
|
186 else: |
|
187 am_content = AMPM_EMPTY % ("amCell", "am") |
|
188 if pm_events: |
|
189 pm_content = AMPM_CONTENT % ("pmCell", "pm", '\n'.join(pm_events)) |
|
190 else: |
|
191 pm_content = AMPM_EMPTY % ("pmCell", "pm") |
|
192 return am_content, pm_content |
|
193 |
|
194 |
|
195 |
|
196 class YearCalendarView(_CalendarView): |
|
197 __regid__ = 'calendaryear' |
|
198 title = _('calendar (year)') |
|
199 |
|
200 def call(self, year=None, month=None): |
|
201 """this view renders a 3x3 calendars' table""" |
|
202 year = year or int(self._cw.form.get('year', date.today().year)) |
|
203 month = month or int(self._cw.form.get('month', date.today().month)) |
|
204 center_date = date(year, month, 1) |
|
205 begin, end = self.get_date_range(day=center_date) |
|
206 schedule = self._mk_schedule(begin, end) |
|
207 self.w(self.nav_header(center_date)) |
|
208 calendars = tuple(self.build_calendars(schedule, begin, end)) |
|
209 self.w(SMALL_CALENDARS_PAGE % calendars) |
|
210 |
|
211 |
|
212 class SemesterCalendarView(_CalendarView): |
|
213 """this view renders three semesters as three rows of six columns, |
|
214 one column per month |
|
215 """ |
|
216 __regid__ = 'calendarsemester' |
|
217 title = _('calendar (semester)') |
|
218 |
|
219 def call(self, year=None, month=None): |
|
220 year = year or int(self._cw.form.get('year', date.today().year)) |
|
221 month = month or int(self._cw.form.get('month', date.today().month)) |
|
222 begin = previous_month(date(year, month, 1), 2) |
|
223 end = next_month(date(year, month, 1), 3) |
|
224 schedule = self._mk_schedule(begin, end) |
|
225 self.w(self.nav_header(date(year, month, 1), 1, 6)) |
|
226 self.w(u'<table class="semesterCalendar">') |
|
227 self.build_calendars(schedule, begin, end) |
|
228 self.w(u'</table>') |
|
229 self.w(self.nav_header(date(year, month, 1), 1, 6)) |
|
230 |
|
231 def build_calendars(self, schedule, begin, end): |
|
232 self.w(u'<tr>') |
|
233 rql = self.cw_rset.printable_rql() |
|
234 for cur_month in date_range(begin, end, incmonth=1): |
|
235 umonth = u'%s %s' % (self._cw.format_date(cur_month, '%B'), cur_month.year) |
|
236 url = self._cw.build_url(rql=rql, vid=self.__regid__, |
|
237 year=cur_month.year, month=cur_month.month) |
|
238 self.w(u'<th colspan="2"><a href="%s">%s</a></th>' % (xml_escape(url), |
|
239 umonth)) |
|
240 self.w(u'</tr>') |
|
241 _ = self._cw._ |
|
242 for day_num in xrange(31): |
|
243 self.w(u'<tr>') |
|
244 for cur_month in date_range(begin, end, incmonth=1): |
|
245 if day_num >= days_in_month(cur_month): |
|
246 self.w(u'%s%s' % (NO_CELL, NO_CELL)) |
|
247 else: |
|
248 day = date(cur_month.year, cur_month.month, day_num+1) |
|
249 events = schedule.get(day) |
|
250 self.w(u'<td>%s %s</td>\n' % (_(WEEKDAYS[day.weekday()])[0].upper(), day_num+1)) |
|
251 self.format_day_events(day, events) |
|
252 self.w(u'</tr>') |
|
253 |
|
254 def format_day_events(self, day, events): |
|
255 if events: |
|
256 events = ['\n'.join(event) for event in events.values()] |
|
257 self.w(WEEK_CELL % '\n'.join(events)) |
|
258 else: |
|
259 self.w(WEEK_EMPTY_CELL) |
|
260 |
|
261 |
|
262 class MonthCalendarView(_CalendarView): |
|
263 """this view renders a 3x1 calendars' table""" |
|
264 __regid__ = 'calendarmonth' |
|
265 title = _('calendar (month)') |
|
266 |
|
267 def call(self, year=None, month=None): |
|
268 year = year or int(self._cw.form.get('year', date.today().year)) |
|
269 month = month or int(self._cw.form.get('month', date.today().month)) |
|
270 center_date = date(year, month, 1) |
|
271 begin, end = self.get_date_range(day=center_date, shift=1) |
|
272 schedule = self._mk_schedule(begin, end) |
|
273 calendars = self.build_calendars(schedule, begin, end) |
|
274 self.w(self.nav_header(center_date, 1, 3)) |
|
275 self.w(BIG_CALENDARS_PAGE % tuple(calendars)) |
|
276 self.w(self.nav_header(center_date, 1, 3)) |
|
277 |
|
278 |
|
279 class WeekCalendarView(_CalendarView): |
|
280 """this view renders a calendar for week events""" |
|
281 __regid__ = 'calendarweek' |
|
282 title = _('calendar (week)') |
|
283 |
|
284 def call(self, year=None, week=None): |
|
285 year = year or int(self._cw.form.get('year', date.today().year)) |
|
286 week = week or int(self._cw.form.get('week', date.today().isocalendar()[1])) |
|
287 day0 = date(year, 1, 1) |
|
288 first_day_of_week = day0 - day0.weekday()*ONEDAY + ONEWEEK |
|
289 begin, end = first_day_of_week- ONEWEEK, first_day_of_week + 2*ONEWEEK |
|
290 schedule = self._mk_schedule(begin, end, itemvid='calendarlargeitem') |
|
291 self.w(self.nav_header(first_day_of_week)) |
|
292 self.w(u'<table class="weekCalendar">') |
|
293 _weeks = [(first_day_of_week-ONEWEEK, first_day_of_week-ONEDAY), |
|
294 (first_day_of_week, first_day_of_week+6*ONEDAY), |
|
295 (first_day_of_week+ONEWEEK, first_day_of_week+13*ONEDAY)] |
|
296 self.build_calendar(schedule, _weeks) |
|
297 self.w(u'</table>') |
|
298 self.w(self.nav_header(first_day_of_week)) |
|
299 |
|
300 def build_calendar(self, schedule, weeks): |
|
301 rql = self.cw_rset.printable_rql() |
|
302 _ = self._cw._ |
|
303 for monday, sunday in weeks: |
|
304 umonth = self._cw.format_date(monday, '%B %Y') |
|
305 url = self._cw.build_url(rql=rql, vid='calendarmonth', |
|
306 year=monday.year, month=monday.month) |
|
307 monthlink = '<a href="%s">%s</a>' % (xml_escape(url), umonth) |
|
308 self.w(u'<tr><th colspan="3">%s %s (%s)</th></tr>' \ |
|
309 % (_('week'), monday.isocalendar()[1], monthlink)) |
|
310 for day in date_range(monday, sunday+ONEDAY): |
|
311 self.w(u'<tr>') |
|
312 self.w(u'<td>%s</td>' % _(WEEKDAYS[day.weekday()])) |
|
313 self.w(u'<td>%s</td>' % (day.strftime('%Y-%m-%d'))) |
|
314 events = schedule.get(day) |
|
315 if events: |
|
316 events = ['\n'.join(event) for event in events.values()] |
|
317 self.w(WEEK_CELL % '\n'.join(events)) |
|
318 else: |
|
319 self.w(WEEK_EMPTY_CELL) |
|
320 self.w(u'</tr>') |
|
321 |
|
322 def nav_header(self, date, smallshift=1, bigshift=3): |
|
323 """prints shortcut links to go to previous/next steps (month|week)""" |
|
324 prev1 = date - ONEWEEK * smallshift |
|
325 prev2 = date - ONEWEEK * bigshift |
|
326 next1 = date + ONEWEEK * smallshift |
|
327 next2 = date + ONEWEEK * bigshift |
|
328 rql = self.cw_rset.printable_rql() |
|
329 return self.NAV_HEADER % ( |
|
330 xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=prev2.year, week=prev2.isocalendar()[1])), |
|
331 xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=prev1.year, week=prev1.isocalendar()[1])), |
|
332 xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=next1.year, week=next1.isocalendar()[1])), |
|
333 xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=next2.year, week=next2.isocalendar()[1]))) |
|
334 |
|
335 |
|
336 |
|
337 class AMPMYearCalendarView(YearCalendarView): |
|
338 __regid__ = 'ampmcalendaryear' |
|
339 title = _('am/pm calendar (year)') |
|
340 |
|
341 def build_calendar(self, schedule, first_day): |
|
342 """method responsible for building *one* HTML calendar""" |
|
343 umonth = self._cw.format_date(first_day, '%B %Y') # localized month name |
|
344 rows = [] # each row is: (am,pm), (am,pm) ... week_title |
|
345 current_row = [(NO_CELL, NO_CELL, NO_CELL)] * first_day.weekday() |
|
346 rql = self.cw_rset.printable_rql() |
|
347 for daynum in xrange(0, days_in_month(first_day)): |
|
348 # build cells day |
|
349 day = first_day + timedelta(daynum) |
|
350 events = schedule.get(day) |
|
351 if events: |
|
352 current_row.append((AMPM_DAY % (daynum+1),) + self._build_ampm_cells(events)) |
|
353 else: |
|
354 current_row.append((AMPM_DAY % (daynum+1), |
|
355 AMPM_EMPTY % ("amCell", "am"), |
|
356 AMPM_EMPTY % ("pmCell", "pm"))) |
|
357 # store & reset current row on Sundays |
|
358 if day.weekday() == 6: |
|
359 url = self._cw.build_url(rql=rql, vid='ampmcalendarweek', |
|
360 year=day.year, week=day.isocalendar()[1]) |
|
361 weeklink = '<a href="%s">%s</a>' % (xml_escape(url), |
|
362 day.isocalendar()[1]) |
|
363 current_row.append(WEEKNUM_CELL % weeklink) |
|
364 rows.append(current_row) |
|
365 current_row = [] |
|
366 current_row.extend([(NO_CELL, NO_CELL, NO_CELL)] * (6-day.weekday())) |
|
367 url = self._cw.build_url(rql=rql, vid='ampmcalendarweek', |
|
368 year=day.year, week=day.isocalendar()[1]) |
|
369 weeklink = '<a href="%s">%s</a>' % (xml_escape(url), day.isocalendar()[1]) |
|
370 current_row.append(WEEKNUM_CELL % weeklink) |
|
371 rows.append(current_row) |
|
372 # build two rows for each week: am & pm |
|
373 formatted_rows = [] |
|
374 for row in rows: |
|
375 week_title = row.pop() |
|
376 day_row = [day for day, am, pm in row] |
|
377 am_row = [am for day, am, pm in row] |
|
378 pm_row = [pm for day, am, pm in row] |
|
379 formatted_rows.append('<tr>%s%s</tr>'% (week_title, '\n'.join(day_row))) |
|
380 formatted_rows.append('<tr class="amRow"><td> </td>%s</tr>'% '\n'.join(am_row)) |
|
381 formatted_rows.append('<tr class="pmRow"><td> </td>%s</tr>'% '\n'.join(pm_row)) |
|
382 # tigh everything together |
|
383 url = self._cw.build_url(rql=rql, vid='ampmcalendarmonth', |
|
384 year=first_day.year, month=first_day.month) |
|
385 monthlink = '<a href="%s">%s</a>' % (xml_escape(url), umonth) |
|
386 return CALENDAR(self._cw) % (monthlink, '\n'.join(formatted_rows)) |
|
387 |
|
388 |
|
389 |
|
390 class AMPMSemesterCalendarView(SemesterCalendarView): |
|
391 """this view renders a 3x1 calendars' table""" |
|
392 __regid__ = 'ampmcalendarsemester' |
|
393 title = _('am/pm calendar (semester)') |
|
394 |
|
395 def build_calendars(self, schedule, begin, end): |
|
396 self.w(u'<tr>') |
|
397 rql = self.cw_rset.printable_rql() |
|
398 for cur_month in date_range(begin, end, incmonth=1): |
|
399 umonth = u'%s %s' % (self._cw.format_date(cur_month, '%B'), cur_month.year) |
|
400 url = self._cw.build_url(rql=rql, vid=self.__regid__, |
|
401 year=cur_month.year, month=cur_month.month) |
|
402 self.w(u'<th colspan="3"><a href="%s">%s</a></th>' % (xml_escape(url), |
|
403 umonth)) |
|
404 self.w(u'</tr>') |
|
405 _ = self._cw._ |
|
406 for day_num in xrange(31): |
|
407 self.w(u'<tr>') |
|
408 for cur_month in date_range(begin, end, incmonth=1): |
|
409 if day_num >= days_in_month(cur_month): |
|
410 self.w(u'%s%s%s' % (NO_CELL, NO_CELL, NO_CELL)) |
|
411 else: |
|
412 day = date(cur_month.year, cur_month.month, day_num+1) |
|
413 events = schedule.get(day) |
|
414 self.w(u'<td>%s %s</td>\n' % (_(WEEKDAYS[day.weekday()])[0].upper(), |
|
415 day_num+1)) |
|
416 self.format_day_events(day, events) |
|
417 self.w(u'</tr>') |
|
418 |
|
419 def format_day_events(self, day, events): |
|
420 if events: |
|
421 self.w(u'\n'.join(self._build_ampm_cells(events))) |
|
422 else: |
|
423 self.w(u'%s %s'% (AMPM_EMPTY % ("amCell", "am"), |
|
424 AMPM_EMPTY % ("pmCell", "pm"))) |
|
425 |
|
426 |
|
427 class AMPMMonthCalendarView(MonthCalendarView): |
|
428 """this view renders a 3x1 calendars' table""" |
|
429 __regid__ = 'ampmcalendarmonth' |
|
430 title = _('am/pm calendar (month)') |
|
431 |
|
432 def build_calendar(self, schedule, first_day): |
|
433 """method responsible for building *one* HTML calendar""" |
|
434 umonth = self._cw.format_date(first_day, '%B %Y') # localized month name |
|
435 rows = [] # each row is: (am,pm), (am,pm) ... week_title |
|
436 current_row = [(NO_CELL, NO_CELL, NO_CELL)] * first_day.weekday() |
|
437 rql = self.cw_rset.printable_rql() |
|
438 for daynum in xrange(0, days_in_month(first_day)): |
|
439 # build cells day |
|
440 day = first_day + timedelta(daynum) |
|
441 events = schedule.get(day) |
|
442 if events: |
|
443 current_row.append((AMPM_DAY % (daynum+1),) + self._build_ampm_cells(events)) |
|
444 else: |
|
445 current_row.append((AMPM_DAY % (daynum+1), |
|
446 AMPM_EMPTY % ("amCell", "am"), |
|
447 AMPM_EMPTY % ("pmCell", "pm"))) |
|
448 # store & reset current row on Sundays |
|
449 if day.weekday() == 6: |
|
450 url = self._cw.build_url(rql=rql, vid='ampmcalendarweek', |
|
451 year=day.year, week=day.isocalendar()[1]) |
|
452 weeklink = '<a href="%s">%s</a>' % (xml_escape(url), |
|
453 day.isocalendar()[1]) |
|
454 current_row.append(WEEKNUM_CELL % weeklink) |
|
455 rows.append(current_row) |
|
456 current_row = [] |
|
457 current_row.extend([(NO_CELL, NO_CELL, NO_CELL)] * (6-day.weekday())) |
|
458 url = self._cw.build_url(rql=rql, vid='ampmcalendarweek', |
|
459 year=day.year, week=day.isocalendar()[1]) |
|
460 weeklink = '<a href="%s">%s</a>' % (xml_escape(url), |
|
461 day.isocalendar()[1]) |
|
462 current_row.append(WEEKNUM_CELL % weeklink) |
|
463 rows.append(current_row) |
|
464 # build two rows for each week: am & pm |
|
465 formatted_rows = [] |
|
466 for row in rows: |
|
467 week_title = row.pop() |
|
468 day_row = [day for day, am, pm in row] |
|
469 am_row = [am for day, am, pm in row] |
|
470 pm_row = [pm for day, am, pm in row] |
|
471 formatted_rows.append('<tr>%s%s</tr>'% (week_title, '\n'.join(day_row))) |
|
472 formatted_rows.append('<tr class="amRow"><td> </td>%s</tr>'% '\n'.join(am_row)) |
|
473 formatted_rows.append('<tr class="pmRow"><td> </td>%s</tr>'% '\n'.join(pm_row)) |
|
474 # tigh everything together |
|
475 url = self._cw.build_url(rql=rql, vid='ampmcalendarmonth', |
|
476 year=first_day.year, month=first_day.month) |
|
477 monthlink = '<a href="%s">%s</a>' % (xml_escape(url), |
|
478 umonth) |
|
479 return CALENDAR(self._cw) % (monthlink, '\n'.join(formatted_rows)) |
|
480 |
|
481 |
|
482 |
|
483 class AMPMWeekCalendarView(WeekCalendarView): |
|
484 """this view renders a 3x1 calendars' table""" |
|
485 __regid__ = 'ampmcalendarweek' |
|
486 title = _('am/pm calendar (week)') |
|
487 |
|
488 def build_calendar(self, schedule, weeks): |
|
489 rql = self.cw_rset.printable_rql() |
|
490 w = self.w |
|
491 _ = self._cw._ |
|
492 for monday, sunday in weeks: |
|
493 umonth = self._cw.format_date(monday, '%B %Y') |
|
494 url = self._cw.build_url(rql=rql, vid='ampmcalendarmonth', |
|
495 year=monday.year, month=monday.month) |
|
496 monthlink = '<a href="%s">%s</a>' % (xml_escape(url), umonth) |
|
497 w(u'<tr>%s</tr>' % ( |
|
498 WEEK_TITLE % (_('week'), monday.isocalendar()[1], monthlink))) |
|
499 w(u'<tr><th>%s</th><th> </th></tr>'% _(u'Date')) |
|
500 for day in date_range(monday, sunday+ONEDAY): |
|
501 events = schedule.get(day) |
|
502 style = day.weekday() % 2 and "even" or "odd" |
|
503 w(u'<tr class="%s">' % style) |
|
504 if events: |
|
505 hours = events.keys() |
|
506 hours.sort() |
|
507 w(AMPM_DAYWEEK % ( |
|
508 len(hours), _(WEEKDAYS[day.weekday()]), |
|
509 self._cw.format_date(day))) |
|
510 w(AMPM_WEEK_CELL % ( |
|
511 hours[0].hour, hours[0].minute, |
|
512 '\n'.join(events[hours[0]]))) |
|
513 w(u'</tr>') |
|
514 for hour in hours[1:]: |
|
515 w(u'<tr class="%s">%s</tr>'% ( |
|
516 style, AMPM_WEEK_CELL % (hour.hour, hour.minute, |
|
517 '\n'.join(events[hour])))) |
|
518 else: |
|
519 w(AMPM_DAYWEEK_EMPTY % ( |
|
520 _(WEEKDAYS[day.weekday()]), |
|
521 self._cw.format_date(day))) |
|
522 w(WEEK_EMPTY_CELL) |
|
523 w(u'</tr>') |
|
524 |
|
525 |
|
526 SMALL_CALENDARS_PAGE = u"""<table class="smallCalendars"> |
|
527 <tr><td class="calendar">%s</td><td class="calendar">%s</td><td class="calendar">%s</td></tr> |
|
528 <tr><td class="calendar">%s</td><td class="calendar">%s</td><td class="calendar">%s</td></tr> |
|
529 <tr><td class="calendar">%s</td><td class="calendar">%s</td><td class="calendar">%s</td></tr> |
|
530 </table> |
|
531 """ |
|
532 |
|
533 BIG_CALENDARS_PAGE = u"""<table class="bigCalendars"> |
|
534 <tr><td class="calendar">%s</td></tr> |
|
535 <tr><td class="calendar">%s</td></tr> |
|
536 <tr><td class="calendar">%s</td></tr> |
|
537 </table> |
|
538 """ |
|
539 |
|
540 WEEKNUM_CELL = u'<td class="weeknum">%s</td>' |
|
541 |
|
542 def CALENDAR(req): |
|
543 _ = req._ |
|
544 WEEKNUM_HEADER = u'<th class="weeknum">%s</th>' % _('week') |
|
545 CAL_HEADER = WEEKNUM_HEADER + u' \n'.join([u'<th class="weekday">%s</th>' % _(day)[0].upper() |
|
546 for day in WEEKDAYS]) |
|
547 return u"""<table> |
|
548 <tr><th class="month" colspan="8">%%s</th></tr> |
|
549 <tr> |
|
550 %s |
|
551 </tr> |
|
552 %%s |
|
553 </table> |
|
554 """ % (CAL_HEADER,) |
|
555 |
|
556 |
|
557 DAY_TEMPLATE = """<tr><td class="weekday">%(daylabel)s</td><td>%(dmydate)s</td><td>%(dayschedule)s</td> |
|
558 """ |
|
559 |
|
560 NO_CELL = u'<td class="noday"></td>' |
|
561 EMPTY_CELL = u'<td class="cellEmpty"><span class="cellTitle">%s</span></td>' |
|
562 CELL = u'<td class="cell"><span class="cellTitle">%s</span><div class="cellContent">%s</div></td>' |
|
563 |
|
564 AMPM_DAY = u'<td class="cellDay">%d</td>' |
|
565 AMPM_EMPTY = u'<td class="%sEmpty"><span class="cellTitle">%s</span></td>' |
|
566 AMPM_CONTENT = u'<td class="%s"><span class="cellTitle">%s</span><div class="cellContent">%s</div></td>' |
|
567 |
|
568 WEEK_TITLE = u'<th class="weekTitle" colspan="2">%s %s (%s)</th>' |
|
569 WEEK_EMPTY_CELL = u'<td class="weekEmptyCell"> </td>' |
|
570 WEEK_CELL = u'<td class="weekCell"><div class="cellContent">%s</div></td>' |
|
571 |
|
572 AMPM_DAYWEEK_EMPTY = u'<td>%s %s</td>' |
|
573 AMPM_DAYWEEK = u'<td rowspan="%d">%s %s</td>' |
|
574 AMPM_WEEK_CELL = u'<td class="ampmWeekCell"><div class="cellContent">%02d:%02d - %s</div></td>' |
|