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

增强上传测试样例时的功能,使通过压缩文件夹方式也可以成功上传 #276

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 21 additions & 1 deletion problem/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@


class TestCaseZipProcessor(object):
def process_zip(self, uploaded_zip_file, spj, dir=""):
def process_zip(self, uploaded_zip_file, spj, dir=None):
try:
zip_file = zipfile.ZipFile(uploaded_zip_file, "r")
except zipfile.BadZipFile:
raise APIError("Bad zip file")
name_list = zip_file.namelist()
if not dir:
dir = self.judge_dir(name_list)
test_case_list = self.filter_name_list(name_list, spj=spj, dir=dir)
if not test_case_list:
raise APIError("Empty file")
Expand Down Expand Up @@ -110,6 +112,24 @@ def filter_name_list(self, name_list, spj, dir=""):
else:
return sorted(ret, key=natural_sort_key)

def judge_dir(self, name_list):
is_native = True
for i, item_i in enumerate(name_list):
if "/" not in item_i:
return ""
if not is_native:
break
for j, item_j in enumerate(name_list):
if (item_i not in item_j):
break
if (j == len(name_list) - 1):
is_native = False
if not is_native:
dir = name_list[0]
else:
dir = name_list[0].split("/")[0]+"/"
return dir


class TestCaseAPI(CSRFExemptAPIView, TestCaseZipProcessor):
request_parsers = ()
Expand Down