Exemplo n.º 1
0
def post_attachment_view(request, file_field):
    settings = request.registry.settings
    keep_old_files = asbool(settings.get('attachment.keep_old_files', False))
    if not keep_old_files:
        # Remove potential existing attachment.
        utils.delete_attachment(request)

    # Store file locally.
    content = request.POST.get(file_field)

    randomize = True
    if 'randomize' in request.GET:
        randomize = asbool(request.GET['randomize'])

    attachment = utils.save_file(content, request, randomize=randomize)
    # Update related record.
    record = {k: v for k, v in request.POST.items() if k != file_field}
    for k, v in record.items():
        record[k] = json.loads(v)

    record.setdefault('data', {})[file_field] = attachment
    utils.patch_record(record, request)

    # Return attachment data (with location header)
    request.response.headers['Location'] = utils.record_uri(request,
                                                            prefix=True)
    return attachment
Exemplo n.º 2
0
def post_attachment_view(request, file_field):
    settings = request.registry.settings
    keep_old_files = asbool(settings.get('attachment.keep_old_files', False))

    if not keep_old_files:
        # Remove potential existing attachment.
        utils.delete_attachment(request)

    # Store file locally.
    content = request.POST.get(file_field)

    randomize = True
    if 'randomize' in request.GET:
        randomize = asbool(request.GET['randomize'])

    gzipped = asbool(settings.get('attachment.gzipped', False))
    if 'gzipped' in request.GET:
        gzipped = asbool(request.GET['gzipped'])

    attachment = utils.save_file(content, request, randomize=randomize,
                                 gzipped=gzipped)

    # Update related record.
    record = {k: v for k, v in request.POST.items() if k != file_field}
    for k, v in record.items():
        record[k] = json.loads(v)

    record.setdefault('data', {})[file_field] = attachment
    utils.patch_record(record, request)

    # Return attachment data (with location header)
    request.response.headers['Location'] = utils.record_uri(request,
                                                            prefix=True)
    return attachment
Exemplo n.º 3
0
    def test_save_file_not_gzip(self):
        my_font = cgi.FieldStorage()
        my_font.filename = "font.ttf"
        my_font.file = BytesIO(b"content")
        my_font.type = "application/x-font"

        request = _Request()
        res = save_file(my_font, request)
        self.assertFalse("original" in res)
Exemplo n.º 4
0
    def test_save_file_not_gzip(self):
        my_font = mock.Mock()
        my_font.filename = 'font.ttf'
        my_font.file = BytesIO(b'content')
        my_font.type = 'application/x-font'

        request = _Request()
        res = save_file(my_font, request)
        self.assertFalse('original' in res)
Exemplo n.º 5
0
    def test_save_file_gzip(self):
        my_font = cgi.FieldStorage()
        my_font.filename = 'font.ttf'
        my_font.file = BytesIO(b'content')
        my_font.type = 'application/x-font'

        request = _Request()
        res = save_file(my_font, request, gzipped=True)
        self.assertTrue('original' in res)
Exemplo n.º 6
0
    def test_save_file_not_gzip(self):
        my_font = mock.Mock()
        my_font.filename = 'font.ttf'
        my_font.file = BytesIO(b'content')
        my_font.type = 'application/x-font'

        request = _Request()
        res = save_file(my_font, request)
        self.assertFalse('original' in res)
Exemplo n.º 7
0
    def test_save_file_gzip(self):
        my_font = cgi.FieldStorage()
        my_font.filename = 'font.ttf'
        my_font.file = BytesIO(b'content')
        my_font.type = 'application/x-font'

        request = _Request()
        res = save_file(my_font, request, gzipped=True)
        self.assertTrue('original' in res)
Exemplo n.º 8
0
def post_attachment_view(request, file_field):
    settings = request.registry.settings
    keep_old_files = asbool(settings.get('attachment.keep_old_files', False))

    if not keep_old_files:
        # Remove potential existing attachment.
        utils.delete_attachment(request)

    if "multipart/form-data" not in request.headers.get('Content-Type', ''):
        raise http_error(httpexceptions.HTTPBadRequest(),
                         errno=ERRORS.INVALID_PARAMETERS,
                         message="Content-Type should be multipart/form-data")

    # Store file locally.
    try:
        content = request.POST.get(file_field)
    except ValueError as e:
        raise http_error(httpexceptions.HTTPBadRequest(),
                         errno=ERRORS.INVALID_PARAMETERS.value,
                         message=str(e))

    if content is None:
        raise http_error(httpexceptions.HTTPBadRequest(),
                         errno=ERRORS.INVALID_POSTED_DATA,
                         message="Attachment missing.")

    randomize = True
    if 'randomize' in request.GET:
        randomize = asbool(request.GET['randomize'])

    gzipped = asbool(settings.get('attachment.gzipped', False))
    if 'gzipped' in request.GET:
        gzipped = asbool(request.GET['gzipped'])

    attachment = utils.save_file(content,
                                 request,
                                 randomize=randomize,
                                 gzipped=gzipped)

    # Update related record.
    posted_data = {k: v for k, v in request.POST.items() if k != file_field}
    record = {'data': {}}
    for field in ('data', 'permissions'):
        if field in posted_data:
            try:
                record[field] = json.loads(posted_data.pop(field))
            except ValueError as e:
                error_msg = "body: %s is not valid JSON (%s)" % (field, str(e))
                raise http_error(httpexceptions.HTTPBadRequest(),
                                 errno=ERRORS.INVALID_POSTED_DATA,
                                 message=error_msg)
    # Some fields remaining in posted_data after popping: invalid!
    for field in posted_data.keys():
        error_msg = "body: %r not in ('data', 'permissions')" % field
        raise http_error(httpexceptions.HTTPBadRequest(),
                         errno=ERRORS.INVALID_POSTED_DATA,
                         message=error_msg)

    record['data'][file_field] = attachment

    utils.patch_record(record, request)

    # Return attachment data (with location header)
    request.response.headers['Location'] = utils.record_uri(request,
                                                            prefix=True)
    return attachment
Exemplo n.º 9
0
def post_attachment_view(request, file_field):
    settings = request.registry.settings
    keep_old_files = asbool(settings.get('attachment.keep_old_files', False))

    if not keep_old_files:
        # Remove potential existing attachment.
        utils.delete_attachment(request)

    if "multipart/form-data" not in request.headers.get('Content-Type', ''):
        raise http_error(httpexceptions.HTTPBadRequest(),
                         errno=ERRORS.INVALID_PARAMETERS,
                         message="Content-Type should be multipart/form-data")

    # Store file locally.
    try:
        content = request.POST.get(file_field)
    except ValueError as e:
        raise http_error(httpexceptions.HTTPBadRequest(),
                         errno=ERRORS.INVALID_PARAMETERS.value,
                         message=str(e))

    if content is None:
        raise http_error(httpexceptions.HTTPBadRequest(),
                         errno=ERRORS.INVALID_POSTED_DATA,
                         message="Attachment missing.")

    randomize = True
    if 'randomize' in request.GET:
        randomize = asbool(request.GET['randomize'])

    gzipped = asbool(settings.get('attachment.gzipped', False))
    if 'gzipped' in request.GET:
        gzipped = asbool(request.GET['gzipped'])

    attachment = utils.save_file(content, request, randomize=randomize,
                                 gzipped=gzipped)

    # Update related record.
    posted_data = {k: v for k, v in request.POST.items() if k != file_field}
    record = {'data': {}}
    for field in ('data', 'permissions'):
        if field in posted_data:
            try:
                record[field] = json.loads(posted_data.pop(field))
            except ValueError as e:
                error_msg = "body: %s is not valid JSON (%s)" % (field, str(e))
                raise http_error(httpexceptions.HTTPBadRequest(),
                                 errno=ERRORS.INVALID_POSTED_DATA,
                                 message=error_msg)
    # Some fields remaining in posted_data after popping: invalid!
    for field in posted_data.keys():
        error_msg = "body: %r not in ('data', 'permissions')" % field
        raise http_error(httpexceptions.HTTPBadRequest(),
                         errno=ERRORS.INVALID_POSTED_DATA,
                         message=error_msg)

    record['data'][file_field] = attachment

    utils.patch_record(record, request)

    # Return attachment data (with location header)
    request.response.headers['Location'] = utils.record_uri(request,
                                                            prefix=True)
    return attachment