def handle(self, *args, **options): if len(args) < 2: raise CommandError(_("Not enough arguments")) if len(args) > 2: raise CommandError(_("Too many arguments")) try: problem_id = int(args[0]) except ValueError: raise CommandError(_("Invalid problem id: ") + args[0]) filename = args[1] if not os.path.exists(filename): raise CommandError(_("File not found: ") + filename) try: problem = Problem.objects.get(id=problem_id) except Problem.DoesNotExist: raise CommandError( _("Problem #%d does not exist") % (problem_id, )) try: backend = \ import_string(backend_for_package(filename))() except NoBackend: raise CommandError(_("Package format not recognized")) problem = backend.simple_unpack(filename, existing_problem=problem)
def _reupload_problem(self, request, problem): uploaded_file = request.FILES["package_file"] with uploaded_file_name(uploaded_file) as filename: backend = backend_for_package(filename) problem = backend.unpack(filename, original_filename=uploaded_file.name, existing_problem=problem) self.message_user(request, _("Problem updated.")) return self.redirect_to_list(request, problem)
def handle(self, *args, **options): if len(args) < 2: raise CommandError(_("Not enough arguments")) if len(args) > 2: raise CommandError(_("Too many arguments")) try: problem_id = int(args[0]) except ValueError: raise CommandError(_("Invalid problem id: ") + args[0]) filename = args[1] if not os.path.exists(filename): raise CommandError(_("File not found: ") + filename) try: problem = Problem.objects.get(id=problem_id) except Problem.DoesNotExist: raise CommandError(_("Problem #%d does not exist") % (problem_id,)) try: backend = \ import_string(backend_for_package(filename))() except NoBackend: raise CommandError(_("Package format not recognized")) problem = backend.simple_unpack(filename, existing_problem=problem)
def handle(self, *args, **options): if not args: raise CommandError(_("Missing argument (filename)")) if len(args) > 2: raise CommandError(_("Expected at most two arguments")) # The second argument can have only one value - 'nothrow'. # It's meant to be used in unit tests, # so that if adding the problem failed, # we can still investigate the problem package # and make sure that the errors there are what we expected. throw_on_problem_not_added = True if len(args) > 1: if args[1] == "nothrow": throw_on_problem_not_added = False else: raise CommandError( _("The second argument (if provided) " "can only have value 'nothrow'" " - to be used in testing")) filename = args[0] if not os.path.exists(filename): raise CommandError(_("File not found: ") + filename) try: backend = \ import_string(backend_for_package(filename))() except NoBackend: raise CommandError(_("Package format not recognized")) problem = backend.simple_unpack(filename) if problem is not None: self.stdout.write('%d\n' % (problem.id, )) elif throw_on_problem_not_added: raise CommandError(_("There was an error adding the problem"))
def process_package(self, request, contest, filename, original_filename, existing_problem=None): backend = backend_for_package(filename, original_filename=original_filename) problem = backend.unpack(filename, original_filename=original_filename, existing_problem=existing_problem) messages.success(request, _("Problem package uploaded.")) return problem
def choose_backend(self, path, original_filename=None): """Returns the dotted name of a :class:`~oioioi.package.ProblemPackageBackend` suitable for processing a given package. This function is called when an unpacking environment is created, i.e. from :meth:`~oioioi.problems.problem_sources.ProblemSource.create_env`. """ return backend_for_package(path, original_filename)
def handle(self, *args, **options): if not args: raise CommandError(_("Missing argument (filename)")) if len(args) > 1: raise CommandError(_("Expected only one argument")) filename = args[0] if not os.path.exists(filename): raise CommandError(_("File not found: ") + filename) try: backend = get_object_by_dotted_name(backend_for_package(filename))() except NoBackend: raise CommandError(_("Package format not recognized")) problem = backend.simple_unpack(filename) self.stdout.write("%d\n" % (problem.id,))
def handle(self, *args, **options): if not args: raise CommandError(_("Missing argument (filename)")) if len(args) > 1: raise CommandError(_("Expected only one argument")) filename = args[0] if not os.path.exists(filename): raise CommandError(_("File not found: ") + filename) try: backend = backend_for_package(filename) except NoBackend: raise CommandError(_("Package format not recognized")) problem = backend.unpack(filename) self.stdout.write('%d\n' % (problem.id, ))
def _add_problem(self, request, round): uploaded_file = request.FILES["package_file"] with uploaded_file_name(uploaded_file) as filename: backend = backend_for_package(filename) problem = backend.unpack(filename, original_filename=uploaded_file.name) if not problem.package_backend_name: raise AssertionError( "Problem package backend (%r) did not " "set Problem.package_backend_name. This is a bug in " "the problem package backend." % (backend,) ) if round: problem.contest = round.contest problem.save() pi = ProblemInstance(contest=round.contest, round=round, problem=problem) pi.save() self.message_user(request, _("Problem package uploaded.")) return self.redirect_to_list(request, problem)
def handle(self, *args, **options): problem_id = options['problem_id'] filename = options['filename'] if not os.path.exists(filename): raise CommandError(_("File not found: ") + filename) try: problem = Problem.objects.get(id=problem_id) except Problem.DoesNotExist: raise CommandError( _("Problem #%d does not exist") % (problem_id, )) try: backend = import_string(backend_for_package(filename))() except NoBackend: raise CommandError(_("Package format not recognized")) problem = backend.simple_unpack(filename, existing_problem=problem)
def _add_problem_with_author(self, filename, author, nothrow=False): try: backend = import_string(backend_for_package(filename))() except NoBackend: raise ValueError("Package format not recognized") pp = ProblemPackage(problem=None) pp.package_file.save(filename, File(open(filename, 'rb'))) env = {'author': author} pp.problem_name = backend.get_short_name(filename) pp.save() env['package_id'] = pp.id problem = None with pp.save_operation_status(): backend.unpack(env) problem = Problem.objects.get(id=env['problem_id']) pp.problem = problem pp.save() if problem is None and not nothrow: raise ValueError("Error during unpacking the given package")
def handle(self, *args, **options): # The second argument can have only one value - 'nothrow'. # It's meant to be used in unit tests, # so that if adding the problem failed, # we can still investigate the problem package # and make sure that the errors there are what we expected. throw_on_problem_not_added = True if options['no_throw']: throw_on_problem_not_added = False filename = options['filename'] if not os.path.exists(filename): raise CommandError(_("File not found: ") + filename) try: backend = import_string(backend_for_package(filename))() except NoBackend: raise CommandError(_("Package format not recognized")) problem = backend.simple_unpack(filename) if problem is not None: self.stdout.write('%d\n' % (problem.id, )) elif throw_on_problem_not_added: raise CommandError(_("There was an error adding the problem"))
def handle(self, *args, **options): # The second argument can have only one value - 'nothrow'. # It's meant to be used in unit tests, # so that if adding the problem failed, # we can still investigate the problem package # and make sure that the errors there are what we expected. throw_on_problem_not_added = True if options['no_throw']: throw_on_problem_not_added = False filename = options['filename'] if not os.path.exists(filename): raise CommandError(_("File not found: ") + filename) try: backend = \ import_string(backend_for_package(filename))() except NoBackend: raise CommandError(_("Package format not recognized")) problem = backend.simple_unpack(filename) if problem is not None: self.stdout.write('%d\n' % (problem.id,)) elif throw_on_problem_not_added: raise CommandError(_("There was an error adding the problem"))