Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Checking for likable models is now more robust

`phileo.utils_allowed` handles string arguments, and Model arguments now,
and treats them appropriately. This way, you can pass in either a Model
instance OR a string already in the 'app_name.model' format and both are
handled correctly.

When using the {% likes %} tag, the PHILEO_LIKABLE_MODELS list is
checked before the likes for that model are queried. If the model is not
in PHILEO_LIKABLE_MODELS, that model is skipped.
  • Loading branch information...
commit dbeb5a6fde3f271c7a24e6e0f7ef5840a13aba2a 1 parent a2ab9b7
Tim Heap authored
Showing with 20 additions and 5 deletions.
  1. +10 −2 phileo/templatetags/phileo_tags.py
  2. +10 −3 phileo/utils.py
12 phileo/templatetags/phileo_tags.py
View
@@ -29,8 +29,16 @@ 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(".")
+ for raw_model_name in self.model_list:
+ try:
+ model_name = raw_model_name.resolve(context)
+ except template.VariableDoesNotExist:
+ continue
+
+ if not _allowed(model_name):
+ continue
+
+ app, model = model_name.split(".")
content_type = ContentType.objects.get(app_label=app, model__iexact=model)
content_types.append(content_type)
13 phileo/utils.py
View
@@ -1,7 +1,14 @@
from django.conf import settings
+from django.db.models.base import ModelBase
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
+def _allowed(model):
+ if isinstance(model, ModelBase):
+ app_model = "%s.%s" % (model._meta.app_label, model._meta.object_name)
+ elif isinstance(model, str):
+ app_model = model
+ else:
+ app_model = str(model)
+
+ return app_model in LIKABLE_MODELS
Please sign in to comment.
Something went wrong with that request. Please try again.