Compare commits

..

19 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
403d3db941 update 2025-07-15 17:16:48 +08:00
34b9822a3d update 2025-06-15 23:11:52 +08:00
313e326316 fix 2025-06-15 23:07:48 +08:00
30becb56bc update 2025-06-15 23:02:47 +08:00
ac3c7c4dec fix 2025-06-15 22:58:00 +08:00
452b4039fe test 2025-06-15 22:32:59 +08:00
3502ac0c42 test 2025-06-15 22:31:55 +08:00
2dc1c3b005 test 2025-06-15 22:28:48 +08:00
641d4a84b4 test 2025-06-15 22:23:03 +08:00
7edeea299e test 2025-06-15 22:13:35 +08:00
17 changed files with 910 additions and 157 deletions

View File

@@ -1,4 +1,4 @@
FROM python:3.12-slim as builder FROM python:3.13-slim AS builder
WORKDIR /app WORKDIR /app
@@ -18,31 +18,18 @@ RUN pip config set global.index-url https://mirrors.ustc.edu.cn/pypi/web/simple
&& pip install --no-cache-dir -r requirements.txt && pip install --no-cache-dir -r requirements.txt
# 最终阶段 # 最终阶段
FROM python:3.12-slim FROM python:3.13-slim
WORKDIR /app WORKDIR /app
# 创建非root用户
RUN useradd -m -u 1000 appuser
# 从builder阶段复制Python包 # 从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/ COPY --from=builder /usr/local/bin/ /usr/local/bin/
# 复制应用代码 # 复制应用代码
COPY . . COPY . .
# 设置权限 RUN chmod +x /app/entrypoint.sh
RUN chown -R appuser:appuser /app \
&& chmod +x /app/entrypoint.sh
# 在最终阶段,创建必要的目录并设置权限
RUN mkdir -p /app/media \
&& chown -R appuser:appuser /app/media \
&& chmod 755 /app/media
# 切换到非root用户
USER appuser
EXPOSE 8000 EXPOSE 8000

View File

@@ -9,8 +9,20 @@ https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
import os 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 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

@@ -27,18 +27,22 @@ BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = os.getenv("SECRET_KEY") SECRET_KEY = os.getenv("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.getenv("ENV") != "production" DEBUG = False
if DEBUG: DEV = os.getenv("ENV") != "production"
if DEV:
ALLOWED_HOSTS = ["localhost", "127.0.0.1"] ALLOWED_HOSTS = ["localhost", "127.0.0.1"]
CSRF_TRUSTED_ORIGINS = ["http://localhost:3000"] CSRF_TRUSTED_ORIGINS = ["http://localhost:3000"]
else: else:
ALLOWED_HOSTS = ["web.xuyue.cc", "10.13.114.114"] 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 # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
"daphne",
"channels",
"django.contrib.admin", "django.contrib.admin",
"django.contrib.auth", "django.contrib.auth",
"django.contrib.contenttypes", "django.contrib.contenttypes",
@@ -51,6 +55,7 @@ INSTALLED_APPS = [
"account", "account",
"task", "task",
"submission", "submission",
"chat",
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@@ -83,7 +88,7 @@ TEMPLATES = [
] ]
WSGI_APPLICATION = "api.wsgi.application" WSGI_APPLICATION = "api.wsgi.application"
ASGI_APPLICATION = "api.asgi.application"
# Database # Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases # https://docs.djangoproject.com/en/5.1/ref/settings/#databases
@@ -102,7 +107,7 @@ PROD_DATABASES = {
"USER": os.getenv("POSTGRES_USER"), "USER": os.getenv("POSTGRES_USER"),
"PASSWORD": os.getenv("POSTGRES_PASSWORD"), "PASSWORD": os.getenv("POSTGRES_PASSWORD"),
"HOST": os.getenv("POSTGRES_HOST"), "HOST": os.getenv("POSTGRES_HOST"),
"PORT": "5432", "PORT": os.getenv("POSTGRES_PORT", "5432"),
}, },
} }
@@ -116,7 +121,7 @@ PROD_CACHES = {
} }
} }
if DEBUG: if DEV:
DATABASES = DEV_DATABASES DATABASES = DEV_DATABASES
else: else:
# DATABASES = DEV_DATABASES # DATABASES = DEV_DATABASES
@@ -127,6 +132,16 @@ else:
# 配置缓存 # 配置缓存
CACHES = PROD_CACHES CACHES = PROD_CACHES
# WebSocket 的缓存
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(os.getenv("REDIS_HOST"), 6379)],
},
},
}
# Password validation # Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
@@ -173,7 +188,7 @@ DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
AUTH_USER_MODEL = "account.User" AUTH_USER_MODEL = "account.User"
if DEBUG: if DEV:
CORS_ALLOWED_ORIGINS = ["http://localhost:3000"] CORS_ALLOWED_ORIGINS = ["http://localhost:3000"]
else: else:
CORS_ALLOWED_ORIGINS = ["https://web.xuyue.cc"] CORS_ALLOWED_ORIGINS = ["https://web.xuyue.cc"]

View File

@@ -34,4 +34,4 @@ apis = [
path("api/", api.urls), path("api/", api.urls),
] ]
urlpatterns = apis + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns = apis + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

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

@@ -15,12 +15,6 @@ python manage.py collectstatic --noinput
# 计算worker数量 (CPU核心数 * 2 + 1) # 计算worker数量 (CPU核心数 * 2 + 1)
WORKERS=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count() * 2 + 1)') WORKERS=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count() * 2 + 1)')
# 确保媒体目录存在并有正确的权限
echo "Setting up media directory..."
mkdir -p /app/media
chown -R appuser:appuser /app/media
chmod 755 /app/media
# 启动 Gunicorn # 启动 Gunicorn
echo "Starting Gunicorn with $WORKERS workers..." echo "Starting Gunicorn with $WORKERS workers..."
exec gunicorn api.asgi:application \ exec gunicorn api.asgi:application \

View File

@@ -13,7 +13,9 @@ dependencies = [
"psycopg[binary]>=3.2.5", "psycopg[binary]>=3.2.5",
"pydantic[email]>=2.10.6", "pydantic[email]>=2.10.6",
"python-dotenv>=1.0.1", "python-dotenv>=1.0.1",
"uvicorn>=0.34.0", "uvicorn[standard]>=0.34.0",
"django-redis>=5.4.0", "django-redis>=5.4.0",
"redis>=5.0.1", "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 annotated-types==0.7.0
asgiref==3.8.1 anyio==4.12.0
click==8.2.1 asgiref==3.11.0
django==5.2.3 attrs==25.4.0
django-cors-headers==4.7.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-extensions==4.1
django-ninja==1.4.3 django-ninja==1.5.1
django-redis==5.4.0 django-redis==6.0.0
dnspython==2.7.0 dnspython==2.8.0
email-validator==2.2.0 email-validator==2.3.0
gunicorn==23.0.0 gunicorn==23.0.0
h11==0.16.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 packaging==25.0
psycopg==3.2.9 psycopg==3.3.2
psycopg-binary==3.2.9 psycopg-binary==3.3.2
pydantic==2.11.7 py-ubjson==0.16.1
pydantic-core==2.33.2 pyasn1==0.6.1
python-dotenv==1.1.0 pyasn1-modules==0.4.2
redis==6.2.0 pycparser==2.23
sqlparse==0.5.3 pydantic==2.12.5
typing-extensions==4.14.0 pydantic-core==2.41.5
typing-inspection==0.4.1 pyopenssl==25.3.0
uvicorn==0.34.3 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,8 +5,9 @@ from ninja.errors import HttpError
from ninja.pagination import paginate from ninja.pagination import paginate
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.db.models import OuterRef, Subquery, IntegerField
from account.decorators import admin_required
from .schemas import ( from .schemas import (
SubmissionFilter, SubmissionFilter,
SubmissionIn, SubmissionIn,
@@ -17,7 +18,6 @@ from .schemas import (
from .models import Rating, Submission from .models import Rating, Submission
from task.models import Task from task.models import Task
from account.models import User
router = Router() router = Router()
@@ -46,23 +46,30 @@ def list_submissions(request, filters: SubmissionFilter = Query(...)):
""" """
获取提交列表,支持按任务和用户过滤 获取提交列表,支持按任务和用户过滤
""" """
submissions = Submission.objects.all() submissions = Submission.objects.select_related("task", "user")
if filters.task_id: if filters.task_id:
task = get_object_or_404(Task, id=filters.task_id) task = get_object_or_404(Task, id=filters.task_id)
submissions = submissions.select_related("task").filter(task=task) submissions = submissions.filter(task=task)
if filters.task_type: elif filters.task_type:
tasks = Task.objects.filter(task_type=filters.task_type) submissions = submissions.filter(task__task_type=filters.task_type)
submissions = submissions.select_related("task").filter(task__in=tasks)
if filters.username: if filters.username:
users = User.objects.filter(username__icontains=filters.username) submissions = submissions.filter(user__username__icontains=filters.username)
submissions = submissions.select_related("user").filter(user__in=users)
ratings = Rating.objects.select_related("user", "submission").filter( user_rating_subquery = Subquery(
user=request.user, submission__in=submissions Rating.objects.filter(user=request.user, submission=OuterRef("pk")).values(
"score"
)[:1],
output_field=IntegerField(),
) )
rating_dict = {rating.submission_id: rating.score for rating in ratings} submissions = submissions.annotate(my_score=user_rating_subquery)
return [SubmissionOut.list(submission, rating_dict) for 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 [get_submission_data(submission) for submission in submissions]
@router.get("/{submission_id}", response=SubmissionOut) @router.get("/{submission_id}", response=SubmissionOut)

View File

@@ -1,6 +1,5 @@
from typing import List from typing import List
from ninja import Router from ninja import Router
from ninja.errors import HttpError
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from account.decorators import super_required from account.decorators import super_required
from .schemas import TutorialAll, TutorialIn, TutorialSlim from .schemas import TutorialAll, TutorialIn, TutorialSlim

856
uv.lock generated

File diff suppressed because it is too large Load Diff