monitorImage.py 2.83 KB
Newer Older
1 2 3
#!/usr/bin/env python
# -*- coding: utf_8 -*-

wanli's avatar
wanli committed
4 5 6 7
from datetime import datetime
from application.app import db
from models.monitorImage import MonitorImageModel
from webcreator.log import logger
8
from webcreator.response import ResponseCode
wanli's avatar
wanli committed
9 10 11 12 13 14 15 16 17 18

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

    def get(self, uuid, params):
        # handle business
        filters = [MonitorImageModel.is_delete==False, MonitorImageModel.uuid==uuid]
        result = MonitorImageModel.query.filter(*filters).first()
        if result:
19 20
            return result, ResponseCode.HTTP_SUCCESS
        return None, ResponseCode.HTTP_NOT_FOUND
wanli's avatar
wanli committed
21 22 23 24 25 26 27

    def getList(self, params):
        # handle business
        logger.warn(params)
        filters = [MonitorImageModel.is_delete==False]
        result = MonitorImageModel.query.filter(*filters).order_by(MonitorImageModel.create_at).paginate(params.get('page', 1), params.get('pageSize', 10), error_out=False)

28 29 30
        if result:
            return result, ResponseCode.HTTP_SUCCESS
        return None, ResponseCode.HTTP_NOT_FOUND
wanli's avatar
wanli committed
31 32 33 34 35 36 37 38 39

    def post(self, params, jwt={}):
        # handle business
        result = MonitorImageModel.query.filter(MonitorImageModel.watch == params.get('watch')).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()
40
            return True, ResponseCode.HTTP_SUCCESS
wanli's avatar
wanli committed
41
        elif result and result.is_delete == False:
42
            return False, ResponseCode.HTTP_INVAILD_REQUEST
wanli's avatar
wanli committed
43 44 45 46

        result = MonitorImageModel(**params)
        db.session.add(result)
        db.session.commit()
47
        return True, ResponseCode.HTTP_SUCCESS
wanli's avatar
wanli committed
48 49 50 51 52

    def put(self, uuid, params, jwt={}):
        # handle business
        result = MonitorImageModel.query.filter(MonitorImageModel.uuid==uuid).first()
        if not result:
53
            return None, ResponseCode.HTTP_NOT_FOUND
wanli's avatar
wanli committed
54 55 56 57 58 59 60

        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()
61
            return True, ResponseCode.HTTP_SUCCESS
wanli's avatar
wanli committed
62
        else:
63
            return False, ResponseCode.HTTP_INVAILD_REQUEST
wanli's avatar
wanli committed
64 65 66 67 68

    def delete(self, uuid, jwt={}):
        # handle business
        result = MonitorImageModel.query.filter(MonitorImageModel.uuid==uuid).first()
        if not result:
69
            return False, ResponseCode.HTTP_NOT_FOUND
wanli's avatar
wanli committed
70 71 72 73 74 75

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

monitorImageManager = MonitorImageResource()