File tree Expand file tree Collapse file tree 4 files changed +89
-0
lines changed
Expand file tree Collapse file tree 4 files changed +89
-0
lines changed Original file line number Diff line number Diff line change 1+ from flask import abort
2+
3+ from api .extensions import db
4+ from api .lib .common_setting .resp_format import ErrFormat
5+ from api .models .common_setting import CommonData
6+
7+
8+ class CommonDataCRUD (object ):
9+
10+ @staticmethod
11+ def get_data_by_type (data_type ):
12+ return CommonData .get_by (data_type = data_type )
13+
14+ @staticmethod
15+ def get_data_by_id (_id , to_dict = True ):
16+ return CommonData .get_by (first = True , id = _id , to_dict = to_dict )
17+
18+ @staticmethod
19+ def create_new_data (data_type , ** kwargs ):
20+ try :
21+ return CommonData .create (data_type = data_type , ** kwargs )
22+ except Exception as e :
23+ db .session .rollback ()
24+ abort (400 , str (e ))
25+
26+ @staticmethod
27+ def update_data (_id , ** kwargs ):
28+ existed = CommonDataCRUD .get_data_by_id (_id , to_dict = False )
29+ if not existed :
30+ abort (404 , ErrFormat .common_data_not_found .format (_id ))
31+ try :
32+ return existed .update (** kwargs )
33+ except Exception as e :
34+ db .session .rollback ()
35+ abort (400 , str (e ))
36+
37+ @staticmethod
38+ def delete (_id ):
39+ existed = CommonDataCRUD .get_data_by_id (_id , to_dict = False )
40+ if not existed :
41+ abort (404 , ErrFormat .common_data_not_found .format (_id ))
42+ try :
43+ existed .soft_delete ()
44+ except Exception as e :
45+ db .session .rollback ()
46+ abort (400 , str (e ))
Original file line number Diff line number Diff line change @@ -54,3 +54,4 @@ class ErrFormat(CommonErrFormat):
5454 email_is_required = "邮箱不能为空"
5555 email_format_error = "邮箱格式错误"
5656
57+ common_data_not_found = "ID {} 找不到记录"
Original file line number Diff line number Diff line change @@ -80,3 +80,10 @@ class InternalMessage(Model):
8080 category = db .Column (db .VARCHAR (128 ), nullable = False )
8181 message_data = db .Column (db .JSON , nullable = True )
8282 employee_id = db .Column (db .Integer , db .ForeignKey ('common_employee.employee_id' ), comment = 'ID' )
83+
84+
85+ class CommonData (Model ):
86+ __table_name__ = 'common_data'
87+
88+ data_type = db .Column (db .VARCHAR (255 ), default = '' )
89+ data = db .Column (db .JSON )
Original file line number Diff line number Diff line change 1+ from flask import request
2+
3+ from api .lib .common_setting .common_data import CommonDataCRUD
4+ from api .resource import APIView
5+
6+ prefix = '/data'
7+
8+
9+ class DataView (APIView ):
10+ url_prefix = (f'{ prefix } /<string:data_type>' ,)
11+
12+ def get (self , data_type ):
13+ data_list = CommonDataCRUD .get_data_by_type (data_type )
14+
15+ return self .jsonify (data_list )
16+
17+ def post (self , data_type ):
18+ params = request .json
19+ CommonDataCRUD .create_new_data (data_type , ** params )
20+
21+ return self .jsonify (params )
22+
23+
24+ class DataViewWithId (APIView ):
25+ url_prefix = (f'{ prefix } /<string:data_type>/<int:_id>' ,)
26+
27+ def put (self , data_type , _id ):
28+ params = request .json
29+ res = CommonDataCRUD .update_data (_id , ** params )
30+
31+ return self .jsonify (res .to_dict ())
32+
33+ def delete (self , data_type , _id ):
34+ CommonDataCRUD .delete (_id )
35+ return self .jsonify ({})
You can’t perform that action at this time.
0 commit comments