Skip to content
Browse files

Refactored template tags to use a common Node class.

  • Loading branch information...
1 parent 215e1fc commit 51a9737107e4c79f740ca3182fe1da08d5f9d932 @jacobian jacobian committed May 9, 2011
Showing with 17 additions and 29 deletions.
  1. +11 −23 faq/templatetags/faqtags.py
  2. +6 −6 faq/tests/test_templatetags.py
View
34 faq/templatetags/faqtags.py
@@ -6,26 +6,27 @@
register = template.Library()
-class FaqsForTopicNode(template.Node):
- def __init__(self, num, topic, varname):
+class FaqListNode(template.Node):
+ def __init__(self, num, varname, topic=None):
self.num = template.Variable(num)
- self.topic = template.Variable(topic)
+ self.topic = template.Variable(topic) if topic else None
self.varname = varname
def render(self, context):
try:
num = self.num.resolve(context)
- topic = self.topic.resolve(context)
+ topic = self.topic.resolve(context) if self.topic else None
except template.VariableDoesNotExist:
return ''
-
+
if isinstance(topic, Topic):
qs = Question.objects.filter(topic=topic)
- else:
+ elif topic is not None:
qs = Question.objects.filter(topic__slug=topic)
-
+ else:
+ qs = Question.objects.all()
+
context[self.varname] = qs.filter(status=STATUS_ACTIVE)[:num]
-
return ''
@register.tag
@@ -45,21 +46,8 @@ def faqs_for_topic(parser, token):
if args[3] != 'as':
raise template.TemplateSyntaxError("third argument to the %s tag must be 'as'" % args[0])
- return FaqsForTopicNode(args[1], args[2], args[4])
+ return FaqListNode(num=args[1], topic=args[2], varname=args[4])
-class FaqNode(template.Node):
- def __init__(self, num, varname):
- self.num = template.Variable(num)
- self.varname = varname
-
- def render(self, context):
- try:
- num = self.num.resolve(context)
- except template.VariableDoesNotExist:
- return ''
-
- context[self.varname] = Question.objects.filter(status=STATUS_ACTIVE)[:num]
- return ''
@register.tag
def faq_list(parser, token):
@@ -77,4 +65,4 @@ def faq_list(parser, token):
if args[2] != 'as':
raise template.TemplateSyntaxError("second argument to the %s tag must be 'as'" % args[0])
- return FaqNode(args[1], args[3])
+ return FaqListNode(num=args[1], varname=args[3])
View
12 faq/tests/test_templatetags.py
@@ -78,7 +78,7 @@ class FAQTagsNodeTests(django.test.TestCase):
def test_faqs_for_topic_node(self):
context = template.Context()
- node = faqtags.FaqsForTopicNode('5', '"silly-questions"', "faqs")
+ node = faqtags.FaqListNode(num='5', topic='"silly-questions"', varname="faqs")
content = node.render(context)
self.assertEqual(content, "")
self.assertQuerysetEqual(context['faqs'],
@@ -91,22 +91,22 @@ def test_faqs_for_topic_node_variable_arguments(self):
"""
context = template.Context({'topic': Topic.objects.get(pk=1),
'number': 1})
- node = faqtags.FaqsForTopicNode('number', 'topic', "faqs")
+ node = faqtags.FaqListNode(num='number', topic='topic', varname="faqs")
content = node.render(context)
self.assertEqual(content, "")
self.assertQuerysetEqual(context['faqs'], ["<Question: What is your favorite color?>"])
def test_faqs_for_topic_node_invalid_variables(self):
context = template.Context()
- node = faqtags.FaqsForTopicNode('number', 'topic', "faqs")
+ node = faqtags.FaqListNode(num='number', topic='topic', varname="faqs")
content = node.render(context)
self.assertEqual(content, "")
self.assert_("faqs" not in context,
"faqs variable shouldn't have been added to the context.")
def test_faq_list_node(self):
context = template.Context()
- node = faqtags.FaqNode('5', "faqs")
+ node = faqtags.FaqListNode(num='5', varname="faqs")
content = node.render(context)
self.assertEqual(content, "")
self.assertQuerysetEqual(context['faqs'],
@@ -120,14 +120,14 @@ def test_faq_list_node_variable_arguments(self):
"""
context = template.Context({'topic': Topic.objects.get(pk=1),
'number': 1})
- node = faqtags.FaqNode('number', "faqs")
+ node = faqtags.FaqListNode(num='number', varname="faqs")
content = node.render(context)
self.assertEqual(content, "")
self.assertQuerysetEqual(context['faqs'], ["<Question: What is your favorite color?>"])
def test_faq_list_node_invalid_variables(self):
context = template.Context()
- node = faqtags.FaqNode('number', "faqs")
+ node = faqtags.FaqListNode(num='number', varname="faqs")
content = node.render(context)
self.assertEqual(content, "")
self.assert_("faqs" not in context,

0 comments on commit 51a9737

Please sign in to comment.
Something went wrong with that request. Please try again.