author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 09 Dec 2015 17:44:17 +0100 | |
changeset 11001 | 37c5c9b3b3bf |
parent 10666 | 7f6b5f023884 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
18 |
"""html timetable views""" |
0 | 19 |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
20 |
__docformat__ = "restructuredtext en" |
10666
7f6b5f023884
[py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10609
diff
changeset
|
21 |
from cubicweb import _ |
0 | 22 |
|
10609
e2d8e81bfe68
[py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
8190
diff
changeset
|
23 |
from six.moves import range |
e2d8e81bfe68
[py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
8190
diff
changeset
|
24 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
25 |
from logilab.mtconverter import xml_escape |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
26 |
from logilab.common.date import ONEDAY, date_range, todatetime |
0 | 27 |
|
8190
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6677
diff
changeset
|
28 |
from cubicweb.predicates import adaptable |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
29 |
from cubicweb.view import EntityView |
0 | 30 |
|
31 |
||
32 |
class _TaskEntry(object): |
|
33 |
def __init__(self, task, color, column): |
|
34 |
self.task = task |
|
35 |
self.color = color |
|
36 |
self.column = column |
|
37 |
self.lines = 1 |
|
38 |
||
39 |
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
|
40 |
ALL_USERS = object() |
0 | 41 |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
42 |
class TimeTableView(EntityView): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
43 |
__regid__ = 'timetable' |
0 | 44 |
title = _('timetable') |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
45 |
__select__ = adaptable('ICalendarable') |
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
46 |
paginable = False |
0 | 47 |
|
48 |
def call(self, title=None): |
|
49 |
"""Dumps a timetable from a resultset composed of a note (anything |
|
50 |
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
|
51 |
self._cw.add_css('cubicweb.timetable.css') |
0 | 52 |
dates = {} |
53 |
users = [] |
|
54 |
users_max = {} |
|
55 |
# XXX: try refactoring with calendar.py:OneMonthCal |
|
10609
e2d8e81bfe68
[py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
8190
diff
changeset
|
56 |
for row in range(self.cw_rset.rowcount): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
57 |
task = self.cw_rset.get_entity(row, 0) |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
58 |
icalendarable = task.cw_adapt_to('ICalendarable') |
6677
5f105a374ffe
[timetable view] ensure we actually have a user in the rset cell
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5569
diff
changeset
|
59 |
if len(self.cw_rset[row]) > 1 and self.cw_rset.description[row][1] == 'CWUser': |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
60 |
user = self.cw_rset.get_entity(row, 1) |
0 | 61 |
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
|
62 |
user = ALL_USERS |
0 | 63 |
the_dates = [] |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
64 |
if icalendarable.start and icalendarable.stop: |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
65 |
if icalendarable.start.toordinal() == icalendarable.stop.toordinal(): |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
66 |
the_dates.append(icalendarable.start) |
0 | 67 |
else: |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
68 |
the_dates += date_range(icalendarable.start, |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
69 |
icalendarable.stop + ONEDAY) |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
70 |
elif icalendarable.start: |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
71 |
the_dates.append(icalendarable.start) |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
72 |
elif icalendarable.stop: |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
73 |
the_dates.append(icalendarable.stop) |
0 | 74 |
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
|
75 |
d = todatetime(d) |
0 | 76 |
d_users = dates.setdefault(d, {}) |
1132 | 77 |
u_tasks = d_users.setdefault(user, set()) |
0 | 78 |
u_tasks.add( task ) |
1132 | 79 |
task_max = users_max.setdefault(user, 0) |
0 | 80 |
if len(u_tasks)>task_max: |
81 |
users_max[user] = len(u_tasks) |
|
82 |
if user not in users: |
|
83 |
# keep original ordering |
|
84 |
users.append(user) |
|
85 |
if not dates: |
|
86 |
return |
|
87 |
date_min = min(dates) |
|
88 |
date_max = max(dates) |
|
89 |
#users = list(sorted(users, key=lambda u:u.login)) |
|
90 |
||
91 |
rows = [] |
|
92 |
# colors here are class names defined in cubicweb.css |
|
10609
e2d8e81bfe68
[py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
8190
diff
changeset
|
93 |
colors = ["col%x" % i for i in range(12)] |
0 | 94 |
next_color_index = 0 |
95 |
||
96 |
visited_tasks = {} # holds a description of a task for a user |
|
97 |
task_colors = {} # remember a color assigned to a task |
|
5569
cb14af012a96
[adapters] refactoring fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
98 |
for date in date_range(date_min, date_max + ONEDAY): |
0 | 99 |
columns = [date] |
100 |
d_users = dates.get(date, {}) |
|
101 |
for user in users: |
|
102 |
# every user has its column "splitted" in at least MIN_COLS |
|
103 |
# sub-columns (for overlapping tasks) |
|
104 |
user_columns = [None] * max(MIN_COLS, users_max[user]) |
|
105 |
# every task that is "visited" for the first time |
|
106 |
# require a special treatment, so we put them in |
|
107 |
# 'postpone' |
|
108 |
postpone = [] |
|
109 |
for task in d_users.get(user, []): |
|
110 |
key = (task, user) |
|
111 |
if key in visited_tasks: |
|
112 |
task_descr = visited_tasks[ key ] |
|
113 |
user_columns[task_descr.column] = task_descr, False |
|
1133 | 114 |
task_descr.lines += 1 |
0 | 115 |
else: |
116 |
postpone.append(key) |
|
117 |
for key in postpone: |
|
118 |
# to every 'new' task we must affect a color |
|
119 |
# (which must be the same for every user concerned |
|
120 |
# by the task) |
|
121 |
task, user = key |
|
1132 | 122 |
for i, t in enumerate(user_columns): |
0 | 123 |
if t is None: |
124 |
if task in task_colors: |
|
125 |
color = task_colors[task] |
|
126 |
else: |
|
127 |
color = colors[next_color_index] |
|
128 |
next_color_index = (next_color_index+1)%len(colors) |
|
129 |
task_colors[task] = color |
|
130 |
task_descr = _TaskEntry(task, color, i) |
|
131 |
user_columns[i] = task_descr, True |
|
132 |
visited_tasks[key] = task_descr |
|
133 |
break |
|
134 |
else: |
|
135 |
raise RuntimeError("is it possible we got it wrong?") |
|
136 |
||
137 |
columns.append( user_columns ) |
|
138 |
rows.append( columns ) |
|
139 |
||
140 |
widths = [ len(col) for col in rows[0][1:] ] |
|
141 |
self.w(u'<div class="section">') |
|
142 |
if title: |
|
143 |
self.w(u'<h4>%s</h4>\n' % title) |
|
144 |
self.w(u'<table class="listing timetable">') |
|
145 |
self.render_col_headers(users, widths) |
|
146 |
self.render_rows(rows) |
|
147 |
self.w(u'</table>') |
|
148 |
self.w(u'</div>\n') |
|
149 |
||
1132 | 150 |
def render_col_headers(self, users, widths): |
0 | 151 |
""" render column headers """ |
152 |
self.w(u'<tr class="header">\n') |
|
153 |
||
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
154 |
self.w(u'<th class="ttdate"> </th>\n') |
0 | 155 |
columns = [] |
1132 | 156 |
for user, width in zip(users, widths): |
157 |
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
|
158 |
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
|
159 |
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
|
160 |
else: |
2471
3e2b50ece726
begin to drop ref to secondary view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
161 |
user.view('oneline', w=self.w) |
0 | 162 |
self.w(u'</th>') |
163 |
self.w(u'</tr>\n') |
|
164 |
return columns |
|
165 |
||
166 |
def render_rows(self, rows): |
|
167 |
""" render table content (row headers and central content) """ |
|
168 |
odd = False |
|
169 |
previous_is_empty = False |
|
170 |
for row in rows: |
|
171 |
date = row[0] |
|
172 |
empty_line = True |
|
173 |
for group in row[1:]: |
|
174 |
for value in group: |
|
175 |
if value: |
|
176 |
empty_line = False |
|
177 |
break |
|
178 |
else: |
|
179 |
continue |
|
180 |
break |
|
181 |
if empty_line and previous_is_empty: |
|
182 |
continue |
|
183 |
previous_is_empty = False |
|
184 |
||
185 |
klass = "even" |
|
1575
5eb02a4a4bcf
datetime fixes use weekday() instead of day_of_week
sylvain.thenault@logilab.fr
parents:
1555
diff
changeset
|
186 |
if date.weekday() in (5, 6) and not empty_line: |
0 | 187 |
klass = "odd" |
188 |
self.w(u'<tr class="%s">' % klass) |
|
189 |
odd = not odd |
|
190 |
||
191 |
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
|
192 |
self.w(u'<th class="ttdate">%s</th>' % self._cw.format_date(date) ) |
0 | 193 |
else: |
194 |
self.w(u'<th>...</th>' ) |
|
195 |
previous_is_empty = True |
|
196 |
||
197 |
empty_klasses = [ "ttle", "ttme", "ttre" ] |
|
198 |
filled_klasses = [ "ttlf", "ttmf", "ttrf" ] |
|
199 |
kj = 0 # 0: left, 1: mid, 2: right |
|
200 |
for uid, group in enumerate(row[1:]): |
|
201 |
for i, value in enumerate(group): |
|
202 |
if i == 0: |
|
203 |
kj = 0 |
|
204 |
elif i == len(group): |
|
205 |
kj = 2 |
|
206 |
else: |
|
207 |
kj = 1 |
|
208 |
if value: |
|
209 |
task_descr, first_row = value |
|
210 |
if first_row: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
211 |
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
|
212 |
self.w(u'<td rowspan="%d" class="%s %s" onclick="document.location=\'%s\'"> <div>' % ( |
0 | 213 |
task_descr.lines, task_descr.color, filled_klasses[kj], url)) |
214 |
task_descr.task.view('tooltip', w=self.w) |
|
215 |
self.w(u'</div></td>') |
|
216 |
else: |
|
217 |
if empty_line: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
218 |
self.w(u'<td class="ttempty"> </td>') |
0 | 219 |
else: |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
220 |
self.w(u'<td class="%s"> </td>' % empty_klasses[kj] ) |
0 | 221 |
self.w(u'</tr>\n') |