1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'''
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
'''
#!/usr/bin/env python
# -*- coding: utf_8 -*-
from datetime import datetime
from application.app import db
from models.device import DeviceModel
from webcreator.log import logger
from webcreator.response import ResponseCode
class MonitorWatchResource(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:
return result, ResponseCode.HTTP_SUCCESS
return None, ResponseCode.HTTP_NOT_FOUND
def getList(self, params):
# handle business
logger.warn(params)
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)
if result:
return result, ResponseCode.HTTP_SUCCESS
return None, ResponseCode.HTTP_NOT_FOUND
def post(self, params, jwt={}):
# handle business
result = DeviceModel.query.filter(DeviceModel.imei == params.get('imei')).first()
if result and result.is_delete:
result.is_delete = False
result.update_by = jwt.get("id", "")
result.update_date = datetime.now()
db.session.commit()
return True, ResponseCode.HTTP_SUCCESS
elif result and result.is_delete == False:
return False, ResponseCode.HTTP_INVAILD_REQUEST
result = DeviceModel(**params)
db.session.add(result)
db.session.commit()
return True, ResponseCode.HTTP_SUCCESS
def put(self, uuid, params, jwt={}):
# handle business
result = DeviceModel.query.filter(DeviceModel.uuid==uuid).first()
if not result:
return None, ResponseCode.HTTP_NOT_FOUND
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()
return True, ResponseCode.HTTP_SUCCESS
else:
return False, ResponseCode.HTTP_INVAILD_REQUEST
def delete(self, uuid, jwt={}):
# handle business
result = DeviceModel.query.filter(DeviceModel.uuid==uuid).first()
if not result:
return False, ResponseCode.HTTP_NOT_FOUND
result.update_by = jwt.get("id", "")
result.update_date = datetime.now()
result.is_delete = True
db.session.delete(result)
db.session.commit()
return True, ResponseCode.HTTP_SUCCESS
monitorWatchManager = MonitorWatchResource()