forked from pinax/pinax-likes
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
20 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|