Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 54 lines (45 sloc) 1.788 kb
898c9e92 » timheap
2011-11-28 Likable models must be registered with phileo first
1 from django.http import HttpResponse, HttpResponseForbidden
b97af220 » paltman
2011-09-05 initial commit
2 from django.utils import simplejson as json
3 from django.shortcuts import get_object_or_404, redirect
065ccae0 » paltman
2012-08-20 Refactor how the phileo widget works
4 from django.template import RequestContext
5 from django.template.loader import render_to_string
b97af220 » paltman
2011-09-05 initial commit
6 from django.views.decorators.http import require_POST
7
68d607fa » paltman
2012-08-20 Add auth backend and use of has_perm
8 from django.contrib.auth.decorators import login_required
9 from django.contrib.contenttypes.models import ContentType
10
b97af220 » paltman
2011-09-05 initial commit
11 from phileo.models import Like
12 from phileo.signals import object_liked, object_unliked
065ccae0 » paltman
2012-08-20 Refactor how the phileo widget works
13 from phileo.utils import widget_context
898c9e92 » timheap
2011-11-28 Likable models must be registered with phileo first
14
b97af220 » paltman
2011-09-05 initial commit
15
16 @login_required
68d607fa » paltman
2012-08-20 Add auth backend and use of has_perm
17 @require_POST
b90c8ced » paltman
2011-10-27 Add support for self-updating like widget out of the box
18 def like_toggle(request, content_type_id, object_id):
b97af220 » paltman
2011-09-05 initial commit
19 content_type = get_object_or_404(ContentType, pk=content_type_id)
68d607fa » paltman
2012-08-20 Add auth backend and use of has_perm
20 obj = content_type.get_object_for_this_type(pk=object_id)
c207595e » paltman
2011-11-28 Change registry to a setting
21
68d607fa » paltman
2012-08-20 Add auth backend and use of has_perm
22 if not request.user.has_perm("phileo.can_like", obj):
898c9e92 » timheap
2011-11-28 Likable models must be registered with phileo first
23 return HttpResponseForbidden()
b97af220 » paltman
2011-09-05 initial commit
24
25 like, created = Like.objects.get_or_create(
68d607fa » paltman
2012-08-20 Add auth backend and use of has_perm
26 sender=request.user,
27 receiver_content_type=content_type,
28 receiver_object_id=object_id
b97af220 » paltman
2011-09-05 initial commit
29 )
30
31 if created:
87cdf5c9 » paltman
2011-12-27 Add request object to signals
32 object_liked.send(sender=Like, like=like, request=request)
b97af220 » paltman
2011-09-05 initial commit
33 else:
34 like.delete()
35 object_unliked.send(
36 sender=Like,
68d607fa » paltman
2012-08-20 Add auth backend and use of has_perm
37 object=obj,
87cdf5c9 » paltman
2011-12-27 Add request object to signals
38 request=request
b97af220 » paltman
2011-09-05 initial commit
39 )
40
41 if request.is_ajax():
065ccae0 » paltman
2012-08-20 Refactor how the phileo widget works
42 html_ctx = widget_context(request.user, obj)
43 data = {
44 "html": render_to_string(
45 "phileo/_widget.html",
46 html_ctx,
47 context_instance=RequestContext(request)
48 ),
49 "likes_count": html_ctx["like_count"],
50 "liked": html_ctx["liked"],
51 }
52 return HttpResponse(json.dumps(data), mimetype="application/json")
b97af220 » paltman
2011-09-05 initial commit
53
54 return redirect(request.META["HTTP_REFERER"])
Something went wrong with that request. Please try again.