user.py 3.18 KB
Newer Older
wanli's avatar
wanli committed
1 2
#!/usr/bin/env python
# -*- coding: utf_8 -*-
wanli's avatar
wanli committed
3 4

from application.app import db, ma
wanli's avatar
wanli committed
5
from .base import PrimaryModel
wanli's avatar
wanli committed
6
from marshmallow import Schema, fields, INCLUDE, EXCLUDE
wanli's avatar
wanli committed
7 8
from webcreator import utils
from webcreator.log import logger
wanli's avatar
wanli committed
9 10 11 12

class UserModel(PrimaryModel):
    __tablename__ = 'evm_user'

wanli's avatar
wanli committed
13 14 15 16 17 18
    username = db.Column(db.String(70), index = True, nullable = False)
    account = db.Column(db.String(200), nullable = False)
    password = db.Column(db.String(20), nullable = False)
    role = db.Column(db.Integer, nullable = False, default = 0)
    email = db.Column(db.String, nullable = False, default = '')
    phone = db.Column(db.String, nullable = False, default = '')
wanli's avatar
wanli committed
19 20 21 22 23

    # __table_args__ = (
    #     db.Index('idx_xxx', 'xxx', mysql_using='btree'),
    # )

wanli's avatar
wanli committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37
    def __init__(self, username, account, password, role=0, email='', phone=''):
        self.username = username
        self.account = account
        self.password = password
        self.role = role
        self.email = email
        self.phone = phone

    def __setattr__(self, key, value):
        # logger.info(f"execute __setattr__:key={key}, value={value}")
        if key == 'password':
            self.__dict__[key] = utils.md5_encryption(value)
        else:
            self.__dict__[key] = value
wanli's avatar
wanli committed
38 39

    def __repr__(self):
wanli's avatar
wanli committed
40
        return '<UserModel %r>' % (self.username)
wanli's avatar
wanli committed
41

wanli's avatar
wanli committed
42
    def to_dict(self):
wanli's avatar
wanli committed
43
        return {
wanli's avatar
wanli committed
44 45 46 47 48 49
            'username': self.username,
            'account': self.account,
            'password': self.password,
            'role': self.role,
            'email': self.email,
            'phone': self.phone,
wanli's avatar
wanli committed
50 51 52 53 54 55
        }


class PostUserSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
wanli's avatar
wanli committed
56
        unknown = EXCLUDE # 未知字段默认排除
wanli's avatar
wanli committed
57 58
        model = UserModel

wanli's avatar
wanli committed
59 60 61
    username = ma.auto_field()
    account = ma.auto_field()
    password = ma.auto_field()
wanli's avatar
wanli committed
62 63 64 65 66 67

postUserSchema = PostUserSchema()

class DeleteUserSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
wanli's avatar
wanli committed
68
        unknown = EXCLUDE # 未知字段默认排除
wanli's avatar
wanli committed
69 70 71 72 73 74 75 76
        model = UserModel


deleteUserSchema = DeleteUserSchema()

class GetListUserSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
wanli's avatar
wanli committed
77
        unknown = EXCLUDE # 未知字段默认排除
wanli's avatar
wanli committed
78 79
        model = UserModel

wanli's avatar
wanli committed
80 81 82
    uuid = ma.auto_field()
    username = ma.auto_field()
    account = ma.auto_field()
wanli's avatar
wanli committed
83 84

getListUserSchema = GetListUserSchema()
85
getListUsersSchema = GetListUserSchema(many=True)
wanli's avatar
wanli committed
86 87 88 89

class GetUserSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
wanli's avatar
wanli committed
90
        unknown = EXCLUDE # 未知字段默认排除
wanli's avatar
wanli committed
91 92
        model = UserModel

wanli's avatar
wanli committed
93 94
    username = ma.auto_field()
    account = ma.auto_field()
wanli's avatar
wanli committed
95 96 97 98 99 100

getUserSchema = GetUserSchema()

class PutUserSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
wanli's avatar
wanli committed
101
        unknown = EXCLUDE # 未知字段默认排除
wanli's avatar
wanli committed
102 103
        model = UserModel

wanli's avatar
wanli committed
104 105 106
    account = fields.String(required=False, allow_none=True)
    username = fields.String(required=False, allow_none=True)
    password = fields.String(required=False, allow_none=True)
wanli's avatar
wanli committed
107 108

putUserSchema = PutUserSchema()