flask_velox.fields

Fields to help with common functionality, for example Upload fields with WTForms.

Note

The following packages must be installed:

  • Flask-WTF
class flask_velox.fields.UploadFileField(upload_to=None, *args, **kwargs)

Bases: flask_wtf.file.FileField

A django style upload field, allowing files to be uploaded and deleted when changed.

Warning

An application config variable called MEDIA_ROOT needs to be defined. This should be an absolute path to a directory to where uploaded files will be written.

Example

1
2
3
4
5
6
7
8
from flask.ext.velox.fields import UploadFileField
from flask_wtf import Form
from flask_wtf.file import FileAllowed, FileRequired

class MyForm(Form):
    name = UploadFileField('name', validators=[
        FileRequired(),
        FileAllowed(['pdf', ], 'PDF Only')])
absolute_dir_path

Returns the absolute path to the directory in which the file should be written too.

Returns:Path to directory
Return type:str
Raises:NotImplementedError – If MEDIA_ROOT is not defined in app config
delete(relative_path)

Delete an existing file

Parameters:relative (str) – The path to the saved the file to delete
make_paths(obj)

Returns built paths for saving to the file system, storing a relative path in a DB and the secure filename. In the case where the exact same filename exists in the same directory a UNIX timestamp is appended to the end of the filename to ensure no overwrites of existing files occure.

Parameters:obj (obj) – The object being populated by the form
Returns:Realtive path, Absolute path, filename
Return type:tuple
populate_obj(obj, name)

Called when populating an object such as a SQLAlchemy model with field data.

In the case of UploadFileField this method will trigger the proceess of safely saving the file and setting the field data to be a relative path to the file.

Parameters:
  • obj (obj) – Instance of the object
  • name (str) – Name of the field
relative_dir_path

Returns the relative path of the directory, this is used for storing relative paths in a DB rather than absolute ones. By default this will be blank so the relative path for a file names foo.jpg would be foo.jpg

Returns:Relative path to file
Return type:str
save(absolute_path)

Save the file to the file system. In the event the destination directory does not exist it will attempt to create it.

Parameters:absolute_path (str) – The path to save the file too

http://thisissoon.com

Related Topics

This Page