This repository has been archived by the owner on Sep 3, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import re | ||
|
||
from django import template | ||
from django.conf import settings | ||
from django.db import models | ||
|
||
Bookmark = models.get_model('bookmarks', 'bookmark') | ||
|
||
register = template.Library() | ||
|
||
class LatestBookmarks(template.Node): | ||
""" | ||
Get a list of the latest bookmarks. | ||
""" | ||
def __init__(self, limit, var_name): | ||
self.limit = limit | ||
self.var_name = var_name | ||
|
||
def render(self, context): | ||
bookmarks = Bookmark.objects.published()[:int(self.limit)] | ||
|
||
if (int(self.limit) == 1): | ||
context[self.var_name] = bookmarks[0] | ||
else: | ||
context[self.var_name] = bookmarks | ||
|
||
return '' | ||
|
||
@register.tag | ||
def get_latest_bookmarks(parser, token): | ||
""" | ||
Gets any number of latest bookmarks and stores them in a varable. | ||
Syntax:: | ||
{% get_latest_bookmarks [limit] as [var_name] %} | ||
Example usage:: | ||
{% get_latest_bookmarks 10 as latest_bookmark_list %} | ||
""" | ||
try: | ||
tag_name, arg = token.contents.split(None, 1) | ||
except ValueError: | ||
raise template.TeamplteSyntaxError, "%s tag requires arguments" % token.contents.split()[0] | ||
|
||
m = re.search(r'(.*?) as (\w+)', arg) | ||
|
||
if not m: | ||
raise template.TemplateSyntaxError, "%s tag had invalid arguments" % tag_name | ||
|
||
format_string, var_name = m.groups() | ||
|
||
return LatestBookmarks(format_string[0], var_name) |