from django.db import models
from django.contrib import admin
from django.contrib.sites.models import Site
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User, Group, Permission
from django.utils.translation import ugettext_lazy as _, ugettext

from ella.core.models import Category
from ella.core.cache import get_cached_object

#TODO @cache_this
def has_category_permission(user, model, category, permission):
    if user.has_perm(permission):
        return True

    app_label, code = permission.split('.', 1)
    perm = get_cached_object(Permission, content_type__app_label=app_label, codename=code)

    if CategoryUserRole.objects.filter(
            category=category,
            user=user,
            group__permissions=perm
).count():
        return True

    # fallback to site permissions
    return has_site_permission(user, model, category.site_id, permission)

def has_site_permission(user, model, site, permission):
    if user.has_perm(permission):
        return True

    app_label, code = permission.split('.', 1)
    perm = get_cached_object(Permission, content_type__app_label=app_label, codename=code)

    if SiteUserRole.objects.filter(site=site, user=user, group__permissions=perm).count():
        return True

    return False

def applicable_sites(user, permission=None):
    q = SiteUserRole.objects.filter(user=user).distinct().values('site')
    if permission:
        app_label, code = permission.split('.', 1)
        perm = get_cached_object(Permission, content_type__app_label=app_label, codename=code)
        q = q.filter(group__permissions=perm)
    else:
        # take any permission
        q = q.filter(group__permissions__id__isnull=False)
    return [ d['site'] for d in q ]



def applicable_categories(user, permission=None):
    q = CategoryUserRole.objects.filter(user=user).distinct().values('category')

    if permission:
        app_label, code = permission.split('.', 1)
        perm = Permission.objects.get(content_type__app_label=app_label, codename=code)
        q = q.filter(group__permissions=perm)
    else:
        # take any permission
        q = q.filter(group_permissions__id__isnull=False)

    return [ d['category'] for d in q ]

class CategoryUserRole(models.Model):
    """
    Apply all group's permission for the given user to this category.
    """
    user = models.ForeignKey(User)
    group = models.ForeignKey(Group)
    category = models.ForeignKey(Category)

    def __unicode__(self):
        return ugettext(u'User %(user)s is a %(group)s for %(category)s.') % {
                'user' : self.user,
                'group' : self.group,
                'category' : self.category,
}

    class Meta:
        verbose_name = _("User role in category")
        verbose_name_plural = _("User roles in categories")

class SiteUserRole(models.Model):
    """
    Apply all group's permission for the given user to this site.
    """
    user = models.ForeignKey(User)
    group = models.ForeignKey(Group)
    site = models.ForeignKey(Site)

    def __unicode__(self):
        return ugettext(u'User %(user)s is a %(group)s for %(site)s.') % {
                'user' : self.user,
                'group' : self.group,
                'site' : self.site,
}

    class Meta:
        verbose_name = _("User role in site")
        verbose_name_plural = _("User roles in site")

class SiteUserRoleOptions(admin.ModelAdmin):
    list_filter = ('user', 'group', 'site',)
    list_display = ('user', 'group', 'site',)

class CategoryUserRoleOptions(admin.ModelAdmin):
    list_filter = ('user', 'group', 'category',)
    list_display = ('user', 'group', 'category',)

admin.site.register(CategoryUserRole, CategoryUserRoleOptions)
admin.site.register(SiteUserRole, SiteUserRoleOptions)

@@ -2172,16 +2172,17 @@
 r(group_
+_
 permissi

fixed a typo