fix:exam 修改考试的证书

This commit is contained in:
zty 2024-07-29 16:06:25 +08:00
parent aa37c99ac4
commit 03b273d6cd
2 changed files with 41 additions and 53 deletions

View File

@ -688,8 +688,11 @@ class ExamRecordViewSet(ListModelMixin, DestroyModelMixin, RetrieveModelMixin, G
courese_ids = [i.id for i in course] courese_ids = [i.id for i in course]
current_date = now_data.strftime('%Y-%m-%d') current_date = now_data.strftime('%Y-%m-%d')
cer_number = now_data.strftime('%Y%m%d') cer_number = now_data.strftime('%Y%m%d')
# redis 连接并获取锁 # 获取锁
cache.set("certificate", str(uuid.uuid4()), timeout=60) iden = acquire_lock('certificate')
if iden is None:
raise ParseError("系统忙,请稍后再试。", status=503)
try:
# 查询证明编号创建时间为最后一个 # 查询证明编号创建时间为最后一个
cer = Certificate.objects.latest('证书编号') cer = Certificate.objects.latest('证书编号')
if cer: if cer:
@ -711,10 +714,12 @@ class ExamRecordViewSet(ListModelMixin, DestroyModelMixin, RetrieveModelMixin, G
serializer = CertificateSerializer(data=data_dict) serializer = CertificateSerializer(data=data_dict)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
serializer.save() serializer.save()
val = cache.get("certificate") except Exception as e:
if val: # 如果存在,则删除 import traceback
# 释放锁 raise ParseError(traceback.print_exc())
cache.delete("certificate") finally:
release_lock('certificate', iden)
er.took = (now - er.create_time).total_seconds() er.took = (now - er.create_time).total_seconds()
er.end_time = now er.end_time = now
er.belong_dept=request.user.dept er.belong_dept=request.user.dept
@ -723,31 +728,14 @@ class ExamRecordViewSet(ListModelMixin, DestroyModelMixin, RetrieveModelMixin, G
return Response(ExamRecordListSerializer(instance=er).data) return Response(ExamRecordListSerializer(instance=er).data)
def acquire_lock(redis_conn, lock_name, ttl): def acquire_lock(lock_name, timeout=60):
identifier = str(uuid.uuid4()) identifier = str(uuid.uuid4())
lock = redis_conn.set(lock_name, identifier, nx=True, ex=ttl) lock = cache.add(lock_name, identifier, timeout=timeout, nx=True)
return identifier if lock else None return identifier if lock else None
def release_lock(lock_name, identifier):
import redis if cache.get(lock_name) == identifier:
def release_lock(redis_conn, lock_name, identifier): cache.delete(lock_name)
with redis_conn.pipeline() as pipe:
try:
while True:
try:
pipe.watch(lock_name)
# 使用 multi/exec 模式来确保原子性
if pipe.get(lock_name) == identifier:
pipe.multi()
pipe.delete(lock_name)
pipe.execute()
return True return True
# 如果锁已经被修改,取消监视 else:
pipe.unwatch() raise ParseError('Lock identifier does not match')
return False
except redis.exceptions.WatchError:
# 如果在监视期间锁被修改,重试
continue
except redis.exceptions.RedisError as e:
print(f"Error releasing lock: {e}")
return False

View File

@ -1,5 +1,5 @@
from .settings import * from .settings import *
DEBUG = True DEBUG = False
DATABASES = { DATABASES = {
# 'default': { # 'default': {
# 'ENGINE': 'django.db.backends.postgresql', # 'ENGINE': 'django.db.backends.postgresql',