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