Compare commits

...

9 Commits

Author SHA1 Message Date
8e91884d75 fix list submissions 2025-12-22 18:29:18 +08:00
d1b5b67725 update 2025-12-20 22:42:05 +08:00
f43b1c62f6 update 2025-09-04 18:13:12 +08:00
36c795e6ec update 2025-09-04 17:31:08 +08:00
ebfe08a97c update 2025-09-04 17:20:55 +08:00
bb41f05bbe update 2025-07-16 01:15:15 +08:00
eb09245467 fix 2025-07-16 01:10:27 +08:00
108a125810 add ws 2025-07-16 01:02:12 +08:00
01e5c9097c update 2025-07-15 17:20:20 +08:00
15 changed files with 894 additions and 128 deletions

View File

@@ -1,4 +1,4 @@
FROM python:3.12-slim as builder
FROM python:3.13-slim AS builder
WORKDIR /app
@@ -18,12 +18,12 @@ RUN pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/web/simple
&& pip install --no-cache-dir -r requirements.txt
# 最终阶段
FROM python:3.12-slim
FROM python:3.13-slim
WORKDIR /app
# 从builder阶段复制Python包
COPY --from=builder /usr/local/lib/python3.12/site-packages/ /usr/local/lib/python3.12/site-packages/
COPY --from=builder /usr/local/lib/python3.13/site-packages/ /usr/local/lib/python3.13/site-packages/
COPY --from=builder /usr/local/bin/ /usr/local/bin/
# 复制应用代码

View File

@@ -9,8 +9,20 @@ https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
import os
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application
from chat.url import websocket_urlpatterns
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "api.settings")
application = get_asgi_application()
application = ProtocolTypeRouter(
{
"http": get_asgi_application(),
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
),
}
)

View File

@@ -36,11 +36,13 @@ if DEV:
CSRF_TRUSTED_ORIGINS = ["http://localhost:3000"]
else:
ALLOWED_HOSTS = ["web.xuyue.cc", "10.13.114.114"]
CSRF_TRUSTED_ORIGINS = ["https://web.xuyue.cc", "http://10.13.114.114"]
CSRF_TRUSTED_ORIGINS = ["https://web.xuyue.cc", "http://10.13.114.114:91"]
# Application definition
INSTALLED_APPS = [
"daphne",
"channels",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
@@ -53,6 +55,7 @@ INSTALLED_APPS = [
"account",
"task",
"submission",
"chat",
]
MIDDLEWARE = [
@@ -85,7 +88,7 @@ TEMPLATES = [
]
WSGI_APPLICATION = "api.wsgi.application"
ASGI_APPLICATION = "api.asgi.application"
# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
@@ -104,7 +107,7 @@ PROD_DATABASES = {
"USER": os.getenv("POSTGRES_USER"),
"PASSWORD": os.getenv("POSTGRES_PASSWORD"),
"HOST": os.getenv("POSTGRES_HOST"),
"PORT": "5432",
"PORT": os.getenv("POSTGRES_PORT", "5432"),
},
}
@@ -129,6 +132,16 @@ else:
# 配置缓存
CACHES = PROD_CACHES
# WebSocket 的缓存
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(os.getenv("REDIS_HOST"), 6379)],
},
},
}
# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators

0
chat/__init__.py Normal file
View File

0
chat/api.py Normal file
View File

6
chat/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class ChatConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'chat'

17
chat/consumers.py Normal file
View File

@@ -0,0 +1,17 @@
import json
from channels.generic.websocket import WebsocketConsumer
class ChatConsumer(WebsocketConsumer):
def connect(self):
self.accept()
def disconnect(self, close_code):
pass
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json["message"]
self.send(text_data=json.dumps({"message": message}))

View File

3
chat/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

6
chat/url.py Normal file
View File

@@ -0,0 +1,6 @@
from django.urls import path
from .consumers import ChatConsumer
websocket_urlpatterns = [
path("ws/chat/", ChatConsumer.as_asgi()),
]

View File

@@ -13,7 +13,9 @@ dependencies = [
"psycopg[binary]>=3.2.5",
"pydantic[email]>=2.10.6",
"python-dotenv>=1.0.1",
"uvicorn>=0.34.0",
"uvicorn[standard]>=0.34.0",
"django-redis>=5.4.0",
"redis>=5.0.1",
"channels[daphne]>=4.2.2",
"channels-redis>=4.2.1"
]

View File

@@ -1,24 +1,53 @@
annotated-types==0.7.0
asgiref==3.8.1
click==8.2.1
django==5.2.3
django-cors-headers==4.7.0
anyio==4.12.0
asgiref==3.11.0
attrs==25.4.0
autobahn==25.12.2
automat==25.4.16
cbor2==5.7.1
cffi==2.0.0
channels==4.3.2
channels-redis==4.3.0
click==8.3.1
constantly==23.10.4
cryptography==46.0.3
daphne==4.2.1
django==6.0
django-cors-headers==4.9.0
django-extensions==4.1
django-ninja==1.4.3
django-redis==5.4.0
dnspython==2.7.0
email-validator==2.2.0
django-ninja==1.5.1
django-redis==6.0.0
dnspython==2.8.0
email-validator==2.3.0
gunicorn==23.0.0
h11==0.16.0
idna==3.10
httptools==0.7.1
hyperlink==21.0.0
idna==3.11
incremental==24.11.0
msgpack==1.1.2
packaging==25.0
psycopg==3.2.9
psycopg-binary==3.2.9
pydantic==2.11.7
pydantic-core==2.33.2
python-dotenv==1.1.0
redis==6.2.0
sqlparse==0.5.3
typing-extensions==4.14.0
typing-inspection==0.4.1
uvicorn==0.34.3
psycopg==3.3.2
psycopg-binary==3.3.2
py-ubjson==0.16.1
pyasn1==0.6.1
pyasn1-modules==0.4.2
pycparser==2.23
pydantic==2.12.5
pydantic-core==2.41.5
pyopenssl==25.3.0
python-dotenv==1.2.1
pyyaml==6.0.3
redis==7.1.0
service-identity==24.2.0
sqlparse==0.5.5
twisted==25.5.0
txaio==25.12.2
typing-extensions==4.15.0
typing-inspection==0.4.2
ujson==5.11.0
uvicorn==0.38.0
uvloop==0.22.1
watchfiles==1.1.1
websockets==15.0.1
zope-interface==8.1.1

View File

@@ -5,6 +5,7 @@ from ninja.errors import HttpError
from ninja.pagination import paginate
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import login_required
from django.db.models import OuterRef, Subquery, IntegerField
from .schemas import (
@@ -55,19 +56,20 @@ def list_submissions(request, filters: SubmissionFilter = Query(...)):
if filters.username:
submissions = submissions.filter(user__username__icontains=filters.username)
# 获取所有提交
submissions = submissions.prefetch_related("ratings")
user_rating_subquery = Subquery(
Rating.objects.filter(user=request.user, submission=OuterRef("pk")).values(
"score"
)[:1],
output_field=IntegerField(),
)
submissions = submissions.annotate(my_score=user_rating_subquery)
# 获取当前用户的评分
user_ratings = {
rating.submission_id: rating.score
for rating in Rating.objects.filter(
user=request.user,
submission__in=submissions
)
}
def get_submission_data(submission):
"""从 submission 对象构建 SubmissionOut 数据"""
my_score = getattr(submission, "my_score", None) or 0
return SubmissionOut.list(submission, {submission.id: my_score})
return [SubmissionOut.list(submission, user_ratings) for submission in submissions]
return [get_submission_data(submission) for submission in submissions]
@router.get("/{submission_id}", response=SubmissionOut)

850
uv.lock generated

File diff suppressed because it is too large Load Diff