From 770838f0fc1794c47d43af98dd52220f0fa93799 Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Fri, 24 Apr 2015 11:57:40 +0200 Subject: [PATCH] Tweaks to file_server initialisation We are setting a sensible default for the storage folder and loading that automatically, this way we avoid having a compulsory config file. --- attract/application/__init__.py | 5 +++-- attract/application/file_server.py | 7 ++++--- attract/config.py.example | 10 ---------- attract/manage.py | 23 ++++++++++++++++------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/attract/application/__init__.py b/attract/application/__init__.py index d21da72c..7e084b0b 100644 --- a/attract/application/__init__.py +++ b/attract/application/__init__.py @@ -14,8 +14,6 @@ from bson import ObjectId from datetime import datetime from datetime import timedelta -from file_server import file_server - RFC1123_DATE_FORMAT = '%a, %d %b %Y %H:%M:%S GMT' @@ -184,4 +182,7 @@ def post_item(entry, data): app = Eve(validator=ValidateCustomFields, auth=CustomTokenAuth) + +# The file_server module needs app to be defined +from file_server import file_server app.register_blueprint(file_server, url_prefix='/file_server') diff --git a/attract/application/file_server.py b/attract/application/file_server.py index 8a8c7e3e..e9e90188 100644 --- a/attract/application/file_server.py +++ b/attract/application/file_server.py @@ -3,7 +3,8 @@ import os from flask import Blueprint from flask import request -import config +from application import app + file_server = Blueprint('file_server', __name__, template_folder='templates', @@ -22,10 +23,10 @@ def index(file_name=None): #POST file file_name = request.form['name'] folder_name = file_name[:2] - file_folder_path = os.path.join(config.Development.FILE_STORAGE, + file_folder_path = os.path.join(app.config['FILE_STORAGE'], folder_name) if not os.path.exists(file_folder_path): - os.mkdir(file_folder_path) + os.mkdir(file_folder_path) file_path = os.path.join(file_folder_path, file_name) request.files['data'].save(file_path) diff --git a/attract/config.py.example b/attract/config.py.example index 64b10499..fc3d258d 100644 --- a/attract/config.py.example +++ b/attract/config.py.example @@ -1,6 +1,5 @@ import os - class Config(object): # Configured for GMAIL MAIL_SERVER = '' @@ -20,19 +19,10 @@ class Config(object): SECURITY_PASSWORD_SALT = '' SECURITY_EMAIL_SENDER = '' - class Development(Config): SECRET_KEY = '' - #SERVER_NAME = 'attract.local:5555' HOST = '0.0.0.0' PORT = 5000 DEBUG = True - SQLALCHEMY_DATABASE_URI = '' - SECURITY_REGISTERABLE = True - SECURITY_LOGIN_USER_TEMPLATE = 'security/login_user.html' - MEDIA_FOLDER = '' - MEDIA_URL = '' - MEDIA_THUMBNAIL_FOLDER = '' - MEDIA_THUMBNAIL_URL = '' FILE_STORAGE = '{0}/application/static/storage'.format( os.path.join(os.path.dirname(__file__))) diff --git a/attract/manage.py b/attract/manage.py index 0a4e4439..7ae84f6a 100644 --- a/attract/manage.py +++ b/attract/manage.py @@ -1,3 +1,4 @@ +import os from application import app from application import post_item from flask.ext.script import Manager @@ -7,14 +8,22 @@ manager = Manager(app) @manager.command def runserver(): try: - import config - PORT = config.Development.PORT - HOST = config.Development.HOST - DEBUG = config.Development.DEBUG + import config + PORT = config.Development.PORT + HOST = config.Development.HOST + DEBUG = config.Development.DEBUG + app.config['FILE_STORAGE'] = config.Development.FILE_STORAGE except ImportError: - PORT = 5000 - HOST = '0.0.0.0' - DEBUG = True + # Default settings + PORT = 5000 + HOST = '0.0.0.0' + DEBUG = True + app.config['FILE_STORAGE'] = '{0}/application/static/storage'.format( + os.path.join(os.path.dirname(__file__))) + + # Automatic creation of FILE_STORAGE path if it's missing + if not os.path.exists(app.config['FILE_STORAGE']): + os.mkdir(app.config['FILE_STORAGE']) app.run( port=PORT,