author | sylvain.thenault@logilab.fr |
Tue, 28 Apr 2009 16:22:00 +0200 | |
branch | tls-sprint |
changeset 1508 | 45632d28ace3 |
parent 1398 | 5fe84a5f7035 |
child 1604 | fd943737d630 |
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 |
0 | 18 |
from cubicweb.common.uilib import ajax_replace_url |
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 |
) |
|
1025 | 32 |
|
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)) |
|
102 |
||
103 |
class CalendarLargeItemView(CalendarItemView): |
|
104 |
id = 'calendarlargeitem' |
|
105 |
||
0 | 106 |
|
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 |
|
119 |
||
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() |
|
123 |
||
0 | 124 |
class OneMonthCal(EntityView): |
125 |
"""At some point, this view will probably replace ampm calendars""" |
|
767 | 126 |
id = 'onemonthcal' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
127 |
__select__ = implements(ICalendarable) |
0 | 128 |
need_navigation = False |
129 |
title = _('one month') |
|
130 |
||
131 |
def call(self): |
|
132 |
self.req.add_js('cubicweb.ajax.js') |
|
133 |
self.req.add_css('cubicweb.calendar.css') |
|
134 |
# XXX: restrict courses directy with RQL |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
135 |
_today = datetime.today() |
0 | 136 |
|
137 |
if 'year' in self.req.form: |
|
138 |
year = int(self.req.form['year']) |
|
139 |
else: |
|
140 |
year = _today.year |
|
141 |
if 'month' in self.req.form: |
|
142 |
month = int(self.req.form['month']) |
|
143 |
else: |
|
144 |
month = _today.month |
|
145 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
146 |
first_day_of_month = date(year, month, 1) |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
147 |
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
|
148 |
if month >= 12: |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
149 |
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
|
150 |
else: |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
151 |
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
|
152 |
lastday = last_day_of_month + timedelta(6 - last_day_of_month.weekday()) |
0 | 153 |
month_dates = list(date_range(firstday, lastday)) |
154 |
dates = {} |
|
155 |
task_max = 0 |
|
156 |
for row in xrange(self.rset.rowcount): |
|
1132 | 157 |
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
|
158 |
if len(self.rset[row]) > 1 and self.rset.description[row][1] == 'CWUser': |
1132 | 159 |
user = self.rset.get_entity(row, 1) |
0 | 160 |
else: |
161 |
user = None |
|
162 |
the_dates = [] |
|
1025 | 163 |
tstart = todate(task.start) |
164 |
if tstart: |
|
165 |
if tstart > lastday: |
|
0 | 166 |
continue |
1025 | 167 |
the_dates = [tstart] |
168 |
tstop = todate(task.start) |
|
169 |
if tstop: |
|
170 |
if tstop < firstday: |
|
0 | 171 |
continue |
1025 | 172 |
the_dates = [tstop] |
173 |
if tstart and tstop: |
|
174 |
if tstart.isocalendar() == tstop.isocalendar(): |
|
175 |
if firstday <= tstart <= lastday: |
|
176 |
the_dates = [tstart] |
|
0 | 177 |
else: |
1025 | 178 |
the_dates = date_range(max(tstart, firstday), |
179 |
min(tstop, lastday)) |
|
0 | 180 |
if not the_dates: |
181 |
continue |
|
182 |
||
183 |
for d in the_dates: |
|
184 |
d_tasks = dates.setdefault((d.year, d.month, d.day), {}) |
|
1132 | 185 |
t_users = d_tasks.setdefault(task, set()) |
0 | 186 |
t_users.add( user ) |
1132 | 187 |
if len(d_tasks) > task_max: |
0 | 188 |
task_max = len(d_tasks) |
189 |
||
190 |
days = [] |
|
1132 | 191 |
nrows = max(3, task_max) |
0 | 192 |
# colors here are class names defined in cubicweb.css |
1132 | 193 |
colors = [ "col%x" % i for i in range(12) ] |
0 | 194 |
next_color_index = 0 |
195 |
||
196 |
visited_tasks = {} # holds a description of a task |
|
197 |
task_colors = {} # remember a color assigned to a task |
|
1025 | 198 |
for mdate in month_dates: |
199 |
d_tasks = dates.get((mdate.year, mdate.month, mdate.day), {}) |
|
0 | 200 |
rows = [None] * nrows |
201 |
# every task that is "visited" for the first time |
|
202 |
# require a special treatment, so we put them in |
|
203 |
# 'postpone' |
|
204 |
postpone = [] |
|
205 |
for task in d_tasks: |
|
206 |
if task in visited_tasks: |
|
207 |
task_descr = visited_tasks[ task ] |
|
208 |
rows[task_descr.index] = task_descr |
|
209 |
else: |
|
210 |
postpone.append(task) |
|
211 |
for task in postpone: |
|
212 |
# to every 'new' task we must affect a color |
|
213 |
# (which must be the same for every user concerned |
|
214 |
# by the task) |
|
1132 | 215 |
for i, t in enumerate(rows): |
0 | 216 |
if t is None: |
217 |
if task in task_colors: |
|
218 |
color = task_colors[task] |
|
219 |
else: |
|
220 |
color = colors[next_color_index] |
|
221 |
next_color_index = (next_color_index+1)%len(colors) |
|
222 |
task_colors[task] = color |
|
223 |
task_descr = _TaskEntry(task, color, i) |
|
224 |
rows[i] = task_descr |
|
225 |
visited_tasks[task] = task_descr |
|
226 |
break |
|
227 |
else: |
|
228 |
raise RuntimeError("is it possible we got it wrong?") |
|
229 |
||
230 |
days.append( rows ) |
|
231 |
||
232 |
curdate = first_day_of_month |
|
233 |
self.w(u'<div id="onemonthcalid">') |
|
234 |
# build schedule |
|
235 |
self.w(u'<table class="omcalendar">') |
|
236 |
prevlink, nextlink = self._prevnext_links(curdate) # XXX |
|
237 |
self.w(u'<tr><th><a href="%s"><<</a></th><th colspan="5">%s %s</th>' |
|
238 |
u'<th><a href="%s">>></a></th></tr>' % |
|
239 |
(html_escape(prevlink), self.req._(curdate.strftime('%B').lower()), |
|
240 |
curdate.year, html_escape(nextlink))) |
|
241 |
||
242 |
# output header |
|
243 |
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>' % |
|
244 |
tuple(self.req._(day) for day in WEEKDAYS)) |
|
245 |
||
246 |
# build calendar |
|
1025 | 247 |
for mdate, task_rows in zip(month_dates, days): |
248 |
if mdate.weekday() == 0: |
|
0 | 249 |
self.w(u'<tr>') |
1025 | 250 |
self._build_calendar_cell(mdate, task_rows, curdate) |
251 |
if mdate.weekday() == 6: |
|
0 | 252 |
self.w(u'</tr>') |
253 |
self.w(u'</table></div>') |
|
254 |
||
255 |
def _prevnext_links(self, curdate): |
|
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
256 |
prevdate = curdate - timedelta(31) |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
257 |
nextdate = curdate + timedelta(31) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
258 |
rql = self.rset.printable_rql() |
0 | 259 |
prevlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal', |
260 |
year=prevdate.year, month=prevdate.month) |
|
261 |
nextlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal', |
|
262 |
year=nextdate.year, month=nextdate.month) |
|
263 |
return prevlink, nextlink |
|
264 |
||
1149 | 265 |
def _build_calendar_cell(self, celldate, rows, curdate): |
0 | 266 |
curmonth = curdate.month |
267 |
classes = "" |
|
1149 | 268 |
if celldate.month != curmonth: |
0 | 269 |
classes += " outOfRange" |
1149 | 270 |
if celldate == date.today(): |
0 | 271 |
classes += " today" |
272 |
self.w(u'<td class="cell%s">' % classes) |
|
273 |
self.w(u'<div class="calCellTitle%s">' % classes) |
|
1149 | 274 |
self.w(u'<div class="day">%s</div>' % celldate.day) |
0 | 275 |
|
276 |
if len(self.rset.column_types(0)) == 1: |
|
277 |
etype = list(self.rset.column_types(0))[0] |
|
278 |
url = self.build_url(vid='creation', etype=etype, |
|
279 |
schedule=True, |
|
1149 | 280 |
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
|
281 |
__redirectrql=self.rset.printable_rql(), |
0 | 282 |
__redirectparams=self.req.build_url_params(year=curdate.year, month=curmonth), |
283 |
__redirectvid=self.id |
|
284 |
) |
|
285 |
self.w(u'<div class="cmd"><a href="%s">%s</a></div>' % (html_escape(url), self.req._(u'add'))) |
|
286 |
self.w(u' ') |
|
287 |
self.w(u'</div>') |
|
288 |
self.w(u'<div class="cellContent">') |
|
289 |
for task_descr in rows: |
|
290 |
if task_descr: |
|
291 |
task = task_descr.task |
|
292 |
self.w(u'<div class="task %s">' % task_descr.color) |
|
293 |
task.view('calendaritem', w=self.w ) |
|
294 |
url = task.absolute_url(vid='edition', |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
295 |
__redirectrql=self.rset.printable_rql(), |
0 | 296 |
__redirectparams=self.req.build_url_params(year=curdate.year, month=curmonth), |
297 |
__redirectvid=self.id |
|
298 |
) |
|
299 |
||
300 |
self.w(u'<div class="tooltip" ondblclick="stopPropagation(event); window.location.assign(\'%s\'); return false;">' % html_escape(url)) |
|
301 |
task.view('tooltip', w=self.w ) |
|
302 |
self.w(u'</div>') |
|
303 |
else: |
|
304 |
self.w(u'<div class="task">') |
|
305 |
self.w(u" ") |
|
306 |
self.w(u'</div>') |
|
307 |
self.w(u'</div>') |
|
308 |
self.w(u'</td>') |
|
309 |
||
310 |
||
311 |
class OneWeekCal(EntityView): |
|
312 |
"""At some point, this view will probably replace ampm calendars""" |
|
767 | 313 |
id = 'oneweekcal' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
314 |
__select__ = implements(ICalendarable) |
0 | 315 |
need_navigation = False |
316 |
title = _('one week') |
|
317 |
||
318 |
def call(self): |
|
319 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.calendar.js') ) |
|
320 |
self.req.add_css('cubicweb.calendar.css') |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
321 |
# XXX: restrict directly with RQL |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
322 |
_today = datetime.today() |
0 | 323 |
if 'year' in self.req.form: |
324 |
year = int(self.req.form['year']) |
|
325 |
else: |
|
326 |
year = _today.year |
|
327 |
if 'week' in self.req.form: |
|
328 |
week = int(self.req.form['week']) |
|
329 |
else: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
330 |
week = _today.isocalendar()[1] |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
331 |
# 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
|
332 |
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
|
333 |
lastday = first_day_of_week + timedelta(6) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
334 |
firstday = first_day_of_week |
0 | 335 |
dates = [[] for i in range(7)] |
336 |
task_colors = {} # remember a color assigned to a task |
|
337 |
# colors here are class names defined in cubicweb.css |
|
1132 | 338 |
colors = [ "col%x" % i for i in range(12) ] |
0 | 339 |
next_color_index = 0 |
340 |
done_tasks = [] |
|
341 |
for row in xrange(self.rset.rowcount): |
|
1132 | 342 |
task = self.rset.get_entity(row, 0) |
0 | 343 |
if task in done_tasks: |
344 |
continue |
|
345 |
done_tasks.append(task) |
|
346 |
the_dates = [] |
|
1025 | 347 |
tstart = todate(task.start) |
348 |
tstop = todate(task.stop) |
|
349 |
if tstart: |
|
350 |
if tstart > lastday: |
|
0 | 351 |
continue |
1025 | 352 |
the_dates = [tstart] |
353 |
if tstop: |
|
354 |
if tstop < firstday: |
|
0 | 355 |
continue |
1025 | 356 |
the_dates = [tstop] |
357 |
if tstart and tstop: |
|
358 |
the_dates = date_range(max(tstart, firstday), |
|
359 |
min(tstop, lastday)) |
|
0 | 360 |
if not the_dates: |
361 |
continue |
|
362 |
||
363 |
if task not in task_colors: |
|
364 |
task_colors[task] = colors[next_color_index] |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
365 |
next_color_index = (next_color_index+1) % len(colors) |
0 | 366 |
|
367 |
for d in the_dates: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
368 |
day = d.weekday() |
0 | 369 |
task_descr = _TaskEntry(task, task_colors[task]) |
370 |
dates[day].append(task_descr) |
|
371 |
||
372 |
self.w(u'<div id="oneweekcalid">') |
|
373 |
# build schedule |
|
374 |
self.w(u'<table class="omcalendar" id="week">') |
|
375 |
prevlink, nextlink = self._prevnext_links(first_day_of_week) # XXX |
|
376 |
self.w(u'<tr><th class="transparent"></th>') |
|
377 |
self.w(u'<th><a href="%s"><<</a></th><th colspan="5">%s %s %s</th>' |
|
378 |
u'<th><a href="%s">>></a></th></tr>' % |
|
379 |
(html_escape(prevlink), first_day_of_week.year, |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
380 |
self.req._(u'week'), first_day_of_week.isocalendar()[1], |
0 | 381 |
html_escape(nextlink))) |
382 |
||
383 |
# output header |
|
384 |
self.w(u'<tr>') |
|
385 |
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
|
386 |
_today = date.today() |
0 | 387 |
for i, day in enumerate(WEEKDAYS): |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
388 |
wdate = first_day_of_week + timedelta(i) |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
389 |
if wdate.isocalendar() == _today.isocalendar(): |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
390 |
self.w(u'<th class="today">%s<br/>%s</th>' % (self.req._(day), self.format_date(wdate))) |
0 | 391 |
else: |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
392 |
self.w(u'<th>%s<br/>%s</th>' % (self.req._(day), self.format_date(wdate))) |
0 | 393 |
self.w(u'</tr>') |
394 |
||
395 |
# build week calendar |
|
396 |
self.w(u'<tr>') |
|
397 |
self.w(u'<td style="width:5em;">') # column for hours |
|
398 |
extra = "" |
|
399 |
for h in range(8, 20): |
|
400 |
self.w(u'<div class="hour" %s>'%extra) |
|
401 |
self.w(u'%02d:00'%h) |
|
402 |
self.w(u'</div>') |
|
403 |
self.w(u'</td>') |
|
404 |
||
405 |
for i, day in enumerate(WEEKDAYS): |
|
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
406 |
wdate = first_day_of_week + timedelta(i) |
0 | 407 |
classes = "" |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
408 |
if wdate.isocalendar() == _today.isocalendar(): |
0 | 409 |
classes = " today" |
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
410 |
self.w(u'<td class="column %s" id="%s">' % (classes, day)) |
0 | 411 |
if len(self.rset.column_types(0)) == 1: |
412 |
etype = list(self.rset.column_types(0))[0] |
|
413 |
url = self.build_url(vid='creation', etype=etype, |
|
414 |
schedule=True, |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
415 |
__redirectrql=self.rset.printable_rql(), |
0 | 416 |
__redirectparams=self.req.build_url_params(year=year, week=week), |
417 |
__redirectvid=self.id |
|
418 |
) |
|
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
419 |
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
|
420 |
wdate.year, wdate.month, wdate.day, html_escape(url)) |
0 | 421 |
else: |
422 |
extra = "" |
|
423 |
self.w(u'<div class="columndiv"%s>'% extra) |
|
424 |
for h in range(8, 20): |
|
425 |
self.w(u'<div class="hourline" style="top:%sex;">'%((h-7)*8)) |
|
426 |
self.w(u'</div>') |
|
427 |
if dates[i]: |
|
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
428 |
self._build_calendar_cell(wdate, dates[i]) |
0 | 429 |
self.w(u'</div>') |
430 |
self.w(u'</td>') |
|
431 |
self.w(u'</tr>') |
|
432 |
self.w(u'</table></div>') |
|
433 |
self.w(u'<div id="coord"></div>') |
|
434 |
self.w(u'<div id="debug"> </div>') |
|
435 |
||
436 |
def _build_calendar_cell(self, date, task_descrs): |
|
1025 | 437 |
inday_tasks = [t for t in task_descrs if t.is_one_day_task() and t.in_working_hours()] |
438 |
wholeday_tasks = [t for t in task_descrs if not t.is_one_day_task()] |
|
0 | 439 |
inday_tasks.sort(key=lambda t:t.task.start) |
440 |
sorted_tasks = [] |
|
441 |
for i, t in enumerate(wholeday_tasks): |
|
442 |
t.index = i |
|
443 |
ncols = len(wholeday_tasks) |
|
444 |
while inday_tasks: |
|
445 |
t = inday_tasks.pop(0) |
|
446 |
for i, c in enumerate(sorted_tasks): |
|
447 |
if not c or c[-1].task.stop <= t.task.start: |
|
448 |
c.append(t) |
|
449 |
t.index = i+ncols |
|
450 |
break |
|
451 |
else: |
|
452 |
t.index = len(sorted_tasks) + ncols |
|
453 |
sorted_tasks.append([t]) |
|
454 |
ncols += len(sorted_tasks) |
|
455 |
if ncols == 0: |
|
456 |
return |
|
457 |
||
458 |
inday_tasks = [] |
|
459 |
for tasklist in sorted_tasks: |
|
460 |
inday_tasks += tasklist |
|
461 |
width = 100.0/ncols |
|
462 |
for task_desc in wholeday_tasks + inday_tasks: |
|
463 |
task = task_desc.task |
|
464 |
start_hour = 8 |
|
465 |
start_min = 0 |
|
466 |
stop_hour = 20 |
|
467 |
stop_min = 0 |
|
468 |
if task.start: |
|
1025 | 469 |
if date < todate(task.start) < date + ONEDAY: |
0 | 470 |
start_hour = max(8, task.start.hour) |
471 |
start_min = task.start.minute |
|
472 |
if task.stop: |
|
1025 | 473 |
if date < todate(task.stop) < date + ONEDAY: |
0 | 474 |
stop_hour = min(20, task.stop.hour) |
475 |
if stop_hour < 20: |
|
476 |
stop_min = task.stop.minute |
|
477 |
||
478 |
height = 100.0*(stop_hour+stop_min/60.0-start_hour-start_min/60.0)/(20-8) |
|
479 |
top = 100.0*(start_hour+start_min/60.0-8)/(20-8) |
|
480 |
left = width*task_desc.index |
|
481 |
style = "height: %s%%; width: %s%%; top: %s%%; left: %s%%; " % \ |
|
482 |
(height, width, top, left) |
|
483 |
self.w(u'<div class="task %s" style="%s">' % \ |
|
484 |
(task_desc.color, style)) |
|
485 |
task.view('calendaritem', dates=False, w=self.w) |
|
486 |
url = task.absolute_url(vid='edition', |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
487 |
__redirectrql=self.rset.printable_rql(), |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
488 |
__redirectparams=self.req.build_url_params(year=date.year, week=date.isocalendar()[1]), |
0 | 489 |
__redirectvid=self.id |
490 |
) |
|
491 |
||
492 |
self.w(u'<div class="tooltip" ondblclick="stopPropagation(event); window.location.assign(\'%s\'); return false;">' % html_escape(url)) |
|
493 |
task.view('tooltip', w=self.w) |
|
494 |
self.w(u'</div>') |
|
495 |
if task.start is None: |
|
496 |
self.w(u'<div class="bottommarker">') |
|
497 |
self.w(u'<div class="bottommarkerline" style="margin: 0px 3px 0px 3px; height: 1px;">') |
|
498 |
self.w(u'</div>') |
|
499 |
self.w(u'<div class="bottommarkerline" style="margin: 0px 2px 0px 2px; height: 1px;">') |
|
500 |
self.w(u'</div>') |
|
501 |
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;">') |
|
502 |
self.w(u'end') |
|
503 |
self.w(u'</div>') |
|
504 |
self.w(u'</div>') |
|
505 |
self.w(u'</div>') |
|
506 |
||
507 |
||
508 |
def _prevnext_links(self, curdate): |
|
1033
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
509 |
prevdate = curdate - timedelta(7) |
f5be65616a31
more datetime fixes and cleanup
sylvain.thenault@logilab.fr
parents:
1025
diff
changeset
|
510 |
nextdate = curdate + timedelta(7) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
511 |
rql = self.rset.printable_rql() |
0 | 512 |
prevlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal', |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
513 |
year=prevdate.year, week=prevdate.isocalendar()[1]) |
0 | 514 |
nextlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal', |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
515 |
year=nextdate.year, week=nextdate.isocalendar()[1]) |
0 | 516 |
return prevlink, nextlink |
517 |