From e5e5632fb3432e153d0677b4bf145bc2eb72ef65 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Sat, 18 Oct 2014 15:25:30 -0500 Subject: Break out Jinja2-specific template helpers Signed-off-by: Dan McGee --- packages/templatetags/jinja2.py | 98 +++++++++++++++++++++++++++++++++ packages/templatetags/package_extras.py | 95 +------------------------------- 2 files changed, 99 insertions(+), 94 deletions(-) create mode 100644 packages/templatetags/jinja2.py (limited to 'packages') diff --git a/packages/templatetags/jinja2.py b/packages/templatetags/jinja2.py new file mode 100644 index 00000000..263fc156 --- /dev/null +++ b/packages/templatetags/jinja2.py @@ -0,0 +1,98 @@ +from urllib import urlencode, quote as urlquote, unquote +from django.utils.html import escape +from django_jinja import library +from main.templatetags import pgp + + +@library.filter +def url_unquote(original_url): + try: + url = original_url + if isinstance(url, unicode): + url = url.encode('ascii') + url = unquote(url).decode('utf-8') + return url + except UnicodeError: + return original_url + + +def link_encode(url, query): + # massage the data into all utf-8 encoded strings first, so urlencode + # doesn't barf at the data we pass it + query = {k: unicode(v).encode('utf-8') for k, v in query.items()} + data = urlencode(query) + return "%s?%s" % (url, data) + + +@library.global_function +def maintainer_link(user): + if user: + # TODO don't hardcode + title = escape('View packages maintained by ' + user.get_full_name()) + return '%s' % ( + user.username, + title, + user.get_full_name(), + ) + return '' + + +@library.global_function +def packager_link(user): + if user: + # TODO don't hardcode + title = escape('View packages packaged by ' + user.get_full_name()) + return '%s' % ( + user.username, + title, + user.get_full_name(), + ) + return '' + + +@library.global_function +def pgp_key_link(key_id, link_text=None): + return pgp.pgp_key_link(key_id, link_text) + + +@library.global_function +def scm_link(package, operation): + parts = (package.repo.svn_root, operation, package.pkgbase) + linkbase = ( + "https://projects.archlinux.org/svntogit/%s.git/%s/trunk?" + "h=packages/%s") + return linkbase % tuple(urlquote(part.encode('utf-8')) for part in parts) + + +@library.global_function +def wiki_link(package): + url = "https://wiki.archlinux.org/index.php/Special:Search" + data = { + 'search': package.pkgname, + } + return link_encode(url, data) + + +@library.global_function +def bugs_list(package): + url = "https://bugs.archlinux.org/" + data = { + 'project': package.repo.bugs_project, + 'cat[]': package.repo.bugs_category, + 'string': package.pkgname, + } + return link_encode(url, data) + + +@library.global_function +def bug_report(package): + url = "https://bugs.archlinux.org/newtask" + data = { + 'project': package.repo.bugs_project, + 'product_category': package.repo.bugs_category, + 'item_summary': '[%s] PLEASE ENTER SUMMARY' % package.pkgname, + } + return link_encode(url, data) + + +# vim: set ts=4 sw=4 et: diff --git a/packages/templatetags/package_extras.py b/packages/templatetags/package_extras.py index f6d6ee73..73a39092 100644 --- a/packages/templatetags/package_extras.py +++ b/packages/templatetags/package_extras.py @@ -1,38 +1,15 @@ -from urllib import urlencode, quote as urlquote, unquote +from urllib import urlencode try: from urlparse import parse_qs except ImportError: from cgi import parse_qs from django import template -from django.utils.html import escape -from django_jinja import library -from main.templatetags import pgp register = template.Library() -def link_encode(url, query): - # massage the data into all utf-8 encoded strings first, so urlencode - # doesn't barf at the data we pass it - query = {k: unicode(v).encode('utf-8') for k, v in query.items()} - data = urlencode(query) - return "%s?%s" % (url, data) - - -@library.filter -def url_unquote(original_url): - try: - url = original_url - if isinstance(url, unicode): - url = url.encode('ascii') - url = unquote(url).decode('utf-8') - return url - except UnicodeError: - return original_url - - class BuildQueryStringNode(template.Node): def __init__(self, sortfield): self.sortfield = sortfield @@ -82,74 +59,4 @@ def pkg_details_link(pkg, link_title=None, honor_flagged=False): return link % (pkg.get_absolute_url(), pkg.pkgname, link_content) -@library.global_function -def maintainer_link(user): - if user: - # TODO don't hardcode - title = escape('View packages maintained by ' + user.get_full_name()) - return '%s' % ( - user.username, - title, - user.get_full_name(), - ) - return '' - - -@library.global_function -def packager_link(user): - if user: - # TODO don't hardcode - title = escape('View packages packaged by ' + user.get_full_name()) - return '%s' % ( - user.username, - title, - user.get_full_name(), - ) - return '' - - -@library.global_function -def pgp_key_link(key_id, link_text=None): - return pgp.pgp_key_link(key_id, link_text) - - -@library.global_function -def scm_link(package, operation): - parts = (package.repo.svn_root, operation, package.pkgbase) - linkbase = ( - "https://projects.archlinux.org/svntogit/%s.git/%s/trunk?" - "h=packages/%s") - return linkbase % tuple(urlquote(part.encode('utf-8')) for part in parts) - - -@library.global_function -def wiki_link(package): - url = "https://wiki.archlinux.org/index.php/Special:Search" - data = { - 'search': package.pkgname, - } - return link_encode(url, data) - - -@library.global_function -def bugs_list(package): - url = "https://bugs.archlinux.org/" - data = { - 'project': package.repo.bugs_project, - 'cat[]': package.repo.bugs_category, - 'string': package.pkgname, - } - return link_encode(url, data) - - -@library.global_function -def bug_report(package): - url = "https://bugs.archlinux.org/newtask" - data = { - 'project': package.repo.bugs_project, - 'product_category': package.repo.bugs_category, - 'item_summary': '[%s] PLEASE ENTER SUMMARY' % package.pkgname, - } - return link_encode(url, data) - # vim: set ts=4 sw=4 et: -- cgit v1.2.3-55-g3dc8