device.py 3.54 KB
Newer Older
1 2 3 4 5 6 7 8
'''
Author: your name
Date: 2021-07-15 09:33:39
LastEditTime: 2021-07-19 18:21:58
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \evm-store\tools\build_out\controllers\device.py
'''
9 10 11
#!/usr/bin/env python
# -*- coding: utf_8 -*-

wanli's avatar
wanli committed
12 13
from datetime import datetime
from application.app import db
14
from models.user import UserModel
wanli's avatar
wanli committed
15 16
from models.device import DeviceModel
from webcreator.log import logger
17
from webcreator.response import ResponseCode
wanli's avatar
wanli committed
18 19 20 21 22 23 24 25 26 27

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

    def get(self, uuid, params):
        # handle business
        filters = [DeviceModel.is_delete==False, DeviceModel.uuid==uuid]
        result = DeviceModel.query.filter(*filters).first()
        if result:
28 29
            return result, ResponseCode.HTTP_SUCCESS
        return None, ResponseCode.HTTP_NOT_FOUND
wanli's avatar
wanli committed
30 31 32 33 34

    def getList(self, params):
        # handle business
        logger.warn(params)
        filters = [DeviceModel.is_delete==False]
35 36 37
        if params.get("type"): filters.append(DeviceModel.type==params.get("type"))
        if params.get("name"): filters.append(DeviceModel.name==params.get("name"))
        if params.get("imei"): filters.append(DeviceModel.imei==params.get("imei"))
wanli's avatar
wanli committed
38 39
        result = DeviceModel.query.filter(*filters).order_by(DeviceModel.create_at).paginate(params.get('page', 1), params.get('pageSize', 10), error_out=False)

40 41 42
        if result:
            return result, ResponseCode.HTTP_SUCCESS
        return None, ResponseCode.HTTP_NOT_FOUND
wanli's avatar
wanli committed
43 44 45

    def post(self, params, jwt={}):
        # handle business
wanli's avatar
wanli committed
46
        result = DeviceModel.query.filter(DeviceModel.imei == params.get('imei')).first()
wanli's avatar
wanli committed
47 48 49 50 51
        if result and result.is_delete:
            result.is_delete = False
            result.update_by = jwt.get("id", "")
            result.update_date = datetime.now()
            db.session.commit()
52
            return True, ResponseCode.HTTP_SUCCESS
wanli's avatar
wanli committed
53
        elif result and result.is_delete == False:
54
            return False, ResponseCode.IMEI_EXISTS
wanli's avatar
wanli committed
55

56 57 58 59 60 61 62 63 64 65
        user = UserModel.query.filter(UserModel.uuid==jwt.get("uuid")).one_or_none()
        if not user:
            return False, ResponseCode.USER_NOT_EXISTS

        params.update({
            'create_at': datetime.now(),
            'create_by': user.id,
            'update_at': datetime.now(),
            'update_by': user.id
        })
wanli's avatar
wanli committed
66 67 68
        result = DeviceModel(**params)
        db.session.add(result)
        db.session.commit()
69
        return True, ResponseCode.HTTP_SUCCESS
wanli's avatar
wanli committed
70 71 72 73 74

    def put(self, uuid, params, jwt={}):
        # handle business
        result = DeviceModel.query.filter(DeviceModel.uuid==uuid).first()
        if not result:
75
            return None, ResponseCode.HTTP_NOT_FOUND
wanli's avatar
wanli committed
76 77 78 79 80 81 82

        if params:
            for key, value in params.items():
                if value != None: setattr(result, key, value)
            result.update_by = jwt.get("id", "")
            result.update_date = datetime.now()
            db.session.commit()
83
            return True, ResponseCode.HTTP_SUCCESS
wanli's avatar
wanli committed
84
        else:
85
            return False, ResponseCode.HTTP_INVAILD_REQUEST
wanli's avatar
wanli committed
86 87 88 89 90

    def delete(self, uuid, jwt={}):
        # handle business
        result = DeviceModel.query.filter(DeviceModel.uuid==uuid).first()
        if not result:
91
            return False, ResponseCode.HTTP_NOT_FOUND
wanli's avatar
wanli committed
92 93 94 95 96 97

        result.update_by = jwt.get("id", "")
        result.update_date = datetime.now()
        result.is_delete = True
        db.session.delete(result)
        db.session.commit()
98
        return True, ResponseCode.HTTP_SUCCESS
wanli's avatar
wanli committed
99 100

deviceManager = DeviceResource()