def test_day(self): """enumerate days""" r = list(date_range(self.datecls(2000, 1, 1), self.datecls(2000, 1, 4))) expected = [ self.datecls(2000, 1, 1), self.datecls(2000, 1, 2), self.datecls(2000, 1, 3) ] self.assertListEqual(r, expected) r = list( date_range(self.datecls(2000, 1, 31), self.datecls(2000, 2, 3))) expected = [ self.datecls(2000, 1, 31), self.datecls(2000, 2, 1), self.datecls(2000, 2, 2) ] self.assertListEqual(r, expected) r = list( date_range(self.datecls(2000, 1, 1), self.datecls(2000, 1, 6), 2)) expected = [ self.datecls(2000, 1, 1), self.datecls(2000, 1, 3), self.datecls(2000, 1, 5) ] self.assertListEqual(r, expected)
def test_month(self): """enumerate months""" r = list(date_range(self.datecls(2000, 1, 2), self.datecls(2000, 4, 4), endOfMonth)) expected = [self.datecls(2000, 1, 2), self.datecls(2000, 2, 29), self.datecls(2000, 3, 31)] self.assertListEqual(r, expected) r = list(date_range(self.datecls(2000, 11, 30), self.datecls(2001, 2, 3), endOfMonth)) expected = [self.datecls(2000, 11, 30), self.datecls(2000, 12, 31), self.datecls(2001, 1, 31)] self.assertListEqual(r, expected)
def test_day(self): """enumerate days""" r = list(date_range(self.datecls(2000, 1, 1), self.datecls(2000, 1, 4))) expected = [self.datecls(2000, 1, 1), self.datecls(2000, 1, 2), self.datecls(2000, 1, 3)] self.assertListEqual(r, expected) r = list(date_range(self.datecls(2000, 1, 31), self.datecls(2000, 2, 3))) expected = [self.datecls(2000, 1, 31), self.datecls(2000, 2, 1), self.datecls(2000, 2, 2)] self.assertListEqual(r, expected) r = list(date_range(self.datecls(2000, 1, 1), self.datecls(2000, 1, 6), 2)) expected = [self.datecls(2000, 1, 1), self.datecls(2000, 1, 3), self.datecls(2000, 1, 5)] self.assertListEqual(r, expected)
def test_month(self): """enumerate months""" r = list(date_range(self.datecls(2006, 5, 6), self.datecls(2006, 8, 27), incmonth=True)) expected = [ self.datecls(2006, 5, 6), self.datecls(2006, 6, 1), self.datecls(2006, 7, 1), self.datecls(2006, 8, 1), ] self.assertListEqual(expected, r)
def call(self, title=None): """Dumps a timetable from a resultset composed of a note (anything with start/stop) and a user (anything)""" self._cw.add_css('cubicweb.timetable.css') dates = {} users = [] users_max = {} # XXX: try refactoring with calendar.py:OneMonthCal for row in range(self.cw_rset.rowcount): task = self.cw_rset.get_entity(row, 0) icalendarable = task.cw_adapt_to('ICalendarable') if len(self.cw_rset[row] ) > 1 and self.cw_rset.description[row][1] == 'CWUser': user = self.cw_rset.get_entity(row, 1) else: user = ALL_USERS the_dates = [] if icalendarable.start and icalendarable.stop: if icalendarable.start.toordinal( ) == icalendarable.stop.toordinal(): the_dates.append(icalendarable.start) else: the_dates += date_range(icalendarable.start, icalendarable.stop + ONEDAY) elif icalendarable.start: the_dates.append(icalendarable.start) elif icalendarable.stop: the_dates.append(icalendarable.stop) for d in the_dates: d = todatetime(d) d_users = dates.setdefault(d, {}) u_tasks = d_users.setdefault(user, set()) u_tasks.add(task) task_max = users_max.setdefault(user, 0) if len(u_tasks) > task_max: users_max[user] = len(u_tasks) if user not in users: # keep original ordering users.append(user) if not dates: return date_min = min(dates) date_max = max(dates) #users = list(sorted(users, key=lambda u:u.login)) rows = [] # colors here are class names defined in cubicweb.css colors = ["col%x" % i for i in range(12)] next_color_index = 0 visited_tasks = {} # holds a description of a task for a user task_colors = {} # remember a color assigned to a task for date in date_range(date_min, date_max + ONEDAY): columns = [date] d_users = dates.get(date, {}) for user in users: # every user has its column "splitted" in at least MIN_COLS # sub-columns (for overlapping tasks) user_columns = [None] * max(MIN_COLS, users_max[user]) # every task that is "visited" for the first time # require a special treatment, so we put them in # 'postpone' postpone = [] for task in d_users.get(user, []): key = (task, user) if key in visited_tasks: task_descr = visited_tasks[key] user_columns[task_descr.column] = task_descr, False task_descr.lines += 1 else: postpone.append(key) for key in postpone: # to every 'new' task we must affect a color # (which must be the same for every user concerned # by the task) task, user = key for i, t in enumerate(user_columns): if t is None: if task in task_colors: color = task_colors[task] else: color = colors[next_color_index] next_color_index = (next_color_index + 1) % len(colors) task_colors[task] = color task_descr = _TaskEntry(task, color, i) user_columns[i] = task_descr, True visited_tasks[key] = task_descr break else: raise RuntimeError("is it possible we got it wrong?") columns.append(user_columns) rows.append(columns) widths = [len(col) for col in rows[0][1:]] self.w(u'<div class="section">') if title: self.w(u'<h4>%s</h4>\n' % title) self.w(u'<table class="listing timetable">') self.render_col_headers(users, widths) self.render_rows(rows) self.w(u'</table>') self.w(u'</div>\n')
def test_month(self): """enumerate months""" r = list(date_range(self.datecls(2006, 5, 6), self.datecls(2006, 8, 27), incmonth=True)) expected = [self.datecls(2006, 5, 6), self.datecls(2006, 6, 1), self.datecls(2006, 7, 1), self.datecls(2006, 8, 1)] self.assertListEqual(expected, r)