|
@@ -1,4 +1,7 @@
|
|
|
+from flask import jsonify, request
|
|
|
+from flask_login import current_user
|
|
|
from flask_restful import Resource # type: ignore
|
|
|
+from werkzeug.exceptions import NotFound
|
|
|
|
|
|
from controllers.console import api
|
|
|
from controllers.console.wraps import (
|
|
@@ -14,12 +17,59 @@ class DeptAccountListApi(Resource):
|
|
|
@login_required
|
|
|
@account_initialization_required
|
|
|
def get(self):
|
|
|
- dept_account_list=DeptService.get_dept_account_list()
|
|
|
+ dept_account_list = DeptService.get_dept_account_list()
|
|
|
response = {
|
|
|
+ "result": "success",
|
|
|
"data": dept_account_list,
|
|
|
}
|
|
|
return response, 200
|
|
|
|
|
|
+ @setup_required
|
|
|
+ @login_required
|
|
|
+ @account_initialization_required
|
|
|
+ def post(self):
|
|
|
+ data = request.get_json()
|
|
|
+ if not data:
|
|
|
+ raise NotFound("Invalid JSON")
|
|
|
+ dept_account_list = data.get("account_ids")
|
|
|
+ dept_id = data.get("dept_id")
|
|
|
+
|
|
|
+ DeptService.save_dept_account_list(dept_id, dept_account_list)
|
|
|
+ response = {
|
|
|
+ "result": "success",
|
|
|
+ }
|
|
|
+ return response, 200
|
|
|
+
|
|
|
+ @setup_required
|
|
|
+ @login_required
|
|
|
+ @account_initialization_required
|
|
|
+ def delete(self):
|
|
|
+ data = request.get_json()
|
|
|
+ if not data:
|
|
|
+ raise NotFound("Invalid JSON")
|
|
|
+ dept_account_list = data.get("account_ids")
|
|
|
+ dept_id = data.get("dept_id")
|
|
|
+
|
|
|
+ DeptService.delete_dept_account_list(dept_id, dept_account_list)
|
|
|
+ response = {
|
|
|
+ "result": "success",
|
|
|
+ }
|
|
|
+ return response, 200
|
|
|
+
|
|
|
+
|
|
|
+class DeptAccountApi(Resource):
|
|
|
+ @setup_required
|
|
|
+ @login_required
|
|
|
+ @account_initialization_required
|
|
|
+ def get(self, dept_id):
|
|
|
+ dept_account = DeptService.get_dept_account(dept_id)
|
|
|
+ response = {
|
|
|
+ "result": "success",
|
|
|
+ "data": dept_account,
|
|
|
+ }
|
|
|
+ return response, 200
|
|
|
+
|
|
|
+
|
|
|
class DeptListApi(Resource):
|
|
|
@setup_required
|
|
|
@login_required
|
|
@@ -27,9 +77,66 @@ class DeptListApi(Resource):
|
|
|
def get(self):
|
|
|
dept_list = DeptService.get_dept_list()
|
|
|
response = {
|
|
|
+ "result": "success",
|
|
|
"data": dept_list,
|
|
|
}
|
|
|
return response, 200
|
|
|
|
|
|
+ @setup_required
|
|
|
+ @login_required
|
|
|
+ @account_initialization_required
|
|
|
+ def post(self):
|
|
|
+ data = request.get_json()
|
|
|
+ if not data:
|
|
|
+ raise NotFound("Invalid JSON")
|
|
|
+ dept_name = data.get("dept_name")
|
|
|
+ dept_id = data.get("dept_id")
|
|
|
+ dept_by_name = DeptService.get_dept_by_name(dept_name)
|
|
|
+ if dept_id not in {None, ""}:
|
|
|
+ dept_by_id = DeptService.get_dept_by_id(dept_id)
|
|
|
+ if dept_by_id != None:
|
|
|
+ if dept_by_id.dept_name != dept_name:
|
|
|
+ # 修改
|
|
|
+ if dept_by_name != None:
|
|
|
+ return jsonify({"error": "'dept_name' repeat"}), 400
|
|
|
+ else:
|
|
|
+ DeptService.update_dept(dept_id, dept_name, current_user)
|
|
|
+ else:
|
|
|
+ raise NotFound("Dept not found.")
|
|
|
+ else:
|
|
|
+ if dept_by_name != None:
|
|
|
+ return jsonify({"error": "'dept_name' repeat"}), 400
|
|
|
+ else:
|
|
|
+ DeptService.save_dept(dept_name, current_user)
|
|
|
+ return {"result": "success"}, 204
|
|
|
+
|
|
|
+
|
|
|
+class DeptApi(Resource):
|
|
|
+ @setup_required
|
|
|
+ @login_required
|
|
|
+ @account_initialization_required
|
|
|
+ def get(self, dept_id):
|
|
|
+ dept_id = str(dept_id)
|
|
|
+ dept = DeptService.get_dept_by_id(dept_id)
|
|
|
+ response = {
|
|
|
+ "result": "success",
|
|
|
+ "data": dept,
|
|
|
+ }
|
|
|
+ return response, 200
|
|
|
+
|
|
|
+ @setup_required
|
|
|
+ @login_required
|
|
|
+ @account_initialization_required
|
|
|
+ def delete(self, dept_id):
|
|
|
+ dept_id = str(dept_id)
|
|
|
+ dept = DeptService.get_dept_by_id(dept_id)
|
|
|
+ if dept is None:
|
|
|
+ raise NotFound("Dept not found.")
|
|
|
+ DeptService.delete_dept(dept)
|
|
|
+ return {"result": "success"}, 204
|
|
|
+
|
|
|
+
|
|
|
api.add_resource(DeptAccountListApi, "/dept/dept-accounts")
|
|
|
-api.add_resource(DeptListApi, "/dept")
|
|
|
+api.add_resource(DeptAccountApi, "/dept/dept-accounts/<uuid:dept_id>")
|
|
|
+api.add_resource(DeptListApi, "/depts")
|
|
|
+api.add_resource(DeptApi, "/depts/<uuid:dept_id>")
|