Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Handle anonymous users by not showing the button #6

Merged
merged 3 commits into from

2 participants

@timheap

A rehash of the old anonymous users pull request, as requested.

If an anonymous user access a page with a like widget, the page breaks. This is because the AnonymousUser class does not have a likes collection, whereas the normal User class does. This patch checks for anonymous users before attempting to get their likes. Additionally, the likes widget does NOT show the like button if the user is not logged in.

timheap added some commits
@timheap timheap Handle anonymous users by not showing the button 1ce168e
@timheap timheap Include the user in the phileo_widget context
Anonymous user detection wasnt working correctly, as the user was not
always in the template context at render time. This resulted in the
'like' button just plain old not being shown in some circumstances,
which is incorrect. By explicitly including the user in the context,
this problem is solved
2bca3dc
phileo/templatetags/phileo_tags.py
((14 lines not shown))
- 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),
@paltman Owner
paltman added a note

could we just use ct here instead of another ContentType query?

@timheap
timheap added a note

Yup, I missed the ct variable there. Ill make use of it instead of querying for it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@paltman paltman merged commit 957252a into pinax:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 28, 2011
  1. @timheap
Commits on Nov 29, 2011
  1. @timheap

    Include the user in the phileo_widget context

    timheap authored
    Anonymous user detection wasnt working correctly, as the user was not
    always in the template context at render time. This resulted in the
    'like' button just plain old not being shown in some circumstances,
    which is incorrect. By explicitly including the user in the context,
    this problem is solved
  2. @timheap
This page is out of date. Refresh to see the latest.
View
4 phileo/templates/phileo/_widget.html
@@ -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">
View
25 phileo/templatetags/phileo_tags.py
@@ -78,22 +78,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 = ct,
+ receiver_object_id = obj.pk
+ ).exists()
return {
+ "user": user,
"like_url": like_url,
"widget_id": widget_id,
"like_type": like_type,
Something went wrong with that request. Please try again.