app.py 2.43 KB
Newer Older
wanli's avatar
wanli committed
1 2 3
from datetime import datetime
from application.app import db
from models.app import AppModel
wanli's avatar
wanli committed
4 5
from webcreator.log import logger
from webcreator.response import ResponseCode, response_result
wanli's avatar
wanli committed
6 7 8 9 10

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

11
    def get(self, uuid, params):
wanli's avatar
wanli committed
12
        # handle business
13 14 15 16 17
        filters = [AppModel.is_delete==False, AppModel.uuid==uuid]
        result = AppModel.query.filter(*filters).first()
        if result:
            return (True, result)
        return (False, result)
wanli's avatar
wanli committed
18 19 20 21

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

25
        return (True, result.items, result.total)
wanli's avatar
wanli committed
26

27
    def post(self, params, jwt={}):
wanli's avatar
wanli committed
28
        # handle business
wanli's avatar
wanli committed
29
        result = AppModel.query.filter(AppModel.app_name == params.get('app_name')).first()
wanli's avatar
wanli committed
30 31
        if result and result.is_delete:
            result.is_delete = False
32
            result.update_by = jwt.get("id", "")
wanli's avatar
wanli committed
33 34
            result.update_date = datetime.now()
            db.session.commit()
35
            return (True, None)
wanli's avatar
wanli committed
36
        elif result and result.is_delete == False:
37
            return (False, "record code exists")
wanli's avatar
wanli committed
38 39 40 41

        result = AppModel(**params)
        db.session.add(result)
        db.session.commit()
42
        return (True, None)
wanli's avatar
wanli committed
43

44
    def put(self, uuid, params, jwt={}):
wanli's avatar
wanli committed
45
        # handle business
46 47 48 49
        result = AppModel.query.filter(AppModel.uuid==uuid).first()
        if not result:
            return (False, "record not exists")

wanli's avatar
wanli committed
50 51 52
        if params:
            for key, value in params.items():
                if value != None: setattr(result, key, value)
53
            result.update_by = jwt.get("id", "")
wanli's avatar
wanli committed
54 55
            result.update_date = datetime.now()
            db.session.commit()
56
            return (True, None)
wanli's avatar
wanli committed
57
        else:
58
            return (False, "params is null")
wanli's avatar
wanli committed
59

60
    def delete(self, uuid, jwt={}):
wanli's avatar
wanli committed
61
        # handle business
62 63 64 65 66 67 68 69 70 71
        result = AppModel.query.filter(AppModel.uuid==uuid).first()
        if not result:
            return (False, "record not exists")

        result.update_by = jwt.get("id", "")
        result.update_date = datetime.now()
        result.is_delete = True
        db.session.delete(result)
        db.session.commit()
        return (True, None)
wanli's avatar
wanli committed
72 73

appManager = AppResource()