monitorWatch.py 2.95 KB
Newer Older
1 2 3 4 5 6 7 8
'''
Author: your name
Date: 2021-07-15 09:33:39
LastEditTime: 2021-07-20 01:18:27
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \evm-store\tools\build_out\controllers\monitorWatch.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.device import DeviceModel
wanli's avatar
wanli committed
15
from webcreator.log import logger
16
from webcreator.response import ResponseCode
wanli's avatar
wanli committed
17 18 19 20 21 22 23

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

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

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

36 37 38
        if result:
            return result, ResponseCode.HTTP_SUCCESS
        return None, ResponseCode.HTTP_NOT_FOUND
wanli's avatar
wanli committed
39 40 41

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

52
        result = DeviceModel(**params)
wanli's avatar
wanli committed
53 54
        db.session.add(result)
        db.session.commit()
55
        return True, ResponseCode.HTTP_SUCCESS
wanli's avatar
wanli committed
56 57 58

    def put(self, uuid, params, jwt={}):
        # handle business
59
        result = DeviceModel.query.filter(DeviceModel.uuid==uuid).first()
wanli's avatar
wanli committed
60
        if not result:
61
            return None, ResponseCode.HTTP_NOT_FOUND
wanli's avatar
wanli committed
62 63 64 65 66 67 68

        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()
69
            return True, ResponseCode.HTTP_SUCCESS
wanli's avatar
wanli committed
70
        else:
71
            return False, ResponseCode.HTTP_INVAILD_REQUEST
wanli's avatar
wanli committed
72 73 74

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

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

monitorWatchManager = MonitorWatchResource()