Permalink
Browse files

Merge branches 'render_like', 'default-model-list' and 'anonymous-users'

* render_like:
  Add a {% render_like %} template tag

* default-model-list:
  Like list tag defaults to all registerd models

* anonymous-users:
  Include the user in the phileo_widget context
  Handle anonymous users by not showing the button
  • Loading branch information...
Tim Heap
Tim Heap committed Nov 29, 2011
3 parents 21a83aa + a2ab9b7 + 2bca3dc commit 085b0e455c891579d156990f5c3a1cb49a162132
Showing with 39 additions and 19 deletions.
  1. +3 −1 phileo/templates/phileo/_widget.html
  2. +28 −10 phileo/templatetags/phileo_tags.py
  3. +7 −0 phileo/utils.py
  4. +1 −8 phileo/views.py
@@ -1,7 +1,9 @@
{% block phileo_widget_form %}
<form class="phileo {{ is_liked }} {{ like_type }}" action="{{ like_url }}" method="POST" id="{{ widget_id }}">{% csrf_token %}
{% block phileo_widget_button %}
- <input type="submit" class="phileo-toggle" value="Like" />
+ {% if user.is_authenticated %}
+ <input type="submit" class="phileo-toggle" value="Like" />
+ {% endif %}
{% endblock phileo_widget_button %}
{% block phileo_widget_count %}
<span class="phileo-count">
@@ -6,6 +6,7 @@
from django.contrib.contenttypes.models import ContentType
from phileo.models import Like
+from phileo.utils import _allowed, LIKABLE_MODELS
register = template.Library()
@@ -14,20 +15,31 @@ class LikesNode(template.Node):
def __init__(self, user, model_list, varname):
self.user = template.Variable(user)
+
+ # Default to all the registered models
+ if len(model_list) == 0:
+ # These need to look like strings, otherwise they will be treated as variables
+ # when they are `resolve()`d later
+ model_list = ['"%s"' % model for model in LIKABLE_MODELS]
+
self.model_list = [template.Variable(m) for m in model_list]
+
self.varname = varname
def render(self, context):
user = self.user.resolve(context)
content_types = []
+
for model_name in self.model_list:
app, model = model_name.resolve(context).split(".")
content_type = ContentType.objects.get(app_label=app, model__iexact=model)
content_types.append(content_type)
+
context[self.varname] = Like.objects.filter(
sender=user,
receiver_content_type__in=content_types
)
+
return ""
@@ -40,6 +52,7 @@ def likes(parser, token):
user = tokens[1]
varname = tokens[-1]
model_list = tokens[2:-2]
+
return LikesNode(user, model_list, varname)
@@ -121,22 +134,27 @@ def phileo_widget(user, obj, widget_id=None, like_type="like", toggle_class="phi
receiver_object_id = obj.pk
).count()
- liked = user.liking.filter(
- receiver_content_type = ct,
- receiver_object_id = obj.pk
- ).exists()
-
if widget_id == None:
widget_id = "phileo_%s_%s_%s" % (like_type, ct.pk, obj.pk)
like_count_id = "%s_count" % widget_id
-
- like_url = reverse("phileo_like_toggle", kwargs={
- "content_type_id": ct.pk,
- "object_id": obj.pk
- })
+
+ if user.is_anonymous():
+ liked = False
+ like_url = settings.LOGIN_URL
+ else:
+ like_url = reverse("phileo_like_toggle", kwargs={
+ "content_type_id": ct.id,
+ "object_id": obj.pk
+ })
+ liked = Like.objects.filter(
+ sender = user,
+ receiver_content_type = ContentType.objects.get_for_model(obj),
+ receiver_object_id = obj.pk
+ ).exists()
return {
+ "user": user,
"like_url": like_url,
"widget_id": widget_id,
"like_type": like_type,
View
@@ -0,0 +1,7 @@
+from django.conf import settings
+LIKABLE_MODELS = getattr(settings, "PHILEO_LIKABLE_MODELS", [])
+
+def _allowed(obj):
+ model_name = "%s.%s" % (obj._meta.app_label, obj._meta.object_name)
+ return model_name in LIKABLE_MODELS
+
View
@@ -9,14 +9,7 @@
from phileo.models import Like
from phileo.signals import object_liked, object_unliked
-
-
-LIKABLE_MODELS = getattr(settings, "PHILEO_LIKABLE_MODELS", [])
-
-
-def _allowed(obj):
- model_name = "%s.%s" % (obj._meta.app_label, obj._meta.object_name)
- return model_name in LIKABLE_MODELS
+from phileo.utils import _allowed
@require_POST

0 comments on commit 085b0e4

Please sign in to comment.