user.py 4.38 KB
Newer Older
wanli's avatar
wanli committed
1 2 3
from datetime import datetime
from application.app import db
from models.user import UserModel
wanliofficial's avatar
wanliofficial committed
4
<<<<<<< HEAD
wanli's avatar
wanli committed
5
from webcreator.utils import ResponseCode, response_result
wanliofficial's avatar
wanliofficial committed
6
=======
wanli's avatar
wanli committed
7 8
from webcreator.log import logger
from webcreator.response import ResponseCode, response_result
wanliofficial's avatar
wanliofficial committed
9
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
wanli's avatar
wanli committed
10 11 12 13 14

class UserResource(object):
    def __init__(self):
        super().__init__()

wanliofficial's avatar
wanliofficial committed
15
<<<<<<< HEAD
wanli's avatar
wanli committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    def get(self, params):
        # handle business
        filters = []
        result = UserModel.query.filter(*filters).order_by(UserModel.areaId).paginate(params.get('page', 1), params.get('pageSize', 10), error_out=False)

        return result

    def post(self, params, jwt=None):
        # handle business
        result = UserModel.query.filter(UserModel.areaName == params.get('areaName')).first()
        if result and result.is_delete:
            result.is_delete = False
            result.update_by = jwt['id']
            result.update_date = datetime.now()
            db.session.commit()
            return response_result(ResponseCode.OK)
        elif result and result.is_delete == False:
            return response_result(ResponseCode.EXISTS_ERROR)
wanliofficial's avatar
wanliofficial committed
34
=======
35
    def get(self, uuid, params):
wanli's avatar
wanli committed
36
        # handle business
37 38 39 40 41
        filters = [UserModel.is_delete==False, UserModel.uuid==uuid]
        result = UserModel.query.filter(*filters).first()
        if result:
            return (True, result)
        return (False, result)
wanli's avatar
wanli committed
42 43 44 45

    def getList(self, params):
        # handle business
        logger.warn(params)
46 47
        filters = [UserModel.is_delete==False]
        result = UserModel.query.filter(*filters).order_by(UserModel.create_at).paginate(params.get('page', 1), params.get('pageSize', 10), error_out=False)
wanli's avatar
wanli committed
48

49
        return (True, result.items, result.total)
wanli's avatar
wanli committed
50

51
    def post(self, params, jwt={}):
wanli's avatar
wanli committed
52
        # handle business
wanli's avatar
wanli committed
53
        result = UserModel.query.filter(UserModel.app_name == params.get('app_name')).first()
wanli's avatar
wanli committed
54 55
        if result and result.is_delete:
            result.is_delete = False
56
            result.update_by = jwt.get("id", "")
wanli's avatar
wanli committed
57 58
            result.update_date = datetime.now()
            db.session.commit()
59
            return (True, None)
wanli's avatar
wanli committed
60
        elif result and result.is_delete == False:
61
            return (False, "record code exists")
wanliofficial's avatar
wanliofficial committed
62
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
wanli's avatar
wanli committed
63 64 65 66

        result = UserModel(**params)
        db.session.add(result)
        db.session.commit()
wanliofficial's avatar
wanliofficial committed
67
<<<<<<< HEAD
wanli's avatar
wanli committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        return response_result(ResponseCode.OK)

    def put(self, id, params, jwt=None):
        # handle business
        result = UserModel.query.get(id)
        if not result: return response_result(ResponseCode.NO_DATA)
        if params:
            for key, value in params.items():
                if value != None: setattr(result, key, value)
            result.update_by = jwt['id']
            result.update_date = datetime.now()
            db.session.commit()
        else:
            return response_result(ResponseCode.PARAM_NULL)

    def delete(self, id, jwt=None):
        # handle business
        result = UserModel.query.get(id)
        if not result: return response_result(ResponseCode.NO_DATA_FOUND)
        else:
            result.update_by = jwt['id']
            result.update_date = datetime.now()
            result.is_delete = True
            db.session.delete(result)
            db.session.commit()
wanliofficial's avatar
wanliofficial committed
93
=======
94
        return (True, None)
wanli's avatar
wanli committed
95

96
    def put(self, uuid, params, jwt={}):
wanli's avatar
wanli committed
97
        # handle business
98 99 100 101
        result = UserModel.query.filter(UserModel.uuid==uuid).first()
        if not result:
            return (False, "record not exists")

wanli's avatar
wanli committed
102 103 104
        if params:
            for key, value in params.items():
                if value != None: setattr(result, key, value)
105
            result.update_by = jwt.get("id", "")
wanli's avatar
wanli committed
106 107
            result.update_date = datetime.now()
            db.session.commit()
108
            return (True, None)
wanli's avatar
wanli committed
109
        else:
110
            return (False, "params is null")
wanli's avatar
wanli committed
111

112
    def delete(self, uuid, jwt={}):
wanli's avatar
wanli committed
113
        # handle business
114 115 116 117 118 119 120 121 122 123
        result = UserModel.query.filter(UserModel.uuid==uuid).first()
        if not result:
            return (False, "record not exists")

        result.update_by = jwt.get("id", "")
        result.update_date = datetime.now()
        result.is_delete = True
        db.session.delete(result)
        db.session.commit()
        return (True, None)
wanliofficial's avatar
wanliofficial committed
124
>>>>>>> 735d39eb4d0c3134b62bf4fe1b7a2ca0ea8da1ca
wanli's avatar
wanli committed
125 126

userManager = UserResource()