From 03c68419b0dd60c9d161fb184ff3326515b2eb5e Mon Sep 17 00:00:00 2001 From: zema1 Date: Sun, 16 Dec 2018 10:30:19 +0800 Subject: [PATCH] remove restriction when using open api --- account/middleware.py | 1 + contest/views/admin.py | 2 +- fps/parser.py | 1 - problem/utils.py | 6 +++--- submission/views/oj.py | 4 ++++ 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/account/middleware.py b/account/middleware.py index 3177da92..8834b153 100644 --- a/account/middleware.py +++ b/account/middleware.py @@ -14,6 +14,7 @@ class APITokenAuthMiddleware(MiddlewareMixin): try: request.user = User.objects.get(open_api_appkey=appkey, open_api=True, is_disabled=False) request.csrf_processing_done = True + request.auth_method = "api_key" except User.DoesNotExist: pass diff --git a/contest/views/admin.py b/contest/views/admin.py index dd22ce62..9adb45ad 100644 --- a/contest/views/admin.py +++ b/contest/views/admin.py @@ -57,7 +57,7 @@ class ContestAPI(APIView): for ip_range in data["allowed_ip_ranges"]: try: ip_network(ip_range, strict=False) - except ValueError as e: + except ValueError: return self.error(f"{ip_range} is not a valid cidr network") if not contest.real_time_rank and data.get("real_time_rank"): cache_key = f"{CacheKey.contest_rank_cache}:{contest.id}" diff --git a/fps/parser.py b/fps/parser.py index b9446d73..4f989ef6 100644 --- a/fps/parser.py +++ b/fps/parser.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -import os import base64 import copy import random diff --git a/problem/utils.py b/problem/utils.py index 9e29cd67..df70f9bf 100644 --- a/problem/utils.py +++ b/problem/utils.py @@ -14,9 +14,9 @@ TEMPLATE_BASE = """//PREPEND BEGIN def parse_problem_template(template_str): - prepend = re.findall("//PREPEND BEGIN\n([\s\S]+?)//PREPEND END", template_str) - template = re.findall("//TEMPLATE BEGIN\n([\s\S]+?)//TEMPLATE END", template_str) - append = re.findall("//APPEND BEGIN\n([\s\S]+?)//APPEND END", template_str) + prepend = re.findall(r"//PREPEND BEGIN\n([\s\S]+?)//PREPEND END", template_str) + template = re.findall(r"//TEMPLATE BEGIN\n([\s\S]+?)//TEMPLATE END", template_str) + append = re.findall(r"//APPEND BEGIN\n([\s\S]+?)//APPEND END", template_str) return {"prepend": prepend[0] if prepend else "", "template": template[0] if template else "", "append": append[0] if append else ""} diff --git a/submission/views/oj.py b/submission/views/oj.py index 7ef9ff93..672b427a 100644 --- a/submission/views/oj.py +++ b/submission/views/oj.py @@ -18,6 +18,10 @@ from ..serializers import SubmissionSafeModelSerializer, SubmissionListSerialize class SubmissionAPI(APIView): def throttling(self, request): + # 使用 open_api 的请求暂不做限制 + auth_method = getattr(request, "auth_method", "") + if auth_method == "api_key": + return user_bucket = TokenBucket(key=str(request.user.id), redis_conn=cache, **SysOptions.throttling["user"]) can_consume, wait = user_bucket.consume()