diff --git a/problem/views/admin.py b/problem/views/admin.py index ee04dc13b..b713c345c 100644 --- a/problem/views/admin.py +++ b/problem/views/admin.py @@ -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") @@ -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 = ()