Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加了批量毕业指定年级学生的脚本 #842

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions dm/management/commands/autograduate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.core.management.base import BaseCommand, CommandParser, CommandError
from django.db import transaction

from app.models import (
NaturalPerson
)

class Command(BaseCommand):
help = '通过学号检定批量调整指定年级的学生状态为“已毕业”,延迟毕业的学生需通过管理站点手动再次调整回来'

def add_arguments(self, parser: CommandParser):
parser.add_argument(
'--year',
type=int,
help='指定年级,以两位方式填写(例如20)'
)

def handle(self, *args, **options):
year = options['year']

# 检查参数是否为空
if year is None:
raise CommandError('请指定年级参数')
# 检查年份是否合法
if year < 15 or year > 29:
Morgen-Kornblume marked this conversation as resolved.
Show resolved Hide resolved
raise CommandError('年级参数不合法')

with transaction.atomic():
# 选出指定年级的自然人(通过学号前两位判断,person_id为学号)
people = NaturalPerson.objects.filter(person_id__username__startswith=str(year))
for person in people:
if person.identity == NaturalPerson.Identity.TEACHER:
#防止误伤教师
continue
person.status = NaturalPerson.GraduateStatus.GRADUATED
person.save()
Morgen-Kornblume marked this conversation as resolved.
Show resolved Hide resolved
# 打印结果
self.stdout.write(self.style.SUCCESS('成功将%d级学生状态调整为“已毕业”' % year))

Morgen-Kornblume marked this conversation as resolved.
Show resolved Hide resolved
Loading