diff --git a/account/views/admin.py b/account/views/admin.py index 94af4b9f..6ae3baa4 100644 --- a/account/views/admin.py +++ b/account/views/admin.py @@ -1,4 +1,3 @@ -from django.core.exceptions import MultipleObjectsReturned from django.db.models import Q from utils.api import APIView, validate_serializer diff --git a/deploy/run.sh b/deploy/run.sh index c95fe35a..c8ca9d58 100644 --- a/deploy/run.sh +++ b/deploy/run.sh @@ -26,7 +26,7 @@ if [ $? -ne 0 ]; then let "n+=1" continue fi -python manage.py initadmin +python manage.py initinstall break done diff --git a/problem/serializers.py b/problem/serializers.py index b9ebfd09..e78eb554 100644 --- a/problem/serializers.py +++ b/problem/serializers.py @@ -120,3 +120,8 @@ class ContestProblemSafeSerializer(BaseProblemSerializer): model = Problem exclude = ("test_case_score", "test_case_id", "visible", "is_public", "difficulty", "submission_number", "accepted_number", "statistic_info") + + +class ContestProblemMakePublicSerializer(serializers.Serializer): + id = serializers.IntegerField() + display_id = serializers.CharField(max_length=32) diff --git a/problem/views/admin.py b/problem/views/admin.py index a7e24db9..cf944474 100644 --- a/problem/views/admin.py +++ b/problem/views/admin.py @@ -13,7 +13,7 @@ from utils.shortcuts import rand_str, natural_sort_key from ..models import Problem, ProblemRuleType, ProblemTag from ..serializers import (CreateContestProblemSerializer, ContestProblemAdminSerializer, CreateProblemSerializer, EditProblemSerializer, EditContestProblemSerializer, - ProblemAdminSerializer, TestCaseUploadForm) + ProblemAdminSerializer, TestCaseUploadForm, ContestProblemMakePublicSerializer) class TestCaseUploadAPI(CSRFExemptAPIView): @@ -339,15 +339,19 @@ class ContestProblemAPI(ProblemBase): class MakeContestProblemPublicAPIView(APIView): + @validate_serializer(ContestProblemMakePublicSerializer) @problem_permission_required def post(self, request): - problem_id = request.data.get("problem_id") - if not problem_id: - return self.error("problem_id is required") + data = request.data + display_id = data.get("display_id") + if Problem.objects.filter(_id=display_id, contest_id__isnull=True).exists(): + return self.error("Duplicate display ID") + try: - problem = Problem.objects.get(id=problem_id) + problem = Problem.objects.get(id=data["id"]) except Problem.DoesNotExist: return self.error("Problem does not exist") + if not problem.contest or problem.is_public: return self.error("Alreay be a public problem") problem.is_public = True @@ -356,6 +360,7 @@ class MakeContestProblemPublicAPIView(APIView): tags = problem.tags.all() problem.pk = None problem.contest = None + problem._id = display_id problem.submission_number = problem.accepted_number = 0 problem.statistic_info = {} problem.save() diff --git a/submission/views/oj.py b/submission/views/oj.py index b0751c35..fec15cc1 100644 --- a/submission/views/oj.py +++ b/submission/views/oj.py @@ -4,6 +4,7 @@ from judge.tasks import judge_task # from judge.dispatcher import JudgeDispatcher from problem.models import Problem, ProblemRuleType from contest.models import Contest, ContestStatus, ContestRuleType +from options.options import SysOptions from utils.api import APIView, validate_serializer from utils.throttling import TokenBucket, BucketController from utils.captcha import Captcha @@ -144,7 +145,7 @@ class SubmissionListAPI(APIView): except Problem.DoesNotExist: return self.error("Problem doesn't exist") submissions = submissions.filter(problem=problem) - if myself and myself == "1": + if (myself and myself == "1") and not SysOptions.submission_list_show_all: submissions = submissions.filter(user_id=request.user.id) elif username: submissions = submissions.filter(username=username) diff --git a/utils/management/commands/initadmin.py b/utils/management/commands/initadmin.py index c29463ad..78e8a458 100644 --- a/utils/management/commands/initadmin.py +++ b/utils/management/commands/initadmin.py @@ -1,4 +1,3 @@ -import os from django.core.management.base import BaseCommand from account.models import AdminType, ProblemPermission, User, UserProfile @@ -10,17 +9,16 @@ class Command(BaseCommand): try: admin = User.objects.get(username="root") if admin.admin_type == AdminType.SUPER_ADMIN: - if os.environ.get("OJ_ENV") != "production": - self.stdout.write(self.style.WARNING("Super admin user 'root' already exists, " - "would you like to reset it's password?\n" - "Input yes to confirm: ")) - if input() == "yes": - rand_password = "rootroot" - admin.save() - self.stdout.write(self.style.SUCCESS("Successfully created super admin user password.\n" - "Username: root\nPassword: %s\n" - "Remember to change password and turn on two factors auth " - "after installation." % rand_password)) + self.stdout.write(self.style.WARNING("Super admin user 'root' already exists, " + "would you like to reset it's password?\n" + "Input yes to confirm: ")) + if input() == "yes": + rand_password = "rootroot" + admin.save() + self.stdout.write(self.style.SUCCESS("Successfully created super admin user password.\n" + "Username: root\nPassword: %s\n" + "Remember to change password and turn on two factors auth " + "after installation." % rand_password)) else: self.stdout.write(self.style.SUCCESS("Nothing happened")) else: diff --git a/utils/management/commands/initinstall.py b/utils/management/commands/initinstall.py new file mode 100644 index 00000000..bdb0f0a3 --- /dev/null +++ b/utils/management/commands/initinstall.py @@ -0,0 +1,17 @@ +import os +from account.models import User +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + def handle(self, *args, **options): + if User.objects.exists(): + self.stdout.write(self.style.WARNING("Nothing happened\n")) + return + try: + if os.system("python manage.py initadmin") != 0: + self.stdout.write(self.style.ERROR("Failed to execute command 'initadmin'")) + exit(1) + self.stdout.write(self.style.SUCCESS("Done")) + except Exception as e: + self.stdout.write(self.style.ERROR("Failed to initialize, error: " + str(e)))