Skip to content

Commit

Permalink
Checking for likable models is now more robust
Browse files Browse the repository at this point in the history
`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
askeyt committed Nov 29, 2011
1 parent a2ab9b7 commit dbeb5a6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 10 additions & 2 deletions phileo/templatetags/phileo_tags.py
Expand Up @@ -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)

Expand Down
13 changes: 10 additions & 3 deletions phileo/utils.py
@@ -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

0 comments on commit dbeb5a6

Please sign in to comment.