Compare commits
19 Commits
e5e9115c50
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8e91884d75 | |||
| d1b5b67725 | |||
| f43b1c62f6 | |||
| 36c795e6ec | |||
| ebfe08a97c | |||
| bb41f05bbe | |||
| eb09245467 | |||
| 108a125810 | |||
| 01e5c9097c | |||
| 403d3db941 | |||
| 34b9822a3d | |||
| 313e326316 | |||
| 30becb56bc | |||
| ac3c7c4dec | |||
| 452b4039fe | |||
| 3502ac0c42 | |||
| 2dc1c3b005 | |||
| 641d4a84b4 | |||
| 7edeea299e |
21
Dockerfile
21
Dockerfile
@@ -1,4 +1,4 @@
|
||||
FROM python:3.12-slim as builder
|
||||
FROM python:3.13-slim AS builder
|
||||
|
||||
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
|
||||
|
||||
# 最终阶段
|
||||
FROM python:3.12-slim
|
||||
FROM python:3.13-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# 创建非root用户
|
||||
RUN useradd -m -u 1000 appuser
|
||||
|
||||
# 从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 . .
|
||||
|
||||
# 设置权限
|
||||
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
|
||||
RUN chmod +x /app/entrypoint.sh
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
|
||||
16
api/asgi.py
16
api/asgi.py
@@ -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))
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -27,18 +27,22 @@ BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
SECRET_KEY = os.getenv("SECRET_KEY")
|
||||
|
||||
# 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"]
|
||||
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",
|
||||
@@ -51,6 +55,7 @@ INSTALLED_APPS = [
|
||||
"account",
|
||||
"task",
|
||||
"submission",
|
||||
"chat",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@@ -83,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
|
||||
@@ -102,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"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -116,7 +121,7 @@ PROD_CACHES = {
|
||||
}
|
||||
}
|
||||
|
||||
if DEBUG:
|
||||
if DEV:
|
||||
DATABASES = DEV_DATABASES
|
||||
else:
|
||||
# DATABASES = DEV_DATABASES
|
||||
@@ -127,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
|
||||
|
||||
@@ -173,7 +188,7 @@ DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||
|
||||
AUTH_USER_MODEL = "account.User"
|
||||
|
||||
if DEBUG:
|
||||
if DEV:
|
||||
CORS_ALLOWED_ORIGINS = ["http://localhost:3000"]
|
||||
else:
|
||||
CORS_ALLOWED_ORIGINS = ["https://web.xuyue.cc"]
|
||||
|
||||
@@ -34,4 +34,4 @@ apis = [
|
||||
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
0
chat/__init__.py
Normal file
0
chat/api.py
Normal file
0
chat/api.py
Normal file
6
chat/apps.py
Normal file
6
chat/apps.py
Normal 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
17
chat/consumers.py
Normal 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}))
|
||||
0
chat/migrations/__init__.py
Normal file
0
chat/migrations/__init__.py
Normal file
3
chat/models.py
Normal file
3
chat/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
6
chat/url.py
Normal file
6
chat/url.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.urls import path
|
||||
from .consumers import ChatConsumer
|
||||
|
||||
websocket_urlpatterns = [
|
||||
path("ws/chat/", ChatConsumer.as_asgi()),
|
||||
]
|
||||
@@ -15,12 +15,6 @@ python manage.py collectstatic --noinput
|
||||
# 计算worker数量 (CPU核心数 * 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
|
||||
echo "Starting Gunicorn with $WORKERS workers..."
|
||||
exec gunicorn api.asgi:application \
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,8 +5,9 @@ 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 account.decorators import admin_required
|
||||
from .schemas import (
|
||||
SubmissionFilter,
|
||||
SubmissionIn,
|
||||
@@ -17,7 +18,6 @@ from .schemas import (
|
||||
|
||||
from .models import Rating, Submission
|
||||
from task.models import Task
|
||||
from account.models import User
|
||||
|
||||
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:
|
||||
task = get_object_or_404(Task, id=filters.task_id)
|
||||
submissions = submissions.select_related("task").filter(task=task)
|
||||
if filters.task_type:
|
||||
tasks = Task.objects.filter(task_type=filters.task_type)
|
||||
submissions = submissions.select_related("task").filter(task__in=tasks)
|
||||
submissions = submissions.filter(task=task)
|
||||
elif filters.task_type:
|
||||
submissions = submissions.filter(task__task_type=filters.task_type)
|
||||
if filters.username:
|
||||
users = User.objects.filter(username__icontains=filters.username)
|
||||
submissions = submissions.select_related("user").filter(user__in=users)
|
||||
submissions = submissions.filter(user__username__icontains=filters.username)
|
||||
|
||||
ratings = Rating.objects.select_related("user", "submission").filter(
|
||||
user=request.user, submission__in=submissions
|
||||
user_rating_subquery = Subquery(
|
||||
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}
|
||||
return [SubmissionOut.list(submission, rating_dict) for submission in submissions]
|
||||
submissions = submissions.annotate(my_score=user_rating_subquery)
|
||||
|
||||
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)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from typing import List
|
||||
from ninja import Router
|
||||
from ninja.errors import HttpError
|
||||
from django.shortcuts import get_object_or_404
|
||||
from account.decorators import super_required
|
||||
from .schemas import TutorialAll, TutorialIn, TutorialSlim
|
||||
|
||||
Reference in New Issue
Block a user