From 3ac609218721e394d51820bc3118a150c2853246 Mon Sep 17 00:00:00 2001 From: Quentin Rouland Date: Mon, 23 Jan 2017 00:04:51 +0100 Subject: [PATCH] =?UTF-8?q?TG-103=20Am=C3=A9lioration=20structure=20du=20p?= =?UTF-8?q?rojet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ backend/README.md | 19 ++++-------- backend/app/api/exampleapi.py | 2 +- backend/app/{config.py => config.py.example} | 13 ++++++++ backend/app/core.py | 31 +++++++------------- backend/app/model.py | 2 +- backend/app/urls.py | 3 +- backend/manage.py | 19 ++++++------ backend/requirements/common.txt | 5 +--- 9 files changed, 48 insertions(+), 49 deletions(-) rename backend/app/{config.py => config.py.example} (63%) diff --git a/.gitignore b/.gitignore index bcb9495..bb25ae9 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,6 @@ pip-selfcheck.json htmlcov/ .coverage coverage.xml + +#Config files +config.py diff --git a/backend/README.md b/backend/README.md index b371c5f..220a436 100644 --- a/backend/README.md +++ b/backend/README.md @@ -35,22 +35,15 @@ Installer les dépendances via pip pour les tests : ### Fichier Configuration -Dans app/config.py: -* Configuration les paramètres de la base de données -* Changer la SECRET_KEY en production +Copier le fichier app/config.py.example en app/config.py et configurer avec vos parametres -### Init App +Principalement: +* Configuration des paramètres de la base de données +* Changement de la SECRET_KEY en production -First you need to create the db and seed it with an admin user (admin@admin.com/admin). -Migration database is handle via flask-migrate using alembic. -See command available : -``` - python manage.py db -``` - -#### Init db -Installez la dernière version de MariaDB: +### Init db +Installez la dernière version de MariaDB. Lancer le script : ``` diff --git a/backend/app/api/exampleapi.py b/backend/app/api/exampleapi.py index f1d625d..82945de 100644 --- a/backend/app/api/exampleapi.py +++ b/backend/app/api/exampleapi.py @@ -15,4 +15,4 @@ class SomeApi(Resource): return {'somedelete': 'somedeletedata'}, 204 def put(self, id=None): - return {'someput': 'someputdata'}, 204 + return {'someput': 'someputdata'}, 201 diff --git a/backend/app/config.py b/backend/app/config.py.example similarity index 63% rename from backend/app/config.py rename to backend/app/config.py.example index 3fadc0f..e46de8e 100644 --- a/backend/app/config.py +++ b/backend/app/config.py.example @@ -2,6 +2,19 @@ import os class Config: + ACTIVE_CONFIG = "app.config.Prod" + + @staticmethod + def configure_app(config="prod"): + if config.lower() == "prod": + Config.ACTIVE_CONFIG = 'app.config.Prod' + elif config.lower() == "debug": + Config.ACTIVE_CONFIG = 'app.config.Debug' + elif config.lower() == "test": + Config.ACTIVE_CONFIG = 'app.config.Test' + else: + raise Exception('{} n\'est pas configuration une configuration valide'.format(config)) + DEBUG = False TESTING = False BASE_DIR = os.path.abspath(os.path.dirname(__file__)) diff --git a/backend/app/core.py b/backend/app/core.py index 0b9c784..8d621a2 100644 --- a/backend/app/core.py +++ b/backend/app/core.py @@ -6,30 +6,21 @@ from flask_restful import Api from flask_sqlalchemy import SQLAlchemy from sqlalchemy import create_engine from sqlalchemy.ext.automap import automap_base - - -def configure_app(config="prod"): - if config.lower() == "debug": - app.config.from_object('app.config.Debug') - elif config.lower() == "test": - app.config.from_object('app.config.Test') - else: - app.config.from_object('app.config.Prod') - - app.permanent_session_lifetime = \ - timedelta( - minutes=app.config - ['SESSION_VALIDITY_DURATION_WITHOUT_ACTIVITY_MIN'] - ) - - @app.before_request - def before_request(): - session.modified = True +from app.config import Config # initialization Flask app = Flask(__name__) -configure_app() +app.config.from_object(Config.ACTIVE_CONFIG) + +app.permanent_session_lifetime = \ + timedelta( + minutes=app.config['SESSION_VALIDITY_DURATION_WITHOUT_ACTIVITY_MIN'] + ) + +@app.before_request +def before_request(): + session.modified = True # SQLAlchemy db = SQLAlchemy(app) diff --git a/backend/app/model.py b/backend/app/model.py index 9f88456..9fc2663 100644 --- a/backend/app/model.py +++ b/backend/app/model.py @@ -1,4 +1,4 @@ -from backend.app.core import Base +from app.core import Base USER = Base.classes.user SETTINGS = Base.classes.settings diff --git a/backend/app/urls.py b/backend/app/urls.py index 70dc355..6d20a18 100644 --- a/backend/app/urls.py +++ b/backend/app/urls.py @@ -1,4 +1,5 @@ from app.core import api +from app.api.exampleapi import SomeApi # Some Api resource -api.add_resource(api, '/api/someapi', '/api/someapi/') +api.add_resource(SomeApi, '/api/someapi', '/api/someapi/') diff --git a/backend/manage.py b/backend/manage.py index c98218d..93493c3 100644 --- a/backend/manage.py +++ b/backend/manage.py @@ -7,7 +7,7 @@ import warnings from flask_script import Manager, Command from flask_script import prompt_bool -from backend.app.core import configure_app +from app.config import Config warnings.simplefilter('ignore') @@ -18,14 +18,15 @@ group.add_argument("-t", "--test", action="store_true") args, _ = parser.parse_known_args() if args.debug: - configure_app(config="debug") -if args.test: - configure_app(config="test") + Config.configure_app(config="debug") +elif args.test: + Config.configure_app(config="test") +else: + Config.configure_app(config="prod") -db = importlib.import_module("db", "backend.app.core") -app = importlib.import_module("app", "backend.app.core") +core = importlib.import_module("app.core") -manager = Manager(app) +manager = Manager(core.app) manager.add_option("-d", "--debug", action="store_true", dest="debug", required=False) manager.add_option("-t", "--test", @@ -58,7 +59,7 @@ class CheckDB(Command): def run(self): print("List of parsed tables:") - print(db.metadata.tables.keys()) + print(core.db.metadata.tables.keys()) manager.add_command('checkdb', CheckDB()) @@ -67,7 +68,7 @@ manager.add_command('checkdb', CheckDB()) class RunTests(Command): """Seed the db """ def run(self): - configure_app(config="test") + Config.configure_app(config="test") os.system("python manage.py -t db downgrade base") os.system("python manage.py -t db upgrade") test_loader = unittest.defaultTestLoader diff --git a/backend/requirements/common.txt b/backend/requirements/common.txt index c3b430f..2a9108d 100644 --- a/backend/requirements/common.txt +++ b/backend/requirements/common.txt @@ -1,8 +1,5 @@ flask < 0.13 flask-script < 2.1 flask-sqlalchemy < 2.2 -flask-migrate < 2.1 -flask-bootstrap < 3.4 flask-restful < 0.4 -passlib < 1.8 -mysqlclient < 1.4 \ No newline at end of file +mysqlclient < 1.4