About us | Join us | Hire us | Contact us | Google Group

TimelineHack

Timeline is a very useful function. We can see many events by timeline. How to implement it? Please follow me.

CodeReview is a plugin, you can see from here.

At first, line 29-34 @ codereview.py

class CodeReviewMain(Component):
    implements(IRequestHandler,
              INavigationContributor,
              ITemplateProvider,
              IPermissionRequestor,
              ITimelineEventProvider)

CodeReviewMain? is a subclass of Component and implements a interface: ITimelineEventProvider.

Then, line 65-69 @ codereview.py

def get_timeline_filters(self, req):
    if req.perm.has_permission('CODE_REVIEW_VIEW') or \
       req.perm.has_permission('CODE_REVIEW_EDIT') or \
       req.perm.has_permission('CODE_REVIEW_ADMIN'):
        yield ('codereview', 'Code Review changes')

This is a method of ITimelineEventProvider. It allows somebody visiting if somebody has permission. If a class implements ITimelineEventProvider, it must rewrite all methods of ITimelineEventProvider. The same to another method of ITimelineEventProvider.

Line 72-89 @ codereview.py

def get_timeline_events(self, req, start, stop, filters):

    # whether visit codereview
    if 'codereview' in filters:

        # get database connection.
        db = self.env.get_db_cnx()
        cursor = db.cursor()

        # select codereview information from table codereview.
        cursor.execute("SELECT time, author, text, id, status, version FROM codereview " \
                       "WHERE time>=%s AND time<=%s ORDER BY time", (start, stop))
        for t, author, text, cr_id, status, version in cursor:

            # if a codereview status is -1 (no need to review), ignore it.
            if status == -1:
                continue

            # if a codereview status is 0 (completed), then do something.
            elif status == 0:
                title = Markup('CodeReview : [ <em>%s</em> ] completed by %s', cr_id, author)

            # if a codereview status is 1 (uncompleted). then ...
            elif version == 1:
                title = Markup('CodeReview : [ <em>%s</em> ] created by %s', cr_id, author)
            else:
                title = Markup('CodeReview : [ <em>%s</em> ] edited by %s', cr_id, author)
            href = "%s/%s" % (self.env.href.CodeReview(), cr_id)

            # call wiki_to_oneliner to render a html line from wiki page.
            text = wiki_to_oneliner(text, self.env, db, shorten=True)

            # the first returned value is a class of filters, the second is a linker to the content,
            # the third is the title, 4th is changed time, 5th is the author and 6th is the short description. 
            yield 'codereview', href, title, t, author, text

It looks like very easy.