Permalink
Browse files

Change registry to a setting

  • Loading branch information...
1 parent 898c9e9 commit c207595e68839bdbf55a8d37579e337262fb0139 @paltman paltman committed Nov 28, 2011
Showing with 26 additions and 39 deletions.
  1. +1 −0 docs/changelog.rst
  2. +11 −15 docs/usage.rst
  3. +0 −19 phileo/handlers.py
  4. +4 −3 phileo/templatetags/phileo_tags.py
  5. +10 −2 phileo/views.py
View
@@ -6,6 +6,7 @@ ChangeLog
0.3
---
+- Renamed `likes_css` and `likes_widget` to `phileo_css` and `phileo_widget`
- Turned the JavaScript code in to a jQuery plugin, removed most of the initialization
code from the individual widget templates to a external JavaScript file, and added a
{% phileo_js %} tag to load this plugin.
View
@@ -3,22 +3,18 @@
Usage
=====
-In your models
---------------
+In your settings
+----------------
-You need to register the models that will be 'likeable' with phileo, before
-you use phileo in templates::
+You need to add each model that you want to be likable to the
+`PHILEO_LIKABLE_MODELS` setting::
- # in models.py
- from phileo.handlers import library as phileo_library
+ PHILEO_LIKABLE_MODELS = [
+ "profiles.Profile",
+ "videos.Video",
+ "biblion.Post"
+ ]
- # 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
------------
@@ -31,7 +27,7 @@ to load the tags::
Then in the <head> section of your template load the css::
- {% likes_css %}
+ {% phileo_css %}
Load the required JavaScript file, wherever you load your JavaScript libraries::
@@ -41,7 +37,7 @@ Load the required JavaScript file, wherever you load your JavaScript libraries::
In the body where you want the liking widget to go, add::
- {% likes_widget request.user post %}
+ {% phileo_widget request.user post %}
That's all you need to do to get the basics working.
View
@@ -1,19 +0,0 @@
-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()
-
@@ -2,7 +2,6 @@
from django.conf import settings
from django.core.urlresolvers import reverse
-from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from phileo.models import Like
@@ -61,16 +60,17 @@ def likes_count(obj):
@register.inclusion_tag("phileo/_css.html")
-def likes_css():
+def phileo_css():
return {"STATIC_URL": settings.STATIC_URL}
+
@register.inclusion_tag("phileo/_js.html")
def phileo_js():
return {"STATIC_URL": settings.STATIC_URL}
@register.inclusion_tag("phileo/_widget.html")
-def likes_widget(user, obj, widget_id=None, like_type="like", toggle_class="phileo-liked"):
+def phileo_widget(user, obj, widget_id=None, like_type="like", toggle_class="phileo-liked"):
ct = ContentType.objects.get_for_model(obj)
like_count = Like.objects.filter(
@@ -103,6 +103,7 @@ def likes_widget(user, obj, widget_id=None, like_type="like", toggle_class="phil
"is_liked": toggle_class if liked else ""
}
+
class LikedObjectsNode(template.Node):
def __init__(self, objects, user, varname):
View
@@ -1,6 +1,7 @@
from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType
+from django.conf import settings
from django.http import HttpResponse, HttpResponseForbidden
from django.utils import simplejson as json
from django.shortcuts import get_object_or_404, redirect
@@ -9,14 +10,21 @@
from phileo.models import Like
from phileo.signals import object_liked, object_unliked
-from phileo.handlers import library
+
+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
@require_POST
@login_required
def like_toggle(request, content_type_id, object_id):
content_type = get_object_or_404(ContentType, pk=content_type_id)
- if not library.is_registered(content_type.model_class):
+
+ if not _allowed(content_type.model_class()):
return HttpResponseForbidden()
like, created = Like.objects.get_or_create(

0 comments on commit c207595

Please sign in to comment.