from django.core.urlresolvers import reverse
from django.db import models

from django.contrib.contenttypes.models import ContentType

from phileo.models import Like
from phileo.settings import LIKABLE_MODELS


def name(obj):
    return "{0}.{1}".format(obj._meta.app_label, obj._meta.object_name)


def _allowed(model):
    if isinstance(model, models.Model):
        app_model = name(model)
    elif isinstance(model, str):
        app_model = model
    else:
        app_model = str(model)
    return app_model in LIKABLE_MODELS


def get_config(obj):
    return LIKABLE_MODELS[name(obj)]


def per_model_perm_check(user, obj):
    config = get_config(obj)
    if callable(config.get("allowed")):
        return config["allowed"](user, obj)
    else:
        return True


def widget_context(user, obj):
    ct = ContentType.objects.get_for_model(obj)
    config = get_config(obj)
    like_count = Like.objects.filter(
        receiver_content_type=ct,
        receiver_object_id=obj.pk
    ).count()
    if like_count == 1:
        counts_text = config["count_text_singular"]
    else:
        counts_text = config["count_text_plural"]

    can_like = user.has_perm("phileo.can_like", obj)

    ctx = {
        "can_like": can_like,
        "like_count": like_count,
        "counts_text": counts_text,
    }

    if can_like:
        liked = Like.objects.filter(
            sender=user,
            receiver_content_type=ct,
            receiver_object_id=obj.pk
        ).exists()

        if liked:
            like_text = config["like_text_on"]
            like_class = config["css_class_on"]
        else:
            like_text = config["like_text_off"]
            like_class = config["css_class_off"]

        ctx.update({
            "like_url": reverse("phileo_like_toggle", kwargs={
                "content_type_id": ct.id,
                "object_id": obj.pk
            }),
            "liked": liked,
            "like_text": like_text,
            "like_class": like_class
        })
    else:
        ctx.update({
            "like_text": config["like_text_off"],
            "like_class": config["css_class_off"],
        })

    return ctx

@@ -395,34 +395,31 @@
    elif 
-isinstance
+hasattr
 (model, 
 str):%0A  
@@ -410,19 +410,23 @@
 (model, 
-str
+%22_meta%22
 ):%0A     
@@ -444,22 +444,51 @@
 l = 
+name(
 model
+)
 %0A    el
-se
+if isinstance(model, str)
 :%0A  
@@ -509,18 +509,44 @@
 l = 
-str(
 model
-)
+%0A    else:%0A        return False
 %0A   

Allow both model class and instance in check