from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.decorators import action
from rest_framework.exceptions import APIException
from rest_framework.generics import ListAPIView, UpdateAPIView
from rest_framework.viewsets import GenericViewSet
from exchangecode.filters import CodeFilter
from exchangecode.serializers.codesers import CodeSerializer
from exchangecode.models import Code, School, Area,Teacher
from exchangecode.common.response import CommonResponse
class CodeView(ListAPIView, UpdateAPIView):
authentication_classes = []
permission_classes = []
queryset = Code.objects.all()
serializer_class = CodeSerializer
filter_backends = [DjangoFilterBackend]
filterset_class = CodeFilter
def put(self, request, *args, **kwargs):
request.data["status"]=Code.status_emun.USED
return super().update(request, *args, **kwargs)
class CodeOperateView(GenericViewSet):
queryset = Code.objects.all()
serializer_class = CodeSerializer
# divide_code函数中,实现update的具体逻辑
def update_divide_code(self, instance_list, school_id, counter):
# 将对象转为字典,因为在反序列化的时候需要data信息
code_data = instance_list[counter].__dict__
code_data["school_id"] = school_id
# 从数据库取出来的code对象,由于batch是外键,取出来的字段名字是batch_id,这里为了反序列化成功,自己添加一个batch字段
code_data["batch"] = instance_list[counter].batch_id
ser = CodeSerializer(data=code_data, instance=instance_list[counter])
ret = ser.is_valid()
if not ret:
raise APIException(detail="dividecode时反序列化失败!")
ser.save()
# 参数:"batch_id": 要分配的批次id
# "shcool_num": { 要分配的分校id:对应分校的分配数量
# "3": 0,
# "2": 4}
# 将兑换码分配分校
@action(methods=['post'], detail=False)
def divide_code(self, request):
batch_id = request.data.get("batch_id")
school_num = request.data.get("shcool_num")
# 1.根据前端传的批次码,判断当前批次是否还有空闲兑换码
instance_list = self.get_queryset().filter(batch=batch_id, status=Code.status_emun.FREE)
if not instance_list:
raise APIException(detail="批次编号={}没有找到可用兑换码".format(batch_id))
# 2.判断兑换码要分配的分校和批次的校区是否对应
batch_area = instance_list[0].batch.area_id
for school in school_num.keys():
school_info = School.objects.filter(pk=school).first()
if school_info.area_id != batch_area:
raise APIException(
detail="当前批次为{},无法分配给{}".format(Area.objects.filter(pk=batch_area).first().area_name,
school_info.school_name))
# 3.兑换码数量校验
divide_num = sum(school_num.values())
free_code_num = len(instance_list)
if divide_num == 0:
raise APIException(detail="本次分配需要兑换码数 {} 无需分配".format(divide_num))
if free_code_num < divide_num:
raise APIException(detail="本次分配需要兑换码数{} > 当前批次剩余兑换码数{},无法分配".format(divide_num, free_code_num))
# 4.开始分配
counter = 0
for school, num in school_num.items():
for i in range(0, num):
self.update_divide_code(instance_list, school, counter)
counter += 1
# 分配总数<空闲数,将剩余未分配数据分校置为空
while counter < free_code_num:
self.update_divide_code(instance_list, None, counter)
counter += 1
return CommonResponse(data="success")
# 参数batch
@action(methods=["GET"],detail=False)
def get_code(self, request, *args, **kwargs):
# 1.获取用户信息,得到用户所在分校
# 无论是否是主管,都用teacher表里的所属分校信息(school)
teacher_info = Teacher.objects.filter(pk=request.auth.payload["user_id"]).first()
if not teacher_info:
raise APIException("没有找到当前登录老师信息,请确认后重新操作")
if not teacher_info.is_active:
raise APIException("当前登录老师为离职状态,请确认后重新操作")
teacher_school=teacher_info.school_id
# 2.根据批次id和老师所在分校,获取空闲兑换码
batch_id = request.query_params.get("batch")
code = Code.objects.filter(batch=batch_id,school_id=teacher_school,status=Code.status_emun.FREE).first()
code_data={}
if not code:
code_data["error"]="未在batch={}批次中找到分校={}的可用兑换码去,请联系主管进行调整!".format(batch_id,teacher_info.school)
else:
code_data = {"id":code.pk,"code":code.code_name}
return CommonResponse(data=code_data)
# 参数:batch
@action(methods=["GET"],detail=False)
def free_code_num(self,request):
# 从前端拿到批次号
batch_id = request.query_params.get("batch")
# 根据批次号查库
total = self.get_queryset().filter(batch=batch_id).count()
used_total = self.get_queryset().filter(batch=batch_id,status=Code.status_emun.USED).count()
free_total = self.get_queryset().filter(batch=batch_id,status=Code.status_emun.FREE).count()
divide_total = self.get_queryset().filter(batch=batch_id).exclude(school_id=None).count()
not_divide_total = total-divide_total
divide_free_total=self.get_queryset().filter(batch=batch_id,status=Code.status_emun.FREE).exclude(school_id=None).count()
num_data = {"total":total,"free_total":free_total,"used_total":used_total,
"divide_total":divide_total,"not_divide_total":not_divide_total,
"divide_free_total":divide_free_total,"detail":{}}
if free_total != 0:
# 根据批次找到区域,再拿区域下所有的分校
area = self.get_queryset().filter(batch=batch_id).first().batch.area_id
school_list=School.objects.filter(area=area)
for school in school_list:
school_id = school.pk
num = self.get_queryset().filter(batch=batch_id,status=Code.status_emun.FREE,school_id=school_id).count()
num_data["detail"][school.school_name]=num
return CommonResponse(data=num_data)
没有合适的资源?快使用搜索试试~ 我知道了~
教培后台管理系统(python)
共81个文件
py:37个
pyc:33个
xml:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 194 浏览量
2023-06-15
07:09:48
上传
评论
收藏 64KB ZIP 举报
温馨提示
教培后台管理系统(python)
资源推荐
资源详情
资源评论
收起资源包目录
siteproject-master.zip (81个子文件)
siteproject-master
siteproject
__init__.py 154B
wsgi.py 399B
urls.py 939B
settings.py 7KB
__pycache__
__init__.cpython-310.pyc 265B
wsgi.cpython-310.pyc 576B
settings.cpython-310.pyc 4KB
urls.cpython-310.pyc 1KB
asgi.py 399B
db.sqlite3 0B
.idea
vcs.xml 180B
misc.xml 186B
dataSources.xml 631B
siteproject.iml 1KB
inspectionProfiles
Project_Default.xml 446B
profiles_settings.xml 174B
modules.xml 274B
.gitignore 176B
manage.py 667B
exchangecode
__init__.py 0B
tests.py 61B
admin.py 63B
migrations
__init__.py 0B
0001_initial.py 6KB
0002_rename_area_id_teacher_area_and_more.py 499B
0003_alter_teacher_area_alter_teacher_school.py 786B
__pycache__
0002_rename_area_id_teacher_area_and_more.cpython-310.pyc 629B
0001_initial.cpython-310.pyc 4KB
__init__.cpython-310.pyc 177B
0003_alter_teacher_area_alter_teacher_school.cpython-310.pyc 858B
constant.py 53B
apps.py 190B
models.py 3KB
adminx.py 337B
common
exceptionhandel.py 689B
response.py 410B
__pycache__
views.cpython-310.pyc 1KB
exceptionhandel.cpython-310.pyc 696B
response.cpython-310.pyc 745B
userinfo.py 357B
views.py 1KB
urls.py 2KB
serializers
__init__.py 0B
schoolsers.py 205B
batchsers.py 3KB
areasers.py 199B
teachersers.py 3KB
codesers.py 520B
serializer1111s.py 4KB
__pycache__
schoolsers.cpython-310.pyc 650B
batchsers.cpython-310.pyc 3KB
codesers.cpython-310.pyc 871B
__init__.cpython-310.pyc 178B
teachersers.cpython-310.pyc 2KB
areasers.cpython-310.pyc 642B
__pycache__
constant.cpython-310.pyc 228B
filters.cpython-310.pyc 2KB
adminx.cpython-310.pyc 631B
__init__.cpython-310.pyc 166B
apps.cpython-310.pyc 494B
admin.cpython-310.pyc 207B
urls.cpython-310.pyc 1KB
models.cpython-310.pyc 4KB
views
__init__.py 0B
batch.py 1KB
area.py 272B
teacher.py 734B
code.py 7KB
school.py 711B
__pycache__
login.cpython-310.pyc 754B
code.cpython-310.pyc 5KB
school.cpython-310.pyc 1KB
area.cpython-310.pyc 591B
batch.cpython-310.pyc 1KB
__init__.cpython-310.pyc 172B
teacher.cpython-310.pyc 999B
login.py 414B
filters.py 1KB
.gitignore 1KB
__pycache__
manage.cpython-310.pyc 841B
log
debug.log 6KB
共 81 条
- 1
资源评论
Roc-xb
- 粉丝: 12w+
- 资源: 8207
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功