Ejemplo n.º 1
0
 def test_uploaded_file_name(self):
     tmp_file = TemporaryUploadedFile('whatever',
             'application/octet-stream', 0, 'utf-8')
     with utils.uploaded_file_name(tmp_file) as name:
         self.assertEquals(name, tmp_file.file.name)
     mem_file = SimpleUploadedFile('whatever', 'hello42')
     with utils.uploaded_file_name(mem_file) as name:
         self.assertTrue(name.endswith('whatever'))
         self.assertEqual(open(name, 'rb').read(), 'hello42')
     self.assertFalse(os.path.exists(name))
Ejemplo n.º 2
0
 def test_uploaded_file_name(self):
     tmp_file = TemporaryUploadedFile('whatever',
             'application/octet-stream', 0, 'utf-8')
     with utils.uploaded_file_name(tmp_file) as name:
         self.assertEquals(name, tmp_file.file.name)
     mem_file = SimpleUploadedFile('whatever', 'hello42')
     with utils.uploaded_file_name(mem_file) as name:
         self.assertTrue(name.endswith('whatever'))
         self.assertEqual(open(name, 'rb').read(), 'hello42')
     self.assertFalse(os.path.exists(name))
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 def process_valid_form(self,
                        request,
                        contest,
                        form,
                        existing_problem=None):
     uploaded_file = request.FILES['package_file']
     with uploaded_file_name(uploaded_file) as filename:
         return self.process_package(request, contest, filename,
                                     uploaded_file.name, existing_problem)
Ejemplo n.º 5
0
def problem_site_package_download_file(request, problem):
    original_package = OriginalPackage.objects.get(problem=problem)
    problem = original_package.problem
    package = original_package.problem_package
    contest = problem.contest
    archive = Archive(package.package_file)
    file_names = archive.filenames()
    if request.method == 'POST':
        form = PackageFileReuploadForm(file_names, contest, request.POST)
        if form.is_valid():
            if 'upload_button' in request.POST:
                package_name = file_names[0].split(os.path.sep, 1)[0]
                package_archive, tmpdir = \
                    _prepare_changed_package(request, form, archive, package_name)
                if package_archive is not None:
                    package_file = File(open(package_archive, 'rb'),
                                        os.path.basename(package_archive))
                    original_filename = package_file.name
                    file_manager = uploaded_file_name(package_file)
                    source = UploadedPackageSource()
                    try:
                        source.process_package(request, file_manager, request.user,
                                           contest, existing_problem=problem,
                                           original_filename=original_filename,
                                           visibility=problem.visibility)
                    except Exception as e:
                        logger.error("Error processing package", exc_info=True,
                                     extra={'omit_sentry': True})
                        form._errors['__all__'] = form.error_class([smart_str(e)])
                    finally:
                        shutil.rmtree(tmpdir)
                    return source._redirect_response(request)
            elif 'download_button' in request.POST:
                file_name = request.POST.get('file_name', None)
                if file_name is None:
                    form.add_error('file_name', _('No file selected'))
                else:
                    return redirect('download_package_file',
                                    package_id=package.id,
                                    file_name=file_name)
    else:
        form = PackageFileReuploadForm(file_names, contest)
    return TemplateResponse(request,
                            'problems/manage-problem-package-files.html',
                            {'form': form,
                             'can_reupload':
                                 _problem_can_be_reuploaded(request, problem)})
Ejemplo n.º 6
0
 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 process_valid_form(self,
                        request,
                        contest,
                        form,
                        existing_problem=None):
     uploaded_file = request.FILES['package_file']
     with uploaded_file_name(uploaded_file) as filename:
         problem = self.process_package(request, contest, filename,
                                        uploaded_file.name,
                                        existing_problem)
         if isinstance(problem, Problem):
             problem_data, _created = ZeusProblemData.objects \
                 .get_or_create(problem=problem)
             problem_data.zeus_id = form.cleaned_data['zeus_id']
             problem_data.zeus_problem_id = \
                 form.cleaned_data['zeus_problem_id']
             problem_data.save()
         messages.success(request, _("ZeusProblem package uploaded."))
         return problem
Ejemplo n.º 8
0
 def get_package_file(self, request, contest, form, existing_problem=None):
     package_file = request.FILES['package_file']
     return package_file.name, uploaded_file_name(package_file)
Ejemplo n.º 9
0
 def get_package_file(self, request, contest, form, existing_problem=None):
     package_file = request.FILES['package_file']
     return package_file.name, uploaded_file_name(package_file)
Ejemplo n.º 10
0
 def process_valid_form(self, request, contest, form,
         existing_problem=None):
     uploaded_file = request.FILES['package_file']
     with uploaded_file_name(uploaded_file) as filename:
         return self.process_package(request, contest, filename,
                 uploaded_file.name, existing_problem)