source: worklogplugin/trunk/worklog/timeline.py

Last change on this file was 15985, checked in by Ryan J Ollos, 7 years ago

1.0.0dev: Support Trac 1.0+, including Trac 1.2

The changes have been minimally tested, and only with SQLite.
Please report any issues to #12099.

Fixes #12099.

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (c) 2007-2012 Colin Guthrie <trac@colin.guthr.ie>
4# Copyright (c) 2011-2016 Ryan J Ollos <ryan.j.ollos@gmail.com>
5# All rights reserved.
6#
7# This software is licensed as described in the file COPYING, which
8# you should have received as part of this distribution.
9
10from datetime import datetime
11
12from genshi.builder import tag
13from trac.core import Component, implements
14from trac.resource import Resource
15from trac.ticket.api import TicketSystem
16from trac.timeline.api import ITimelineEventProvider
17from trac.util.datefmt import pretty_timedelta, to_timestamp, utc
18from trac.util.text import shorten_line
19from trac.web.chrome import add_stylesheet
20from trac.wiki.formatter import format_to_oneliner
21
22
23class WorklogTimelineEventProvider(Component):
24    implements(ITimelineEventProvider)
25
26    # ITimelineEventProvider methods
27
28    def get_timeline_filters(self, req):
29        if req.perm.has_permission('WORK_VIEW'):
30            yield ('workstart', 'Work started', True)
31            yield ('workstop', 'Work stopped', True)
32
33    def get_timeline_events(self, req, start, stop, filters):
34        # Worklog changes
35        show_starts = 'workstart' in filters
36        show_stops = 'workstop' in filters
37        if show_starts or show_stops:
38            add_stylesheet(req, "worklog/worklogplugin.css")
39
40            ts_start = to_timestamp(start)
41            ts_stop = to_timestamp(stop)
42
43            ticket_realm = Resource('ticket')
44
45            for (worker, tid, ts, ts_start, comment, kind, summary, status,
46                 resolution, type) in self.env.db_query("""
47                        SELECT wl.worker,wl.ticket,wl.time,wl.starttime,
48                               wl.comment,wl.kind,t.summary,t.status,
49                               t.resolution,t.type
50                        FROM (
51                         SELECT worker, ticket, starttime
52                          AS time, starttime, comment, 'start' AS kind
53                         FROM work_log
54                         UNION
55                         SELECT worker,ticket,endtime AS time,starttime,
56                                comment,'stop' AS kind
57                         FROM work_log) AS wl
58                         INNER JOIN ticket t ON t.id = wl.ticket
59                          AND wl.time>=%s AND wl.time<=%s
60                         ORDER BY wl.time
61                    """, (ts_start, ts_stop)):
62                ticket = ticket_realm(id=tid)
63                time = datetime.fromtimestamp(ts, utc)
64                started = None
65                if kind == 'start':
66                    if not show_starts:
67                        continue
68                    yield ('workstart', time, worker,
69                           (ticket, summary, status, resolution,
70                            type, started, ""))
71                else:
72                    if not show_stops:
73                        continue
74                    started = datetime.fromtimestamp(ts_start, utc)
75                    if comment:
76                        comment = "(Time spent: %s)[[BR]]%s" \
77                                  % (pretty_timedelta(started, time), comment)
78                    else:
79                        comment = '(Time spent: %s)' \
80                                  % pretty_timedelta(started, time)
81                    yield ('workstop', time, worker,
82                           (ticket, summary, status, resolution,
83                            type, started, comment))
84
85    def render_timeline_event(self, context, field, event):
86        ticket, summary, status, resolution, type, started, comment = event[3]
87        if field == 'url':
88            return context.href.ticket(ticket.id)
89        elif field == 'title':
90            title = TicketSystem(self.env).format_summary(summary, status,
91                                                          resolution, type)
92            return tag('Work ', started and 'stopped' or 'started',
93                       ' on Ticket ', tag.em('#', ticket.id, title=title),
94                       ' (', shorten_line(summary), ') ')
95        elif field == 'description':
96            if self.config['timeline'].getbool('abbreviated_messages'):
97                comment = shorten_line(comment)
98            markup = format_to_oneliner(self.env, context(resource=ticket),
99                                        comment)
100            #if wiki_page.version > 1:
101            #    diff_href = context.href.wiki(
102            #        wiki_page.id, version=wiki_page.version, action='diff')
103            #    markup = tag(markup, ' ', tag.a('(diff)', href=diff_href))
104            return markup
Note: See TracBrowser for help on using the repository browser.