diff --git a/serve/serve/commonViews/__pycache__/commonViews.cpython-311.pyc b/serve/serve/commonViews/__pycache__/commonViews.cpython-311.pyc new file mode 100644 index 0000000..a57b072 Binary files /dev/null and b/serve/serve/commonViews/__pycache__/commonViews.cpython-311.pyc differ diff --git a/serve/serve/commonViews/commonViews.py b/serve/serve/commonViews/commonViews.py new file mode 100644 index 0000000..bae5367 --- /dev/null +++ b/serve/serve/commonViews/commonViews.py @@ -0,0 +1,93 @@ +from datetime import datetime + +from django.http import JsonResponse +from django.urls import path +from serve.utils.connectdb import connectdb +from bson.objectid import ObjectId +from serve.middleware.handleErrorMethod import handleErrorMethod +from serve.middleware.handleEmpty import handleEmpty +from serve.utils.pyjwt import get_jwt_token, decode_jwt_token +from serve.utils.decodeBody import decodeBody + + +def delete(request, collection): + result = handleErrorMethod(request.method, 'DELETE') + if result[0]: + ret = decode_jwt_token(request.META.get('HTTP_AUTHORIZATION')) + if ret['code'] == 2000: + data = decodeBody(request) + _ids = list(data['_ids']) + filter_criteria = {'_id': {'$in': [ObjectId(_id) for _id in _ids]}} + deleteResult = collection.delete_many(filter_criteria) + res = handleEmpty(deleteResult.deleted_count, '删除') + return JsonResponse(res) + else: + return JsonResponse(ret) + + else: + return JsonResponse(result[1]) + + +def getAll(request, collection): + result = handleErrorMethod(request.method, 'GET') + if result[0]: + ret = decode_jwt_token(request.META.get('HTTP_AUTHORIZATION')) + if ret['code'] == 2000: + pageSize = int(request.GET.get('pageSize', None)) + pageIndex = int(request.GET.get('pageIndex', None)) + # 计算要跳过的文档数量 + skip_count = (pageIndex - 1) * pageSize + # 执行分页查询 + cursor = collection.find().skip(skip_count).limit(pageSize) + # 将查询结果转换为列表 + result = [{**doc, '_id': str(doc['_id'])} for doc in cursor] + # 构建JSON格式的响应 + response_data = { + 'total': collection.count_documents({}), # 获取总文档数 + 'data': result, + 'code': 2000 + } + return JsonResponse(response_data) + else: + return JsonResponse(ret) + + else: + return JsonResponse(result[1]) + + +def create(request, collection, useDate=False): + result = handleErrorMethod(request.method, 'POST') + if result[0]: + ret = decode_jwt_token(request.META.get('HTTP_AUTHORIZATION')) + if ret['code'] == 2000: + data = decodeBody(request) + if useDate: + data['date'] = datetime.now() + collection.insert_one(data) + return JsonResponse({"code": 2000, "msg": "添加成功"}) + else: + return JsonResponse(ret) + + else: + return JsonResponse(result[1]) + + +def update(request, collection, useDate=False): + result = handleErrorMethod(request.method, 'PUT') + if result[0]: + ret = decode_jwt_token(request.META.get('HTTP_AUTHORIZATION')) + if ret['code'] == 2000: + data = decodeBody(request) + if useDate: + data['date'] = datetime.now() + _id = data['_id'] + update_data = data['data'] + print(_id) + updateResult = collection.update_one({'_id': ObjectId(_id)}, {"$set": update_data}) + res = handleEmpty(updateResult.modified_count, '更新') + return JsonResponse(res) + else: + return JsonResponse(ret) + + else: + return JsonResponse(result[1]) diff --git a/serve/serve/models/request.py b/serve/serve/models/request.py index e69de29..77a6161 100644 --- a/serve/serve/models/request.py +++ b/serve/serve/models/request.py @@ -0,0 +1,10 @@ +from datetime import datetime + +from mongoengine import Document, EmbeddedDocument, fields, DateTimeField + + +class RequestModel(Document): + content = fields.StringField(max_length=30) + employeeName = fields.StringField(max_length=30) + status = fields.StringField(max_length=30) + date = DateTimeField(default=datetime.now) diff --git a/serve/serve/utils/__pycache__/decodeBody.cpython-311.pyc b/serve/serve/utils/__pycache__/decodeBody.cpython-311.pyc new file mode 100644 index 0000000..01ac8f8 Binary files /dev/null and b/serve/serve/utils/__pycache__/decodeBody.cpython-311.pyc differ diff --git a/serve/serve/utils/__pycache__/pyjwt.cpython-311.pyc b/serve/serve/utils/__pycache__/pyjwt.cpython-311.pyc index 65b50db..ab00651 100644 Binary files a/serve/serve/utils/__pycache__/pyjwt.cpython-311.pyc and b/serve/serve/utils/__pycache__/pyjwt.cpython-311.pyc differ diff --git a/serve/serve/utils/decodeBody.py b/serve/serve/utils/decodeBody.py new file mode 100644 index 0000000..015c56e --- /dev/null +++ b/serve/serve/utils/decodeBody.py @@ -0,0 +1,8 @@ +import json + + +# 解码字节字符串并解析为 JSON 数据 +def decodeBody(request): + body_str = request.body.decode('utf-8') + data = json.loads(body_str) + return data diff --git a/serve/serve/utils/pyjwt.py b/serve/serve/utils/pyjwt.py index 00c96a7..dee28f6 100644 --- a/serve/serve/utils/pyjwt.py +++ b/serve/serve/utils/pyjwt.py @@ -9,7 +9,7 @@ def get_jwt_token(username, password, role_data='default'): 生成jwt-token """ payload = { - 'exp': datetime.utcnow() + timedelta(seconds=300), # 单位秒 + 'exp': datetime.utcnow() + timedelta(seconds=999999999), # 单位秒 'iat': datetime.utcnow(), 'data': {'username': username, 'password': password, 'role_data': role_data} } diff --git a/serve/serve/views/__pycache__/index.cpython-311.pyc b/serve/serve/views/__pycache__/index.cpython-311.pyc index 094097a..a49c1d5 100644 Binary files a/serve/serve/views/__pycache__/index.cpython-311.pyc and b/serve/serve/views/__pycache__/index.cpython-311.pyc differ diff --git a/serve/serve/views/__pycache__/request.cpython-311.pyc b/serve/serve/views/__pycache__/request.cpython-311.pyc new file mode 100644 index 0000000..9b37c4d Binary files /dev/null and b/serve/serve/views/__pycache__/request.cpython-311.pyc differ diff --git a/serve/serve/views/__pycache__/user.cpython-311.pyc b/serve/serve/views/__pycache__/user.cpython-311.pyc index 3693cfe..57f9655 100644 Binary files a/serve/serve/views/__pycache__/user.cpython-311.pyc and b/serve/serve/views/__pycache__/user.cpython-311.pyc differ diff --git a/serve/serve/views/index.py b/serve/serve/views/index.py index 7adf145..c07cb4f 100644 --- a/serve/serve/views/index.py +++ b/serve/serve/views/index.py @@ -1,5 +1,7 @@ from serve.views.user import user_url +from serve.views.request import request_url def initUrls(urlpatterns): urlpatterns += user_url + urlpatterns += request_url diff --git a/serve/serve/views/request.py b/serve/serve/views/request.py index e69de29..04505ac 100644 --- a/serve/serve/views/request.py +++ b/serve/serve/views/request.py @@ -0,0 +1,29 @@ +from django.urls import path +from serve.utils.connectdb import connectdb +from serve.commonViews.commonViews import delete, getAll, create, update + +collection = connectdb('request_model') + + +def createRequest(request): + return create(request, collection, True) + + +def getAllRequest(request): + return getAll(request, collection) + + +def deleteRequest(request): + return delete(request, collection) + + +def updateRequest(request): + return update(request, collection, True) + + +request_url = [ + path('request/create', createRequest), + path('request/get-all', getAllRequest), + path('request/delete', deleteRequest), + path('request/update', updateRequest) +] diff --git a/serve/serve/views/user.py b/serve/serve/views/user.py index 872505a..7fa7875 100644 --- a/serve/serve/views/user.py +++ b/serve/serve/views/user.py @@ -1,77 +1,25 @@ from django.http import JsonResponse -import json from django.urls import path from serve.utils.connectdb import connectdb from bson.objectid import ObjectId from serve.middleware.handleErrorMethod import handleErrorMethod -from serve.middleware.handleEmpty import handleEmpty from serve.utils.pyjwt import get_jwt_token, decode_jwt_token +from serve.utils.decodeBody import decodeBody +from serve.commonViews.commonViews import delete, getAll, create, update collection = connectdb('user_model') -def handleUserData(user): - user['_id'] = str(user['_id']) - return user - - -# 解码字节字符串并解析为 JSON 数据 -def decodeBody(request): - body_str = request.body.decode('utf-8') - data = json.loads(body_str) - return data - - def createUser(request): - result = handleErrorMethod(request.method, 'POST') - if result[0]: - ret = decode_jwt_token(request.META.get('HTTP_AUTHORIZATION')) - if ret['code'] == 2000: - data = decodeBody(request) - collection.insert_one(data) - return JsonResponse({"code": 2000, "msg": "添加成功"}) - else: - return JsonResponse(ret) - - else: - return JsonResponse(result[1]) + return create(request, collection) def deleteUser(request): - result = handleErrorMethod(request.method, 'DELETE') - if result[0]: - ret = decode_jwt_token(request.META.get('HTTP_AUTHORIZATION')) - if ret['code'] == 2000: - data = decodeBody(request) - _ids = list(data['_ids']) - filter_criteria = {'_id': {'$in': [ObjectId(_id) for _id in _ids]}} - deleteResult = collection.delete_many(filter_criteria) - res = handleEmpty(deleteResult.deleted_count, '删除') - return JsonResponse(res) - else: - return JsonResponse(ret) - - else: - return JsonResponse(result[1]) + return delete(request, collection) def updateUser(request): - result = handleErrorMethod(request.method, 'PUT') - if result[0]: - ret = decode_jwt_token(request.META.get('HTTP_AUTHORIZATION')) - if ret['code'] == 2000: - data = decodeBody(request) - _id = data['_id'] - update_data = data['userData'] - print(_id) - updateResult = collection.update_one({'_id': ObjectId(_id)}, {"$set": update_data}) - res = handleEmpty(updateResult.modified_count, '更新用户') - return JsonResponse(res) - else: - return JsonResponse(ret) - - else: - return JsonResponse(result[1]) + return update(request, collection) def getUser(request): @@ -94,30 +42,7 @@ def getUser(request): def getAllUser(request): - result = handleErrorMethod(request.method, 'GET') - if result[0]: - ret = decode_jwt_token(request.META.get('HTTP_AUTHORIZATION')) - if ret['code'] == 2000: - pageSize = int(request.GET.get('pageSize', None)) - pageIndex = int(request.GET.get('pageIndex', None)) - # 计算要跳过的文档数量 - skip_count = (pageIndex - 1) * pageSize - # 执行分页查询 - cursor = collection.find().skip(skip_count).limit(pageSize) - # 将查询结果转换为列表 - result = [{**doc, '_id': str(doc['_id'])} for doc in cursor] - # 构建JSON格式的响应 - response_data = { - 'total': collection.count_documents({}), # 获取总文档数 - 'data': result, - 'code': 2000 - } - return JsonResponse(response_data) - else: - return JsonResponse(ret) - - else: - return JsonResponse(result[1]) + return getAll(request, collection) def loginUser(request):