Created
October 11, 2015 04:14
-
-
Save pawl/ae9149408be1919448e0 to your computer and use it in GitHub Desktop.
Example for flask-admin issue 874
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import os.path as op | |
| from flask import Flask | |
| from flask_sqlalchemy import SQLAlchemy | |
| from sqlalchemy.event import listens_for | |
| from flask_admin import Admin, form | |
| from flask_admin.contrib import sqla | |
| # Create application | |
| app = Flask(__name__, static_folder='files') | |
| # Create dummy secrey key so we can use sessions | |
| app.config['SECRET_KEY'] = '123456790' | |
| # Create in-memory database | |
| app.config['DATABASE_FILE'] = 'sample_db.sqlite' | |
| app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE'] | |
| app.config['SQLALCHEMY_ECHO'] = True | |
| db = SQLAlchemy(app) | |
| # Create directory for file fields to use | |
| file_path = op.join(op.dirname(__file__), 'files') | |
| try: | |
| os.mkdir(file_path) | |
| except OSError: | |
| pass | |
| # Create models | |
| class File(db.Model): | |
| id = db.Column(db.Integer, primary_key=True) | |
| name = db.Column(db.Unicode(64)) | |
| path = db.Column(db.Unicode(128)) | |
| def __unicode__(self): | |
| return self.name | |
| # Delete hooks for models, delete files if models are getting deleted | |
| @listens_for(File, 'after_delete') | |
| def del_file(mapper, connection, target): | |
| if target.path: | |
| try: | |
| os.remove(op.join(file_path, target.path)) | |
| except OSError: | |
| # Don't care if was not deleted because it does not exist | |
| pass | |
| # Administrative views | |
| class FileView(sqla.ModelView): | |
| # Override form field to use Flask-Admin FileUploadField | |
| form_overrides = { | |
| 'path': form.FileUploadField | |
| } | |
| # Pass additional parameters to 'path' to FileUploadField constructor | |
| form_args = { | |
| 'path': { | |
| 'label': 'File', | |
| 'base_path': file_path, | |
| 'allow_overwrite': False, | |
| 'allowed_extensions': ['jpg', 'gif', 'png'] | |
| } | |
| } | |
| # Flask views | |
| @app.route('/') | |
| def index(): | |
| return '<a href="/admin/">Click me to get to Admin!</a>' | |
| # Create admin | |
| admin = Admin(app, 'Example: Forms', template_mode='bootstrap3') | |
| # Add views | |
| admin.add_view(FileView(File, db.session)) | |
| def build_sample_db(): | |
| """ | |
| Populate a small db with some example entries. | |
| """ | |
| db.drop_all() | |
| db.create_all() | |
| for i in [1, 2, 3]: | |
| file = File() | |
| file.name = "Example " + str(i) | |
| file.path = "example_" + str(i) + ".pdf" | |
| db.session.add(file) | |
| db.session.commit() | |
| return | |
| if __name__ == '__main__': | |
| # Build a sample db on the fly, if one does not exist yet. | |
| app_dir = op.realpath(os.path.dirname(__file__)) | |
| database_path = op.join(app_dir, app.config['DATABASE_FILE']) | |
| if not os.path.exists(database_path): | |
| build_sample_db() | |
| # Start app | |
| app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment