Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完善代码下载功能 #232

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions account/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ def get(self, request):
user = user.filter(Q(username__icontains=keyword) |
Q(userprofile__real_name__icontains=keyword) |
Q(email__icontains=keyword))

only_admin = request.GET.get("onlyadmin", None)
if only_admin:
user = user.filter(Q(admin_type__icontains="Admin"))

return self.success(self.paginate_data(request, user, UserAdminSerializer))

def delete_one(self, user_id):
Expand Down
2 changes: 1 addition & 1 deletion contest/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_update_contest(self):
response_data = response.data["data"]
for k in data.keys():
if isinstance(data[k], datetime):
continue
continue
self.assertEqual(response_data[k], data[k])

def test_get_contests(self):
Expand Down
30 changes: 25 additions & 5 deletions contest/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
from submission.models import Submission, JudgeStatus
from utils.api import APIView, validate_serializer
from utils.cache import cache
from utils.constants import CacheKey
from utils.constants import CacheKey, ContestRuleType
from utils.shortcuts import rand_str
from utils.tasks import delete_files
from ..models import Contest, ContestAnnouncement, ACMContestRank
from ..serializers import (ContestAnnouncementSerializer, ContestAdminSerializer,
CreateConetestSeriaizer, CreateContestAnnouncementSerializer,
EditConetestSeriaizer, EditContestAnnouncementSerializer,
ACMContesHelperSerializer, )
from django.utils.encoding import escape_uri_path


class ContestAPI(APIView):
Expand Down Expand Up @@ -200,10 +201,13 @@ def _dump_submissions(self, contest, exclude_admin=True):
problem_ids = contest.problem_set.all().values_list("id", "_id")
id2display_id = {k[0]: k[1] for k in problem_ids}
ac_map = {k[0]: False for k in problem_ids}
submissions = Submission.objects.filter(contest=contest, result=JudgeStatus.ACCEPTED).order_by("-create_time")
if contest.rule_type == ContestRuleType.OI:
submissions = Submission.objects.filter(contest=contest).order_by("-create_time")
else:
submissions = Submission.objects.filter(contest=contest, result=JudgeStatus.ACCEPTED).order_by("-create_time")
user_ids = submissions.values_list("user_id", flat=True)
users = User.objects.filter(id__in=user_ids)
path = f"/tmp/{rand_str()}.zip"
path = f"/tmp/{contest.title}-{rand_str()}.zip"
with zipfile.ZipFile(path, "w") as zip_file:
for user in users:
if user.is_admin_role() and exclude_admin:
Expand All @@ -214,7 +218,22 @@ def _dump_submissions(self, contest, exclude_admin=True):
problem_id = submission.problem_id
if user_ac_map[problem_id]:
continue
file_name = f"{user.username}_{id2display_id[submission.problem_id]}.txt"
suffix = "cpp"
if submission.language == "Java":
suffix = "java"
elif submission.language == "C":
suffix = "c"
elif submission.language == "Python2":
suffix = "py"
elif submission.language == "Python3":
suffix = "py"

file_name = f"{user.username}/{user.username}_{id2display_id[submission.problem_id]}"
if contest.rule_type == ContestRuleType.OI:
statistic_info = submission.statistic_info
score = statistic_info["score"]
file_name = file_name + f"_{score}"
file_name = file_name + "." + suffix
compression = zipfile.ZIP_DEFLATED
zip_file.writestr(zinfo_or_arcname=f"{file_name}",
data=submission.code,
Expand All @@ -237,5 +256,6 @@ def get(self, request):
delete_files.apply_async((zip_path,), countdown=300)
resp = FileResponse(open(zip_path, "rb"))
resp["Content-Type"] = "application/zip"
resp["Content-Disposition"] = f"attachment;filename={os.path.basename(zip_path)}"
file_name = os.path.basename(zip_path)
resp["Content-Disposition"] = f"attachment;filename=\"{escape_uri_path(file_name)}\""
return resp