# coding=utf-8

CONTENT_TYPE_SORTING = (
    # 系统设置
    'agent-agent',  # 代理管理
    'agent-store',  # 门店管理
    'account-user',  # 用户管理
    'account-managestoreuser',  # 权限管理
    'option-option',  # 自定义项
    'customer-reportcustomer', # 报备客户
    'customer-newcustomer', # 潜客跟踪

)

MENU_TO_MODEL = (
    (
        u'基础数据', (
            'account-user',
            'account-managestoreuser',
            'agent-agent',
            'agent-store',
            'option-option',
        )
    ),
    (
        u'潜客作业', (
             'customer-reportcustomer',
             'customer-newcustomer',
        )
    ),
    (
        u'订单管理', (
            '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'未分类'
