Пример #1
0
    def new(self, package_id, format="html", **kwargs):
        """Retrieve the form for uploading a package version.

        If the user isn't logged in, this presents a login screen. If they are
        logged in but don't have admin priviliges or don't own the package, this
        redirects to /packages/.

        This accepts arbitrary keyword arguments to support OAuth.
        """
        is_json = format == "json"
        user = handlers.get_current_user()

        package = handlers.request().maybe_package
        if not user:
            if is_json:
                handlers.http_error(403, "OAuth authentication failed.")
            else:
                raise cherrypy.HTTPRedirect(users.create_login_url(cherrypy.url()))
        elif package and user not in package.uploaders:
            message = "You aren't an uploader for package '%s'." % package.name
            if is_json:
                handlers.http_error(403, message)
            else:
                handlers.flash(message)
                raise cherrypy.HTTPRedirect("/packages/%s" % package.name)
        elif not handlers.is_current_user_admin():
            message = "Currently only admins may create packages."
            if is_json:
                handlers.http_error(403, message)
            else:
                handlers.flash(message)
                raise cherrypy.HTTPRedirect("/packages")
        elif PrivateKey.get() is None:
            if is_json:
                handlers.http_error(500, "No private key set.")
            else:
                raise cherrypy.HTTPRedirect("/admin#tab-private-key")

        id = str(uuid4())
        redirect_url = handlers.request().url(action="create", id=id)
        upload = cloud_storage.Upload(
            "tmp/" + id, acl="project-private", size_range=(0, Package.MAX_SIZE), success_redirect=redirect_url
        )

        # If the package hasn't been validated and moved out of tmp in five
        # minutes, delete it. This could happen if the user uploads the package
        # to cloud storage, but closes the browser before "create" is run.
        deferred.defer(self._remove_tmp_package, id, _countdown=5 * 60)

        if is_json:
            return upload.to_json()

        if package is not None:
            title = "Upload a new version of %s" % package.name
        else:
            title = "Upload a new package"

            return handlers.render(
                "packages/versions/new", form=upload.to_form(), package=package, layout={"title": title}
            )
Пример #2
0
    def new_dartdoc(self, package_id, id, format):
        """Retrieve the form for uploading dartdoc for this package version."""
        if PrivateKey.get() is None:
            handlers.http_error(500, 'No private key set.')

        version = handlers.request().package_version(id)
        upload = cloud_storage.Upload(version.dartdoc_storage_path,
                                      acl='public-read',
                                      size_range=(0, Package.MAX_SIZE))

        return upload.to_json()
Пример #3
0
    def new_dartdoc(self, package_id, id, format):
        """Retrieve the form for uploading dartdoc for this package version."""
        if PrivateKey.get() is None:
            handlers.http_error(500, 'No private key set.')

        version = handlers.request().package_version(id)
        upload = cloud_storage.Upload(version.dartdoc_storage_path,
                                      acl='public-read',
                                      size_range=(0, Package.MAX_SIZE))

        return upload.to_json()
Пример #4
0
    def test_admin_creates_key(self):
        self.be_admin_user()

        get_response = self.testapp.get('/admin')
        self.assertEqual(get_response.status_int, 200)
        form = get_response.forms["private-key"]
        self.assertEqual(form.method, 'POST')

        form['key'] = 'key'
        post_response = form.submit()

        self.assertEqual(post_response.status_int, 302)
        self.assertEqual(post_response.headers['Location'],
                         'http://localhost:80/admin#tab-private-key')
        self.assertEqual(PrivateKey.get(), 'key')
Пример #5
0
    def test_admin_creates_key(self):
        self.be_admin_user()

        get_response = self.testapp.get('/admin')
        self.assertEqual(get_response.status_int, 200)
        form = get_response.forms["private-key"]
        self.assertEqual(form.method, 'POST')

        form['key'] = 'key'
        post_response = form.submit()

        self.assertEqual(post_response.status_int, 302)
        self.assertEqual(post_response.headers['Location'],
                         'http://localhost:80/admin#tab-private-key')
        self.assertEqual(PrivateKey.get(), 'key')
Пример #6
0
    def admin(self):
        """Retrieve a page for performing administrative tasks."""

        if not users.get_current_user():
            raise cherrypy.HTTPRedirect(users.create_login_url(cherrypy.url()))
        elif not users.is_current_user_admin():
            raise handlers.http_error(403)

        reload_status = PackageVersion.get_reload_status()
        if reload_status is not None:
            reload_status['percentage'] = '%d%%' % (
                100.0 * reload_status['count'] / reload_status['total'])

        return handlers.render('admin',
                               reload_status=reload_status,
                               private_key_set=PrivateKey.get() is not None,
                               production=handlers.is_production(),
                               layout={'title': 'Admin Console'})
Пример #7
0
    def admin(self):
        """Retrieve a page for performing administrative tasks."""

        if not users.get_current_user():
            raise cherrypy.HTTPRedirect(users.create_login_url(cherrypy.url()))
        elif not users.is_current_user_admin():
            raise handlers.http_error(403)

        reload_status = PackageVersion.get_reload_status()
        if reload_status is not None:
            reload_status['percentage'] = '%d%%' % (
                100.0 * reload_status['count'] / reload_status['total'])

        return handlers.render('admin',
                               reload_status=reload_status,
                               private_key_set=PrivateKey.get() is not None,
                               production=handlers.is_production(),
                               layout={'title': 'Admin Console'})
Пример #8
0
    def new(self, package_id, format='json', **kwargs):
        """Retrieve the form for uploading a package version.

        This accepts arbitrary keyword arguments to support OAuth.
        """
        if PrivateKey.get() is None:
            handlers.http_error(500, 'No private key set.')

        id = str(uuid4())
        redirect_url = handlers.request().url(action="create", id=id)
        upload = cloud_storage.Upload("tmp/" + id, acl="project-private",
                                      size_range=(0, Package.MAX_SIZE),
                                      success_redirect=redirect_url)

        # If the package hasn't been validated and moved out of tmp in five
        # minutes, delete it. This could happen if the user uploads the package
        # to cloud storage, but closes the browser before "create" is run.
        deferred.defer(self._remove_tmp_package, id, _countdown=5*60)

        return upload.to_json()
Пример #9
0
def requires_private_key(fn, *args, **kwargs):
    """A decorator for actions that require a private key to be set."""
    if PrivateKey.get() is None: http_error(500, 'No private key set.')
    return fn(*args, **kwargs)
Пример #10
0
def requires_private_key(fn, *args, **kwargs):
    """A decorator for actions that require a private key to be set."""
    if PrivateKey.get() is None: http_error(500, 'No private key set.')
    return fn(*args, **kwargs)