This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
historytrail.py
48 lines (40 loc) · 1.87 KB
/
historytrail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from django import template
from django.db import models
from django.db.models import get_model
register = template.Library()
class HistoryTrailNode(template.Node):
"""
Prints a Version History Trail for the given Model.
The HTML is currently harcoded (see below), which
should be changed in a later version.
Usage:
{% historytrail object %}
or to only show links to the last 5 revisions:
{% historytrail object 5 %}
"""
def __init__(self, model, count=0):
self.model = model
self.count = int(count)
def render(self, context):
self.instance = template.resolve_variable(self.model, context)
revs = self.instance.get_changed_revisions()
out = ""
if self.count > 0:
revs = revs[:self.count]
for c, rev in enumerate(revs):
if c > 0:
out += '<a rel="nofollow" href="%sdiff/%s/%s/">←</a> <a rel="nofollow" href="%srev/%s/">[%s]</a> ' %(self.instance.get_absolute_url(),rev,revs[c-1],self.instance.get_absolute_url(),rev,rev)
else:
out += '<a rel="nofollow" href="%s">head</a> <a rel="nofollow" href="%sdiff/%s/head/">←</a> <a rel="nofollow" href="%srev/%s/">[%s]</a> ' % (self.instance.get_absolute_url(), self.instance.get_absolute_url(), rev, self.instance.get_absolute_url(), rev, rev)
return out
def historytrail(parser, token):
bits = token.contents.split()
if len(bits) < 2:
raise template.TemplateSyntaxError, "historytrail tag takes at least one argument"
if len(bits) > 3:
raise template.TemplateSyntaxError, "historytrail tag takes at most two arguments"
if len(bits) == 2:
return HistoryTrailNode(bits[1])
if len(bits) == 3:
return HistoryTrailNode(bits[1], count=bits[2])
historytrail = register.tag(historytrail)