def _create_problem_or_reuse_if_exists(self, existing_problem): if existing_problem: self.problem = existing_problem self._ensure_short_name_equality_with(existing_problem) else: self.problem = self._create_problem_instance() problem_site = ProblemSite(problem=self.problem, url_key=generate_key()) problem_site.save() self.problem.problem_site = problem_site self.main_problem_instance = self.problem.main_problem_instance self.problem.package_backend_name = self.package_backend_name self.problem.save()
def setUp(self): class MockRequest(object): def __init__(self): self.user = AnonymousUser() # mocking up a request below becouse I am NOT testing whole view self.request = MockRequest() for i in range(1, 5): name = 'problem_%s_name' % i url_key = 'problem_%s_key' % i problem = Problem(name=name, short_name=name, is_public=True) problem.save() site = ProblemSite(problem=problem, url_key=url_key) site.save()
def setUp(self): class MockRequest(object): def __init__(self): self.user = AnonymousUser() # mocking up a request below because I am NOT testing the whole view self.request = MockRequest() for i in range(1, 5): name = 'problem_%s_name' % i url_key = 'problem_%s_key' % i problem = Problem(name=name, short_name=name, visibility=Problem.VISIBILITY_PUBLIC) problem.save() site = ProblemSite(problem=problem, url_key=url_key) site.save()
def unpack(self, env, package): self.short_name = self._find_main_folder() self.env = env self.package = package existing_problem = self.package.problem if existing_problem: self.problem = existing_problem self.main_problem_instance = self.problem.main_problem_instance if existing_problem.short_name != self.short_name: raise ProblemPackageError(_("Tried to replace problem " "'%(oldname)s' with '%(newname)s'. For safety, changing " "problem short name is not possible.") % dict(oldname=existing_problem.short_name, newname=self.short_name)) else: author_username = env.get('author') if author_username: author = User.objects.get(username=author_username) else: author = None self.problem = Problem.create( name=self.short_name, short_name=self.short_name, controller_name=self.controller_name, contest=self.package.contest, is_public=(author is None), author=author) problem_site = ProblemSite(problem=self.problem, url_key=generate_key()) problem_site.save() self.problem.problem_site = problem_site self.main_problem_instance = self.problem.main_problem_instance self.problem.package_backend_name = self.package_backend_name self.problem.save() tmpdir = tempfile.mkdtemp() logger.info("%s: tmpdir is %s", self.filename, tmpdir) try: self.archive.extract(to_path=tmpdir) self.rootdir = os.path.join(tmpdir, self.short_name) self.process_package() return self.problem finally: shutil.rmtree(tmpdir) if self.prog_archive: get_client().delete_file(self.prog_archive)