# coding: utf-8
'''
文件作用:user相关操作
'''
from Library import flaskhelper
from Library.minihelper import saveData
from flask_login import login_required
from flask import Blueprint, g, session
from flask.templating import render_template
from Library.flaskhelper import getargs, isGetMethod
from Library.minihelper import getGridData, saveTreeData
from autodb.Logic.PermissionLogic import power, getMenusByUser_code, getMenus, IsAdmin

from pony.orm import *

from autodb.Logic.PermissionLogic import geturlmap

permission = Blueprint("permission", __name__)


@permission.route('/clearcache')
def clearcache() :
    '''
    清空缓存
    :return:
    '''

    IsAdmin('管理员')

    from autodb import cache

    cache.clear()
    return "清理完毕"


@permission.route('/reg')
@db_session
def reg() :
    """
    自动注册所有模块

    """

    from autodb.models.portal import modulelist

    # 将现有module设置为不可用
    data = select(p for p in modulelist)
    for d in data :
        modulelist.get(modulename = d.modulename).set(state = "no")
    # 重新获取现有路由，并注册
    rus = geturlmap()
    for x in rus :
        doc = rus[x]['doc'].decode("utf8").split('\n')[1] if rus[x]['doc'] else ""
        url = ';'.join(rus[x]['rule'])
        c = x.split('.')[0]  # controller
        a = x.split('.')[1]  # action
        single = modulelist.get(modulename = x)
        if single :
            modulelist.get(modulename = x).set(url = url, doc = doc, state = 'ok', c = c, a = a)
        else :
            modulelist(modulename = x, url = url, doc = doc, state = 'ok', c = c, a = a)
    # 删除无用module
    data = select(p for p in modulelist if p.state == "no")
    for d in data :
        modulelist.get(modulename = d.modulename).delete()
    return "注册完成"


@permission.route('/g_p_list', methods = ['GET', 'POST'])
@db_session
def group_module() :
    '''
    group_Module对应关系
    :return:
    '''
    modulename = getargs("modulename")
    if isGetMethod() :
        return render_template("permission.html")
    from autodb.models.portal import group_module as gm

    data = select(p for p in gm if p.modulename == modulename)
    return getGridData(entity = gm, data = data)


@permission.route('/index', methods = ['GET', 'POST'])
@db_session
@power
def index() :
    '''
    获取模块列表
    :return:
    '''

    from autodb.models.portal import modulelist

    if isGetMethod() :
        return render_template("permission.html")

    data = select(p for p in modulelist).order_by(modulelist.modulename)
    return getGridData(entity = modulelist, data = data)


@permission.route('/save', methods = ['POST'])
@db_session
@login_required
@power
def save_g_m() :
    '''
    保存模块角色对应关系
    :return:
    '''
    from autodb.models.portal import group_module

    data = flaskhelper.getargs2json("data")
    if data :
        saveData(group_module, data, operator = g.user.get_id())
    return "操作完成"


@permission.route('/getMenu', methods = ['GET', 'POST'])
@db_session
def getMenu() :
    '''
    获取菜单列表
    :return:
    '''

    if isGetMethod() and getargs("getpage") :
        return render_template("menutree.html")
    if IsAdmin(['系统管理员']) :
        # print("系统管理员")
        return getMenus()
    return getMenusByUser_code(g.user.get_id())


@permission.route('/savemenu', methods = ['GET', 'POST'])
@db_session
@login_required
def savemenu() :
    '''
    保存菜单
    :return:
    '''

    from autodb.models.portal import menutree

    operator = g.user.get_id()
    saveTreeData(menutree, operator)

    return "操作完成"

