Files
django-vue3-admin-gd/backend/utils/decorators.py

28 lines
856 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from rest_framework.response import Response
from rest_framework import status
from utils.idempotency_helper import generate_idempotency_key, check_idempotency, get_user_identifier
def idempotent(timeout=10):
"""
幂等性装饰器用于单个DRF接口
:param timeout: 重复判断时间窗口(秒)
"""
def decorator(view_func):
def wrapper(self, request, *args, **kwargs):
user_id = get_user_identifier(request)
key = generate_idempotency_key(user_id, request.path, request.body)
if check_idempotency(key, timeout):
return Response(
{"error": "请勿重复提交"},
status=status.HTTP_409_CONFLICT
)
return view_func(self, request, *args, **kwargs)
return wrapper
return decorator