题目AC后不计入AC计数器;

支持图片上传
This commit is contained in:
zema1 2017-11-01 22:25:14 +08:00
parent 225d68b413
commit b86ebf0ed7
10 changed files with 69 additions and 9 deletions

View File

@ -106,8 +106,8 @@ class TwoFactorAuthCodeSerializer(serializers.Serializer):
code = serializers.IntegerField()
class AvatarUploadForm(forms.Form):
file = forms.FileField()
class ImageUploadForm(forms.Form):
image = forms.FileField()
class RankInfoSerializer(serializers.ModelSerializer):

View File

@ -23,7 +23,7 @@ from ..serializers import (ApplyResetPasswordSerializer, ResetPasswordSerializer
UserRegisterSerializer, UsernameOrEmailCheckSerializer,
RankInfoSerializer, UserChangeEmailSerializer)
from ..serializers import (TwoFactorAuthCodeSerializer, UserProfileSerializer,
EditUserProfileSerializer, AvatarUploadForm)
EditUserProfileSerializer, ImageUploadForm)
from ..tasks import send_email_async
@ -62,9 +62,9 @@ class AvatarUploadAPI(APIView):
@login_required
def post(self, request):
form = AvatarUploadForm(request.POST, request.FILES)
form = ImageUploadForm(request.POST, request.FILES)
if form.is_valid():
avatar = form.cleaned_data["file"]
avatar = form.cleaned_data["image"]
else:
return self.error("Invalid file content")
if avatar.size > 2 * 1024 * 1024:

View File

@ -35,5 +35,5 @@ if [ $n -eq 3 ]; then
exit 1
fi
chown -R nobody:nogroup /data/log /data/test_case /data/avatar
chown -R nobody:nogroup /data/log /data/test_case /data/avatar /data/upload
exec supervisord -c /app/deploy/supervisor.conf

View File

@ -176,13 +176,15 @@ class JudgeDispatcher(object):
user_profile = user.userprofile
if problem.rule_type == ProblemRuleType.ACM:
user_profile.submission_number += 1
if self.submission.result == JudgeStatus.ACCEPTED:
user_profile.accepted_number += 1
acm_problems_status = user_profile.acm_problems_status.get("problems", {})
if problem_id not in acm_problems_status:
acm_problems_status[problem_id] = {"status": self.submission.result, "_id": self.problem._id}
if self.submission.result == JudgeStatus.ACCEPTED:
user_profile.accepted_number += 1
elif acm_problems_status[problem_id]["status"] != JudgeStatus.ACCEPTED:
acm_problems_status[problem_id]["status"] = self.submission.result
if self.submission.result == JudgeStatus.ACCEPTED:
user_profile.accepted_number += 1
user_profile.acm_problems_status["problems"] = acm_problems_status
user_profile.save(update_fields=["submission_number", "accepted_number", "acm_problems_status"])

View File

@ -31,6 +31,9 @@ LOG_PATH = f"{BASE_DIR}/log/"
AVATAR_URI_PREFIX = "/static/avatar"
AVATAR_UPLOAD_DIR = f"{BASE_DIR}{AVATAR_URI_PREFIX}"
UPLOAD_PREFIX = "/static/upload"
UPLOAD_DIR = f"{BASE_DIR}{UPLOAD_PREFIX}"
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]

View File

@ -28,6 +28,9 @@ ALLOWED_HOSTS = ['*']
AVATAR_URI_PREFIX = "/static/avatar"
AVATAR_UPLOAD_DIR = "/data/avatar"
UPLOAD_PREFIX = "/static/upload"
UPLOAD_DIR = "/data/upload"
TEST_CASE_DIR = "/data/test_case"
LOG_PATH = "/data/log"
DEFAULT_JUDGE_SERVER_SERVICE_URL = "http://judge-server:8080/"

View File

@ -31,7 +31,7 @@ VENDOR_APPS = (
'rest_framework',
)
LOCAL_APPS = (
'account',
'account',
'announcement',
'conf',
'problem',

View File

@ -12,4 +12,5 @@ urlpatterns = [
url(r"^api/admin/", include("contest.urls.admin")),
url(r"^api/", include("contest.urls.oj")),
url(r"^api/", include("submission.urls.oj")),
url(r"^api/admin/", include("utils.urls")),
]

7
utils/urls.py Normal file
View File

@ -0,0 +1,7 @@
from django.conf.urls import url
from .views import SimditorImageUploadAPIView
urlpatterns = [
url(r"^upload_image/?$", SimditorImageUploadAPIView.as_view(), name="upload_image")
]

44
utils/views.py Normal file
View File

@ -0,0 +1,44 @@
import os
from django.conf import settings
from account.serializers import ImageUploadForm
from utils.shortcuts import rand_str
from utils.api import CSRFExemptAPIView
import logging
logger = logging.getLogger(__name__)
class SimditorImageUploadAPIView(CSRFExemptAPIView):
request_parsers = ()
def post(self, request):
form = ImageUploadForm(request.POST, request.FILES)
if form.is_valid():
img = form.cleaned_data["image"]
else:
return self.response({
"success": False,
"msg": "Upload failed",
"file_path": ""})
suffix = os.path.splitext(img.name)[-1].lower()
if suffix not in [".gif", ".jpg", ".jpeg", ".bmp", ".png"]:
return self.response({
"success": False,
"msg": "Unsupported file format",
"file_path": ""})
img_name = rand_str(10) + suffix
try:
with open(os.path.join(settings.UPLOAD_DIR, img_name), "wb") as imgFile:
for chunk in img:
imgFile.write(chunk)
except IOError as e:
logger.error(e)
return self.response({
"success": True,
"msg": "Upload Error",
"file_path": f"{settings.UPLOAD_PREFIX}/{img_name}"})
return self.response({
"success": True,
"msg": "Success",
"file_path": f"{settings.UPLOAD_PREFIX}/{img_name}"})