Files
OnlineJudge/utils/shortcuts.py
yuetsh fda7515263 refactor: 移除注册验证码,删掉 pillow
验证码只剩注册一处调用方,一并去掉。

- UserRegisterAPI 的校验、UserRegisterSerializer 的 captcha 字段、
  发图的 captcha 路由
- utils/captcha/ 整个包删除,含 Menlo.ttc 和 timesbi.ttf 两个字体
- utils/shortcuts.py 的 img2base64(),唯一调用方是验证码视图
- 依赖删除 pillow(本地实测 PIL/ 5.9M + pillow.libs/ 14M)

注意:注册接口现在只有 SysOptions.allow_register 一个开关,没有限流,
throttling 目前只用在 SubmissionAPI 上。内网自用可以接受,站点若暴露到
公网需要补 IP 维度限流。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-05 23:48:00 -06:00

65 lines
2.0 KiB
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.
import os
import random
import re
from django.utils.crypto import get_random_string
def rand_str(length=32, type="lower_hex"):
"""
生成指定长度的随机字符串或者数字, 可以用于密钥等安全场景
:param length: 字符串或者数字的长度
:param type: str 代表随机字符串num 代表随机数字
:return: 字符串
"""
if type == "str":
return get_random_string(length, allowed_chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
elif type == "lower_str":
return get_random_string(length, allowed_chars="abcdefghijklmnopqrstuvwxyz0123456789")
elif type == "lower_hex":
return random.choice("123456789abcdef") + get_random_string(length - 1, allowed_chars="0123456789abcdef")
else:
return random.choice("123456789") + get_random_string(length - 1, allowed_chars="0123456789")
def build_query_string(kv_data, ignore_none=True):
# {"a": 1, "b": "test"} -> "?a=1&b=test"
query_string = ""
for k, v in kv_data.items():
if ignore_none is True and kv_data[k] is None:
continue
if query_string != "":
query_string += "&"
else:
query_string = "?"
query_string += k + "=" + str(v)
return query_string
def datetime2str(value, format="iso-8601"):
if format.lower() == "iso-8601":
value = value.isoformat()
if value.endswith("+00:00"):
value = value[:-6] + "Z"
return value
return value.strftime(format)
def natural_sort_key(s, _nsre=re.compile(r"(\d+)")):
return [int(text) if text.isdigit() else text.lower() for text in re.split(_nsre, s)]
def get_env(name, default=""):
return os.environ.get(name, default)
def DRAMATIQ_WORKER_ARGS(time_limit=3600_000, max_retries=0, max_age=7200_000):
return {"max_retries": max_retries, "time_limit": time_limit, "max_age": max_age}
def check_is_id(value):
try:
return int(value) > 0
except Exception:
return False