Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Like list tag defaults to all registerd models

{% likes user as like_list %} defaults to using all registered models
when no models are specified, to allow easy listing of all likes in an
application - which is probably the main use case for this.
  • Loading branch information...
commit a2ab9b7ca61e8da7f9ebc8b6c611ae9fb97216dc 1 parent 94d9ff2
@timheap timheap authored
View
13 phileo/templatetags/phileo_tags.py
@@ -5,6 +5,7 @@
from django.contrib.contenttypes.models import ContentType
from phileo.models import Like
+from phileo.utils import _allowed, LIKABLE_MODELS
register = template.Library()
@@ -13,20 +14,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 ""
@@ -39,6 +51,7 @@ def likes(parser, token):
user = tokens[1]
varname = tokens[-1]
model_list = tokens[2:-2]
+
return LikesNode(user, model_list, varname)
View
7 phileo/utils.py
@@ -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 phileo/views.py
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.