From 1bada7a9686a4cac3a8c67421be4e601cdae4835 Mon Sep 17 00:00:00 2001 From: sepro Date: Thu, 16 Feb 2017 12:56:59 +0100 Subject: [PATCH] refactoring code --- planet/__init__.py | 221 ++++++++++++++++++++++----------------------- run_unittest.py | 2 +- 2 files changed, 111 insertions(+), 112 deletions(-) diff --git a/planet/__init__.py b/planet/__init__.py index ec1ce21..cc98233 100644 --- a/planet/__init__.py +++ b/planet/__init__.py @@ -40,33 +40,7 @@ def create_app(config): app.config.from_object(config) - db.app = app - db.init_app(app) - - # Enable login manager - - login_manager.init_app(app) - login_manager.login_view = 'auth.login' - - # Enable cach - cache.init_app(app) - - # Enable Compress - compress.init_app(app) - - # Enable HTMLMIN - htmlmin.init_app(app) - - # Enable DebugToolBar - toolbar.init_app(app) - - LOGIN_ENABLED = app.config['LOGIN_ENABLED'] - BLAST_ENABLED = app.config['BLAST_ENABLED'] - TWITTER_HANDLE = app.config['TWITTER_HANDLE'] if 'TWITTER_HANDLE' in app.config.keys() else None - - # Enable BLAST - if BLAST_ENABLED: - blast_thread.init_app(app) + configure_extensions(app) # Import all models here from planet.models.users import User @@ -96,59 +70,10 @@ def create_app(config): from planet.models.relationships.sequence_xref import SequenceXRefAssociation from planet.models.relationships.family_xref import FamilyXRefAssociation - # Import controllers and register as blueprint - from planet.controllers.main import main - from planet.controllers.auth import auth, no_login - from planet.controllers.blast import blast - from planet.controllers.sequence import sequence - from planet.controllers.species import species - from planet.controllers.go import go - from planet.controllers.interpro import interpro - from planet.controllers.family import family - from planet.controllers.expression_cluster import expression_cluster - from planet.controllers.expression_profile import expression_profile - from planet.controllers.expression_network import expression_network - from planet.controllers.search import search - from planet.controllers.help import help - from planet.controllers.heatmap import heatmap - from planet.controllers.profile_comparison import profile_comparison - from planet.controllers.custom_network import custom_network - from planet.controllers.graph_comparison import graph_comparison - from planet.controllers.clade import clade - from planet.controllers.ecc import ecc - from planet.controllers.specificity_comparison import specificity_comparison - from planet.controllers.admin.controls import admin_controls - - app.register_blueprint(main) - if LOGIN_ENABLED: - app.register_blueprint(auth, url_prefix='/auth') - app.register_blueprint(admin_controls, url_prefix='/admin_controls') - else: - app.register_blueprint(no_login, url_prefix='/auth') - app.register_blueprint(no_login, url_prefix='/admin_controls') - - if BLAST_ENABLED: - app.register_blueprint(blast, url_prefix='/blast') - - app.register_blueprint(sequence, url_prefix='/sequence') - app.register_blueprint(species, url_prefix='/species') - app.register_blueprint(go, url_prefix='/go') - app.register_blueprint(interpro, url_prefix='/interpro') - app.register_blueprint(family, url_prefix='/family') - app.register_blueprint(expression_cluster, url_prefix='/cluster') - app.register_blueprint(expression_profile, url_prefix='/profile') - app.register_blueprint(expression_network, url_prefix='/network') - app.register_blueprint(search, url_prefix='/search') - app.register_blueprint(help, url_prefix='/help') - app.register_blueprint(heatmap, url_prefix='/heatmap') - app.register_blueprint(profile_comparison, url_prefix='/profile_comparison') - app.register_blueprint(custom_network, url_prefix='/custom_network') - app.register_blueprint(graph_comparison, url_prefix='/graph_comparison') - app.register_blueprint(clade, url_prefix='/clade') - app.register_blueprint(ecc, url_prefix='/ecc') - app.register_blueprint(specificity_comparison, url_prefix='/specificity_comparison') + configure_blueprints(app) # Admin panel + LOGIN_ENABLED = app.config['LOGIN_ENABLED'] if LOGIN_ENABLED: from planet.controllers.admin.views import MyAdminIndexView from planet.controllers.admin.views import SpeciesAdminView, GeneFamilyMethodAdminView, ExpressionNetworkMethodAdminView, \ @@ -251,30 +176,108 @@ def create_app(config): admin.add_view(ExpressionSpecificityMethodAdminView(ExpressionSpecificityMethod, db.session, url='specificity/', category="Browse", name='Expression Specificity')) + configure_error_handlers(app) + + configure_hooks(app) + + return app + + +def configure_extensions(app): + db.app = app + db.init_app(app) + + # Enable login manager + + login_manager.init_app(app) + login_manager.login_view = 'auth.login' + + # Enable cach + cache.init_app(app) + + # Enable Compress + compress.init_app(app) + + # Enable HTMLMIN + htmlmin.init_app(app) + + # Enable DebugToolBar + toolbar.init_app(app) + + BLAST_ENABLED = app.config['BLAST_ENABLED'] + + # Enable BLAST + if BLAST_ENABLED: + blast_thread.init_app(app) + + from planet.models.users import User + + @login_manager.user_loader + def load_user(user_id): + return User.get(user_id) + + @login_manager.unauthorized_handler + def unauthorized(): + return render_template('error/403.html'), 403 + + +def configure_blueprints(app): + # Import controllers and register as blueprint + from planet.controllers.main import main + from planet.controllers.auth import auth, no_login + from planet.controllers.blast import blast + from planet.controllers.sequence import sequence + from planet.controllers.species import species + from planet.controllers.go import go + from planet.controllers.interpro import interpro + from planet.controllers.family import family + from planet.controllers.expression_cluster import expression_cluster + from planet.controllers.expression_profile import expression_profile + from planet.controllers.expression_network import expression_network + from planet.controllers.search import search + from planet.controllers.help import help + from planet.controllers.heatmap import heatmap + from planet.controllers.profile_comparison import profile_comparison + from planet.controllers.custom_network import custom_network + from planet.controllers.graph_comparison import graph_comparison + from planet.controllers.clade import clade + from planet.controllers.ecc import ecc + from planet.controllers.specificity_comparison import specificity_comparison + from planet.controllers.admin.controls import admin_controls + + LOGIN_ENABLED = app.config['LOGIN_ENABLED'] + BLAST_ENABLED = app.config['LOGIN_ENABLED'] + + app.register_blueprint(main) + if LOGIN_ENABLED: + app.register_blueprint(auth, url_prefix='/auth') + app.register_blueprint(admin_controls, url_prefix='/admin_controls') + else: + app.register_blueprint(no_login, url_prefix='/auth') + app.register_blueprint(no_login, url_prefix='/admin_controls') - # ______________________________ - # < Beware, code overrides below > - # ------------------------------ - # \ , , - # \ /( )` - # \ \ \___ / | - # /- _ `-/ ' - # (/\/ \ \ /\ - # / / | ` \ - # O O ) / | - # `-^--'`< ' - # (_.) _ ) / - # `.___/` / - # `-----' / - # <----. __ / __ \ - # <----|====O)))==) \) /==== - # <----' `--' `.__,' \ - # | | - # \ / - # ______( (_ / \______ - # ,' ,-----' | \ - # `--{__________) \/ + if BLAST_ENABLED: + app.register_blueprint(blast, url_prefix='/blast') + app.register_blueprint(sequence, url_prefix='/sequence') + app.register_blueprint(species, url_prefix='/species') + app.register_blueprint(go, url_prefix='/go') + app.register_blueprint(interpro, url_prefix='/interpro') + app.register_blueprint(family, url_prefix='/family') + app.register_blueprint(expression_cluster, url_prefix='/cluster') + app.register_blueprint(expression_profile, url_prefix='/profile') + app.register_blueprint(expression_network, url_prefix='/network') + app.register_blueprint(search, url_prefix='/search') + app.register_blueprint(help, url_prefix='/help') + app.register_blueprint(heatmap, url_prefix='/heatmap') + app.register_blueprint(profile_comparison, url_prefix='/profile_comparison') + app.register_blueprint(custom_network, url_prefix='/custom_network') + app.register_blueprint(graph_comparison, url_prefix='/graph_comparison') + app.register_blueprint(clade, url_prefix='/clade') + app.register_blueprint(ecc, url_prefix='/ecc') + app.register_blueprint(specificity_comparison, url_prefix='/specificity_comparison') + +def configure_error_handlers(app): # Custom error handler for 404 errors @app.errorhandler(405) @@ -289,9 +292,15 @@ def page_not_found(e): def access_denied(e): return render_template('error/403.html'), 403 + +def configure_hooks(app): # Register form for basic searches, needs to be done here as it is included on every page! from planet.forms.search import BasicSearchForm + LOGIN_ENABLED = app.config['LOGIN_ENABLED'] + BLAST_ENABLED = app.config['BLAST_ENABLED'] + TWITTER_HANDLE = app.config['TWITTER_HANDLE'] if 'TWITTER_HANDLE' in app.config.keys() else None + @app.before_request def before_request(): g.login_enabled = LOGIN_ENABLED @@ -306,14 +315,4 @@ def before_request(): g.msg = app.config['GLOB_MSG'] g.msg_title = app.config['GLOB_MSG_TITLE'] if 'GLOB_MSG_TITLE' in app.config else 'info' else: - g.msg = None - - @login_manager.user_loader - def load_user(user_id): - return User.get(user_id) - - @login_manager.unauthorized_handler - def unauthorized(): - return render_template('error/403.html'), 403 - - return app + g.msg = None \ No newline at end of file diff --git a/run_unittest.py b/run_unittest.py index b07c89f..b7f7ff0 100644 --- a/run_unittest.py +++ b/run_unittest.py @@ -4,7 +4,7 @@ import unittest import os -cov = coverage(branch=True, omit=['virtualenv/*', 'tests/*', 'config.py', 'utils/benchmark.py']) +cov = coverage(branch=True, omit=['venv/*', 'tests/*', 'config.py', 'utils/benchmark.py']) cov.start()