menu_manager.py 4.01 KB
Newer Older
wanli's avatar
wanli committed
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/usr/bin/env python
# -*- coding: utf_8 -*-

import copy
import time
import types
import json
import logging
import traceback
from datetime import datetime
from pony.orm import *
from flask import request
from model import fullStackDB
wanli's avatar
wanli committed
14
from model.menu import Menu
wanli's avatar
wanli committed
15 16 17
from model.user import User
from utils import sql_filter

wanli's avatar
wanli committed
18
logger = logging.getLogger("MenuManager")
wanli's avatar
wanli committed
19

wanli's avatar
wanli committed
20
class MenuManager(object):
wanli's avatar
wanli committed
21
    def __init__(self):
wanli's avatar
wanli committed
22
        super(MenuManager, self).__init__()
wanli's avatar
wanli committed
23 24 25 26 27 28

    def add(self, data):
        editor = User.get(id=request.current_user.get("id"))
        if not editor:
            return False, "current user is not exists"

wanli's avatar
wanli committed
29 30 31 32
        result = Menu.get(path=data.get("path"))
        if result:
            return False, "menu path has been exists."

wanli's avatar
wanli committed
33 34 35 36 37 38 39
        data.update({
            'create_by': editor,
            'create_at': datetime.now(),
            'update_by': editor,
            'update_at': datetime.now(),
        })

wanli's avatar
wanli committed
40 41
        result = fullStackDB.add(Menu, **data)
        return result, "add menu {}.".format("success" if result else "fail")
wanli's avatar
wanli committed
42 43 44 45 46 47

    def delete(self, uuid):
        editor = User.get(id=request.current_user.get("id"))
        if not editor:
            return False, "current user is not exists"

wanli's avatar
wanli committed
48 49
        result = fullStackDB.update(Menu, { 'uuid': uuid }, is_delete=True, delete_at=datetime.now(), delete_by=editor)
        return result, "delete menu {}.".format("success" if result else "fail")
wanli's avatar
wanli committed
50 51

    def get(self, data):
wanli's avatar
wanli committed
52
        result = Menu.get(**data)
wanli's avatar
wanli committed
53 54
        if result:
            result = result.to_dict(only=["uuid", "name", "create_at", "update_at"])
wanli's avatar
wanli committed
55
        return result, "get menu {}.".format("success" if result else "no data")
wanli's avatar
wanli committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

    def getList(self, data):
        if not data or len(data) <= 0:
            return False, 0, "parameters can not be null."

        temp = copy.deepcopy(data)
        if 'pagenum' in temp:
            temp.pop('pagenum')
        if 'pagesize' in temp:
            temp.pop('pagesize')
        if 'scope_type' in temp:
            temp.pop('scope_type')
        if 'props' in temp:
            temp.pop('props')
        temp.setdefault("is_delete", False)

        if "scope_type" in data and data.get("scope_type") == "list":
wanli's avatar
wanli committed
73
            result = Menu.select().where(**temp).order_by(desc(Menu.create_at))
wanli's avatar
wanli committed
74 75 76 77 78 79
            temp = []
            fields = ["uuid", "name"]
            if "props" in data and isinstance(data.get("props"), list):
                fields = data.get("props")
            for item in result:
                temp.append(item.to_dict(only=fields))
wanli's avatar
wanli committed
80
            return temp, len(temp), "get menu {}.".format("success" if temp else "fail")
wanli's avatar
wanli committed
81

wanli's avatar
wanli committed
82 83
        result = fullStackDB.pagination(Menu, Menu.create_at, pagenum=data.get("pagenum", 1), pagesize=data.get("pagesize", 10), **temp)
        count = fullStackDB.count(Menu, **temp)
wanli's avatar
wanli committed
84 85 86 87 88 89 90 91 92 93 94 95

        if result and len(result):
            temp = []
            for item in result:
                t = item.to_dict(with_collections=True, related_objects=True)
                t.update({ "create_at": item.create_at.strftime("%Y-%m-%d %H:%M:%S") })
                t.update({ "update_at": item.update_at.strftime("%Y-%m-%d %H:%M:%S") })
                t.update({ "create_by": item.create_by.to_dict(only=["uuid", "username"]) })
                t.update({ "update_by": item.update_by.to_dict(only=["uuid", "username"]) })
                temp.append(t)
            result = temp

wanli's avatar
wanli committed
96
        return result, count, "get menu {}.".format("success" if result else "no data")
wanli's avatar
wanli committed
97 98 99 100 101 102 103 104 105 106 107

    def update(self, uuid, data):
        # 当参数为空时,直接返回错误
        if len(data) <= 0 or (len(data.keys()) == 1 and "id" in data):
            return False, "parameters can not be null."

        # 查询请求者是否存在
        editor = User.get(id=request.current_user.get("id"))
        if not editor:
            return False, "current user is not exists"

wanli's avatar
wanli committed
108 109
        result = fullStackDB.update(Menu, { 'uuid': uuid }, update_at=datetime.now(), update_by=editor, **data)
        return result, "update menu {}.".format("success" if result else "fail")
wanli's avatar
wanli committed
110

wanli's avatar
wanli committed
111
menuManager = MenuManager()