Browse Source

用户列表、设备类型列表、设备列表、公司列表接口增加名称模糊查询,下载接口修改

zhangnaiwen 2 năm trước cách đây
mục cha
commit
ddc712c87a
4 tập tin đã thay đổi với 48 bổ sung20 xóa
  1. 26 6
      src/app/api/device.py
  2. 5 11
      src/app/api/download.py
  3. 7 1
      src/app/api/organization.py
  4. 10 2
      src/app/api/users.py

+ 26 - 6
src/app/api/device.py

@@ -18,10 +18,12 @@ from config import Config
 ns = Namespace('device', description='设备管理接口')
 
 config = Config()
+device_type_list = reqparse.RequestParser(bundle_errors=True)
+device_type_list.add_argument(name='name', type=str, location='args', required=False, help='过滤设备类别名')
 
 device_type = reqparse.RequestParser(bundle_errors=True)
-device_type.add_argument(name='id', type=str, location='form', required=True, help='设备类别id')
-device_type.add_argument(name='name', type=str, location='form', required=True, help='设备类别名称')
+device_type.add_argument(name='id', type=str, location='form', required=False, help='设备类别id')
+device_type.add_argument(name='name', type=str, location='form', required=False, help='设备类别名称')
 
 
 @ns.route('/device_type')
@@ -29,19 +31,25 @@ class DeviceTypeApi(Resource):
     method_decorators = [login_required]
 
     @ns.doc(id='device_list', description='设备类别列表')
+    @ns.expect(device_type_list)
     def get(self):
         """设备类型列表"""
 
+        name = request.args.get('name')
+
         try:
             with Session(current_app.engine) as session:
                 stmt = select(DeviceType)
+
+                if name:
+                    stmt = stmt.where(DeviceType.type_name.like("%{}%".format(name)))
+
                 results = session.execute(stmt).scalars().all()
 
             save_log(request, Module.DEVICE, OperationType.INQUIRE, StatesCode.SUCCESS)
 
             return jsonify(code=StatesCode.SUCCESS, message='成功', data=to_dict(results))
 
-
         except Exception as e:
             save_log(request, Module.DEVICE, OperationType.INQUIRE, StatesCode.UNKNOWN_ERROR)
             return jsonify(code=StatesCode.UNKNOWN_ERROR, message=str(e))
@@ -59,7 +67,7 @@ class DeviceTypeApi(Resource):
         try:
             with Session(current_app.engine) as session:
                 stmt = insert(DeviceType).values(
-                    name=device_name
+                    type_name=device_name
                 )
                 session.execute(stmt)
                 session.commit()
@@ -101,7 +109,10 @@ class DeviceTypeApi(Resource):
 
 
 device_list = reqparse.RequestParser(bundle_errors=True)
-device_list.add_argument(name='id', type=str, location='args', required=True, help='设备类别id')
+device_list.add_argument(name='id', type=str, location='args', required=False, help='设备类别id')
+device_list.add_argument(name='name', type=str, location='args', required=False, help='过滤设备名')
+device_list.add_argument(name='page_size', type=int, location='args', required=False, help='每页记录数量,默认:20')
+device_list.add_argument(name='page', type=int, location='args', required=False, help='第几页')
 
 
 @ns.route('/device_list')
@@ -114,13 +125,22 @@ class DeviceListApi(Resource):
         """获取设备列表"""
 
         device_type_id = request.args.get('id')
+        device_name = request.args.get('name')
+        page_size = int(request.args.get('page_size', 20))
+        page = int(request.args.get('page', 1))
 
         if device_type_id is None:
-            return jsonify(code=StatesCode.PARA_ERROR, message='设备类别或设备类别id不能为空')
+            return jsonify(code=StatesCode.PARA_ERROR, message='设备类别id不能为空')
 
         try:
             with Session(current_app.engine) as session:
                 stmt = select(SecurityDevice).where(SecurityDevice.device_group == device_type_id)
+
+                if device_name:
+                    stmt = stmt.where(SecurityDevice.device_name.like("%{}%".format(device_name)))
+
+                stmt = stmt.offset(page_size * (page - 1)).limit(page_size)
+
                 results = session.execute(stmt).scalars().all()
 
             save_log(request, Module.DEVICE, OperationType.INQUIRE, StatesCode.SUCCESS)

+ 5 - 11
src/app/api/download.py

@@ -1,7 +1,7 @@
 import os
 
-from flask import request, jsonify, Response
-from flask_restx import Resource, Namespace, reqparse
+from flask import jsonify, Response
+from flask_restx import Resource, Namespace
 
 from app.defines import StatesCode
 from app.utils.jwt_util import login_required
@@ -11,21 +11,15 @@ ns = Namespace('download', description='文件下载接口')
 
 config = Config()
 
-download_file = reqparse.RequestParser(bundle_errors=True)
-download_file.add_argument(name='name', type=str, location='args', required=False, help='文件名')
 
-
-@ns.route('')
+@ns.route('/<file_name>')
 class DownloadFileApi(Resource):
     method_decorators = [login_required]
 
-    @ns.doc(id='download_file', description='文件下载')
-    @ns.expect(download_file)
-    def get(self):
+    @ns.doc(description='文件下载')
+    def get(self, file_name):
         """文件下载"""
 
-        file_name = request.args.get('name')
-
         file_path = os.path.join(config.common.SAVE_FILE_PATH, file_name)
 
         if not os.path.exists(file_path):

+ 7 - 1
src/app/api/organization.py

@@ -19,6 +19,7 @@ ns = Namespace('organization', description='组织管理接口')
 company_list = reqparse.RequestParser(bundle_errors=True)
 company_list.add_argument(name='page_size', type=int, location='args', required=False, help='每页记录数量,默认:20')
 company_list.add_argument(name='page', type=int, location='args', required=False, help='页数')
+company_list.add_argument(name='name', type=str, location='args', required=False, help='过滤公司名')
 
 config = Config()
 
@@ -34,13 +35,18 @@ class CompanyListApi(Resource):
 
         page_size = int(request.args.get('page_size', 20))
         page = int(request.args.get('page', 1))
+        company_name = request.args.get('name')
 
         try:
             with Session(current_app.engine) as session:
                 count = select(func.count(Company.id))
                 count_results = session.execute(count).scalars().first()
 
-                stmt = select(Company).offset(page_size * (page - 1)).limit(page_size)
+                stmt = select(Company)
+                if company_name:
+                    stmt = stmt.where(Company.company_name.like("%{}%".format(company_name)))
+
+                stmt = stmt.offset(page_size * (page - 1)).limit(page_size)
                 results = session.execute(stmt).scalars().all()
 
             save_log(request, Module.ORGANIZATION, OperationType.INQUIRE, StatesCode.SUCCESS)

+ 10 - 2
src/app/api/users.py

@@ -17,7 +17,8 @@ from app.utils.util import to_dict, MyXlwt
 ns = Namespace('users', description='用户管理接口')
 
 get_users = reqparse.RequestParser(bundle_errors=True)
-get_users.add_argument(name='status', type=float, required=True, location='args', help='用户状态')
+get_users.add_argument(name='status', type=int, required=True, location='args', help='用户状态')
+get_users.add_argument(name='name', type=str, required=True, location='args', help='过滤用户名')
 get_users.add_argument(name='page_size', type=int, location='args', required=False, help='每页记录数量,默认:20')
 get_users.add_argument(name='page', type=int, location='args', required=False, help='第几页')
 
@@ -31,6 +32,7 @@ class GetUserListApi(Resource):
     def get(self):
         """获取用户列表"""
         status = request.args.get('status')
+        name = request.args.get('name')
         page_size = int(request.args.get('page_size', 20))
         page = int(request.args.get('page', 1))
 
@@ -41,7 +43,13 @@ class GetUserListApi(Resource):
                 count = select(func.count(User.id)).where(User.account_status == status)
                 count_results = session.execute(count).scalars().first()
 
-                stmt = select(User).where(User.account_status == status).offset(page_size * (page - 1)).limit(page_size)
+                stmt = select(User).where(User.account_status == status)
+
+                if name:
+                    stmt = stmt.where(User.user_name.like("%{}%".format(name)))
+
+                stmt = stmt.offset(page_size * (page - 1)).limit(page_size)
+
                 results = session.execute(stmt).scalars().all()
 
             save_log(request, Module.USER, OperationType.INQUIRE, StatesCode.SUCCESS)