添加一言
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from ..views import JudgeServerHeartbeatAPI, LanguagesAPI, WebsiteConfigAPI
|
from ..views import HitokotoAPI, JudgeServerHeartbeatAPI, LanguagesAPI, WebsiteConfigAPI
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("website", WebsiteConfigAPI.as_view()),
|
path("website", WebsiteConfigAPI.as_view()),
|
||||||
# 这里必须要有 /
|
# 这里必须要有 /
|
||||||
path("judge_server_heartbeat/", JudgeServerHeartbeatAPI.as_view()),
|
path("judge_server_heartbeat/", JudgeServerHeartbeatAPI.as_view()),
|
||||||
path("languages", LanguagesAPI.as_view())
|
path("languages", LanguagesAPI.as_view()),
|
||||||
|
path("hitokoto", HitokotoAPI.as_view()),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import random
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import smtplib
|
import smtplib
|
||||||
@@ -20,6 +21,7 @@ from options.options import SysOptions
|
|||||||
from problem.models import Problem
|
from problem.models import Problem
|
||||||
from submission.models import Submission
|
from submission.models import Submission
|
||||||
from utils.api import APIView, CSRFExemptAPIView, validate_serializer
|
from utils.api import APIView, CSRFExemptAPIView, validate_serializer
|
||||||
|
from utils.cache import JsonDataLoader
|
||||||
from utils.shortcuts import send_email, get_env
|
from utils.shortcuts import send_email, get_env
|
||||||
from utils.xss_filter import XSSHtml
|
from utils.xss_filter import XSSHtml
|
||||||
from .models import JudgeServer
|
from .models import JudgeServer
|
||||||
@@ -295,10 +297,21 @@ class RandomUsernameAPI(APIView):
|
|||||||
classroom = request.GET.get("classroom", "")
|
classroom = request.GET.get("classroom", "")
|
||||||
if not classroom:
|
if not classroom:
|
||||||
return self.error("需要班级号")
|
return self.error("需要班级号")
|
||||||
usernames = User.objects.filter(username__istartswith=classroom).values_list(
|
usernames = (
|
||||||
"username", flat=True
|
User.objects.filter(username__istartswith=classroom)
|
||||||
).order_by("?")
|
.values_list("username", flat=True)
|
||||||
|
.order_by("?")
|
||||||
|
)
|
||||||
if len(usernames) > 10:
|
if len(usernames) > 10:
|
||||||
return self.success(usernames[:10])
|
return self.success(usernames[:10])
|
||||||
else:
|
else:
|
||||||
return self.success(usernames)
|
return self.success(usernames)
|
||||||
|
|
||||||
|
|
||||||
|
class HitokotoAPI(APIView):
|
||||||
|
def get(self, request):
|
||||||
|
categories = JsonDataLoader.load_data(settings.HITOKOTO_DIR, "categories.json")
|
||||||
|
path = random.choice(categories).get("path")
|
||||||
|
sentences = JsonDataLoader.load_data(settings.HITOKOTO_DIR, path)
|
||||||
|
sentence = random.choice(sentences)
|
||||||
|
return self.success(sentence)
|
||||||
|
|||||||
1
data/hitokoto
Submodule
1
data/hitokoto
Submodule
Submodule data/hitokoto added at b503a98f23
@@ -136,6 +136,8 @@ AVATAR_UPLOAD_DIR = f"{DATA_DIR}{AVATAR_URI_PREFIX}"
|
|||||||
UPLOAD_PREFIX = "/public/upload"
|
UPLOAD_PREFIX = "/public/upload"
|
||||||
UPLOAD_DIR = f"{DATA_DIR}{UPLOAD_PREFIX}"
|
UPLOAD_DIR = f"{DATA_DIR}{UPLOAD_PREFIX}"
|
||||||
|
|
||||||
|
HITOKOTO_DIR = os.path.join(DATA_DIR, "hitokoto")
|
||||||
|
|
||||||
STATICFILES_DIRS = [os.path.join(DATA_DIR, "public")]
|
STATICFILES_DIRS = [os.path.join(DATA_DIR, "public")]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from django.core.cache import cache, caches # noqa
|
import json
|
||||||
from django.conf import settings # noqa
|
from pathlib import Path
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
from django_redis.cache import RedisCache
|
from django_redis.cache import RedisCache
|
||||||
from django_redis.client.default import DefaultClient
|
from django_redis.client.default import DefaultClient
|
||||||
@@ -25,3 +26,22 @@ class MyRedisCache(RedisCache):
|
|||||||
|
|
||||||
def __getattr__(self, item):
|
def __getattr__(self, item):
|
||||||
return getattr(self.client, item)
|
return getattr(self.client, item)
|
||||||
|
|
||||||
|
|
||||||
|
class JsonDataLoader:
|
||||||
|
@classmethod
|
||||||
|
def load_data(cls, dir, filename):
|
||||||
|
cache_key = f"json_data_{filename}"
|
||||||
|
if cached := cache.get(cache_key):
|
||||||
|
return cached
|
||||||
|
|
||||||
|
file_path = Path(dir, filename)
|
||||||
|
try:
|
||||||
|
with open(file_path, "r", encoding="utf-8") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
cache.set(cache_key, data, timeout=7200)
|
||||||
|
return data
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise ValueError(f"Data file {filename} not found")
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
raise ValueError(f"Invalid JSON format in {filename}")
|
||||||
|
|||||||
Reference in New Issue
Block a user