Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added an {% if_relationship ... %} templatetag, a couple template

filters, and tests for them
  • Loading branch information...
commit f45708dc8d25556bfb31fe97d4ccbb471ecd5706 1 parent 026154a
@coleifer authored
View
3  TODO.rst
@@ -1,4 +1,5 @@
Todo
====
-* write tests
+* better templates
+* invitations?
View
0  relationships/templatetags/__init__.py
No changes.
View
102 relationships/templatetags/relationship_tags.py
@@ -0,0 +1,102 @@
+import re
+from django import template
+from django.core.urlresolvers import reverse
+from django.db.models import Q
+
+from relationships.models import Relationship, RelationshipStatus
+
+register = template.Library()
+
+
+class IfRelationshipNode(template.Node):
+ def __init__(self, nodelist_true, nodelist_false, *args):
+ self.nodelist_true = nodelist_true
+ self.nodelist_false = nodelist_false
+ self.from_user, self.to_user, self.status = args
+ self.status = self.status.replace('"', '') # strip quotes
+
+ def render(self, context):
+ from_user = template.resolve_variable(self.from_user, context)
+ to_user = template.resolve_variable(self.to_user, context)
+
+ try:
+ status = RelationshipStatus.objects.get(
+ Q(from_slug=self.status) |
+ Q(to_slug=self.status) |
+ Q(symmetrical_slug=self.status))
+ except RelationshipStatus.DoesNotExist:
+ raise template.TemplateSyntaxError('RelationshipStatus not found')
+
+ if status.from_slug == self.status:
+ val = from_user.relationships.exists(to_user, status)
+ elif status.to_slug == self.status:
+ val = to_user.relationships.exists(from_user, status)
+ else:
+ val = from_user.relationships.symmetrical_exists(to_user, status)
+
+ if val:
+ return self.nodelist_true.render(context)
+ return self.nodelist_false.render(context)
+
+@register.tag
+def if_relationship(parser, token):
+ """
+ Determine if a certain type of relationship exists between two users.
+ The ``status`` parameter must be a slug matching either the from_slug,
+ to_slug or symmetrical_slug of a RelationshipStatus.
+
+ Example::
+
+ {% if_relationship from_user to_user "friends" %}
+ Here are pictures of me drinking alcohol
+ {% else %}
+ Sorry coworkers
+ {% endif_relationship %}
+
+ {% if_relationship from_user to_user "blocking" %}
+ damn seo experts
+ {% endif_relationship %}
+ """
+ bits = list(token.split_contents())
+ if len(bits) != 4:
+ raise TemplateSyntaxError, "%r takes 3 arguments:\n" % \
+ (bits[0], if_relationship.__doc__)
+ end_tag = 'end' + bits[0]
+ nodelist_true = parser.parse(('else', end_tag))
+ token = parser.next_token()
+ if token.contents == 'else':
+ nodelist_false = parser.parse((end_tag,))
+ parser.delete_first_token()
+ else:
+ nodelist_false = template.NodeList()
+ return IfRelationshipNode(nodelist_true, nodelist_false, *bits[1:])
+
+
+@register.filter
+def add_relationship_url(user, status):
+ """
+ Generate a url for adding a relationship on a given user. ``user`` is a
+ User object, and ``status`` is either a relationship_status object or a
+ string matching the ``from_slug`` of a RelationshipStatus
+
+ Usage:
+ href="{{ user|add_relationship_url:"following" }}"
+ """
+ if isinstance(status, RelationshipStatus):
+ status = status.from_slug
+ return reverse('relationship_add', args=[user.username, status])
+
+
+@register.filter
+def remove_relationship_url(user, status):
+ """
+ Generate a url for removing a relationship on a given user. ``user`` is a
+ User object, and ``status`` is either a relationship_status object or a
+ string matching the ``from_slug`` of a RelationshipStatus
+
+ Usage:
+ href="{{ user|remove_relationship_url:"following" }}"
+ """
+ if isinstance(status, RelationshipStatus):
+ status = status.from_slug
+ return reverse('relationship_remove', args=[user.username, status])
View
65 relationships/tests.py
@@ -1,6 +1,7 @@
from django.test import TestCase
from django.contrib.auth.models import User
-from relationships.models import Relationship
+from django.template import Template, Context
+from relationships.models import Relationship, RelationshipStatus
class RelationshipsTestCase(TestCase):
fixtures = ['relationships.json']
@@ -208,8 +209,6 @@ def test_list_views(self):
resp = self.client.get('/relationships/John/walrus-friends/')
self.assertEqual(resp.status_code, 404)
-
-
def test_add_remove_views(self):
# login required
resp = self.client.get('/relationships/add/The_Walrus/following/')
@@ -235,3 +234,63 @@ def test_add_remove_views(self):
resp = self.client.post('/relationships/add/Nobody/following/')
self.assertEqual(resp.status_code, 404)
+
+
+class RelationshipsTagsTestCase(TestCase):
+ fixtures = ['relationships.json']
+
+ def setUp(self):
+ self.walrus = User.objects.get(username='The_Walrus')
+ self.john = User.objects.get(username='John')
+ self.paul = User.objects.get(username='Paul')
+ self.yoko = User.objects.get(username='Yoko')
+ self.following = RelationshipStatus.objects.get(from_slug='following')
+ self.blocking = RelationshipStatus.objects.get(from_slug='blocking')
+
+ def test_add_url_filter(self):
+ t = Template('{% load relationship_tags %}{{ user|add_relationship_url:"following" }}')
+ c = Context({'user': self.paul})
+ rendered = t.render(c)
+ self.assertEqual(rendered, '/relationships/add/Paul/following/')
+
+ t = Template('{% load relationship_tags %}{{ user|add_relationship_url:blocking }}')
+ c = Context({'user': self.paul, 'blocking': self.blocking})
+ rendered = t.render(c)
+ self.assertEqual(rendered, '/relationships/add/Paul/blocking/')
+
+ def test_remove_url_filter(self):
+ t = Template('{% load relationship_tags %}{{ user|remove_relationship_url:"following" }}')
+ c = Context({'user': self.paul})
+ rendered = t.render(c)
+ self.assertEqual(rendered, '/relationships/remove/Paul/following/')
+
+ t = Template('{% load relationship_tags %}{{ user|remove_relationship_url:blocking }}')
+ c = Context({'user': self.paul, 'blocking': self.blocking})
+ rendered = t.render(c)
+ self.assertEqual(rendered, '/relationships/remove/Paul/blocking/')
+
+ def test_if_relationship_tag(self):
+ t = Template('{% load relationship_tags %}{% if_relationship john paul "following" %}y{% else %}n{% endif_relationship %}')
+ c = Context({'john': self.john, 'paul': self.paul})
+ rendered = t.render(c)
+ self.assertEqual(rendered, 'y')
+
+ t = Template('{% load relationship_tags %}{% if_relationship paul john "following" %}y{% else %}n{% endif_relationship %}')
+ c = Context({'john': self.john, 'paul': self.paul})
+ rendered = t.render(c)
+ self.assertEqual(rendered, 'n')
+
+ t = Template('{% load relationship_tags %}{% if_relationship paul john "followers" %}y{% else %}n{% endif_relationship %}')
+ c = Context({'john': self.john, 'paul': self.paul})
+ rendered = t.render(c)
+ self.assertEqual(rendered, 'y')
+
+ t = Template('{% load relationship_tags %}{% if_relationship paul john "friends" %}y{% else %}n{% endif_relationship %}')
+ c = Context({'john': self.john, 'paul': self.paul})
+ rendered = t.render(c)
+ self.assertEqual(rendered, 'n')
+
+ t = Template('{% load relationship_tags %}{% if_relationship john yoko "friends" %}y{% else %}n{% endif_relationship %}')
+ c = Context({'john': self.john, 'yoko': self.yoko})
+ rendered = t.render(c)
+ self.assertEqual(rendered, 'y')
Please sign in to comment.
Something went wrong with that request. Please try again.