Esempio n. 1
0
def upload_attachment(self, request):
    """ Upload an attachment and add it to the notice.

    Raises a HTTP 405 (Metho not Allowed) for non-admins if the notice has
    already been accepted.

    Raises a HTTP 415 (Unsupported Media Type) if the file format is not
    supported.

    """

    if self.state == 'accepted' or self.state == 'published':
        if not request.is_secret(self):
            raise exc.HTTPMethodNotAllowed()

    request.assert_valid_csrf_token()

    attachment = GazetteNoticeFile(id=random_token())
    attachment.name = request.params['file'].filename
    attachment.reference = as_fileintent(request.params['file'].file,
                                         request.params['file'].filename)

    if attachment.reference.content_type != 'application/pdf':
        raise exc.HTTPUnsupportedMediaType()

    self.files.append(attachment)
    self.add_change(request, _("Attachment added."))

    request.message(_("Attachment added."), 'success')
    return redirect(request.link(self, 'attachments'))
Esempio n. 2
0
def code2exception(code, detail):
    """Transforms a code + detail into a WebOb exception"""
    if code == 400:
        return exc.HTTPBadRequest(detail)
    if code == 401:
        return exc.HTTPUnauthorized(detail)
    if code == 402:
        return exc.HTTPPaymentRequired(detail)
    if code == 403:
        return exc.HTTPForbidden(detail)
    if code == 404:
        return exc.HTTPNotFound(detail)
    if code == 405:
        return exc.HTTPMethodNotAllowed(detail)
    if code == 406:
        return exc.HTTPNotAcceptable(detail)
    if code == 407:
        return exc.HTTPProxyAuthenticationRequired(detail)
    if code == 408:
        return exc.HTTPRequestTimeout(detail)
    if code == 409:
        return exc.HTTPConflict(detail)
    if code == 410:
        return exc.HTTPGone(detail)
    if code == 411:
        return exc.HTTPLengthRequired(detail)
    if code == 412:
        return exc.HTTPPreconditionFailed(detail)
    if code == 413:
        return exc.HTTPRequestEntityTooLarge(detail)
    if code == 414:
        return exc.HTTPRequestURITooLong(detail)
    if code == 415:
        return exc.HTTPUnsupportedMediaType(detail)
    if code == 416:
        return exc.HTTPRequestRangeNotSatisfiable(detail)
    if code == 417:
        return exc.HTTPExpectationFailed(detail)
    if code == 500:
        return exc.HTTPInternalServerError(detail)
    if code == 501:
        return exc.HTTPNotImplemented(detail)
    if code == 502:
        return exc.HTTPBadGateway(detail)
    if code == 503:
        return exc.HTTPServiceUnavailable(detail)
    if code == 504:
        return exc.HTTPGatewayTimeout(detail)
    if code == 505:
        return exc.HTTPVersionNotSupported(detail)

    raise NotImplementedError(code)
Esempio n. 3
0
    def _llsd__get(self):
        '''Get, set, or delete the LLSD value stored in this object.'''

        try:
            return self._llsd
        except AttributeError:
            if not self.body:
                raise AttributeError('No llsd attribute has been set')
            else:
                mtype = mime_type(self.content_type)
                try:
                    parser = llsd_parsers[mtype]
                except KeyError:
                    raise exc.HTTPUnsupportedMediaType(
                        'Content type %s not supported' % mtype).exception
                try:
                    self._llsd = parser(self.body)
                except (llsd.LLSDParseError, JsonDecodeError, TypeError), err:
                    raise exc.HTTPBadRequest(
                        'Could not parse body: %r' % err.args).exception
            return self._llsd