style: ruff format 全仓库
行宽 180 下把历史遗留的折行表达式合并,无语义改动。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -151,7 +151,9 @@ def check_contest_permission(check_type="details"):
|
||||
if error:
|
||||
return error
|
||||
return await func(*args, **kwargs)
|
||||
|
||||
return _wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
|
||||
@@ -17,14 +17,10 @@ class Command(BaseCommand):
|
||||
dry_run = options["dry_run"]
|
||||
|
||||
# 所有现存非比赛题目的 PK 集合
|
||||
existing_ids = set(
|
||||
Problem.objects.filter(contest__isnull=True).values_list("id", flat=True)
|
||||
)
|
||||
existing_ids = set(Problem.objects.filter(contest__isnull=True).values_list("id", flat=True))
|
||||
self.stdout.write(f"现存题库题目数: {len(existing_ids)}")
|
||||
|
||||
profiles = UserProfile.objects.select_related("user").exclude(
|
||||
acm_problems_status={}
|
||||
)
|
||||
profiles = UserProfile.objects.select_related("user").exclude(acm_problems_status={})
|
||||
total = profiles.count()
|
||||
self.stdout.write(f"检查用户数: {total}{'(dry-run 模式)' if dry_run else ''}")
|
||||
|
||||
@@ -38,17 +34,11 @@ class Command(BaseCommand):
|
||||
if not stale_keys:
|
||||
continue
|
||||
|
||||
removed_accepted = sum(
|
||||
1
|
||||
for k in stale_keys
|
||||
if problems[k].get("status") in ACCEPTED_STATUSES
|
||||
)
|
||||
removed_accepted = sum(1 for k in stale_keys if problems[k].get("status") in ACCEPTED_STATUSES)
|
||||
|
||||
stale_display = [problems[k].get("_id", k) for k in stale_keys]
|
||||
self.stdout.write(
|
||||
f" 用户 {profile.user.username}"
|
||||
f" | 删除 {len(stale_keys)} 题: {', '.join(stale_display)}"
|
||||
f"{f' | 其中已AC {removed_accepted} 题' if removed_accepted else ''}"
|
||||
f" 用户 {profile.user.username} | 删除 {len(stale_keys)} 题: {', '.join(stale_display)}{f' | 其中已AC {removed_accepted} 题' if removed_accepted else ''}"
|
||||
)
|
||||
|
||||
if dry_run:
|
||||
|
||||
@@ -13,11 +13,6 @@ def send_email_async(from_name, to_email, to_name, subject, content):
|
||||
if not SysOptions.smtp_config:
|
||||
return
|
||||
try:
|
||||
send_email(smtp_config=SysOptions.smtp_config,
|
||||
from_name=from_name,
|
||||
to_email=to_email,
|
||||
to_name=to_name,
|
||||
subject=subject,
|
||||
content=content)
|
||||
send_email(smtp_config=SysOptions.smtp_config, from_name=from_name, to_email=to_email, to_name=to_name, subject=subject, content=content)
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
|
||||
@@ -61,12 +61,7 @@ class UserAdminAPI(APIView):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
ret = User.objects.bulk_create(user_list)
|
||||
UserProfile.objects.bulk_create(
|
||||
[
|
||||
UserProfile(user=ret[i], real_name=data[i][3])
|
||||
for i in range(len(ret))
|
||||
]
|
||||
)
|
||||
UserProfile.objects.bulk_create([UserProfile(user=ret[i], real_name=data[i][3]) for i in range(len(ret))])
|
||||
return self.success()
|
||||
except IntegrityError as e:
|
||||
# Extract detail from exception message
|
||||
@@ -85,17 +80,9 @@ class UserAdminAPI(APIView):
|
||||
user = User.objects.get(id=data["id"])
|
||||
except User.DoesNotExist:
|
||||
return self.error("User does not exist")
|
||||
if (
|
||||
User.objects.filter(username=data["username"].lower())
|
||||
.exclude(id=user.id)
|
||||
.exists()
|
||||
):
|
||||
if User.objects.filter(username=data["username"].lower()).exclude(id=user.id).exists():
|
||||
return self.error("Username already exists")
|
||||
if (
|
||||
User.objects.filter(email=data["email"].lower())
|
||||
.exclude(id=user.id)
|
||||
.exists()
|
||||
):
|
||||
if User.objects.filter(email=data["email"].lower()).exclude(id=user.id).exists():
|
||||
return self.error("Email already exists")
|
||||
|
||||
pre_username = user.username
|
||||
@@ -136,9 +123,7 @@ class UserAdminAPI(APIView):
|
||||
|
||||
user.save()
|
||||
if pre_username != user.username:
|
||||
Submission.objects.filter(username=pre_username).update(
|
||||
username=user.username
|
||||
)
|
||||
Submission.objects.filter(username=pre_username).update(username=user.username)
|
||||
|
||||
UserProfile.objects.filter(user=user).update(real_name=data["real_name"])
|
||||
return self.success(UserAdminSerializer(user).data)
|
||||
@@ -158,7 +143,7 @@ class UserAdminAPI(APIView):
|
||||
|
||||
# 获取排序参数
|
||||
order_by = request.GET.get("order_by", "")
|
||||
|
||||
|
||||
# 根据排序参数设置排序规则
|
||||
if order_by == "-last_login":
|
||||
# 最近登录,将 None 值放在最后
|
||||
@@ -174,11 +159,7 @@ class UserAdminAPI(APIView):
|
||||
|
||||
keyword = request.GET.get("keyword", None)
|
||||
if keyword:
|
||||
user = user.filter(
|
||||
Q(username__icontains=keyword)
|
||||
| Q(userprofile__real_name__icontains=keyword)
|
||||
| Q(email__icontains=keyword)
|
||||
)
|
||||
user = user.filter(Q(username__icontains=keyword) | Q(userprofile__real_name__icontains=keyword) | Q(email__icontains=keyword))
|
||||
return self.success(self.paginate_data(request, user, UserAdminSerializer))
|
||||
|
||||
@super_admin_required
|
||||
@@ -223,9 +204,7 @@ class GenerateUserAPI(APIView):
|
||||
Generate User
|
||||
"""
|
||||
data = request.data
|
||||
number_max_length = max(
|
||||
len(str(data["number_from"])), len(str(data["number_to"]))
|
||||
)
|
||||
number_max_length = max(len(str(data["number_from"])), len(str(data["number_to"])))
|
||||
if number_max_length + len(data["prefix"]) + len(data["suffix"]) > 32:
|
||||
return self.error("Username should not more than 32 characters")
|
||||
if data["number_from"] > data["number_to"]:
|
||||
@@ -253,9 +232,7 @@ class GenerateUserAPI(APIView):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
ret = User.objects.bulk_create(user_list)
|
||||
UserProfile.objects.bulk_create(
|
||||
[UserProfile(user=user) for user in ret]
|
||||
)
|
||||
UserProfile.objects.bulk_create([UserProfile(user=user) for user in ret])
|
||||
for item in user_list:
|
||||
worksheet.write_string(i, 0, item.username)
|
||||
worksheet.write_string(i, 1, item.raw_password)
|
||||
@@ -277,17 +254,17 @@ class ResetUserPasswordAPI(APIView):
|
||||
"""
|
||||
data = request.data
|
||||
user_id = data["id"]
|
||||
|
||||
|
||||
try:
|
||||
user = User.objects.get(id=user_id)
|
||||
except User.DoesNotExist:
|
||||
return self.error("User does not exist")
|
||||
|
||||
|
||||
# 生成6位随机数字密码(不包括0)
|
||||
new_password = get_random_string(6, allowed_chars="123456789")
|
||||
|
||||
|
||||
# 设置新密码
|
||||
user.set_password(new_password)
|
||||
user.save()
|
||||
|
||||
return self.success(new_password)
|
||||
|
||||
return self.success(new_password)
|
||||
|
||||
@@ -431,11 +431,16 @@ class UserRankAPI(AsyncAPIView):
|
||||
except ValueError:
|
||||
n = 0
|
||||
|
||||
profiles = UserProfile.objects.filter(
|
||||
user__admin_type__in=[AdminType.REGULAR_USER, AdminType.STUDENT_ADMIN],
|
||||
user__is_disabled=False,
|
||||
user__username__icontains=username,
|
||||
).select_related("user").filter(accepted_number__gte=0).order_by("-accepted_number", "submission_number")
|
||||
profiles = (
|
||||
UserProfile.objects.filter(
|
||||
user__admin_type__in=[AdminType.REGULAR_USER, AdminType.STUDENT_ADMIN],
|
||||
user__is_disabled=False,
|
||||
user__username__icontains=username,
|
||||
)
|
||||
.select_related("user")
|
||||
.filter(accepted_number__gte=0)
|
||||
.order_by("-accepted_number", "submission_number")
|
||||
)
|
||||
if n > 0:
|
||||
profiles = profiles[:n]
|
||||
return self.success(await self.async_paginate_data(request, profiles, RankInfoSerializer))
|
||||
@@ -457,12 +462,7 @@ class UserActivityRankAPI(AsyncAPIView):
|
||||
create_time__gte=start,
|
||||
result__in=[JudgeStatus.ACCEPTED, JudgeStatus.AST_CHECK_FAILED],
|
||||
).exclude(username__in=hidden_names)
|
||||
data = [
|
||||
row
|
||||
async for row in submissions.values("username")
|
||||
.annotate(count=Count("problem_id", distinct=True))
|
||||
.order_by("-count")[:10]
|
||||
]
|
||||
data = [row async for row in submissions.values("username").annotate(count=Count("problem_id", distinct=True)).order_by("-count")[:10]]
|
||||
await async_cache_set(cache_key, data, 600)
|
||||
return self.success(data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user