author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 12 Apr 2010 15:15:00 +0200 | |
branch | stable |
changeset 5217 | 08e7fa906cdb |
parent 4466 | 8b0ca7904820 |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
0 | 1 |
"""html calendar views |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3917
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1575
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
9 |
from logilab.mtconverter import xml_escape |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
10 |
from logilab.common.date import date_range, todatetime |
0 | 11 |
|
12 |
from cubicweb.interfaces import ITimetableViews |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
13 |
from cubicweb.selectors import implements |
889 | 14 |
from cubicweb.view import AnyRsetView |
0 | 15 |
|
16 |
||
17 |
class _TaskEntry(object): |
|
18 |
def __init__(self, task, color, column): |
|
19 |
self.task = task |
|
20 |
self.color = color |
|
21 |
self.column = column |
|
22 |
self.lines = 1 |
|
23 |
||
24 |
MIN_COLS = 3 # minimum number of task columns for a single user |
|
3914
36be7a5e53ad
fix name error in timetable view, always write unicode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3829
diff
changeset
|
25 |
ALL_USERS = object() |
0 | 26 |
|
27 |
class TimeTableView(AnyRsetView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
28 |
__regid__ = 'timetable' |
0 | 29 |
title = _('timetable') |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
30 |
__select__ = implements(ITimetableViews) |
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
31 |
paginable = False |
0 | 32 |
|
33 |
def call(self, title=None): |
|
34 |
"""Dumps a timetable from a resultset composed of a note (anything |
|
35 |
with start/stop) and a user (anything)""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
36 |
self._cw.add_css('cubicweb.timetable.css') |
0 | 37 |
dates = {} |
38 |
users = [] |
|
39 |
users_max = {} |
|
40 |
# XXX: try refactoring with calendar.py:OneMonthCal |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
41 |
for row in xrange(self.cw_rset.rowcount): |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
42 |
task = self.cw_rset.get_entity(row, 0) |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
43 |
if len(self.cw_rset[row]) > 1: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
44 |
user = self.cw_rset.get_entity(row, 1) |
0 | 45 |
else: |
3829
9674d62a54f9
close #474415 by using 'is' instead of comparison which explicitly fails on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
46 |
user = ALL_USERS |
0 | 47 |
the_dates = [] |
48 |
if task.start and task.stop: |
|
1555 | 49 |
if task.start.toordinal() == task.stop.toordinal(): |
0 | 50 |
the_dates.append(task.start) |
51 |
else: |
|
52 |
the_dates += date_range( task.start, task.stop ) |
|
53 |
elif task.start: |
|
54 |
the_dates.append(task.start) |
|
55 |
elif task.stop: |
|
56 |
the_dates.append(task.stop) |
|
57 |
for d in the_dates: |
|
3917
0f912c1cde28
[views] ticket #34808, make sure timetable view uses datetime and not date
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3914
diff
changeset
|
58 |
d = todatetime(d) |
0 | 59 |
d_users = dates.setdefault(d, {}) |
1132 | 60 |
u_tasks = d_users.setdefault(user, set()) |
0 | 61 |
u_tasks.add( task ) |
1132 | 62 |
task_max = users_max.setdefault(user, 0) |
0 | 63 |
if len(u_tasks)>task_max: |
64 |
users_max[user] = len(u_tasks) |
|
65 |
if user not in users: |
|
66 |
# keep original ordering |
|
67 |
users.append(user) |
|
68 |
if not dates: |
|
69 |
return |
|
70 |
date_min = min(dates) |
|
71 |
date_max = max(dates) |
|
72 |
#users = list(sorted(users, key=lambda u:u.login)) |
|
73 |
||
74 |
rows = [] |
|
75 |
# colors here are class names defined in cubicweb.css |
|
1132 | 76 |
colors = ["col%x" % i for i in xrange(12)] |
0 | 77 |
next_color_index = 0 |
78 |
||
79 |
visited_tasks = {} # holds a description of a task for a user |
|
80 |
task_colors = {} # remember a color assigned to a task |
|
81 |
for date in date_range(date_min, date_max): |
|
82 |
columns = [date] |
|
83 |
d_users = dates.get(date, {}) |
|
84 |
for user in users: |
|
85 |
# every user has its column "splitted" in at least MIN_COLS |
|
86 |
# sub-columns (for overlapping tasks) |
|
87 |
user_columns = [None] * max(MIN_COLS, users_max[user]) |
|
88 |
# every task that is "visited" for the first time |
|
89 |
# require a special treatment, so we put them in |
|
90 |
# 'postpone' |
|
91 |
postpone = [] |
|
92 |
for task in d_users.get(user, []): |
|
93 |
key = (task, user) |
|
94 |
if key in visited_tasks: |
|
95 |
task_descr = visited_tasks[ key ] |
|
96 |
user_columns[task_descr.column] = task_descr, False |
|
1133 | 97 |
task_descr.lines += 1 |
0 | 98 |
else: |
99 |
postpone.append(key) |
|
100 |
for key in postpone: |
|
101 |
# to every 'new' task we must affect a color |
|
102 |
# (which must be the same for every user concerned |
|
103 |
# by the task) |
|
104 |
task, user = key |
|
1132 | 105 |
for i, t in enumerate(user_columns): |
0 | 106 |
if t is None: |
107 |
if task in task_colors: |
|
108 |
color = task_colors[task] |
|
109 |
else: |
|
110 |
color = colors[next_color_index] |
|
111 |
next_color_index = (next_color_index+1)%len(colors) |
|
112 |
task_colors[task] = color |
|
113 |
task_descr = _TaskEntry(task, color, i) |
|
114 |
user_columns[i] = task_descr, True |
|
115 |
visited_tasks[key] = task_descr |
|
116 |
break |
|
117 |
else: |
|
118 |
raise RuntimeError("is it possible we got it wrong?") |
|
119 |
||
120 |
columns.append( user_columns ) |
|
121 |
rows.append( columns ) |
|
122 |
||
123 |
widths = [ len(col) for col in rows[0][1:] ] |
|
124 |
self.w(u'<div class="section">') |
|
125 |
if title: |
|
126 |
self.w(u'<h4>%s</h4>\n' % title) |
|
127 |
self.w(u'<table class="listing timetable">') |
|
128 |
self.render_col_headers(users, widths) |
|
129 |
self.render_rows(rows) |
|
130 |
self.w(u'</table>') |
|
131 |
self.w(u'</div>\n') |
|
132 |
||
1132 | 133 |
def render_col_headers(self, users, widths): |
0 | 134 |
""" render column headers """ |
135 |
self.w(u'<tr class="header">\n') |
|
136 |
||
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
137 |
self.w(u'<th class="ttdate"> </th>\n') |
0 | 138 |
columns = [] |
1132 | 139 |
for user, width in zip(users, widths): |
140 |
self.w(u'<th colspan="%s">' % max(MIN_COLS, width)) |
|
3829
9674d62a54f9
close #474415 by using 'is' instead of comparison which explicitly fails on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
141 |
if user is ALL_USERS: |
3914
36be7a5e53ad
fix name error in timetable view, always write unicode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3829
diff
changeset
|
142 |
self.w(u'*') |
3829
9674d62a54f9
close #474415 by using 'is' instead of comparison which explicitly fails on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
143 |
else: |
2471
3e2b50ece726
begin to drop ref to secondary view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
144 |
user.view('oneline', w=self.w) |
0 | 145 |
self.w(u'</th>') |
146 |
self.w(u'</tr>\n') |
|
147 |
return columns |
|
148 |
||
149 |
def render_rows(self, rows): |
|
150 |
""" render table content (row headers and central content) """ |
|
151 |
odd = False |
|
152 |
previous_is_empty = False |
|
153 |
for row in rows: |
|
154 |
date = row[0] |
|
155 |
empty_line = True |
|
156 |
for group in row[1:]: |
|
157 |
for value in group: |
|
158 |
if value: |
|
159 |
empty_line = False |
|
160 |
break |
|
161 |
else: |
|
162 |
continue |
|
163 |
break |
|
164 |
if empty_line and previous_is_empty: |
|
165 |
continue |
|
166 |
previous_is_empty = False |
|
167 |
||
168 |
klass = "even" |
|
1575
5eb02a4a4bcf
datetime fixes use weekday() instead of day_of_week
sylvain.thenault@logilab.fr
parents:
1555
diff
changeset
|
169 |
if date.weekday() in (5, 6) and not empty_line: |
0 | 170 |
klass = "odd" |
171 |
self.w(u'<tr class="%s">' % klass) |
|
172 |
odd = not odd |
|
173 |
||
174 |
if not empty_line: |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
175 |
self.w(u'<th class="ttdate">%s</th>' % self._cw.format_date(date) ) |
0 | 176 |
else: |
177 |
self.w(u'<th>...</th>' ) |
|
178 |
previous_is_empty = True |
|
179 |
||
180 |
empty_klasses = [ "ttle", "ttme", "ttre" ] |
|
181 |
filled_klasses = [ "ttlf", "ttmf", "ttrf" ] |
|
182 |
kj = 0 # 0: left, 1: mid, 2: right |
|
183 |
for uid, group in enumerate(row[1:]): |
|
184 |
for i, value in enumerate(group): |
|
185 |
if i == 0: |
|
186 |
kj = 0 |
|
187 |
elif i == len(group): |
|
188 |
kj = 2 |
|
189 |
else: |
|
190 |
kj = 1 |
|
191 |
if value: |
|
192 |
task_descr, first_row = value |
|
193 |
if first_row: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
194 |
url = xml_escape(task_descr.task.absolute_url(vid="edition")) |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
195 |
self.w(u'<td rowspan="%d" class="%s %s" onclick="document.location=\'%s\'"> <div>' % ( |
0 | 196 |
task_descr.lines, task_descr.color, filled_klasses[kj], url)) |
197 |
task_descr.task.view('tooltip', w=self.w) |
|
198 |
self.w(u'</div></td>') |
|
199 |
else: |
|
200 |
if empty_line: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
201 |
self.w(u'<td class="ttempty"> </td>') |
0 | 202 |
else: |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
203 |
self.w(u'<td class="%s"> </td>' % empty_klasses[kj] ) |
0 | 204 |
self.w(u'</tr>\n') |