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

wanli's avatar
wanli committed
4 5 6
from datetime import datetime
from application.app import db
from models.area import AreaModel
wanli's avatar
wanli committed
7
from webcreator.log import logger
wanli's avatar
wanli committed
8
from webcreator.response import ResponseCode
wanli's avatar
wanli committed
9 10 11 12 13

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

14

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

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

wanli's avatar
wanli committed
29 30 31
        if result:
            return result, ResponseCode.HTTP_SUCCESS
        return None, ResponseCode.HTTP_NOT_FOUND
32

33
    def post(self, params, jwt={}):
34
        # handle business
wanli's avatar
wanli committed
35
        result = AreaModel.query.filter(AreaModel.areaCode == params.get('areaCode')).first()
36 37
        if result and result.is_delete:
            result.is_delete = False
wanli's avatar
wanli committed
38
            result.update_by = jwt.get("id", "")
39 40
            result.update_date = datetime.now()
            db.session.commit()
wanli's avatar
wanli committed
41
            return True, ResponseCode.HTTP_SUCCESS
42
        elif result and result.is_delete == False:
wanli's avatar
wanli committed
43
            return False, ResponseCode.HTTP_INVAILD_REQUEST
wanli's avatar
wanli committed
44 45 46 47

        result = AreaModel(**params)
        db.session.add(result)
        db.session.commit()
48
        return True, ResponseCode.HTTP_SUCCESS
49

50
    def put(self, uuid, params, jwt={}):
51
        # handle business
52 53
        result = AreaModel.query.filter(AreaModel.uuid==uuid).first()
        if not result:
54
            return None, ResponseCode.HTTP_NOT_FOUND
55

56 57 58
        if params:
            for key, value in params.items():
                if value != None: setattr(result, key, value)
59
            result.update_by = jwt.get("id", "")
60 61
            result.update_date = datetime.now()
            db.session.commit()
wanli's avatar
wanli committed
62
            return True, ResponseCode.HTTP_SUCCESS
63
        else:
wanli's avatar
wanli committed
64
            return False, ResponseCode.HTTP_INVAILD_REQUEST
65

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

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

79

80
areaManager = AreaResource()