summaryrefslogtreecommitdiff
path: root/patchwork/templatetags
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2015-05-24 16:57:33 +0800
committerJeremy Kerr <jk@ozlabs.org>2015-05-27 10:26:41 +0800
commitad2762cf775a8dde508de47164d6429f3fd724f1 (patch)
treee63015a468cfe32c961908f0338d423227799815 /patchwork/templatetags
parentf09e982f58384946111d4157fd2b7c2b31b78612 (diff)
downloadpatchwork-ad2762cf775a8dde508de47164d6429f3fd724f1.tar
patchwork-ad2762cf775a8dde508de47164d6429f3fd724f1.tar.gz
Move to a more recent django project structure
This change updates patchwor to the newer project struture: we've moved the actual application out of the apps/ directory, and the patchwork-specific templates to under the patchwork application. This gives us the manage.py script in the top-level now. Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'patchwork/templatetags')
-rw-r--r--patchwork/templatetags/__init__.py0
-rw-r--r--patchwork/templatetags/filter.py36
-rw-r--r--patchwork/templatetags/listurl.py136
-rw-r--r--patchwork/templatetags/order.py66
-rw-r--r--patchwork/templatetags/patch.py65
-rw-r--r--patchwork/templatetags/person.py43
-rw-r--r--patchwork/templatetags/pwurl.py76
-rw-r--r--patchwork/templatetags/syntax.py75
8 files changed, 497 insertions, 0 deletions
diff --git a/patchwork/templatetags/__init__.py b/patchwork/templatetags/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/patchwork/templatetags/__init__.py
diff --git a/patchwork/templatetags/filter.py b/patchwork/templatetags/filter.py
new file mode 100644
index 0000000..7a5d9df
--- /dev/null
+++ b/patchwork/templatetags/filter.py
@@ -0,0 +1,36 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+from django import template
+from django.utils.html import escape
+
+import re
+
+
+register = template.Library()
+
+@register.filter
+def personify(person):
+ if person.name:
+ linktext = escape(person.name)
+ else:
+ linktext = escape(person.email)
+
+ return '<a href="javascript:personpopup(\'%s\')">%s</a>' % (escape(person.email), linktext)
+
diff --git a/patchwork/templatetags/listurl.py b/patchwork/templatetags/listurl.py
new file mode 100644
index 0000000..5fe03e4
--- /dev/null
+++ b/patchwork/templatetags/listurl.py
@@ -0,0 +1,136 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+from django import template
+from django.utils.html import escape
+from django.utils.safestring import mark_safe
+from django.utils.encoding import smart_str
+from patchwork.filters import filterclasses
+from django.conf import settings
+from django.core.urlresolvers import reverse, NoReverseMatch
+import re
+
+register = template.Library()
+
+# params to preserve across views
+list_params = [ c.param for c in filterclasses ] + ['order', 'page']
+
+class ListURLNode(template.defaulttags.URLNode):
+ def __init__(self, kwargs):
+ super(ListURLNode, self).__init__(None, [], {}, False)
+ self.params = {}
+ for (k, v) in kwargs.iteritems():
+ if k in list_params:
+ self.params[k] = v
+
+ def render(self, context):
+ view_name = template.Variable('list_view.view').resolve(context)
+ kwargs = template.Variable('list_view.view_params') \
+ .resolve(context)
+
+ str = None
+ try:
+ str = reverse(view_name, args=[], kwargs=kwargs)
+ except NoReverseMatch:
+ try:
+ project_name = settings.SETTINGS_MODULE.split('.')[0]
+ str = reverse(project_name + '.' + view_name,
+ args=[], kwargs=kwargs)
+ except NoReverseMatch:
+ raise
+
+ if str is None:
+ return ''
+
+ params = []
+ try:
+ qs_var = template.Variable('list_view.params')
+ params = dict(qs_var.resolve(context))
+ except Exception:
+ pass
+
+ for (k, v) in self.params.iteritems():
+ params[smart_str(k,'ascii')] = v.resolve(context)
+
+ if not params:
+ return str
+
+ return str + '?' + '&'.join(['%s=%s' % (k, escape(v)) \
+ for (k, v) in params.iteritems()])
+
+@register.tag
+def listurl(parser, token):
+ bits = token.contents.split(' ', 1)
+ if len(bits) < 1:
+ raise TemplateSyntaxError("'%s' takes at least one argument"
+ " (path to a view)" % bits[0])
+ kwargs = {}
+ if len(bits) > 1:
+ for arg in bits[1].split(','):
+ if '=' in arg:
+ k, v = arg.split('=', 1)
+ k = k.strip()
+ kwargs[k] = parser.compile_filter(v)
+ else:
+ raise TemplateSyntaxError("'%s' requires name=value params" \
+ % bits[0])
+ return ListURLNode(kwargs)
+
+class ListFieldsNode(template.Node):
+ def __init__(self, params):
+ self.params = params
+
+ def render(self, context):
+ self.view_name = template.Variable('list_view.view').resolve(context)
+ try:
+ qs_var = template.Variable('list_view.params')
+ params = dict(qs_var.resolve(context))
+ except Exception:
+ pass
+
+ params.update(self.params)
+
+ if not params:
+ return ''
+
+ str = ''
+ for (k, v) in params.iteritems():
+ str += '<input type="hidden" name="%s" value="%s"\>' % \
+ (k, escape(v))
+
+ return mark_safe(str)
+
+@register.tag
+def listfields(parser, token):
+ bits = token.contents.split(' ', 1)
+ if len(bits) < 1:
+ raise TemplateSyntaxError("'%s' takes at least one argument"
+ " (path to a view)" % bits[0])
+ params = {}
+ if len(bits) > 2:
+ for arg in bits[2].split(','):
+ if '=' in arg:
+ k, v = arg.split('=', 1)
+ k = k.strip()
+ params[k] = parser.compile_filter(v)
+ else:
+ raise TemplateSyntaxError("'%s' requires name=value params" \
+ % bits[0])
+ return ListFieldsNode(bits[1], params)
+
diff --git a/patchwork/templatetags/order.py b/patchwork/templatetags/order.py
new file mode 100644
index 0000000..e392f03
--- /dev/null
+++ b/patchwork/templatetags/order.py
@@ -0,0 +1,66 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+from django import template
+import re
+
+register = template.Library()
+
+@register.tag(name = 'ifpatcheditable')
+def do_patch_is_editable(parser, token):
+ try:
+ tag_name, name, cur_order = token.split_contents()
+ except ValueError:
+ raise template.TemplateSyntaxError("%r tag requires two arguments" \
+ % token.contents.split()[0])
+
+ end_tag = 'endifpatcheditable'
+ nodelist_true = parser.parse([end_tag, 'else'])
+
+ token = parser.next_token()
+ if token.contents == 'else':
+ nodelist_false = parser.parse([end_tag])
+ parser.delete_first_token()
+ else:
+ nodelist_false = template.NodeList()
+
+ return EditablePatchNode(patch_var, nodelist_true, nodelist_false)
+
+class EditablePatchNode(template.Node):
+ def __init__(self, patch_var, nodelist_true, nodelist_false):
+ self.nodelist_true = nodelist_true
+ self.nodelist_false = nodelist_false
+ self.patch_var = template.Variable(patch_var)
+ self.user_var = template.Variable('user')
+
+ def render(self, context):
+ try:
+ patch = self.patch_var.resolve(context)
+ user = self.user_var.resolve(context)
+ except template.VariableDoesNotExist:
+ return ''
+
+ if not user.is_authenticated():
+ return self.nodelist_false.render(context)
+
+ if not patch.is_editable(user):
+ return self.nodelist_false.render(context)
+
+ return self.nodelist_true.render(context)
diff --git a/patchwork/templatetags/patch.py b/patchwork/templatetags/patch.py
new file mode 100644
index 0000000..bec0cab
--- /dev/null
+++ b/patchwork/templatetags/patch.py
@@ -0,0 +1,65 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+from django import template
+import re
+
+register = template.Library()
+
+@register.tag(name = 'ifpatcheditable')
+def do_patch_is_editable(parser, token):
+ try:
+ tag_name, patch_var = token.split_contents()
+ except ValueError:
+ raise template.TemplateSyntaxError("%r tag requires one argument" \
+ % token.contents.split()[0])
+
+ end_tag = 'endifpatcheditable'
+ nodelist_true = parser.parse([end_tag, 'else'])
+
+ token = parser.next_token()
+ if token.contents == 'else':
+ nodelist_false = parser.parse([end_tag])
+ parser.delete_first_token()
+ else:
+ nodelist_false = template.NodeList()
+
+ return EditablePatchNode(patch_var, nodelist_true, nodelist_false)
+
+class EditablePatchNode(template.Node):
+ def __init__(self, patch_var, nodelist_true, nodelist_false):
+ self.nodelist_true = nodelist_true
+ self.nodelist_false = nodelist_false
+ self.patch_var = template.Variable(patch_var)
+ self.user_var = template.Variable('user')
+
+ def render(self, context):
+ try:
+ patch = self.patch_var.resolve(context)
+ user = self.user_var.resolve(context)
+ except template.VariableDoesNotExist:
+ return ''
+
+ if not user.is_authenticated():
+ return self.nodelist_false.render(context)
+
+ if not patch.is_editable(user):
+ return self.nodelist_false.render(context)
+
+ return self.nodelist_true.render(context)
diff --git a/patchwork/templatetags/person.py b/patchwork/templatetags/person.py
new file mode 100644
index 0000000..c337c74
--- /dev/null
+++ b/patchwork/templatetags/person.py
@@ -0,0 +1,43 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+from django import template
+from django.utils.html import escape
+from django.utils.safestring import mark_safe
+from django.core.urlresolvers import reverse
+from patchwork.filters import SubmitterFilter
+import re
+
+register = template.Library()
+
+@register.filter
+def personify(person, project):
+
+ if person.name:
+ linktext = escape(person.name)
+ else:
+ linktext = escape(person.email)
+
+ url = reverse('patchwork.views.patch.list', kwargs = {'project_id' : project.linkname})
+ str = '<a href="%s?%s=%s">%s</a>' % \
+ (url, SubmitterFilter.param, escape(person.id), linktext)
+
+ return mark_safe(str)
+
+
diff --git a/patchwork/templatetags/pwurl.py b/patchwork/templatetags/pwurl.py
new file mode 100644
index 0000000..98bc1ca
--- /dev/null
+++ b/patchwork/templatetags/pwurl.py
@@ -0,0 +1,76 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+from django import template
+from django.utils.html import escape
+from django.utils.safestring import mark_safe
+from patchwork.filters import filterclasses
+import re
+
+register = template.Library()
+
+# params to preserve across views
+list_params = [ c.param for c in filterclasses ] + ['order', 'page']
+
+class ListURLNode(template.defaulttags.URLNode):
+ def __init__(self, *args, **kwargs):
+ super(ListURLNode, self).__init__(*args, **kwargs)
+ self.params = {}
+ for (k, v) in kwargs:
+ if k in list_params:
+ self.params[k] = v
+
+ def render(self, context):
+ self.view_name = template.Variable('list_view.view')
+ str = super(ListURLNode, self).render(context)
+ if str == '':
+ return str
+ params = []
+ try:
+ qs_var = template.Variable('list_view.params')
+ params = dict(qs_var.resolve(context))
+ except Exception:
+ pass
+
+ params.update(self.params)
+
+ if not params:
+ return str
+
+ return str + '?' + '&'.join(['%s=%s' % (k, escape(v)) \
+ for (k, v) in params.iteritems()])
+
+@register.tag
+def listurl(parser, token):
+ bits = token.contents.split(' ', 1)
+ if len(bits) < 1:
+ raise TemplateSyntaxError("'%s' takes at least one argument"
+ " (path to a view)" % bits[0])
+ args = ['']
+ kwargs = {}
+ if len(bits) > 1:
+ for arg in bits[2].split(','):
+ if '=' in arg:
+ k, v = arg.split('=', 1)
+ k = k.strip()
+ kwargs[k] = parser.compile_filter(v)
+ else:
+ args.append(parser.compile_filter(arg))
+ return PatchworkURLNode(bits[1], args, kwargs)
+
diff --git a/patchwork/templatetags/syntax.py b/patchwork/templatetags/syntax.py
new file mode 100644
index 0000000..abdbb4d
--- /dev/null
+++ b/patchwork/templatetags/syntax.py
@@ -0,0 +1,75 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+from django import template
+from django.utils.html import escape
+from django.utils.safestring import mark_safe
+import re
+
+register = template.Library()
+
+def _compile(t):
+ (r, str) = t
+ return (re.compile(r, re.M | re.I), str)
+
+_patch_span_res = map(_compile, [
+ ('^(Index:?|diff|\-\-\-|\+\+\+|\*\*\*) .*$', 'p_header'),
+ ('^\+.*$', 'p_add'),
+ ('^-.*$', 'p_del'),
+ ('^!.*$', 'p_mod'),
+ ])
+
+_patch_chunk_re = \
+ re.compile('^(@@ \-\d+(?:,\d+)? \+\d+(?:,\d+)? @@)(.*)$', re.M | re.I)
+
+_comment_span_res = map(_compile, [
+ ('^\s*Signed-off-by: .*$', 'signed-off-by'),
+ ('^\s*Acked-by: .*$', 'acked-by'),
+ ('^\s*Nacked-by: .*$', 'nacked-by'),
+ ('^\s*Tested-by: .*$', 'tested-by'),
+ ('^\s*Reviewed-by: .*$', 'reviewed-by'),
+ ('^\s*From: .*$', 'from'),
+ ('^\s*&gt;.*$', 'quote'),
+ ])
+
+_span = '<span class="%s">%s</span>'
+
+@register.filter
+def patchsyntax(patch):
+ content = escape(patch.content)
+
+ for (r,cls) in _patch_span_res:
+ content = r.sub(lambda x: _span % (cls, x.group(0)), content)
+
+ content = _patch_chunk_re.sub( \
+ lambda x: \
+ _span % ('p_chunk', x.group(1)) + ' ' + \
+ _span % ('p_context', x.group(2)), \
+ content)
+
+ return mark_safe(content)
+
+@register.filter
+def commentsyntax(comment):
+ content = escape(comment.content)
+
+ for (r,cls) in _comment_span_res:
+ content = r.sub(lambda x: _span % (cls, x.group(0)), content)
+
+ return mark_safe(content)