Requires GitPython.
Grabs the most recent commits to a Git repository, as defined in settings.py. Inspired by Simon Willison's about page.
Example:
{% get_recent_commits 10 as commits %}
<ul>
{% for commit in commits reversed %}
<li>{{ commit.commited_date }} - {{ commit.message }}</li>
{% endfor %}
</ul>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | from git import *
from django.conf import settings
from django.template import Library, Node
register = Library()
class LatestCommitsNode(Node):
def __init__(self, num, varname):
self.num, self.varname = num, varname
def render(self, context):
repo = Repo(settings.REPO)
context[self.varname] = repo.commits()[-self.num:]
return ''
@register.tag
def get_recent_commits(parser, token):
bits = token.contents.split()
if len(bits) != 4:
raise TemplateSyntaxError, "get_recent_commits tag takes exactly three arguments"
if bits[2] != 'as':
raise TemplateSyntaxError, "second argument to get_recent_commits tag must be 'as'"
return LatestCommitsNode(int(bits[1]), bits[3])
|
More like this
- New Snippet! by Antoliny0919 4 days, 21 hours ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 2 months, 3 weeks ago
- get_object_or_none by azwdevops 6 months, 2 weeks ago
- Mask sensitive data from logger by agusmakmun 8 months, 1 week ago
- Template tag - list punctuation for a list of items by shapiromatron 1 year, 10 months ago
Comments
Please login first before commenting.