Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allow all models to be liked #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
ChangeLog ChangeLog
========= =========


0.3
---
- Likeable models need to be registered in Phileo. This prevents users from liking
anything and everything, which could potentially lead to security problems (eg. liking
entries in permission tables, and thus seeing their content; liking administrative
users and thus getting their username).

0.2 0.2
--- ---


Expand Down
23 changes: 20 additions & 3 deletions docs/usage.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@
Usage Usage
===== =====


Phileo consists of template tags that you place within your project In your models
to get different "liking" functionality. --------------

You need to register the models that will be 'likeable' with phileo, before
you use phileo in templates::

# in models.py
from phileo.handlers import library as phileo_library

# Define your models ...

# Register a single model
phileo_library.register(Post)

# Register a bunch of models at once
phileo_library.register([Page, Entry, Comment, Photo])

In the views
------------


Let's say you have a detail page for a blog post. First you will want Let's say you have a detail page for a blog post. First you will want
to load the tags:: to load the tags::
Expand All @@ -27,4 +44,4 @@ Then at the bottom of your page where include your javascript::
{% likes_js request.user post %} {% likes_js request.user post %}




That's all you need to do to get the basics working. That's all you need to do to get the basics working.
19 changes: 19 additions & 0 deletions phileo/handlers.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.db.models.base import ModelBase

class Registry(object):
def __init__(self):
self._registry = []

def register(self, models):

if isinstance(models, ModelBase):
models = [models]

for model in models:
self._registry.append(model)

def is_registered(self, model):
return not (model in self._registry)

library = Registry()

6 changes: 5 additions & 1 deletion phileo/views.py
Original file line number Original file line Diff line number Diff line change
@@ -1,19 +1,23 @@
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType


from django.http import HttpResponse from django.http import HttpResponse, HttpResponseForbidden
from django.utils import simplejson as json from django.utils import simplejson as json
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST


from phileo.models import Like from phileo.models import Like
from phileo.signals import object_liked, object_unliked from phileo.signals import object_liked, object_unliked


from phileo.handlers import library



@require_POST @require_POST
@login_required @login_required
def like_toggle(request, content_type_id, object_id): def like_toggle(request, content_type_id, object_id):
content_type = get_object_or_404(ContentType, pk=content_type_id) content_type = get_object_or_404(ContentType, pk=content_type_id)
if not library.is_registered(content_type.model_class):
return HttpResponseForbidden()


like, created = Like.objects.get_or_create( like, created = Like.objects.get_or_create(
sender = request.user, sender = request.user,
Expand Down