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