author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 24 Nov 2009 10:51:53 +0100 | |
branch | stable |
changeset 3914 | 36be7a5e53ad |
parent 3829 | 9674d62a54f9 |
child 3917 | 0f912c1cde28 |
permissions | -rw-r--r-- |
0 | 1 |
"""html calendar views |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1575
diff
changeset
|
4 |
:copyright: 2001-2009 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 |
0 | 10 |
|
11 |
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
|
12 |
from cubicweb.selectors import implements |
889 | 13 |
from cubicweb.utils import date_range |
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): |
|
28 |
id = 'timetable' |
|
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) |
0 | 31 |
need_navigation = False |
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)""" |
|
36 |
self.req.add_css('cubicweb.timetable.css') |
|
37 |
dates = {} |
|
38 |
users = [] |
|
39 |
users_max = {} |
|
40 |
# XXX: try refactoring with calendar.py:OneMonthCal |
|
41 |
for row in xrange(self.rset.rowcount): |
|
1132 | 42 |
task = self.rset.get_entity(row, 0) |
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
|
43 |
if len(self.rset[row]) > 1: |
1132 | 44 |
user = self.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: |
|
58 |
d_users = dates.setdefault(d, {}) |
|
1132 | 59 |
u_tasks = d_users.setdefault(user, set()) |
0 | 60 |
u_tasks.add( task ) |
1132 | 61 |
task_max = users_max.setdefault(user, 0) |
0 | 62 |
if len(u_tasks)>task_max: |
63 |
users_max[user] = len(u_tasks) |
|
64 |
if user not in users: |
|
65 |
# keep original ordering |
|
66 |
users.append(user) |
|
67 |
if not dates: |
|
68 |
return |
|
69 |
date_min = min(dates) |
|
70 |
date_max = max(dates) |
|
71 |
#users = list(sorted(users, key=lambda u:u.login)) |
|
72 |
||
73 |
rows = [] |
|
74 |
# colors here are class names defined in cubicweb.css |
|
1132 | 75 |
colors = ["col%x" % i for i in xrange(12)] |
0 | 76 |
next_color_index = 0 |
77 |
||
78 |
visited_tasks = {} # holds a description of a task for a user |
|
79 |
task_colors = {} # remember a color assigned to a task |
|
80 |
for date in date_range(date_min, date_max): |
|
81 |
columns = [date] |
|
82 |
d_users = dates.get(date, {}) |
|
83 |
for user in users: |
|
84 |
# every user has its column "splitted" in at least MIN_COLS |
|
85 |
# sub-columns (for overlapping tasks) |
|
86 |
user_columns = [None] * max(MIN_COLS, users_max[user]) |
|
87 |
# every task that is "visited" for the first time |
|
88 |
# require a special treatment, so we put them in |
|
89 |
# 'postpone' |
|
90 |
postpone = [] |
|
91 |
for task in d_users.get(user, []): |
|
92 |
key = (task, user) |
|
93 |
if key in visited_tasks: |
|
94 |
task_descr = visited_tasks[ key ] |
|
95 |
user_columns[task_descr.column] = task_descr, False |
|
1133 | 96 |
task_descr.lines += 1 |
0 | 97 |
else: |
98 |
postpone.append(key) |
|
99 |
for key in postpone: |
|
100 |
# to every 'new' task we must affect a color |
|
101 |
# (which must be the same for every user concerned |
|
102 |
# by the task) |
|
103 |
task, user = key |
|
1132 | 104 |
for i, t in enumerate(user_columns): |
0 | 105 |
if t is None: |
106 |
if task in task_colors: |
|
107 |
color = task_colors[task] |
|
108 |
else: |
|
109 |
color = colors[next_color_index] |
|
110 |
next_color_index = (next_color_index+1)%len(colors) |
|
111 |
task_colors[task] = color |
|
112 |
task_descr = _TaskEntry(task, color, i) |
|
113 |
user_columns[i] = task_descr, True |
|
114 |
visited_tasks[key] = task_descr |
|
115 |
break |
|
116 |
else: |
|
117 |
raise RuntimeError("is it possible we got it wrong?") |
|
118 |
||
119 |
columns.append( user_columns ) |
|
120 |
rows.append( columns ) |
|
121 |
||
122 |
widths = [ len(col) for col in rows[0][1:] ] |
|
123 |
self.w(u'<div class="section">') |
|
124 |
if title: |
|
125 |
self.w(u'<h4>%s</h4>\n' % title) |
|
126 |
self.w(u'<table class="listing timetable">') |
|
127 |
self.render_col_headers(users, widths) |
|
128 |
self.render_rows(rows) |
|
129 |
self.w(u'</table>') |
|
130 |
self.w(u'</div>\n') |
|
131 |
||
1132 | 132 |
def render_col_headers(self, users, widths): |
0 | 133 |
""" render column headers """ |
134 |
self.w(u'<tr class="header">\n') |
|
135 |
||
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
136 |
self.w(u'<th class="ttdate"> </th>\n') |
0 | 137 |
columns = [] |
1132 | 138 |
for user, width in zip(users, widths): |
139 |
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
|
140 |
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
|
141 |
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
|
142 |
else: |
2471
3e2b50ece726
begin to drop ref to secondary view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
143 |
user.view('oneline', w=self.w) |
0 | 144 |
self.w(u'</th>') |
145 |
self.w(u'</tr>\n') |
|
146 |
return columns |
|
147 |
||
148 |
def render_rows(self, rows): |
|
149 |
""" render table content (row headers and central content) """ |
|
150 |
odd = False |
|
151 |
previous_is_empty = False |
|
152 |
for row in rows: |
|
153 |
date = row[0] |
|
154 |
empty_line = True |
|
155 |
for group in row[1:]: |
|
156 |
for value in group: |
|
157 |
if value: |
|
158 |
empty_line = False |
|
159 |
break |
|
160 |
else: |
|
161 |
continue |
|
162 |
break |
|
163 |
if empty_line and previous_is_empty: |
|
164 |
continue |
|
165 |
previous_is_empty = False |
|
166 |
||
167 |
klass = "even" |
|
1575
5eb02a4a4bcf
datetime fixes use weekday() instead of day_of_week
sylvain.thenault@logilab.fr
parents:
1555
diff
changeset
|
168 |
if date.weekday() in (5, 6) and not empty_line: |
0 | 169 |
klass = "odd" |
170 |
self.w(u'<tr class="%s">' % klass) |
|
171 |
odd = not odd |
|
172 |
||
173 |
if not empty_line: |
|
174 |
self.w(u'<th class="ttdate">%s</th>' % self.format_date(date) ) |
|
175 |
else: |
|
176 |
self.w(u'<th>...</th>' ) |
|
177 |
previous_is_empty = True |
|
178 |
||
179 |
empty_klasses = [ "ttle", "ttme", "ttre" ] |
|
180 |
filled_klasses = [ "ttlf", "ttmf", "ttrf" ] |
|
181 |
kj = 0 # 0: left, 1: mid, 2: right |
|
182 |
for uid, group in enumerate(row[1:]): |
|
183 |
for i, value in enumerate(group): |
|
184 |
if i == 0: |
|
185 |
kj = 0 |
|
186 |
elif i == len(group): |
|
187 |
kj = 2 |
|
188 |
else: |
|
189 |
kj = 1 |
|
190 |
if value: |
|
191 |
task_descr, first_row = value |
|
192 |
if first_row: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
193 |
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
|
194 |
self.w(u'<td rowspan="%d" class="%s %s" onclick="document.location=\'%s\'"> <div>' % ( |
0 | 195 |
task_descr.lines, task_descr.color, filled_klasses[kj], url)) |
196 |
task_descr.task.view('tooltip', w=self.w) |
|
197 |
self.w(u'</div></td>') |
|
198 |
else: |
|
199 |
if empty_line: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
200 |
self.w(u'<td class="ttempty"> </td>') |
0 | 201 |
else: |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
202 |
self.w(u'<td class="%s"> </td>' % empty_klasses[kj] ) |
0 | 203 |
self.w(u'</tr>\n') |