Skip to content

Instantly share code, notes, and snippets.

@mapio mapio/app.py
Created May 11, 2015

Embed
What would you like to do?
A flask-admin bug report concerning FileUploadField
from functools import partial
from os import remove
from os.path import join
from flask import Flask
from flask.ext.admin import Admin, form
from flask.ext.admin.contrib.sqla import ModelView
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.event import listens_for
app = Flask( __name__ )
app.config[ 'SECRET_KEY' ] = '123456790'
app.config[ 'SQLALCHEMY_DATABASE_URI' ] = 'sqlite:////tmp/db.sql'
BASE_PATH = '/tmp'
db = SQLAlchemy( app )
class ModelWithUpload( db.Model ):
id = db.Column( db.Integer, primary_key = True )
model_path = db.Column( db.String )
@listens_for( ModelWithUpload, 'after_delete' )
def del_file( mapper, connection, target ):
if target.model_path:
try:
remove( join( BASE_PATH, target.model_path ) )
except OSError:
pass
class ViewWithUpload( ModelView ):
form_overrides = {
'model_path': partial( form.FileUploadField, allowed_extensions = ( 'txt', ) )
}
form_args = {
'model_path': {
'label': 'A file',
'base_path': BASE_PATH
}
}
db.drop_all()
db.create_all()
admin = Admin( app, template_mode='bootstrap3' )
admin.add_view( ViewWithUpload( ModelWithUpload, db.session ) )
app.run( host = '0.0.0.0', port = 8000, debug = True )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.