# coding=utf-8

CONTENT_TYPE_SORTING = (
    # 系统设置
    'account-user',  # 用户管理
    'option-school',  # 商品管理
    'option-student',  # 商品管理
    'option-commoditylevel',  # 商品管理
    'commodity-commodity',  # 商品管理
    'order-coupon',  # 代理商订单
    'order-order',  # 代理商订单
)

MENU_TO_MODEL = (
    (
        u'基础数据', (
            'account-user',
            'option-school',
            'option-student',
            'option-commoditylevel',
            'commodity-commodity',
        )
    ),
    (
        u'订单管理', (
            'order-coupon',
            'order-order',
        )
    ),
)


class PermissionMenu(object):

    def __init__(self):
        self.sort = CONTENT_TYPE_SORTING
        self.menus = MENU_TO_MODEL

    def get_index(self, app_label, model):
        try:
            return self.sort.index('{}-{}'.format(app_label, model))
        except:
            return 9999

    def sort_perms(self, perms):
        perms = perms.order_by('content_type__model', 'id')
        perms = sorted(perms, key=lambda n: self.get_index(n.content_type.app_label, n.content_type.model))
        return perms

    def get_menuname_of_contenttype(self, app_label, model):
        for menu in self.menus:
            val = '{}-{}'.format(app_label, model)
            if val in menu[1]:
                return menu[0]
        return u'未分类'
