author | sylvain.thenault@logilab.fr |
Mon, 04 May 2009 11:47:33 +0200 | |
branch | tls-sprint |
changeset 1635 | 866563e2d0fc |
parent 1604 | fd943737d630 |
child 1644 | 45bec81a9e1c |
permissions | -rw-r--r-- |
0 | 1 |
"""html calendar views |
2 |
||
3 |
:organization: Logilab |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
524
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
8 |
from datetime import datetime, date, timedelta |
0 | 9 |
|
1132 | 10 |
from vobject import iCalendar |
0 | 11 |
|
12 |
from logilab.mtconverter import html_escape |
|
13 |
||
14 |
from cubicweb.interfaces import ICalendarable |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
524
diff
changeset
|
15 |
from cubicweb.selectors import implements |
1025 | 16 |
from cubicweb.utils import strptime, date_range, todate |
767 | 17 |
from cubicweb.view import EntityView |
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1604
diff
changeset
|
18 |
from cubicweb.web import ajax_replace_url |
0 | 19 |
|
20 |
_ = unicode |
|
21 |
||
1025 | 22 |
# useful constants & functions ################################################ |
0 | 23 |
|
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
24 |
ONEDAY = timedelta(1) |
1025 | 25 |
|
0 | 26 |
WEEKDAYS = (_("monday"), _("tuesday"), _("wednesday"), _("thursday"), |
27 |
_("friday"), _("saturday"), _("sunday")) |
|
28 |
MONTHNAMES = ( _('january'), _('february'), _('march'), _('april'), _('may'), |
|
29 |
_('june'), _('july'), _('august'), _('september'), _('october'), |
|
30 |
_('november'), _('december') |
|
31 |
) |
|
1604 | 32 |
|
1025 | 33 |
# Calendar views ############################################################## |
0 | 34 |
|
35 |
||
36 |
class iCalView(EntityView): |
|
37 |
"""A calendar view that generates a iCalendar file (RFC 2445) |
|
38 |
||
39 |
Does apply to ICalendarable compatible entities |
|
40 |
""" |
|
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
41 |
__select__ = implements(ICalendarable) |
0 | 42 |
need_navigation = False |
43 |
content_type = 'text/calendar' |
|
44 |
title = _('iCalendar') |
|
45 |
templatable = False |
|
46 |
id = 'ical' |
|
47 |
||
48 |
def call(self): |
|
49 |
ical = iCalendar() |
|
50 |
for i in range(len(self.rset.rows)): |
|
51 |
task = self.complete_entity(i) |
|
52 |
event = ical.add('vevent') |
|
53 |
event.add('summary').value = task.dc_title() |
|
54 |
event.add('description').value = task.dc_description() |
|
55 |
if task.start: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
56 |
event.add('dtstart').value = task.start |
0 | 57 |
if task.stop: |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
58 |
event.add('dtend').value = task.stop |
0 | 59 |
|
60 |
buff = ical.serialize() |
|
61 |
if not isinstance(buff, unicode): |
|
62 |
buff = unicode(buff, self.req.encoding) |
|
63 |
self.w(buff) |
|
64 |
||
65 |
class hCalView(EntityView): |
|
66 |
"""A calendar view that generates a hCalendar file |
|
67 |
||
68 |
Does apply to ICalendarable compatible entities |
|
69 |
""" |
|
767 | 70 |
id = 'hcal' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
71 |
__select__ = implements(ICalendarable) |
0 | 72 |
need_navigation = False |
73 |
title = _('hCalendar') |
|
524
eee3983e29e9
hcal is a microformat and can be inserted in html
sylvain.thenault@logilab.fr
parents:
431
diff
changeset
|
74 |
#templatable = False |
0 | 75 |
|
76 |
def call(self): |
|
77 |
self.w(u'<div class="hcalendar">') |
|
78 |
for i in range(len(self.rset.rows)): |
|
79 |
task = self.complete_entity(i) |
|
80 |
self.w(u'<div class="vevent">') |
|
81 |
self.w(u'<h3 class="summary">%s</h3>' % html_escape(task.dc_title())) |
|
82 |
self.w(u'<div class="description">%s</div>' % html_escape(task.dc_description())) |
|
83 |
if task.start: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
84 |
self.w(u'<abbr class="dtstart" title="%s">%s</abbr>' % (task.start.isoformat(), self.format_date(task.start))) |
0 | 85 |
if task.stop: |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
86 |
self.w(u'<abbr class="dtstop" title="%s">%s</abbr>' % (task.stop.isoformat(), self.format_date(task.stop))) |
0 | 87 |
self.w(u'</div>') |
88 |
self.w(u'</div>') |
|
89 |
||
1025 | 90 |
|
91 |
class CalendarItemView(EntityView): |
|
92 |
id = 'calendaritem' |
|
93 |
||
94 |
def cell_call(self, row, col, dates=False): |
|
95 |
task = self.complete_entity(row) |
|
96 |
task.view('oneline', w=self.w) |
|
97 |
if dates: |
|
98 |
if task.start and task.stop: |
|
99 |
self.w('<br/>' % self.req._('from %(date)s' % {'date': self.format_date(task.start)})) |
|
100 |
self.w('<br/>' % self.req._('to %(date)s' % {'date': self.format_date(task.stop)})) |
|
101 |
self.w('<br/>to %s'%self.format_date(task.stop)) |
|
1604 | 102 |
|
1025 | 103 |
class CalendarLargeItemView(CalendarItemView): |
104 |
id = 'calendarlargeitem' |
|
105 |
||
1604 | 106 |
|
0 | 107 |
class _TaskEntry(object): |
108 |
def __init__(self, task, color, index=0): |
|
109 |
self.task = task |
|
110 |
self.color = color |
|
111 |
self.index = index |
|
112 |
self.length = 1 |
|
113 |
||
1025 | 114 |
def in_working_hours(self): |
115 |
"""predicate returning True is the task is in working hours""" |
|
116 |
if self.task.start.hour > 7 and self.task.stop.hour < 20: |
|
117 |
return True |
|
118 |
return False |
|
1604 | 119 |
|
1025 | 120 |
def is_one_day_task(self): |
121 |
task = self.task |
|
122 |
return task.start and task.stop and task.start.isocalendar() == task.stop.isocalendar() |
|
1604 | 123 |
|
124 |
||
0 | 125 |
class OneMonthCal(EntityView): |
126 |
"""At some point, this view will probably replace ampm calendars""" |
|
767 | 127 |
id = 'onemonthcal' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
128 |
__select__ = implements(ICalendarable) |
0 | 129 |
need_navigation = False |
130 |
title = _('one month') |
|
131 |
||
132 |
def call(self): |
|
133 |
self.req.add_js('cubicweb.ajax.js') |
|
134 |
self.req.add_css('cubicweb.calendar.css') |
|
135 |
# XXX: restrict courses directy with RQL |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
136 |
_today = datetime.today() |
0 | 137 |
|
138 |
if 'year' in self.req.form: |
|
139 |
year = int(self.req.form['year']) |
|
140 |
else: |
|
141 |
year = _today.year |
|
142 |
if 'month' in self.req.form: |
|
143 |
month = int(self.req.form['month']) |
|
144 |
else: |
|
145 |
month = _today.month |
|
146 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
147 |
first_day_of_month = date(year, month, 1) |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
148 |
firstday = first_day_of_month - timedelta(first_day_of_month.weekday()) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
149 |
if month >= 12: |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
150 |
last_day_of_month = date(year + 1, 1, 1) - timedelta(1) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
151 |
else: |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
152 |
last_day_of_month = date(year, month + 1, 1) - timedelta(1) |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
153 |
lastday = last_day_of_month + timedelta(6 - last_day_of_month.weekday()) |
0 | 154 |
month_dates = list(date_range(firstday, lastday)) |
155 |
dates = {} |
|
156 |
task_max = 0 |
|
157 |
for row in xrange(self.rset.rowcount): |
|
1132 | 158 |
task = self.rset.get_entity(row, 0) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
159 |
if len(self.rset[row]) > 1 and self.rset.description[row][1] == 'CWUser': |
1132 | 160 |
user = self.rset.get_entity(row, 1) |
0 | 161 |
else: |
162 |
user = None |
|
163 |
the_dates = [] |
|
1025 | 164 |
tstart = todate(task.start) |
165 |
if tstart: |
|
166 |
if tstart > lastday: |
|
0 | 167 |
continue |
1025 | 168 |
the_dates = [tstart] |
169 |
tstop = todate(task.start) |
|
170 |
if tstop: |
|
171 |
if tstop < firstday: |
|
0 | 172 |
continue |
1025 | 173 |
the_dates = [tstop] |
174 |
if tstart and tstop: |
|
175 |
if tstart.isocalendar() == tstop.isocalendar(): |
|
176 |
if firstday <= tstart <= lastday: |
|
177 |
the_dates = [tstart] |
|
0 | 178 |
else: |
1025 | 179 |
the_dates = date_range(max(tstart, firstday), |
180 |
min(tstop, lastday)) |
|
0 | 181 |
if not the_dates: |
182 |
continue |
|
1604 | 183 |
|
0 | 184 |
for d in the_dates: |
185 |
d_tasks = dates.setdefault((d.year, d.month, d.day), {}) |
|
1132 | 186 |
t_users = d_tasks.setdefault(task, set()) |
0 | 187 |
t_users.add( user ) |
1132 | 188 |
if len(d_tasks) > task_max: |
0 | 189 |
task_max = len(d_tasks) |
190 |
||
191 |
days = [] |
|
1132 | 192 |
nrows = max(3, task_max) |
0 | 193 |
# colors here are class names defined in cubicweb.css |
1132 | 194 |
colors = [ "col%x" % i for i in range(12) ] |
0 | 195 |
next_color_index = 0 |
196 |
||
197 |
visited_tasks = {} # holds a description of a task |
|
198 |
task_colors = {} # remember a color assigned to a task |
|
1025 | 199 |
for mdate in month_dates: |
200 |
d_tasks = dates.get((mdate.year, mdate.month, mdate.day), {}) |
|
0 | 201 |
rows = [None] * nrows |
202 |
# every task that is "visited" for the first time |
|
203 |
# require a special treatment, so we put them in |
|
204 |
# 'postpone' |
|
205 |
postpone = [] |
|
206 |
for task in d_tasks: |
|
207 |
if task in visited_tasks: |
|
208 |
task_descr = visited_tasks[ task ] |
|
209 |
rows[task_descr.index] = task_descr |
|
210 |
else: |
|
211 |
postpone.append(task) |
|
212 |
for task in postpone: |
|
213 |
# to every 'new' task we must affect a color |
|
214 |
# (which must be the same for every user concerned |
|
215 |
# by the task) |
|
1132 | 216 |
for i, t in enumerate(rows): |
0 | 217 |
if t is None: |
218 |
if task in task_colors: |
|
219 |
color = task_colors[task] |
|
220 |
else: |
|
221 |
color = colors[next_color_index] |
|
222 |
next_color_index = (next_color_index+1)%len(colors) |
|
223 |
task_colors[task] = color |
|
224 |
task_descr = _TaskEntry(task, color, i) |
|
225 |
rows[i] = task_descr |
|
226 |
visited_tasks[task] = task_descr |
|
227 |
break |
|
228 |
else: |
|
229 |
raise RuntimeError("is it possible we got it wrong?") |
|
230 |
||
231 |
days.append( rows ) |
|
232 |
||
233 |
curdate = first_day_of_month |
|
234 |
self.w(u'<div id="onemonthcalid">') |
|
235 |
# build schedule |
|
236 |
self.w(u'<table class="omcalendar">') |
|
237 |
prevlink, nextlink = self._prevnext_links(curdate) # XXX |
|
238 |
self.w(u'<tr><th><a href="%s"><<</a></th><th colspan="5">%s %s</th>' |
|
239 |
u'<th><a href="%s">>></a></th></tr>' % |
|
240 |
(html_escape(prevlink), self.req._(curdate.strftime('%B').lower()), |
|
241 |
curdate.year, html_escape(nextlink))) |
|
242 |
||
243 |
# output header |
|
244 |
self.w(u'<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>' % |
|
245 |
tuple(self.req._(day) for day in WEEKDAYS)) |
|
1604 | 246 |
|
0 | 247 |
# build calendar |
1025 | 248 |
for mdate, task_rows in zip(month_dates, days): |
249 |
if mdate.weekday() == 0: |
|
0 | 250 |
self.w(u'<tr>') |
1025 | 251 |
self._build_calendar_cell(mdate, task_rows, curdate) |
252 |
if mdate.weekday() == 6: |
|
0 | 253 |
self.w(u'</tr>') |
254 |
self.w(u'</table></div>') |
|
255 |
||
256 |
def _prevnext_links(self, curdate): |
|
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
257 |
prevdate = curdate - timedelta(31) |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
258 |
nextdate = curdate + timedelta(31) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
259 |
rql = self.rset.printable_rql() |
0 | 260 |
prevlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal', |
261 |
year=prevdate.year, month=prevdate.month) |
|
262 |
nextlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal', |
|
263 |
year=nextdate.year, month=nextdate.month) |
|
264 |
return prevlink, nextlink |
|
265 |
||
1149 | 266 |
def _build_calendar_cell(self, celldate, rows, curdate): |
0 | 267 |
curmonth = curdate.month |
268 |
classes = "" |
|
1149 | 269 |
if celldate.month != curmonth: |
0 | 270 |
classes += " outOfRange" |
1149 | 271 |
if celldate == date.today(): |
0 | 272 |
classes += " today" |
273 |
self.w(u'<td class="cell%s">' % classes) |
|
274 |
self.w(u'<div class="calCellTitle%s">' % classes) |
|
1149 | 275 |
self.w(u'<div class="day">%s</div>' % celldate.day) |
1604 | 276 |
|
0 | 277 |
if len(self.rset.column_types(0)) == 1: |
278 |
etype = list(self.rset.column_types(0))[0] |
|
279 |
url = self.build_url(vid='creation', etype=etype, |
|
280 |
schedule=True, |
|
1149 | 281 |
start=self.format_date(celldate), stop=self.format_date(celldate), |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
282 |
__redirectrql=self.rset.printable_rql(), |
0 | 283 |
__redirectparams=self.req.build_url_params(year=curdate.year, month=curmonth), |
284 |
__redirectvid=self.id |
|
285 |
) |
|
286 |
self.w(u'<div class="cmd"><a href="%s">%s</a></div>' % (html_escape(url), self.req._(u'add'))) |
|
287 |
self.w(u' ') |
|
288 |
self.w(u'</div>') |
|
289 |
self.w(u'<div class="cellContent">') |
|
290 |
for task_descr in rows: |
|
291 |
if task_descr: |
|
292 |
task = task_descr.task |
|
293 |
self.w(u'<div class="task %s">' % task_descr.color) |
|
294 |
task.view('calendaritem', w=self.w ) |
|
295 |
url = task.absolute_url(vid='edition', |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
296 |
__redirectrql=self.rset.printable_rql(), |
0 | 297 |
__redirectparams=self.req.build_url_params(year=curdate.year, month=curmonth), |
298 |
__redirectvid=self.id |
|
299 |
) |
|
300 |
||
301 |
self.w(u'<div class="tooltip" ondblclick="stopPropagation(event); window.location.assign(\'%s\'); return false;">' % html_escape(url)) |
|
302 |
task.view('tooltip', w=self.w ) |
|
303 |
self.w(u'</div>') |
|
304 |
else: |
|
305 |
self.w(u'<div class="task">') |
|
306 |
self.w(u" ") |
|
307 |
self.w(u'</div>') |
|
308 |
self.w(u'</div>') |
|
309 |
self.w(u'</td>') |
|
310 |
||
311 |
||
312 |
class OneWeekCal(EntityView): |
|
313 |
"""At some point, this view will probably replace ampm calendars""" |
|
767 | 314 |
id = 'oneweekcal' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
315 |
__select__ = implements(ICalendarable) |
0 | 316 |
need_navigation = False |
317 |
title = _('one week') |
|
1604 | 318 |
|
0 | 319 |
def call(self): |
320 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.calendar.js') ) |
|
321 |
self.req.add_css('cubicweb.calendar.css') |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
322 |
# XXX: restrict directly with RQL |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
323 |
_today = datetime.today() |
0 | 324 |
if 'year' in self.req.form: |
325 |
year = int(self.req.form['year']) |
|
326 |
else: |
|
327 |
year = _today.year |
|
328 |
if 'week' in self.req.form: |
|
329 |
week = int(self.req.form['week']) |
|
330 |
else: |
|
1604 | 331 |
week = _today.isocalendar()[1] |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
332 |
# week - 1 since we get week number > 0 while we want it to start from 0 |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
333 |
first_day_of_week = todate(strptime('%s-%s-1' % (year, week - 1), '%Y-%U-%w')) |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
334 |
lastday = first_day_of_week + timedelta(6) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
335 |
firstday = first_day_of_week |
0 | 336 |
dates = [[] for i in range(7)] |
337 |
task_colors = {} # remember a color assigned to a task |
|
338 |
# colors here are class names defined in cubicweb.css |
|
1132 | 339 |
colors = [ "col%x" % i for i in range(12) ] |
0 | 340 |
next_color_index = 0 |
341 |
done_tasks = [] |
|
342 |
for row in xrange(self.rset.rowcount): |
|
1132 | 343 |
task = self.rset.get_entity(row, 0) |
0 | 344 |
if task in done_tasks: |
345 |
continue |
|
346 |
done_tasks.append(task) |
|
347 |
the_dates = [] |
|
1025 | 348 |
tstart = todate(task.start) |
349 |
tstop = todate(task.stop) |
|
350 |
if tstart: |
|
351 |
if tstart > lastday: |
|
0 | 352 |
continue |
1025 | 353 |
the_dates = [tstart] |
354 |
if tstop: |
|
355 |
if tstop < firstday: |
|
0 | 356 |
continue |
1025 | 357 |
the_dates = [tstop] |
358 |
if tstart and tstop: |
|
359 |
the_dates = date_range(max(tstart, firstday), |
|
360 |
min(tstop, lastday)) |
|
0 | 361 |
if not the_dates: |
362 |
continue |
|
1604 | 363 |
|
0 | 364 |
if task not in task_colors: |
365 |
task_colors[task] = colors[next_color_index] |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
366 |
next_color_index = (next_color_index+1) % len(colors) |
1604 | 367 |
|
0 | 368 |
for d in the_dates: |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
369 |
day = d.weekday() |
1604 | 370 |
task_descr = _TaskEntry(task, task_colors[task]) |
0 | 371 |
dates[day].append(task_descr) |
1604 | 372 |
|
0 | 373 |
self.w(u'<div id="oneweekcalid">') |
374 |
# build schedule |
|
375 |
self.w(u'<table class="omcalendar" id="week">') |
|
376 |
prevlink, nextlink = self._prevnext_links(first_day_of_week) # XXX |
|
377 |
self.w(u'<tr><th class="transparent"></th>') |
|
378 |
self.w(u'<th><a href="%s"><<</a></th><th colspan="5">%s %s %s</th>' |
|
379 |
u'<th><a href="%s">>></a></th></tr>' % |
|
380 |
(html_escape(prevlink), first_day_of_week.year, |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
381 |
self.req._(u'week'), first_day_of_week.isocalendar()[1], |
0 | 382 |
html_escape(nextlink))) |
383 |
||
384 |
# output header |
|
385 |
self.w(u'<tr>') |
|
386 |
self.w(u'<th class="transparent"></th>') # column for hours |
|
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
387 |
_today = date.today() |
0 | 388 |
for i, day in enumerate(WEEKDAYS): |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
389 |
wdate = first_day_of_week + timedelta(i) |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
390 |
if wdate.isocalendar() == _today.isocalendar(): |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
391 |
self.w(u'<th class="today">%s<br/>%s</th>' % (self.req._(day), self.format_date(wdate))) |
0 | 392 |
else: |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
393 |
self.w(u'<th>%s<br/>%s</th>' % (self.req._(day), self.format_date(wdate))) |
0 | 394 |
self.w(u'</tr>') |
1604 | 395 |
|
0 | 396 |
# build week calendar |
397 |
self.w(u'<tr>') |
|
398 |
self.w(u'<td style="width:5em;">') # column for hours |
|
399 |
extra = "" |
|
400 |
for h in range(8, 20): |
|
401 |
self.w(u'<div class="hour" %s>'%extra) |
|
402 |
self.w(u'%02d:00'%h) |
|
1604 | 403 |
self.w(u'</div>') |
0 | 404 |
self.w(u'</td>') |
1604 | 405 |
|
0 | 406 |
for i, day in enumerate(WEEKDAYS): |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
407 |
wdate = first_day_of_week + timedelta(i) |
0 | 408 |
classes = "" |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
409 |
if wdate.isocalendar() == _today.isocalendar(): |
0 | 410 |
classes = " today" |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
411 |
self.w(u'<td class="column %s" id="%s">' % (classes, day)) |
0 | 412 |
if len(self.rset.column_types(0)) == 1: |
413 |
etype = list(self.rset.column_types(0))[0] |
|
414 |
url = self.build_url(vid='creation', etype=etype, |
|
415 |
schedule=True, |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
416 |
__redirectrql=self.rset.printable_rql(), |
0 | 417 |
__redirectparams=self.req.build_url_params(year=year, week=week), |
418 |
__redirectvid=self.id |
|
419 |
) |
|
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
420 |
extra = ' ondblclick="addCalendarItem(event, hmin=8, hmax=20, year=%s, month=%s, day=%s, duration=2, baseurl=\'%s\')"' % ( |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
421 |
wdate.year, wdate.month, wdate.day, html_escape(url)) |
0 | 422 |
else: |
423 |
extra = "" |
|
424 |
self.w(u'<div class="columndiv"%s>'% extra) |
|
425 |
for h in range(8, 20): |
|
426 |
self.w(u'<div class="hourline" style="top:%sex;">'%((h-7)*8)) |
|
1604 | 427 |
self.w(u'</div>') |
0 | 428 |
if dates[i]: |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
429 |
self._build_calendar_cell(wdate, dates[i]) |
0 | 430 |
self.w(u'</div>') |
431 |
self.w(u'</td>') |
|
432 |
self.w(u'</tr>') |
|
433 |
self.w(u'</table></div>') |
|
434 |
self.w(u'<div id="coord"></div>') |
|
435 |
self.w(u'<div id="debug"> </div>') |
|
1604 | 436 |
|
0 | 437 |
def _build_calendar_cell(self, date, task_descrs): |
1025 | 438 |
inday_tasks = [t for t in task_descrs if t.is_one_day_task() and t.in_working_hours()] |
439 |
wholeday_tasks = [t for t in task_descrs if not t.is_one_day_task()] |
|
0 | 440 |
inday_tasks.sort(key=lambda t:t.task.start) |
441 |
sorted_tasks = [] |
|
442 |
for i, t in enumerate(wholeday_tasks): |
|
443 |
t.index = i |
|
444 |
ncols = len(wholeday_tasks) |
|
445 |
while inday_tasks: |
|
446 |
t = inday_tasks.pop(0) |
|
447 |
for i, c in enumerate(sorted_tasks): |
|
448 |
if not c or c[-1].task.stop <= t.task.start: |
|
449 |
c.append(t) |
|
450 |
t.index = i+ncols |
|
451 |
break |
|
452 |
else: |
|
453 |
t.index = len(sorted_tasks) + ncols |
|
454 |
sorted_tasks.append([t]) |
|
455 |
ncols += len(sorted_tasks) |
|
456 |
if ncols == 0: |
|
457 |
return |
|
458 |
||
459 |
inday_tasks = [] |
|
460 |
for tasklist in sorted_tasks: |
|
461 |
inday_tasks += tasklist |
|
462 |
width = 100.0/ncols |
|
463 |
for task_desc in wholeday_tasks + inday_tasks: |
|
464 |
task = task_desc.task |
|
465 |
start_hour = 8 |
|
466 |
start_min = 0 |
|
467 |
stop_hour = 20 |
|
468 |
stop_min = 0 |
|
469 |
if task.start: |
|
1025 | 470 |
if date < todate(task.start) < date + ONEDAY: |
0 | 471 |
start_hour = max(8, task.start.hour) |
472 |
start_min = task.start.minute |
|
473 |
if task.stop: |
|
1025 | 474 |
if date < todate(task.stop) < date + ONEDAY: |
0 | 475 |
stop_hour = min(20, task.stop.hour) |
476 |
if stop_hour < 20: |
|
477 |
stop_min = task.stop.minute |
|
1604 | 478 |
|
0 | 479 |
height = 100.0*(stop_hour+stop_min/60.0-start_hour-start_min/60.0)/(20-8) |
480 |
top = 100.0*(start_hour+start_min/60.0-8)/(20-8) |
|
481 |
left = width*task_desc.index |
|
482 |
style = "height: %s%%; width: %s%%; top: %s%%; left: %s%%; " % \ |
|
483 |
(height, width, top, left) |
|
484 |
self.w(u'<div class="task %s" style="%s">' % \ |
|
485 |
(task_desc.color, style)) |
|
486 |
task.view('calendaritem', dates=False, w=self.w) |
|
487 |
url = task.absolute_url(vid='edition', |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
488 |
__redirectrql=self.rset.printable_rql(), |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
489 |
__redirectparams=self.req.build_url_params(year=date.year, week=date.isocalendar()[1]), |
0 | 490 |
__redirectvid=self.id |
491 |
) |
|
492 |
||
493 |
self.w(u'<div class="tooltip" ondblclick="stopPropagation(event); window.location.assign(\'%s\'); return false;">' % html_escape(url)) |
|
494 |
task.view('tooltip', w=self.w) |
|
495 |
self.w(u'</div>') |
|
496 |
if task.start is None: |
|
497 |
self.w(u'<div class="bottommarker">') |
|
498 |
self.w(u'<div class="bottommarkerline" style="margin: 0px 3px 0px 3px; height: 1px;">') |
|
499 |
self.w(u'</div>') |
|
500 |
self.w(u'<div class="bottommarkerline" style="margin: 0px 2px 0px 2px; height: 1px;">') |
|
501 |
self.w(u'</div>') |
|
502 |
self.w(u'<div class="bottommarkerline" style="margin: 0px 1px 0px 1px; height: 3ex; color: white; font-size: x-small; vertical-align: center; text-align: center;">') |
|
503 |
self.w(u'end') |
|
504 |
self.w(u'</div>') |
|
505 |
self.w(u'</div>') |
|
506 |
self.w(u'</div>') |
|
507 |
||
1604 | 508 |
|
0 | 509 |
def _prevnext_links(self, curdate): |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
510 |
prevdate = curdate - timedelta(7) |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
511 |
nextdate = curdate + timedelta(7) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
512 |
rql = self.rset.printable_rql() |
0 | 513 |
prevlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal', |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
514 |
year=prevdate.year, week=prevdate.isocalendar()[1]) |
0 | 515 |
nextlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal', |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
516 |
year=nextdate.year, week=nextdate.isocalendar()[1]) |
0 | 517 |
return prevlink, nextlink |
518 |