Remove follow tag #89

Closed
wants to merge 3 commits into
from
@@ -1,7 +1,5 @@
-from django.template import Variable, Library, Node, TemplateSyntaxError,\
- VariableDoesNotExist
+from django.template import Variable, Library, Node, TemplateSyntaxError
from django.template.loader import render_to_string
-from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
@@ -10,34 +8,20 @@
register = Library()
-def _is_following_helper(context, actor):
- return Follow.objects.is_following(context.get('user'), actor)
-
-class DisplayActivityFollowLabel(Node):
- def __init__(self, actor, follow, unfollow):
- self.actor = Variable(actor)
- self.follow = follow
- self.unfollow = unfollow
-
- def render(self, context):
- actor_instance = self.actor.resolve(context)
- if _is_following_helper(context, actor_instance):
- return self.follow
- return self.unfollow
-
class DisplayActivityFollowUrl(Node):
def __init__(self, actor):
self.actor = Variable(actor)
def render(self, context):
actor_instance = self.actor.resolve(context)
content_type = ContentType.objects.get_for_model(actor_instance).pk
- if _is_following_helper(context, actor_instance):
+ if Follow.objects.is_following(context.get('user'), actor_instance):
return reverse('actstream_unfollow', kwargs={
'content_type_id': content_type, 'object_id': actor_instance.pk})
return reverse('actstream_follow', kwargs={
'content_type_id': content_type, 'object_id': actor_instance.pk})
+
class DisplayActivityActorUrl(Node):
def __init__(self, actor):
self.actor = Variable(actor)
@@ -48,6 +32,7 @@ def render(self, context):
return reverse('actstream_actor', kwargs={
'content_type_id': content_type, 'object_id': actor_instance.pk})
+
class AsNode(Node):
"""
Base template Node class for template tags that takes a predefined number
@@ -90,6 +75,7 @@ def render(self, context):
def render_result(self, context):
raise NotImplementedError("Must be implemented by a subclass")
+
class DisplayAction(AsNode):
def render_result(self, context):
@@ -101,6 +87,7 @@ def render_result(self, context):
return render_to_string(templates, {'action': action_instance},
context)
+
def display_action(parser, token):
"""
Renders the template for the action description
@@ -111,13 +98,14 @@ def display_action(parser, token):
"""
return DisplayAction.handle_token(parser, token)
+
def is_following(user, actor):
"""
- Returns true if the given request.user is following the actor
+ Returns true if the given user is following the actor
Example::
- {% if request.user|is_following:another_user %}
+ {% if user|is_following:another_user %}
You are already following {{ another_user }}
{% endif %}
"""
@@ -130,49 +118,40 @@ def follow_url(parser, token):
Example::
- <a href="{% follow_url request.user %}">{% follow_label request.user 'stop following' 'follow' %}</a>
+ <a href="{% follow_url other_user %}">
+ {% if user|is_following:other_user %}
+ stop following
+ {% else %}
+ follow
+ {% endif %}
+ </a>
"""
bits = token.split_contents()
if len(bits) != 2:
- raise TemplateSyntaxError, "Accepted format {% follow_url [instance] %}"
- else:
- return DisplayActivityFollowUrl(bits[1])
+ raise TemplateSyntaxError("Accepted format "
+ "{% follow_url [instance] %}")
+ return DisplayActivityFollowUrl(bits[1])
-def follow_label(parser, token):
- """
- Renders the label for following/unfollowing for a particular actor instance
-
- Example::
-
- <a href="{% follow_url request.user %}">{% follow_label request.user 'stop following' 'follow' %}</a>
-
- """
- bits = token.split_contents()
- if len(bits) != 4:
- raise TemplateSyntaxError, "Accepted format {% follow_label [instance] [follow_string] [unfollow_string] %}"
- else:
- return DisplayActivityFollowLabel(*bits[1:])
-
def actor_url(parser, token):
"""
Renders the URL for a particular actor instance
Example::
- <a href="{% actor_url request.user %}">View your actions</a>
+ <a href="{% actor_url user %}">View your actions</a>
<a href="{% actor_url another_user %}">{{ another_user }}'s actions</a>
"""
bits = token.split_contents()
if len(bits) != 4:
- raise TemplateSyntaxError, "Accepted format {% actor_url [actor_instance] %}"
- else:
- return DisplayActivityActorUrl(*bits[1:])
+ raise TemplateSyntaxError("Accepted format "
+ "{% actor_url [actor_instance] %}")
+ return DisplayActivityActorUrl(*bits[1:])
+
register.filter(is_following)
register.tag(display_action)
register.tag(follow_url)
-register.tag(follow_label)
register.tag(actor_url)
View
@@ -189,28 +189,6 @@ def test_tag_follow_url(self):
ct = ContentType.objects.get_for_model(User)
self.assertEqual(output, '/follow/%s/%s/' % (ct.pk, self.user1.pk))
- def test_tag_follow_label(self):
- src = '{% load activity_tags %}'\
- '{% follow_label other_user yup nope %}'
-
- # Anonymous.
- output = Template(src).render(Context({'other_user': self.user1}))
- self.assertEqual(output, 'nope')
-
- output = Template(src).render(Context({'user': AnonymousUser(),
- 'other_user': self.user1}))
- self.assertEqual(output, 'nope')
-
- # Non follower (user2 does not follow user1).
- output = Template(src).render(Context({'user': self.user2,
- 'other_user': self.user1}))
- self.assertEqual(output, 'nope')
-
- # Follower (user1 follows user2).
- output = Template(src).render(Context({'user': self.user1,
- 'other_user': self.user2}))
- self.assertEqual(output, 'yup')
-
def test_model_actions_with_kwargs(self):
"""
Testing the model_actions method of the ActionManager
View
@@ -35,4 +35,4 @@ Templatetags
-------------
.. automodule:: actstream.templatetags.activity_tags
- :members: is_following,display_action,follow_url,follow_label,actor_url
+ :members: is_following,display_action,follow_url,actor_url