formalchemy.ext.fsblob – File system renderer

This extension is used to store binary files/images on filesystem and only store the file path in the database.

This page present a Pylons integration but it should work on most framework.

Renderers

class formalchemy.ext.fsblob.FileFieldRenderer(*args, **kwargs)

render a file input field stored on file system

get_url(relative_path)
return the file url. by default return the relative path stored in the DB
relative_path(filename)
return the file path relative to root
render(**kwargs)
render a file field and the file preview
render_readonly(**kwargs)
render the filename and the binary size in a human readable with a link to the file itself.
class formalchemy.ext.fsblob.ImageFieldRenderer(*args, **kwargs)
render_readonly(**kwargs)
render the image tag with a link to the image itself.

Usage

You must override the storage_path attribute:

# -*- coding: utf-8 -*-
from pylons import config
from pylonsapp import model
from pylonsapp.forms import FieldSet
from formalchemy.ext.fsblob import FileFieldRenderer

# get the storage path from configuration
FileFieldRenderer.storage_path = config['app_conf']['storage_path']

Files = FieldSet(model.Files)
Files.configure(options=[Files.path.with_renderer(FileFieldRenderer)])

As you can see, you can use it like all fields in the .configure method.

Validators

Work like all validators.

formalchemy.ext.fsblob.file_extension(extensions=[], errormsg=None)

Validate a file extension.

>>> from formalchemy.ext.fsblob import file_extension
>>> validator = file_extension(extensions=['txt'])
>>> validator('my image.txt')
>>> validator('my image.png') # doctest: +ELLIPSIS
...
ValidationError: Invalid file extension. Must be txt
formalchemy.ext.fsblob.image_extension(extensions=[, 'jpeg', 'jpg', 'gif', 'png'])

Validate an image extension. default valid extensions are jpeg, jpg, gif, png.

>>> from formalchemy.ext.fsblob import image_extension
>>> validator = image_extension()
>>> validator('my image.png')
>>> validator('my image.txt') # doctest: +ELLIPSIS
...
ValidationError: Invalid image file. Must be jpeg, jpg, gif, png

Sample app

You can have a look at the complete source of the application used for FA’s testing.

Table Of Contents

Previous topic

formalchemy.ext.couchdb – CouchDB support

Next topic

formalchemy.ext.pylons – Pylons extensions

This Page