class ImageAdmin(ModelAdmin): roles_accepted = ('admin', 'editor') column_list = ('title', 'path', 'thumb', 'published') form_columns = ['title', 'path', 'published'] def _list_thumbnail(view, context, model, name): if not model.path: return '' return Markup( '<img src="%s">' % url_for( 'media', filename="images/{}".format(form.thumbgen_filename(model.path)) ) ) column_formatters = { 'thumb': _list_thumbnail } form_extra_fields = { 'path': form.ImageUploadField( 'Image', base_path=os.path.join(settings.MEDIA_ROOT, 'images'), thumbnail_size=(100, 100, True), endpoint="media" ) }
class ImageView(sqla.ModelView): def _list_thumbnail(view, context, model, name): if not model.path: return '' return Markup( '<img src="%s">' % url_for('static', filename=form.thumbgen_filename(model.path))) column_formatters = {'path': _list_thumbnail} # Alternative way to contribute field is to override it completely. # In this case, Flask-Admin won't attempt to merge various parameters for the field. form_extra_fields = { 'path': form.ImageUploadField('Image', base_path=file_path, thumbnail_size=(100, 100, True)) }
def prefix_name(obj, file_data): # Collect name and extension parts = op.splitext(file_data.filename) # Get current time (for unique hash) timestamp = str(round(time.time())) # Has filename only (not extension) file_name = secure_filename(timestamp + '%s' % parts[0]) # Put them together full_name = hashlib.md5(file_name).hexdigest() + parts[1] return full_name image_upload_field = form.ImageUploadField('Image', base_path=file_path, thumbnail_size=(100, 100, True), namegen=prefix_name, endpoint='bp_filemanager.static') # Define wtforms widget and field class CKTextAreaWidget(widgets.TextArea): def __call__(self, field, **kwargs): kwargs.setdefault('class_', 'ckeditor') return super(CKTextAreaWidget, self).__call__(field, **kwargs) class CKTextAreaField(fields.TextAreaField): widget = CKTextAreaWidget()
class TestAutoResizeForm(form.BaseForm): upload = form.ImageUploadField('Upload', base_path=path, max_size=(64, 64, True))
class TestNoResizeForm(form.BaseForm): upload = form.ImageUploadField('Upload', base_path=path, endpoint='test')
class TestForm(form.BaseForm): upload = form.ImageUploadField('Upload', base_path=path, thumbnail_size=(100, 100, True))
def image_upload_field(label): return form.ImageUploadField(label, base_path=file_path, thumbnail_size=(100, 100, True), namegen=prefix_name, endpoint='filemanager.static')