| 1 |
from trac.core import * |
|---|
| 2 |
from trac.Timeline import ITimelineEventProvider |
|---|
| 3 |
from trac.wiki import wiki_to_html, wiki_to_oneliner |
|---|
| 4 |
from trac.util import Markup |
|---|
| 5 |
import time |
|---|
| 6 |
|
|---|
| 7 |
class DiscussionTimeline(Component): |
|---|
| 8 |
""" |
|---|
| 9 |
The timeline module implements raising timeline events when |
|---|
| 10 |
forums, topics and messages are changed. |
|---|
| 11 |
""" |
|---|
| 12 |
implements(ITimelineEventProvider) |
|---|
| 13 |
|
|---|
| 14 |
# ITimelineEventProvider |
|---|
| 15 |
def get_timeline_events(self, req, start, stop, filters): |
|---|
| 16 |
self.log.debug("start: %s, stop: %s, filters: %s" % (start, stop, |
|---|
| 17 |
filters)) |
|---|
| 18 |
if 'discussion' in filters: |
|---|
| 19 |
# Create database context |
|---|
| 20 |
db = self.env.get_db_cnx() |
|---|
| 21 |
cursor = db.cursor() |
|---|
| 22 |
format = req.args.get('format') |
|---|
| 23 |
self.log.debug("format: %s" % (format)) |
|---|
| 24 |
|
|---|
| 25 |
# Get forum events |
|---|
| 26 |
for forum in self._get_changed_forums(cursor, start, stop): |
|---|
| 27 |
self.log.debug("forum: %s" % (forum)) |
|---|
| 28 |
kind = 'changeset' |
|---|
| 29 |
title = Markup('New forum <em>%s</em> created by %s' % |
|---|
| 30 |
(forum['name'], forum['author'])) |
|---|
| 31 |
time = forum['time'] |
|---|
| 32 |
author = forum['author'] |
|---|
| 33 |
if format == 'rss': |
|---|
| 34 |
href = self.env.abs_href.discussion(forum['id']) |
|---|
| 35 |
message = wiki_to_html('%s - %s' % (forum['subject'], |
|---|
| 36 |
forum['description']), self.env, req, db) |
|---|
| 37 |
else: |
|---|
| 38 |
href = self.env.href.discussion(forum['id']) |
|---|
| 39 |
message = wiki_to_oneliner('%s - %s' % (forum['subject'], |
|---|
| 40 |
forum['description']), self.env, db) |
|---|
| 41 |
yield kind, href, title, time, author, message |
|---|
| 42 |
|
|---|
| 43 |
# Get topic events |
|---|
| 44 |
for topic in self._get_changed_topics(cursor, start, stop): |
|---|
| 45 |
self.log.debug("topic: %s" % (topic)) |
|---|
| 46 |
kind = 'newticket' |
|---|
| 47 |
title = Markup('New topic on <em>%s</em> created by %s' % |
|---|
| 48 |
(topic['forum_name'], topic['author'])) |
|---|
| 49 |
time = topic['time'] |
|---|
| 50 |
author = topic['author'] |
|---|
| 51 |
if format == 'rss': |
|---|
| 52 |
href = self.env.abs_href.discussion(topic['forum'], |
|---|
| 53 |
topic['id']) |
|---|
| 54 |
message = wiki_to_html(topic['subject'], self.env, req, db) |
|---|
| 55 |
else: |
|---|
| 56 |
href = self.env.href.discussion(topic['forum'], topic['id']) |
|---|
| 57 |
message = wiki_to_oneliner(topic['subject'], self.env, db) |
|---|
| 58 |
yield kind, href, title, time, author, message |
|---|
| 59 |
|
|---|
| 60 |
# Get message events |
|---|
| 61 |
for message in self._get_changed_messages(cursor, start, stop): |
|---|
| 62 |
self.log.debug("message: %s" % (message)) |
|---|
| 63 |
kind = 'editedticket' |
|---|
| 64 |
title = Markup('New reply on <em>%s</em> created by %s' % |
|---|
| 65 |
(message['forum_name'], message['author'])) |
|---|
| 66 |
time = message['time'] |
|---|
| 67 |
author = message['author'] |
|---|
| 68 |
if format == 'rss': |
|---|
| 69 |
href = self.env.abs_href.discussion(message['forum'], |
|---|
| 70 |
message['topic'], message['id']) + '#%s' % (message['id']) |
|---|
| 71 |
message = wiki_to_html(message['topic_subject'], self.env, |
|---|
| 72 |
req, db) |
|---|
| 73 |
else: |
|---|
| 74 |
href = self.env.href.discussion(message['forum'], |
|---|
| 75 |
message['topic'], message['id']) + '#%s' % (message['id']) |
|---|
| 76 |
message = wiki_to_oneliner(message['topic_subject'], |
|---|
| 77 |
self.env, db) |
|---|
| 78 |
yield kind, href, title, time, author, message |
|---|
| 79 |
|
|---|
| 80 |
def get_timeline_filters(self, req): |
|---|
| 81 |
if req.perm.has_permission('DISCUSSION_VIEW'): |
|---|
| 82 |
yield ('discussion', 'Discussion changes') |
|---|
| 83 |
|
|---|
| 84 |
def _get_changed_forums(self, cursor, start, stop): |
|---|
| 85 |
columns = ('id', 'name', 'author', 'subject', 'description', 'time') |
|---|
| 86 |
sql = "SELECT id, name, author, subject, description, time FROM forum" \ |
|---|
| 87 |
" WHERE time BETWEEN %s AND %s" |
|---|
| 88 |
self.log.debug(sql % (start, stop)) |
|---|
| 89 |
cursor.execute(sql, (start, stop)) |
|---|
| 90 |
for row in cursor: |
|---|
| 91 |
row = dict(zip(columns, row)) |
|---|
| 92 |
yield row |
|---|
| 93 |
|
|---|
| 94 |
def _get_changed_topics(self, cursor, start, stop): |
|---|
| 95 |
columns = ('id', 'subject', 'author', 'time', 'forum', 'forum_name') |
|---|
| 96 |
sql = "SELECT id, subject, author, time, forum, (SELECT name FROM forum" \ |
|---|
| 97 |
" f WHERE f.id = topic.forum) FROM topic WHERE time BETWEEN %s AND %s" |
|---|
| 98 |
self.log.debug(sql % (start, stop)) |
|---|
| 99 |
cursor.execute(sql, (start, stop)) |
|---|
| 100 |
for row in cursor: |
|---|
| 101 |
row = dict(zip(columns, row)) |
|---|
| 102 |
yield row |
|---|
| 103 |
|
|---|
| 104 |
def _get_changed_messages(self, cursor, start, stop): |
|---|
| 105 |
columns = ('id', 'author', 'time', 'forum', 'topic', 'forum_name', |
|---|
| 106 |
'topic_subject') |
|---|
| 107 |
sql = "SELECT id, author, time, forum, topic, (SELECT name FROM forum f" \ |
|---|
| 108 |
" WHERE f.id = message.forum), (SELECT subject FROM topic t WHERE" \ |
|---|
| 109 |
" t.id = message.topic) FROM message WHERE time BETWEEN %s AND %s" |
|---|
| 110 |
self.log.debug(sql % (start, stop)) |
|---|
| 111 |
cursor.execute(sql, (start, stop)) |
|---|
| 112 |
for row in cursor: |
|---|
| 113 |
row = dict(zip(columns, row)) |
|---|
| 114 |
yield row |
|---|