akvo.rest.fields module

class akvo.rest.fields.Base64ImageField(*args, **kwargs)[source]

Bases: ImageField

A django-rest-framework field for handling image-uploads through raw post data. It uses base64 for en-/decoding the contents of the file. Now also supports thumbnails of different sizes. See to_native() for more info.

ALLOWED_IMAGE_TYPES = ('gif', 'jpeg', 'jpg', 'png')
check_file_extension(file_extension)[source]
get_file_extension(filename, decoded_file)[source]
to_internal_value(base64_data)[source]

Transform the incoming primitive data into a native value.

to_representation(value)[source]
Parameters:

value – A Base64ImageField object

Returns:

a path to a thumbnail with a predetermined size, the default thumb

OR a dict with a number of thumbnails, one of which is the default, the others being generated from the query string parameters, and finally the path to the original image keyed to “original”.

The extended functionality, allowing the generation of one or more thumbnails from the original image is triggered by including “image_thumb_name” in the query string. The value for image_thumb_name is a comma separated list of identifiers for the generated thumbs. The names must not be “default” or “original”.

For each thumb thus specified a size must be supplied as a query param on the form

image_thumb_<name>_<dimension>

where <name> is the name of the thumb specified as one of the values for image_thumb_name and <dimension> is one of “width, “height” or “max_size”. width and height must be an integer specifying that dimension in pixels. The image will be scaled correctly in the other dimension. max_size is width and height concatenated with an “x” and sets the maximum size allowed for the respective dimensions, while still maintaining the correct aspect ratio of the image.

Example: the querystring

?image_thumb_name=big,small&image_thumb_small_width=90&image_thumb_big_max_size=300x200

results in the following dict being returned: {

‘original’: ‘/full/path/to/original/image.png’, ‘default’: ‘/full/path/to/default/thumbnail/image.png’, ‘small’: ‘/full/path/to/small/thumbnail/image.png’, ‘big’: ‘/full/path/to/big/thumbnail/image.png’,

} This dict will be converted as appropriate to JSON or XML

NOTE: This special functionality works best when there is only one image field in a model. If there are more, things will still work (I think), but for each image all thumbs returned will have the same dimensions

class akvo.rest.fields.NonNullCharField(*args, **kwargs)[source]

Bases: CharField

Fix fo CharField so that ‘’ is returned if the field value is None see https://github.com/tomchristie/django-rest-framework/pull/1665

from_native(value)[source]
class akvo.rest.fields.NonNullURLField(*args, **kwargs)[source]

Bases: NonNullCharField, URLField