def create_app(config_name=None): if config_name == None: config_name = "production" app = Flask(__name__, template_folder="templates") # https://trstringer.com/logging-flask-gunicorn-the-manageable-way/ if(config_name == "production"): gunicorn_logger = logging.getLogger("gunicorn.error") app.logger.handlers = gunicorn_logger.handlers app.logger.setLevel(gunicorn_logger.level) config_module = f"lightbluetent.config.{config_name.capitalize()}Config" app.config.from_object(config_module) if not app.secret_key and "FLASK_SECRET_KEY" in os.environ: app.secret_key = os.environ["FLASK_SECRET_KEY"] if not app.request_class.trusted_hosts and "FLASK_TRUSTED_HOSTS" in os.environ: app.request_class.trusted_hosts = os.environ["FLASK_TRUSTED_HOSTS"].split(",") app.config["CSRF_CHECK_REFERER"] = False csrf = SeaSurf(app) csp = { "default-src": ["'self'", "www.srcf.net"], "img-src": ["'self'", "data:", "www.srcf.net"], 'style-src': [ '\'self\'', '\'unsafe-inline\'', 'www.srcf.net' ] } Talisman(app, content_security_policy=csp) babel = Babel(app) app.jinja_env.globals["sif"] = sif app.jinja_env.globals["gen_unique_string"] = gen_unique_string app.jinja_env.globals["ordinal"] = ordinal from lightbluetent.models import db, migrate db.init_app(app) migrate.init_app(app, db) app.register_blueprint(admin.bp) app.register_blueprint(home.bp) app.register_blueprint(society.bp) app.register_error_handler(404, page_not_found) app.register_error_handler(500, server_error) @app.context_processor def inject_gh_rev(): return dict( github_rev=subprocess.check_output(["git", "describe", "--tags"]) .strip() .decode() ) return app
from flask_login import LoginManager from flask_mail import Mail from flask_bootstrap import Bootstrap from flask_moment import Moment from flask_babel import Babel, lazy_gettext as _l from elasticsearch import Elasticsearch db = SQLAlchemy() migrate = Migrate() login = LoginManager() login.login_view = 'auth.login' login.login_message = _l('Please log in to access this page.') mail = Mail() bootstrap = Bootstrap() moment = Moment() babel = Babel() def create_app(config_class=Config): app = Flask(__name__) app.config.from_object(Config) db.init_app(app) migrate.init_app(app, db) login.init_app(app) mail.init_app(app) bootstrap.init_app(app) moment.init_app(app) babel.init_app(app) app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) \ if app.config['ELASTICSEARCH_URL'] else None
def create_app(notebook, startup_token=None, debug=False): """ This is the main method to create a running notebook. This is called from the process spawned in run.py """ # Create app app = Flask(__name__) app.startup_token = startup_token app.config.update({ 'SESSION_COOKIE_HTTPONLY': False, 'PROPAGATE_EXCEPTIONS': debug, 'DEBUG': debug, 'SECRET_KEY': os.urandom(24), }) dynamic_javascript = DynamicJs(debug=debug) @app.before_request def set_notebook_object(): g.notebook = notebook g.dynamic_javascript = dynamic_javascript # Handles all uncaught exceptions if not debug activated @app.errorhandler(500) def log_exception(error): return message_template( gettext('''500: Internal server error.'''), username=getattr(g, 'username', UAT_GUEST)), 500 # Template globals app.add_template_global(url_for) # Template filters app.add_template_filter(css_escape) app.add_template_filter(number_of_rows) app.add_template_filter(convert_time_to_string) app.add_template_filter(prettify_time_ago) app.add_template_filter(join_max) app.add_template_filter(max) app.add_template_filter(lambda x: repr(str(x)), name='repr_str') # Default template context @app.context_processor def default_template_context(): return {'sitename': gettext('Sage Notebook'), 'sage_version': SAGE_VERSION, 'MATHJAX': MATHJAX, 'JEDITABLE_TINYMCE': JEDITABLE_TINYMCE, 'conf': notebook.conf} # Register the Blueprints app.register_blueprint(admin) app.register_blueprint(authentication) app.register_blueprint(base) app.register_blueprint(doc, url_prefix='/doc') app.register_blueprint(static_paths) app.register_blueprint(worksheet) app.register_blueprint(worksheet_listing) # # Extensions # Babel babel = Babel(app, default_locale='en_US') # Check if saved default language exists. If not fallback to default @app.before_first_request def check_default_lang(): def_lang = notebook.conf['default_language'] trans_ids = [str(trans) for trans in babel.list_translations()] if def_lang not in trans_ids: notebook.conf['default_language'] = None # register callback function for locale selection # this function must be modified to add per user language support @babel.localeselector def get_locale(): return g.notebook.conf['default_language'] # Themes app.config['THEME_PATHS'] = THEME_PATHS app.config['DEFAULT_THEME'] = DEFAULT_THEME Themes(app, app_identifier='sagewui', loaders=[theme_paths_loader]) name = notebook.conf['theme'] if name not in app.theme_manager.themes: notebook.conf['theme'] = app.config['DEFAULT_THEME'] # autoindex v0.3 doesnt seem to work with modules # routing with app directly does the trick # TODO: Check to see if autoindex 0.4 works with modules idx = AutoIndex(app, browse_root=SRC_PATH, add_url_rules=False) @app.route('/src/') @app.route('/src/<path:path>') @guest_or_login_required def autoindex(path='.'): filename = os.path.join(SRC_PATH, path) if os.path.isfile(filename): with open(filename, 'rb') as f: src = f.read().decode('utf-8', 'ignore') if (os.path.splitext(filename)[1] in ['.py', '.c', '.cc', '.h', '.hh', '.pyx', '.pxd']): return render_template( 'html/source_code.html', src_filename=path, src=src, username=g.username) return src return idx.render_autoindex(path) return app
def create_app(app_name=None): # Configuration settings import config if not app_name: app_name = config.APP_NAME # Only enable password related functionality in server mode. if config.SERVER_MODE is True: # Some times we need to access these config params where application # context is not available (we can't use current_app.config in those # cases even with current_app.app_context()) # So update these params in config itself. # And also these updated config values will picked up by application # since we are updating config before the application instance is # created. config.SECURITY_RECOVERABLE = True config.SECURITY_CHANGEABLE = True # Now we'll open change password page in alertify dialog # we don't want it to redirect to main page after password # change operation so we will open the same password change page again. config.SECURITY_POST_CHANGE_VIEW = 'browser.change_password' """Create the Flask application, startup logging and dynamically load additional modules (blueprints) that are found in this directory.""" app = PgAdmin(__name__, static_url_path='/static') # Removes unwanted whitespace from render_template function app.jinja_env.trim_blocks = True app.config.from_object(config) app.config.update(dict(PROPAGATE_EXCEPTIONS=True)) ########################################################################## # Setup logging and log the application startup ########################################################################## # Add SQL level logging, and set the base logging level logging.addLevelName(25, 'SQL') app.logger.setLevel(logging.DEBUG) app.logger.handlers = [] # We also need to update the handler on the webserver in order to see # request. Setting the level prevents werkzeug from setting up it's own # stream handler thus ensuring all the logging goes through the pgAdmin # logger. logger = logging.getLogger('werkzeug') logger.setLevel(logging.INFO) # Set SQLITE_PATH to TEST_SQLITE_PATH while running test cases if ('PGADMIN_TESTING_MODE' in os.environ and os.environ['PGADMIN_TESTING_MODE'] == '1'): config.SQLITE_PATH = config.TEST_SQLITE_PATH # Ensure the various working directories exist from pgadmin.setup import create_app_data_directory, db_upgrade create_app_data_directory(config) # File logging fh = logging.FileHandler(config.LOG_FILE, encoding='utf-8') fh.setLevel(config.FILE_LOG_LEVEL) fh.setFormatter(logging.Formatter(config.FILE_LOG_FORMAT)) app.logger.addHandler(fh) logger.addHandler(fh) # Console logging ch = logging.StreamHandler() ch.setLevel(config.CONSOLE_LOG_LEVEL) ch.setFormatter(logging.Formatter(config.CONSOLE_LOG_FORMAT)) app.logger.addHandler(ch) logger.addHandler(ch) # Log the startup app.logger.info('########################################################') app.logger.info('Starting %s v%s...', config.APP_NAME, config.APP_VERSION) app.logger.info('########################################################') app.logger.debug("Python syspath: %s", sys.path) ########################################################################## # Setup i18n ########################################################################## # Initialise i18n babel = Babel(app) app.logger.debug('Available translations: %s' % babel.list_translations()) @babel.localeselector def get_locale(): """Get the language for the user.""" language = 'en' if config.SERVER_MODE is False: # Get the user language preference from the miscellaneous module if current_user.is_authenticated: user_id = current_user.id else: user = user_datastore.get_user(config.DESKTOP_USER) if user is not None: user_id = user.id user_language = Preferences.raw_value('miscellaneous', 'user_language', None, user_id) if user_language is not None: language = user_language else: # If language is available in get request then return the same # otherwise check the session or cookie data = request.form if 'language' in data: language = data['language'] or language setattr(session, 'PGADMIN_LANGUAGE', language) elif hasattr(session, 'PGADMIN_LANGUAGE'): language = getattr(session, 'PGADMIN_LANGUAGE', language) elif hasattr(request.cookies, 'PGADMIN_LANGUAGE'): language = getattr(request.cookies, 'PGADMIN_LANGUAGE', language) return language ########################################################################## # Setup authentication ########################################################################## app.config['SQLALCHEMY_DATABASE_URI'] = u'sqlite:///{0}?timeout={1}' \ .format(config.SQLITE_PATH.replace(u'\\', u'/'), getattr(config, 'SQLITE_TIMEOUT', 500) ) # Create database connection object and mailer db.init_app(app) ########################################################################## # Upgrade the schema (if required) ########################################################################## with app.app_context(): # Run migration for the first time i.e. create database from config import SQLITE_PATH if not os.path.exists(SQLITE_PATH): db_upgrade(app) else: version = Version.query.filter_by(name='ConfigDB').first() schema_version = version.value # Run migration if current schema version is greater than the # schema version stored in version table if CURRENT_SCHEMA_VERSION >= schema_version: db_upgrade(app) # Update schema version to the latest if CURRENT_SCHEMA_VERSION > schema_version: version = Version.query.filter_by(name='ConfigDB').first() version.value = CURRENT_SCHEMA_VERSION db.session.commit() Mail(app) import pgadmin.utils.paths as paths paths.init_app(app) # Setup Flask-Security user_datastore = SQLAlchemyUserDatastore(db, User, Role) security = Security(None, user_datastore) ########################################################################## # Setup security ########################################################################## with app.app_context(): config.CSRF_SESSION_KEY = Keys.query.filter_by( name='CSRF_SESSION_KEY').first().value config.SECRET_KEY = Keys.query.filter_by( name='SECRET_KEY').first().value config.SECURITY_PASSWORD_SALT = Keys.query.filter_by( name='SECURITY_PASSWORD_SALT').first().value # Update the app.config with proper security keyes for signing CSRF data, # signing cookies, and the SALT for hashing the passwords. app.config.update( dict({ 'CSRF_SESSION_KEY': config.CSRF_SESSION_KEY, 'SECRET_KEY': config.SECRET_KEY, 'SECURITY_PASSWORD_SALT': config.SECURITY_PASSWORD_SALT })) security.init_app(app, user_datastore) # register custom unauthorised handler. app.login_manager.unauthorized_handler(pga_unauthorised) app.session_interface = create_session_interface(app) # Make the Session more secure against XSS & CSRF when running in web mode if config.SERVER_MODE: paranoid = Paranoid(app) paranoid.redirect_view = 'browser.index' ########################################################################## # Load all available server drivers ########################################################################## driver.init_app(app) ########################################################################## # Register language to the preferences after login ########################################################################## @user_logged_in.connect_via(app) def register_language(sender, user): # After logged in, set the language in the preferences if we get from # the login page data = request.form if 'language' in data: language = data['language'] # Set the user language preference misc_preference = Preferences.module('miscellaneous') user_languages = misc_preference.preference('user_language') if user_languages and language: language = user_languages.set(language) ########################################################################## # Register any local servers we can discover ########################################################################## @user_logged_in.connect_via(app) def on_user_logged_in(sender, user): # Keep hold of the user ID user_id = user.id # Get the first server group for the user servergroup_id = 1 servergroups = ServerGroup.query.filter_by( user_id=user_id).order_by("id") if servergroups.count() > 0: servergroup = servergroups.first() servergroup_id = servergroup.id '''Add a server to the config database''' def add_server(user_id, servergroup_id, name, superuser, port, discovery_id, comment): # Create a server object if needed, and store it. servers = Server.query.filter_by( user_id=user_id, discovery_id=svr_discovery_id).order_by("id") if servers.count() > 0: return svr = Server(user_id=user_id, servergroup_id=servergroup_id, name=name, host='localhost', port=port, maintenance_db='postgres', username=superuser, ssl_mode='prefer', comment=svr_comment, discovery_id=discovery_id) db.session.add(svr) db.session.commit() # Figure out what servers are present if winreg is not None: arch_keys = set() proc_arch = os.environ['PROCESSOR_ARCHITECTURE'].lower() try: proc_arch64 = os.environ['PROCESSOR_ARCHITEW6432'].lower() except Exception as e: proc_arch64 = None if proc_arch == 'x86' and not proc_arch64: arch_keys.add(0) elif proc_arch == 'x86' or proc_arch == 'amd64': arch_keys.add(winreg.KEY_WOW64_32KEY) arch_keys.add(winreg.KEY_WOW64_64KEY) for arch_key in arch_keys: for server_type in ('PostgreSQL', 'EnterpriseDB'): try: root_key = winreg.OpenKey( winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\" + server_type + "\Services", 0, winreg.KEY_READ | arch_key) for i in xrange(0, winreg.QueryInfoKey(root_key)[0]): inst_id = winreg.EnumKey(root_key, i) inst_key = winreg.OpenKey(root_key, inst_id) svr_name = winreg.QueryValueEx( inst_key, 'Display Name')[0] svr_superuser = winreg.QueryValueEx( inst_key, 'Database Superuser')[0] svr_port = winreg.QueryValueEx(inst_key, 'Port')[0] svr_discovery_id = inst_id svr_comment = gettext( "Auto-detected %s installation with the data " "directory at %s" % (winreg.QueryValueEx(inst_key, 'Display Name')[0], winreg.QueryValueEx(inst_key, 'Data Directory')[0])) add_server(user_id, servergroup_id, svr_name, svr_superuser, svr_port, svr_discovery_id, svr_comment) inst_key.Close() except Exception as e: pass else: # We use the postgres-winreg.ini file on non-Windows try: from configparser import ConfigParser except ImportError: from ConfigParser import ConfigParser # Python 2 registry = ConfigParser() try: registry.read('/etc/postgres-reg.ini') sections = registry.sections() # Loop the sections, and get the data from any that are PG or PPAS for section in sections: if (section.startswith('PostgreSQL/') or section.startswith('EnterpriseDB/')): svr_name = registry.get(section, 'Description') svr_superuser = registry.get(section, 'Superuser') svr_port = registry.getint(section, 'Port') svr_discovery_id = section description = registry.get(section, 'Description') data_directory = registry.get(section, 'DataDirectory') if hasattr(str, 'decode'): description = description.decode('utf-8') data_directory = data_directory.decode('utf-8') svr_comment = gettext(u"Auto-detected %s installation " u"with the data directory at %s" % (description, data_directory)) add_server(user_id, servergroup_id, svr_name, svr_superuser, svr_port, svr_discovery_id, svr_comment) except Exception as e: pass @user_logged_in.connect_via(app) @user_logged_out.connect_via(app) def force_session_write(app, user): session.force_write = True ########################################################################## # Load plugin modules ########################################################################## for module in app.find_submodules('pgadmin'): app.logger.info('Registering blueprint module: %s' % module) app.register_blueprint(module) ########################################################################## # Handle the desktop login ########################################################################## @app.before_request def before_request(): """Login the default user if running in desktop mode""" # Check the auth key is valid, if it's set, and we're not in server # mode, and it's not a help file request. if not config.SERVER_MODE and app.PGADMIN_KEY != '': if (('key' not in request.args or request.args['key'] != app.PGADMIN_KEY) and request.cookies.get('PGADMIN_KEY') != app.PGADMIN_KEY and request.endpoint != 'help.static'): abort(401) if not config.SERVER_MODE and not current_user.is_authenticated: user = user_datastore.get_user(config.DESKTOP_USER) # Throw an error if we failed to find the desktop user, to give # the sysadmin a hint. We'll continue to try to login anyway as # that'll through a nice 500 error for us. if user is None: app.logger.error( 'The desktop user %s was not found in the configuration ' 'database.' % config.DESKTOP_USER) abort(401) login_user(user) @app.after_request def after_request(response): if 'key' in request.args: response.set_cookie('PGADMIN_KEY', value=request.args['key']) return response ########################################################################## # Minify output ########################################################################## # HTMLMIN doesn't work with Python 2.6. if not config.DEBUG and sys.version_info >= (2, 7): from flask_htmlmin import HTMLMIN HTMLMIN(app) @app.context_processor def inject_blueprint(): """Inject a reference to the current blueprint, if any.""" return { 'current_app': current_app, 'current_blueprint': current_blueprint } ########################################################################## # All done! ########################################################################## return app
import plotly.graph_objs as go from dash.dependencies import Input, Output, State from flask_babel import Babel, lazy_gettext as _ from calc import calcfunc from calc.electricity import calculate_electricity_supply_emission_factor from components.cards import GraphCard from components.graphs import make_layout from utils.quilt import load_datasets app = dash.Dash(__name__, suppress_callback_exceptions=True) app.css.config.serve_locally = True app.scripts.config.serve_locally = True server = app.server babel = Babel(server) with server.app_context(): server.config.from_object('common.settings') cache.init_app(server) DEFAULT_PRICE_PER_KWH = 12 INITIAL_INSTALL_PRICE = 5000 PRICE_PER_PEAK_KW = 1000 PEAK_WATTS_PER_M2 = 150 hel_buildings, hsy_buildings, fingrid_price = load_datasets([ 'jyrjola/karttahel/buildings', 'jyrjola/hsy/buildings', 'jyrjola/fingrid_hourly/price' ])
from flask_sqlalchemy import SQLAlchemy from flask_mail import Mail from flask_wtf.csrf import CSRFProtect from flask_babel import Babel from flask_debugtoolbar import DebugToolbarExtension import babel import os from config import config from .texts import OLD_ACCOUNT_PASSWORD_CHANGE db = SQLAlchemy() bcrypt = Bcrypt() bootstrap = Bootstrap() mail = Mail() babel_ = Babel() login_manager = LoginManager() login_manager.login_view = "auth.login" login_manager.login_message_category = "danger" login_manager.login_message = u"Veuillez vous connecter pour accéder à cette page." class PrefixMiddleware(object): def __init__(self, app, prefix=''): self.app = app self.prefix = prefix def __call__(self, environ, start_response): if environ['PATH_INFO'].startswith(self.prefix): environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):]
from logging.handlers import SMTPHandler, RotatingFileHandler import logging import os from flask import Flask, request, current_app from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_mail import Mail from flask_babel import Babel, lazy_gettext as _l from config import Config from flask_cors import CORS db = SQLAlchemy() # SQLAlchemy is a wonderful tool for interacting with the database without having to write SQL queries migrate = Migrate() # Like git, but for your database schema mail = Mail() # duh, doesn't actually get used babel = Babel() # Tool for localization, also doesn't get used cors = CORS() # Rules to prevent (or allow, rip) cross site scripting def create_app(config_class=Config): """Defines the "app factory" that's run in totlahtol.py to create the app instance. Adds extensions e.g. loads configuration, initializes database, loads flask_migrate, mail, cors policy, and the api blueprint. Two thirds of this function is mail and logging code that isn't even used, so just ignore it. """ app = Flask(__name__) app.config.from_object(config_class) db.init_app(app) migrate.init_app(app, db) mail.init_app(app) cors.init_app(app, supports_credentials=True, resources={r"/api/*": {"origins":"http://localhost:3000"}}) # app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) if app.config['ELASTICSEARCH_URL'] else None
def make_flask_stack(conf, **app_conf): """ This has to pass the flask app through all the same middleware that Pylons used """ root = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) debug = asbool(app_conf.get('debug', app_conf.get('DEBUG', False))) testing = asbool(app_conf.get('testing', app_conf.get('TESTING', False))) app = flask_app = CKANFlask(__name__) app.debug = debug app.testing = testing app.template_folder = os.path.join(root, 'templates') app.app_ctx_globals_class = CKAN_AppCtxGlobals app.url_rule_class = CKAN_Rule app.jinja_loader = ChoiceLoader( [app.jinja_loader, CkanextTemplateLoader()]) # Update Flask config with the CKAN values. We use the common config # object as values might have been modified on `load_environment` if config: app.config.update(config) else: app.config.update(conf) app.config.update(app_conf) # Do all the Flask-specific stuff before adding other middlewares # Secret key needed for flask-debug-toolbar and sessions if not app.config.get('SECRET_KEY'): app.config['SECRET_KEY'] = config.get('beaker.session.secret') if not app.config.get('SECRET_KEY'): raise RuntimeError(u'You must provide a value for the secret key' ' with the SECRET_KEY config option') if debug: from flask_debugtoolbar import DebugToolbarExtension app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False DebugToolbarExtension(app) # Use Beaker as the Flask session interface class BeakerSessionInterface(SessionInterface): def open_session(self, app, request): if 'beaker.session' in request.environ: return request.environ['beaker.session'] def save_session(self, app, session, response): session.save() namespace = 'beaker.session.' session_opts = dict([(k.replace('beaker.', ''), v) for k, v in config.iteritems() if k.startswith(namespace)]) if (not session_opts.get('session.data_dir') and session_opts.get('session.type', 'file') == 'file'): cache_dir = app_conf.get('cache_dir') or app_conf.get('cache.dir') session_opts['session.data_dir'] = '{data_dir}/sessions'.format( data_dir=cache_dir) app.wsgi_app = SessionMiddleware(app.wsgi_app, session_opts) app.session_interface = BeakerSessionInterface() # Add Jinja2 extensions and filters extensions = [ 'jinja2.ext.do', 'jinja2.ext.with_', jinja_extensions.SnippetExtension, jinja_extensions.CkanExtend, jinja_extensions.CkanInternationalizationExtension, jinja_extensions.LinkForExtension, jinja_extensions.ResourceExtension, jinja_extensions.UrlForStaticExtension, jinja_extensions.UrlForExtension ] for extension in extensions: app.jinja_env.add_extension(extension) app.jinja_env.filters['empty_and_escape'] = \ jinja_extensions.empty_and_escape # Common handlers for all requests app.before_request(ckan_before_request) app.after_request(ckan_after_request) # Template context processors app.context_processor(helper_functions) app.context_processor(c_object) @app.context_processor def ungettext_alias(): u''' Provide `ungettext` as an alias of `ngettext` for backwards compatibility ''' return dict(ungettext=ungettext) # Babel app.config[u'BABEL_TRANSLATION_DIRECTORIES'] = os.path.join(root, u'i18n') app.config[u'BABEL_DOMAIN'] = 'ckan' babel = Babel(app) babel.localeselector(get_locale) @app.route('/hello', methods=['GET']) def hello_world(): return 'Hello World, this is served by Flask' @app.route('/hello', methods=['POST']) def hello_world_post(): return 'Hello World, this was posted to Flask' # Auto-register all blueprints defined in the `views` folder _register_core_blueprints(app) # Set up each IBlueprint extension as a Flask Blueprint for plugin in PluginImplementations(IBlueprint): if hasattr(plugin, 'get_blueprint'): app.register_extension_blueprint(plugin.get_blueprint()) # Set flask routes in named_routes for rule in app.url_map.iter_rules(): if '.' not in rule.endpoint: continue controller, action = rule.endpoint.split('.') needed = list(rule.arguments - set(rule.defaults or {})) route = { rule.endpoint: { 'action': action, 'controller': controller, 'highlight_actions': action, 'needed': needed } } config['routes.named_routes'].update(route) # Start other middleware for plugin in PluginImplementations(IMiddleware): app = plugin.make_middleware(app, config) # Fanstatic if debug: fanstatic_config = { 'versioning': True, 'recompute_hashes': True, 'minified': False, 'bottom': True, 'bundle': False, } else: fanstatic_config = { 'versioning': True, 'recompute_hashes': False, 'minified': True, 'bottom': True, 'bundle': True, } root_path = config.get('ckan.root_path', None) if root_path: root_path = re.sub('/{{LANG}}', '', root_path) fanstatic_config['base_url'] = root_path app = Fanstatic(app, **fanstatic_config) for plugin in PluginImplementations(IMiddleware): try: app = plugin.make_error_log_middleware(app, config) except AttributeError: log.critical('Middleware class {0} is missing the method' 'make_error_log_middleware.'.format( plugin.__class__.__name__)) # Initialize repoze.who who_parser = WhoConfig(conf['here']) who_parser.parse(open(app_conf['who.config_file'])) app = PluggableAuthenticationMiddleware( app, who_parser.identifiers, who_parser.authenticators, who_parser.challengers, who_parser.mdproviders, who_parser.request_classifier, who_parser.challenge_decider, logging.getLogger('repoze.who'), logging.WARN, # ignored who_parser.remote_user_key) # Update the main CKAN config object with the Flask specific keys # that were set here or autogenerated flask_config_keys = set(flask_app.config.keys()) - set(config.keys()) for key in flask_config_keys: config[key] = flask_app.config[key] # Add a reference to the actual Flask app so it's easier to access app._wsgi_app = flask_app return app
# Testing on GitHub Actions application.config[ "SQLALCHEMY_DATABASE_URI" ] = "postgres://*****:*****@localhost:5432/mosp" elif ON_HEROKU: # Deployment on Heroku application.config.from_pyfile("heroku.py", silent=False) else: try: application.config.from_pyfile("production.py", silent=False) except Exception: application.config.from_pyfile("development.py", silent=False) db = SQLAlchemy(application) babel = Babel(application) @babel.localeselector def get_locale(): # if a user is logged in, use the locale from the user settings # user = getattr(g, 'user', None) # if user is not None: # return user.locale # otherwise try to guess the language from the user accept # header the browser transmits. We support de/fr/en in this # example. The best match wins. return request.accept_languages.best_match(["fr", "en"]) # @babel.timezoneselector
def register_extensions(app): """ register extensions to the app """ app.jinja_env.add_extension('jinja2.ext.do') # Global values in jinja # Uncomment to enable profiler # See scripts/profile_analyzer.py to analyze output # app = setup_profiler(app) # Compress app responses with gzip compress = Compress() compress.init_app(app) # Influx db time-series database db.init_app(app) influx_db.init_app(app) # Limit authentication blueprint requests to 60 per minute limiter = Limiter(app, key_func=get_ip_address) limiter.limit("60/minute")(routes_authentication.blueprint) # Language translations babel = Babel(app) @babel.localeselector def get_locale(): try: user = User.query.filter( User.id == flask_login.current_user.id).first() if user and user.language != '': for key in LANGUAGES: if key == user.language: return key # Bypass endpoint test error "'AnonymousUserMixin' object has no attribute 'id'" except AttributeError: pass return request.accept_languages.best_match(LANGUAGES.keys()) # User login management login_manager = flask_login.LoginManager() login_manager.init_app(app) @login_manager.user_loader def user_loader(user_id): user = User.query.filter(User.id == user_id).first() if not user: return return user @login_manager.unauthorized_handler def unauthorized(): flash(gettext('Please log in to access this page'), "error") return redirect(url_for('routes_authentication.do_login')) # Create and populate database if it doesn't exist with app.app_context(): db.create_all() populate_db() # This is disabled because there's a bug that messes up user databases # The upgrade script will execute alembic to upgrade the database # alembic_upgrade_db() # Check user option to force all web connections to use SSL # Fail if the URI is empty (pytest is running) if app.config['SQLALCHEMY_DATABASE_URI'] != 'sqlite://': with session_scope( app.config['SQLALCHEMY_DATABASE_URI']) as new_session: misc = new_session.query(Misc).first() if misc and misc.force_https: SSLify(app)
def create_app(): app = Flask(__name__, static_folder="dist", static_url_path="") if os.environ.get('FLASK_ENV') in ['production', 'staging', 'autopush']: createMiddleWare(app, StackdriverExporter()) import googlecloudprofiler # Profiler initialization. It starts a daemon thread which continuously # collects and uploads profiles. Best done as early as possible. try: # service and service_version can be automatically inferred when # running on GCP. googlecloudprofiler.start(verbose=3) except (ValueError, NotImplementedError) as exc: logging.error(exc) # Setup flask config cfg = libconfig.get_config() app.config.from_object(cfg) # Init extentions from cache import cache cache.init_app(app) # apply the blueprints to the app from routes import (protein, browser, dev, factcheck, place, placelist, ranking, redirects, static, tools) app.register_blueprint(protein.bp) app.register_blueprint(browser.bp) app.register_blueprint(dev.bp) app.register_blueprint(place.bp) app.register_blueprint(placelist.bp) app.register_blueprint(ranking.bp) app.register_blueprint(redirects.bp) app.register_blueprint(tools.bp) from routes.api import (protein as protein_api, browser as browser_api, choropleth, place as place_api, landing_page, ranking as ranking_api, stats, translator) app.register_blueprint(protein_api.bp) app.register_blueprint(browser_api.bp) app.register_blueprint(choropleth.bp) app.register_blueprint(factcheck.bp) app.register_blueprint(place_api.bp) app.register_blueprint(landing_page.bp) app.register_blueprint(ranking_api.bp) app.register_blueprint(static.bp) app.register_blueprint(stats.bp) app.register_blueprint(translator.bp) # Load chart config with open('chart_config.json', encoding='utf-8') as f: chart_config = json.load(f) app.config['CHART_CONFIG'] = chart_config if not cfg.TEST and not cfg.LITE: secret_client = secretmanager.SecretManagerServiceClient() secret_name = secret_client.secret_version_path(cfg.SECRET_PROJECT, 'maps-api-key', '1') secret_response = secret_client.access_secret_version(name=secret_name) app.config['MAPS_API_KEY'] = secret_response.payload.data.decode( 'UTF-8') if cfg.TEST or cfg.WEBDRIVER or cfg.LITE: app.config['PLACEID2DCID'] = { "ChIJCzYy5IS16lQRQrfeQ5K5Oxw": "country/USA", "ChIJPV4oX_65j4ARVW8IJ6IJUYs": "geoId/06" } else: # Load placeid2dcid mapping from GCS storage_client = storage.Client() bucket = storage_client.get_bucket(app.config['GCS_BUCKET']) blob = bucket.get_blob('placeid2dcid.json') app.config['PLACEID2DCID'] = json.loads(blob.download_as_bytes()) # Initialize translations babel = Babel(app, default_domain='all') app.config['BABEL_DEFAULT_LOCALE'] = i18n.DEFAULT_LOCALE app.config['BABEL_TRANSLATION_DIRECTORIES'] = 'i18n' if not cfg.TEST: timeout = 120 # seconds counter = 0 isOpen = False while not isOpen: try: urllib.request.urlopen(cfg.API_ROOT + "/version") break except urllib.error.URLError: time.sleep(1) counter += 1 if counter > timeout: raise RuntimeError("Mixer not ready after %s second" % timeout) @app.before_request def before_request(): requested_locale = request.args.get('hl', i18n.DEFAULT_LOCALE) g.locale_choices = i18n.locale_choices(requested_locale) g.locale = g.locale_choices[0] @babel.localeselector def get_locale(): return g.locale # Propagate hl parameter to all links (if not 'en') @app.url_defaults def add_language_code(endpoint, values): if 'hl' in values or g.locale == i18n.DEFAULT_LOCALE: return values['hl'] = g.locale # Provides locale parameter in all templates @app.context_processor def inject_locale(): return dict(locale=get_locale()) return app
from datetime import date from flask import Flask, g, request, render_template, abort, make_response, url_for, redirect from flask_babel import Babel, gettext EVENT = gettext('PyCon SK 2019') DOMAIN = 'https://2019.pycon.sk' API_DOMAIN = 'https://api.pycon.sk' LANGS = ('en', 'sk') TIME_FORMAT = '%Y-%m-%dT%H:%M:%S+00:00' app = Flask(__name__, static_url_path='/static') # pylint: disable=invalid-name app.config['BABEL_DEFAULT_LOCALE'] = 'sk' app.jinja_options = {'extensions': ['jinja2.ext.with_', 'jinja2.ext.i18n']} babel = Babel(app) # pylint: disable=invalid-name @app.route('/sitemap.xml') def sitemap(): excluded = {'static', 'sitemap'} pages = [] for lang in LANGS: for rule in app.url_map.iter_rules(): if 'GET' in rule.methods and rule.endpoint not in excluded: # `url_for` appends unknown arguments as query parameters. # We want to avoid that when a page isn't localized. values = { 'lang_code': lang } if 'lang_code' in rule.arguments else {}
def create_app(path_to_notebook, *args, **kwds): """ This is the main method to create a running notebook. This is called from the process spawned in run_notebook.py """ global notebook startup_token = kwds.pop('startup_token', None) ############# # OLD STUFF # ############# import sagenb.notebook.notebook as notebook notebook.MATHJAX = True notebook = notebook.load_notebook(path_to_notebook, *args, **kwds) init_updates() ############## # Create app # ############## app = SageNBFlask('flask_version', startup_token=startup_token, template_folder=TEMPLATE_PATH) app.secret_key = os.urandom(24) oid.init_app(app) app.debug = True @app.before_request def set_notebook_object(): g.notebook = notebook #################################### # create Babel translation manager # #################################### babel = Babel(app, default_locale='en_US') #Check if saved default language exists. If not fallback to default @app.before_first_request def check_default_lang(): def_lang = notebook.conf()['default_language'] trans_ids = [str(trans) for trans in babel.list_translations()] if def_lang not in trans_ids: notebook.conf()['default_language'] = None #register callback function for locale selection #this function must be modified to add per user language support @babel.localeselector def get_locale(): return g.notebook.conf()['default_language'] ######################## # Register the modules # ######################## app.register_blueprint(base) from .worksheet_listing import worksheet_listing app.register_blueprint(worksheet_listing) from .admin import admin app.register_blueprint(admin) from .authentication import authentication app.register_blueprint(authentication) from .doc import doc app.register_blueprint(doc) from .worksheet import ws as worksheet app.register_blueprint(worksheet) from .settings import settings app.register_blueprint(settings) # Handles all uncaught exceptions by sending an e-mail to the # administrator(s) and displaying an error page. @app.errorhandler(Exception) def log_exception(error): from sagenb.notebook.notification import logger logger.exception(error) return app.message(gettext('''500: Internal server error.'''), username=getattr(g, 'username', 'guest')), 500 #autoindex v0.3 doesnt seem to work with modules #routing with app directly does the trick #TODO: Check to see if autoindex 0.4 works with modules idx = AutoIndex(app, browse_root=SRC, add_url_rules=False) @app.route('/src/') @app.route('/src/<path:path>') @guest_or_login_required def autoindex(path='.'): filename = os.path.join(SRC, path) if os.path.isfile(filename): from cgi import escape src = escape(open(filename).read().decode('utf-8', 'ignore')) if (os.path.splitext(filename)[1] in ['.py', '.c', '.cc', '.h', '.hh', '.pyx', '.pxd']): return render_template(os.path.join('html', 'source_code.html'), src_filename=path, src=src, username=g.username) return src return idx.render_autoindex(path) return app
import quotes from agenda import Agenda from journaling import Journaling from tasks import Tasks from user import get_user_content_right from workout import Workout NavigationItem = namedtuple('NavigationItem', 'id title icon') app = Flask(__name__) with open('config.yaml', 'r') as f: config = yaml.full_load(f) locale = config['locale'] if 'locale' in config else 'en' babel = Babel(app, default_locale=locale) with app.app_context(): # this is required to have translations in loading functions _agenda = Agenda() _journaling = Journaling(config['journaling']) _workout = None if 'workout' in config: _workout = Workout(config['workout']) _tasks = None if 'redmine' in config: config['redmine']['date_format'] = config['date_format'] # copy from global _tasks = Tasks(config['redmine']) @app.route('/') def index_page():
def __init__(self, app): self.i18n = Babel(app) self.localeselector = self.i18n.localeselector
def create_app(configfile=None): app = Flask(__name__) app.config.from_object('web.config.Config') Markdown(app) # JSON app.json_encoder = CustomJSONEncoder app.json_decoder = CustomJSONDecoder # Typography Jinja2 Filter app.jinja_env.filters['typogrify'] = typogrify_filters.typogrify # Static Assets Config (Javascript and SCSS) env = assets.Environment(app) env.load_path = [ os.path.join(app.config['STATIC_PATH'], 'bower'), os.path.join(app.config['STATIC_PATH'], 'scss'), os.path.join(app.config['STATIC_PATH'], 'javascript') ] env.register('js_all', assets.Bundle('jquery/dist/jquery.min.js', 'leaflet/dist/leaflet.js', assets.Bundle('iconic.min.js'), assets.Bundle('app.js'), output='app.js')) sass = get_filter('scss') sass.load_paths = env.load_path env.register('css_all', assets.Bundle('app.scss', filters=(sass,), depends=(os.path.join(app.config['STATIC_PATH'], 'scss/**/*.scss')), output='app.css')) # i18n Config babel = Babel(app) @app.url_defaults def set_language_code(endpoint, values): if 'lang_code' in values or not g.get('lang_code', None): return if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'): values['lang_code'] = g.lang_code @app.url_value_preprocessor def get_lang_code(endpoint, values): if values is not None: g.lang_code = values.pop('lang_code', None) @app.before_request def ensure_lang_support(): lang_code = g.get('lang_code', None) if lang_code and lang_code not in app.config['LANGUAGES'].keys(): return abort(404) @babel.localeselector def get_locale(): return g.get('lang_code', app.config['BABEL_DEFAULT_LOCALE']) @babel.timezoneselector def get_timezone(): return app.config['BABEL_DEFAULT_TIMEZONE'] @app.context_processor def utility_processor(): def get_talk(slug): return filters.get_talk(slug) return dict(get_talk=get_talk) # Register the Blueprints app.register_blueprint(view_pages, url_prefix='/<lang_code>') app.register_blueprint(view_schedule, url_prefix='/<lang_code>/schedule') # Register the filters app.jinja_env.filters['format_datetime'] = filters.format_datetime app.jinja_env.filters['format_date'] = filters.format_date app.jinja_env.filters['format_time'] = filters.format_time return app
def create_app(test_config=None): # For automated tests # Setup Flask and read config from ConfigClass defined above app = Flask(__name__) app.config.from_object(__name__ + '.ConfigClass') # Load local_settings.py if file exists # For automated tests try: app.config.from_object('local_settings') except: pass # Load optional test_config # For automated tests if test_config: app.config.update(test_config) # Initialize Flask extensions db = SQLAlchemy(app) # Initialize Flask-SQLAlchemy mail = Mail(app) # Initialize Flask-Mail babel = Babel(app) # Initialize Flask-Babel @babel.localeselector def get_locale(): translations = [ str(translation) for translation in babel.list_translations() ] language = request.accept_languages.best_match(translations) return language # Define the User data model. Make sure to add flask.ext.user UserMixin !!! class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) # User authentication information username = db.Column(db.String(50), nullable=False, unique=True) password = db.Column(db.String(255), nullable=False, server_default='') reset_password_token = db.Column(db.String(100), nullable=False, server_default='') # User email information email = db.Column(db.String(255), nullable=False, unique=True) confirmed_at = db.Column(db.DateTime()) # User information active = db.Column('is_active', db.Boolean(), nullable=False, server_default='0') first_name = db.Column(db.String(100), nullable=False, server_default='') last_name = db.Column(db.String(100), nullable=False, server_default='') # Create all database tables db.create_all() # Setup Flask-User db_adapter = SQLAlchemyAdapter(db, User) # Select database adapter user_manager = UserManager(db_adapter, app) # Init Flask-User and bind to app # The Home page is accessible to anyone @app.route('/') def home_page(): return render_template_string(""" {% extends "base.html" %} {% block content %} <h2>{%trans%}Home Page{%endtrans%}</h2> {% if current_user.is_authenticated() %} <p> <a href="{{ url_for('user_profile_page') }}"> {%trans%}Profile Page{%endtrans%}</a></p> <p> <a href="{{ url_for('user.logout') }}"> {%trans%}Sign out{%endtrans%}</a></p> {% else %} <p> <a href="{{ url_for('user.login') }}"> {%trans%}Sign in or Register{%endtrans%}</a></p> {% endif %} {% endblock %} """) if current_user.is_authenticated(): return redirect(url_for('user_profile_page')) else: return redirect(url_for('user.login')) # The Profile page requires a logged-in user @app.route('/user/profile') @login_required # Use of @login_required decorator @confirm_email_required def user_profile_page(): return render_template_string(""" {% extends "base.html" %} {% block content %} <h2>{%trans%}Profile Page{%endtrans%}</h2> <p> {%trans%}Hello{%endtrans%} {{ current_user.username or current_user.email }},</p> <p> <a href="{{ url_for('home_page') }}"> {%trans%}Home Page{%endtrans%}</a></p> <p> <a href="{{ url_for('user.change_username') }}"> {%trans%}Change username{%endtrans%}</a></p> <p> <a href="{{ url_for('user.change_password') }}"> {%trans%}Change password{%endtrans%}</a></p> <p> <a href="{{ url_for('user.logout') }}"> {%trans%}Sign out{%endtrans%}</a></p> {% endblock %} """) return app
def app(request): app = Flask(__name__) app.response_class = Response app.debug = True app.config["SECRET_KEY"] = "secret" app.config["TESTING"] = True app.config["LOGIN_DISABLED"] = False app.config["WTF_CSRF_ENABLED"] = False # Our test emails/domain isn't necessarily valid app.config["SECURITY_EMAIL_VALIDATOR_ARGS"] = { "check_deliverability": False } app.config["SECURITY_TWO_FACTOR_SECRET"] = { "1": "TjQ9Qa31VOrfEzuPy4VHQWPCTmRzCnFzMKLxXYiZu9B" } app.config["SECURITY_SMS_SERVICE"] = "test" app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["SECURITY_PASSWORD_SALT"] = "salty" # Make this plaintext for most tests - reduces unit test time by 50% app.config["SECURITY_PASSWORD_HASH"] = "plaintext" # Make this hex_md5 for token tests app.config["SECURITY_HASHING_SCHEMES"] = ["hex_md5"] app.config["SECURITY_DEPRECATED_HASHING_SCHEMES"] = [] for opt in [ "changeable", "recoverable", "registerable", "trackable", "passwordless", "confirmable", "two_factor", "unified_signin", ]: app.config["SECURITY_" + opt.upper()] = opt in request.keywords pytest_major = int(pytest.__version__.split(".")[0]) if pytest_major >= 4: marker_getter = request.node.get_closest_marker else: marker_getter = request.keywords.get settings = marker_getter("settings") babel = marker_getter("babel") if settings is not None: for key, value in settings.kwargs.items(): app.config["SECURITY_" + key.upper()] = value mail = Mail(app) if not NO_BABEL and (babel is None or babel.args[0]): Babel(app) app.json_encoder = JSONEncoder app.mail = mail @app.route("/") def index(): return render_template("index.html", content="Home Page") @app.route("/profile") @auth_required() def profile(): if hasattr(app, "security"): if app.security._want_json(flask_request): return jsonify(message="profile") return render_template("index.html", content="Profile Page") @app.route("/post_login") @login_required def post_login(): return render_template("index.html", content="Post Login") @app.route("/http") @http_auth_required def http(): return "HTTP Authentication" @app.route("/http_admin_required") @http_auth_required @permissions_required("admin") def http_admin_required(): assert get_request_attr("fs_authn_via") == "basic" return "HTTP Authentication" @app.route("/http_custom_realm") @http_auth_required("My Realm") def http_custom_realm(): assert get_request_attr("fs_authn_via") == "basic" return render_template("index.html", content="HTTP Authentication") @app.route("/token", methods=["GET", "POST"]) @auth_token_required def token(): assert get_request_attr("fs_authn_via") == "token" return render_template("index.html", content="Token Authentication") @app.route("/multi_auth") @auth_required("session", "token", "basic") def multi_auth(): return render_template("index.html", content="Session, Token, Basic auth") @app.route("/post_logout") def post_logout(): return render_template("index.html", content="Post Logout") @app.route("/post_register") def post_register(): return render_template("index.html", content="Post Register") @app.route("/post_confirm") def post_confirm(): return render_template("index.html", content="Post Confirm") @app.route("/admin") @roles_required("admin") def admin(): assert get_request_attr("fs_authn_via") == "session" return render_template("index.html", content="Admin Page") @app.route("/admin_and_editor") @roles_required("admin", "editor") def admin_and_editor(): return render_template("index.html", content="Admin and Editor Page") @app.route("/admin_or_editor") @roles_accepted("admin", "editor") def admin_or_editor(): return render_template("index.html", content="Admin or Editor Page") @app.route("/simple") @roles_accepted("simple") def simple(): return render_template("index.html", content="SimplePage") @app.route("/admin_perm") @permissions_accepted("full-write", "super") def admin_perm(): return render_template("index.html", content="Admin Page with full-write or super") @app.route("/admin_perm_required") @permissions_required("full-write", "super") def admin_perm_required(): return render_template("index.html", content="Admin Page required") @app.route("/page1") def page_1(): return "Page 1" @app.route("/json", methods=["GET", "POST"]) def echo_json(): return jsonify(flask_request.get_json()) @app.route("/unauthz", methods=["GET", "POST"]) def unauthz(): return render_template("index.html", content="Unauthorized") @app.route("/fresh", methods=["GET", "POST"]) @auth_required(within=0) def fresh(): if app.security._want_json(flask_request): return jsonify(title="Fresh Only") else: return render_template("index.html", content="Fresh Only") return app
def create_app(app_name, config={}): """ App Factory Tools Workflow: follow the track_infos,current has 15 steps. primary workflow: 1. call a dirty way to hack depends package,e.g: webargs 1. merge config from env.FANTASY_SETTINGS_MODULE if user defined 1. call error_handle if user defined 1. call run_admin if user defined 1. call run_cli if user defined :return: """ track_info('(00/08)fantasy track mode active...') from . import error_handler, cli logging_level = 'INFO' if os.environ.get('FLASK_ENV', 'dev') == 'dev' else 'ERROR' logging_level = 'DEBUG' if os.environ.get('FLASK_DEBUG') else logging_level if os.environ.get('FANTASY_ACTIVE_LOGGING', 'no') == 'yes': track_info(f' logging mode:{logging_level}') init_logging(logging_level) track_info('(01/08)i18n webargs...') if os.environ.get('LANG') == 'zh_CN.UTF-8': from .i18n import zh_cn zh_cn() pass track_info('(02/08)initial app...') mod = importlib.import_module(app_name) app = FantasyFlask(app_name, root_path=os.path.dirname(mod.__file__)) app.root_app = os.environ.get('FANTASY_APP', app_name) app.logger.info("logger active") track_info('(03/08)update app.config...') if config: app.config.update(config) app.config.update(PROPAGATE_EXCEPTIONS=False) app.log_exception = functools.partial( ff_log_exception, original_log_exception=app.log_exception) # 由外部做显式声明,否则不做调用 config_module = os.environ.get('FANTASY_SETTINGS_MODULE', None) if config_module: track_info(" found config module %s,try load it..." % config_module) app.config.from_object(config_module) track_info(" merge config from os.environment.") for k in config_env_list: if not app.config.get(k): track_info(" key: %s merged" % k) app.config.update({k: os.environ.get(k, None)}) pass pass track_info(" initial SQL database when active.") if app.db: if app.config['FANTASY_AUTO_MIGRATE'] == 'yes': smart_database(app) app.db.init_app(app) track_info(" initial Doc database when active.") if app.doc_db: mongodb_kwargs = { k.upper(): v for (k, v) in app.config.items() if k.upper().startswith('MONGODB_') } app.doc_db.init_app(app, config=mongodb_kwargs) track_info('(04/08)active more extensions...') track_info(" try active CORS rule.") if app.config['FANTASY_ACTIVE_CORS'] == 'yes': from flask_cors import CORS CORS(app, **app.config.get('CORS_KWARGS', {})) pass track_info(" try active i18n rule.") if app.config['FANTASY_ACTIVE_I18N'] == 'yes': from flask_babel import Babel Babel(app) pass track_info(" try active celery rule.") if app.config['FANTASY_ACTIVE_CELERY'] == 'yes': from .celery import Celery connect_celery(app, Celery()) pass track_info(" try active oss storage.") if app.config['FANTASY_STORAGE_MODULE']: st_module_name, st_class_name = app.config[ 'FANTASY_STORAGE_MODULE'].rsplit('.', 1) st_module = importlib.import_module(st_module_name) st_class = getattr(st_module, st_class_name) app.storage = st_class(app) pass track_info(" try active sentry.") if app.config['FANTASY_ACTIVE_SENTRY'] == 'yes': try: import sentry_sdk from sentry_sdk.integrations.flask import FlaskIntegration sentry_sdk.init(os.environ['SENTRY_DSN'], integrations=[FlaskIntegration()]) except Exception: from raven.contrib.flask import Sentry Sentry(app) pass track_info(" try active prometheus.") if app.config['FANTASY_ACTIVE_EXPORTER'] == 'yes': from flask_prometheus_metrics import register_metrics register_metrics(app, app_version='v' + version, app_config=app.config['ENV']) pass track_info(" try active cache.") if app.config['FANTASY_ACTIVE_CACHE'] == 'yes': redis_kwargs = { k.lower().replace('redis_', ''): v for (k, v) in app.config.items() if k.upper().startswith('REDIS_') } import redis app.cache = redis.Redis(**redis_kwargs) pass track_info('(05/08)active something with app context...') with app.app_context(): track_info(" bind sentry trigger handler.") if hasattr(mod, 'run_app'): run_app = getattr(mod, 'run_app') try: run_app(app) except Exception as e: if hasattr(app, 'sentry'): app.sentry.handle_exception(e) pass import sys import traceback traceback.print_exc() sys.exit(-1) pass track_info(" bind auto migrate.") if app.db and app.is_root_app: track_info(f" match root-app flag at: {app.name}, " f"will try execute smart migrate.") smart_migrate(app) pass track_info(" try bind account manager.") if app.config['FANTASY_ACTIVE_ACCOUNT'] == 'yes' and \ app.config['FANTASY_ACCOUNT_MANAGER']: smart_account(app) pass track_info('(06/08)bind error handle...') @app.errorhandler(400) def h_400(error): return error_handler.http400(error) @app.errorhandler(422) def h_422(error): return error_handler.http422(error) @app.errorhandler(500) def h_500(error): return error_handler.http500(error) if hasattr(mod, 'error_handler'): error_handle = getattr(mod, 'error_handle') error_handle(app) pass track_info('(07/08){unstable}bind admin handle...') if app.config['FANTASY_ACTIVE_ADMIN'] == 'yes' \ and hasattr(mod, 'run_admin'): import flask_admin try: admin_name = app.config.get('FANTASY_ADMIN_NAME') except KeyError: admin_name = 'Admin' try: admin_tpl_name = app.config.get('FANTASY_ADMIN_TEMPLATE_MODE') except KeyError: admin_tpl_name = 'bootstrap3' admin = flask_admin.Admin(name=admin_name, template_mode=admin_tpl_name) run_admin = getattr(mod, 'run_admin') run_admin(admin, app) admin.init_app(app) pass pass track_info('(08/08)bind CLI command...') track_info(" try bind ff command.") if app.config['FANTASY_ACTIVE_CLI'] == 'yes' and app.is_root_app: track_info(f" match root-app flag at: {app.name}, " f"will add ff command.") app.cli.add_command(cli.ff) track_info(" try bind custom command.") if app.config['FANTASY_ACTIVE_CLI'] == 'yes' and hasattr(mod, 'run_cli'): track_info(f" found custom command at: {app.name}, " f"will add custom command.") run_cli = getattr(mod, 'run_cli') run_cli(app) pass return app
def create_app(self): app = Flask(__name__) babel = Babel(app, default_locale='fr', default_timezone=USER_TZ) babel.localeselector(en_locale) babel.timezoneselector(user_tz) return app
def babel_cfg_init(app): app.config['BABEL_DEFAULT_LOCALE'] = 'zh_Hans_CN' b = Babel(app) return b
class Blog(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("myuser.id"), nullable=False) title = db.Column(db.Text) text = db.Column(db.UnicodeText) # Setup Flask-Security user_datastore = SQLAlchemyUserDatastore(db, User, Role) app.security = Security(app, user_datastore) # Setup Babel - not strictly necessary but since our virtualenv has Flask-Babel # we need to initialize it Babel(app) # Set this so unit tests can mock out. app.blog_cls = Blog # Create users and roles (and first blog!) @app.before_first_request def create_users(): if current_app.testing: return db.create_all() user_datastore.find_or_create_role( name="admin", permissions={"admin-read", "admin-write", "user-read", "user-write"}, )
def get_locale() -> str | None: """Get locale. Returns: The locale that should be used for Babel. If not given as an option to Fava, guess from browser. """ lang = g.ledger.fava_options.language if lang is not None: return lang return request.accept_languages.best_match(["en"] + LANGUAGES) BABEL = Babel(app) BABEL.localeselector(get_locale) for function in template_filters.FILTERS: app.add_template_filter(function) # type: ignore app.add_template_filter(serialise) @app.url_defaults def _inject_filters(endpoint: str, values: dict[str, str | None]) -> None: if "bfile" not in values and app.url_map.is_endpoint_expecting( endpoint, "bfile"): values["bfile"] = g.beancount_file_slug if endpoint in ["static", "index"]: return for name in ["conversion", "interval", "account", "filter", "time"]:
def create_app(config_name="development", config_file='/etc/privacyidea/pi.cfg', silent=False): """ First the configuration from the config.py is loaded depending on the config type like "production" or "development" or "testing". Then the environment variable PRIVACYIDEA_CONFIGFILE is checked for a config file, that contains additional settings, that will overwrite the default settings from config.py :param config_name: The config name like "production" or "testing" :type config_name: basestring :param config_file: The name of a config file to read configuration from :type config_file: basestring :param silent: If set to True the additional information are not printed to stdout :type silent: bool :return: The flask application :rtype: App object """ if not silent: print("The configuration name is: {0!s}".format(config_name)) if os.environ.get(ENV_KEY): config_file = os.environ[ENV_KEY] if not silent: print( "Additional configuration can be read from the file {0!s}".format( config_file)) app = Flask(__name__, static_folder="static", template_folder="static/templates") if config_name: app.config.from_object(config[config_name]) try: # Try to load the given config_file. # If it does not exist, just ignore it. app.config.from_pyfile(config_file, silent=True) except IOError: sys.stderr.write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n") sys.stderr.write(" WARNING: privacyidea create_app has no access\n") sys.stderr.write(" to {0!s}!\n".format(config_file)) sys.stderr.write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n") # Try to load the file, that was specified in the environment variable # PRIVACYIDEA_CONFIG_FILE # If this file does not exist, we create an error! app.config.from_envvar(ENV_KEY, silent=True) app.register_blueprint(validate_blueprint, url_prefix='/validate') app.register_blueprint(token_blueprint, url_prefix='/token') app.register_blueprint(system_blueprint, url_prefix='/system') app.register_blueprint(resolver_blueprint, url_prefix='/resolver') app.register_blueprint(realm_blueprint, url_prefix='/realm') app.register_blueprint(defaultrealm_blueprint, url_prefix='/defaultrealm') app.register_blueprint(policy_blueprint, url_prefix='/policy') app.register_blueprint(login_blueprint, url_prefix='/') app.register_blueprint(jwtauth, url_prefix='/auth') app.register_blueprint(user_blueprint, url_prefix='/user') app.register_blueprint(audit_blueprint, url_prefix='/audit') app.register_blueprint(machineresolver_blueprint, url_prefix='/machineresolver') app.register_blueprint(machine_blueprint, url_prefix='/machine') app.register_blueprint(application_blueprint, url_prefix='/application') app.register_blueprint(caconnector_blueprint, url_prefix='/caconnector') app.register_blueprint(cert_blueprint, url_prefix='/certificate') app.register_blueprint(ttype_blueprint, url_prefix='/ttype') app.register_blueprint(register_blueprint, url_prefix='/register') app.register_blueprint(smtpserver_blueprint, url_prefix='/smtpserver') app.register_blueprint(recover_blueprint, url_prefix='/recover') app.register_blueprint(radiusserver_blueprint, url_prefix='/radiusserver') app.register_blueprint(periodictask_blueprint, url_prefix='/periodictask') app.register_blueprint(privacyideaserver_blueprint, url_prefix='/privacyideaserver') app.register_blueprint(eventhandling_blueprint, url_prefix='/event') app.register_blueprint(smsgateway_blueprint, url_prefix='/smsgateway') app.register_blueprint(client_blueprint, url_prefix='/client') app.register_blueprint(subscriptions_blueprint, url_prefix='/subscriptions') app.register_blueprint(monitoring_blueprint, url_prefix='/monitoring') db.init_app(app) migrate = Migrate(app, db) try: # Try to read logging config from file log_config_file = app.config.get("PI_LOGCONFIG", "/etc/privacyidea/logging.cfg") if os.path.isfile(log_config_file): logging.config.fileConfig(log_config_file) if not silent: print("Reading Logging settings from {0!s}".format( log_config_file)) else: raise Exception("The config file specified in PI_LOGCONFIG does " "not exist.") except Exception as exx: if not silent: sys.stderr.write("{0!s}\n".format(exx)) sys.stderr.write("Could not use PI_LOGCONFIG. " "Using PI_LOGLEVEL and PI_LOGFILE.\n") level = app.config.get("PI_LOGLEVEL", logging.DEBUG) # If there is another logfile in pi.cfg we use this. logfile = app.config.get("PI_LOGFILE") if logfile: if not silent: sys.stderr.write("Using PI_LOGLEVEL {0!s}.\n".format(level)) sys.stderr.write("Using PI_LOGFILE {0!s}.\n".format(logfile)) PI_LOGGING_CONFIG["handlers"]["file"]["filename"] = logfile PI_LOGGING_CONFIG["handlers"]["file"]["level"] = level PI_LOGGING_CONFIG["loggers"]["privacyidea"]["level"] = level logging.config.dictConfig(PI_LOGGING_CONFIG) else: if not silent: sys.stderr.write("No PI_LOGFILE found. Using default " "config.\n") logging.config.dictConfig(DEFAULT_LOGGING_CONFIG) babel = Babel(app) @babel.localeselector def get_locale(): # if we are not in the request context, return None to use the default # locale if not request: return None # otherwise try to guess the language from the user accept # header the browser transmits. We support de/fr/en in this # example. The best match wins. return request.accept_languages.best_match( ['de', 'fr', 'it', 'es', 'en']) queue.register_app(app) return app
def create_app(configfile=None): app = Flask(__name__) babel = Babel(app) CO2MPAS_VERSION = "3" app.jinja_env.globals.update(humanised=humanised) with open("locale/texts-en.yaml", "r") as stream: co2wui_texts = yaml.safe_load(stream) @app.route("/") def index(): nohints = False if "nohints" in request.cookies: nohints = True return render_template( "layout.html", action="dashboard", data={ "breadcrumb": ["Co2mpas"], "props": { "active": { "run": "", "sync": "", "doc": "", "expert": "" } }, "nohints": nohints, "texts": co2wui_texts, }, ) @app.route("/run/download-template-form") def download_template_form(): return render_template( "layout.html", action="template_download_form", data={ "breadcrumb": ["Co2mpas", _("Download template")], "props": { "active": { "run": "active", "sync": "", "doc": "", "expert": "" } }, "texts": co2wui_texts, }, ) @app.route("/run/download-template") def download_template(): # Temp file name of = next(tempfile._get_candidate_names()) # Input parameters inputs = {"output_file": of, "template_type": "input"} # Dispatcher d = dsp.register() ret = d.dispatch(inputs, ["template", "done"]) # Read from file data = None with open(of, "rb") as xlsx: data = xlsx.read() # Delete files os.remove(of) # Output xls file iofile = io.BytesIO(data) iofile.seek(0) return send_file( iofile, attachment_filename="co2mpas-input-template.xlsx", as_attachment=True, ) @app.route("/run/simulation-form") def simulation_form(): inputs = [ f for f in listdir_inputs("input") if osp.isfile(osp.join("input", f)) ] return render_template( "layout.html", action="simulation_form", data={ "breadcrumb": ["Co2mpas", _("Run simulation")], "props": { "active": { "run": "active", "sync": "", "doc": "", "expert": "" } }, "inputs": inputs, "ta_enabled": ta_enabled(), "texts": co2wui_texts, }, ) def run_process(args): thread = threading.current_thread() files = [ osp.join("input", f) for f in listdir_inputs("input") if osp.isfile(osp.join("input", f)) ] # Create output directory for this execution output_folder = osp.join("output", str(thread.ident)) os.makedirs(output_folder or ".", exist_ok=True) # Dedicated logging for this run fileh = logging.FileHandler( osp.join("output", str(thread.ident), "logfile.txt"), "a") logger = logging.getLogger() logger.setLevel(logging.INFO) frmt = "%(asctime)-15s:%(levelname)5.5s:%(name)s:%(message)s" logging.basicConfig(level=logging.INFO, format=frmt) logger.addHandler(fileh) # Input parameters kwargs = { "output_folder": output_folder, "only_summary": bool(args.get("only_summary")), "hard_validation": bool(args.get("hard_validation")), "declaration_mode": bool(args.get("declaration_mode")), "encryption_keys": "keys/dice.co2mpas.keys" if os.path.exists("keys/dice.co2mpas.keys") else "", "sign_key": "keys/sign.co2mpas.key" if os.path.exists("keys/sign.co2mpas.key") else "", "encryption_keys_passwords": "", "enable_selector": False, "type_approval_mode": bool(args.get("tamode")), } with open(osp.join("output", str(thread.ident), "header.dat"), "wb") as header_file: pickle.dump(kwargs, header_file) inputs = dict( plot_workflow=False, host="127.0.0.1", port=4999, cmd_flags=kwargs, input_files=files, ) # Dispatcher d = dsp.register() ret = d.dispatch(inputs, ["done", "run"]) with open(osp.join("output", str(thread.ident), "result.dat"), "wb") as summary_file: pickle.dump(ret["summary"], summary_file) return "" @app.route("/run/view-summary/<runid>") def view_summary(runid): """Show a modal dialog with a execution's summary formatted in a table """ # Read the header containing run information header = {} with open(osp.join("output", runid, "header.dat"), "rb") as header_file: try: header = pickle.load(header_file) except: return None summaries = get_summary(runid) if summaries is not None: return render_template( "ajax.html", action="summary", title=_("Summary of your Co2mpas execution"), data={ "thread_id": runid, "summaries": summaries, "header": header }, ) else: return "" # Run @app.route("/run/simulation") def run_simulation(): thread = threading.Thread(target=run_process, args=(request.args, )) thread.daemon = False thread.start() id = thread.ident return redirect("/run/progress?layout=layout&id=" + str(thread.ident), code=302) @app.route("/run/progress") def run_progress(): done = False thread_id = request.args.get("id") layout = request.args.get("layout") # Done if there's a result file if os.path.exists(osp.join("output", thread_id, "result.dat")): done = True # See if done or still running page = "run_complete" if done else "run_progress" title = _("Simulation complete") if done else _( "Simulation in progress...") # Read the header containing run information header = {} with open(osp.join("output", thread_id, "header.dat"), "rb") as header_file: try: header = pickle.load(header_file) except: return None # Get the summary of the execution (if ready) summary = get_summary(thread_id) result = "KO" if (summary is None or len(summary[0].keys()) <= 2) else "OK" # Get the log ile log = "" loglines = [] with open(osp.join("output", thread_id, "logfile.txt")) as f: loglines = f.readlines() for logline in reversed(loglines): if not re.search("- INFO -", logline): log += logline # Collect result files results = [] if not (summary is None or len(summary[0].keys()) <= 2): output_files = [ f for f in listdir_outputs(os.path.join("output", thread_id)) if osp.isfile(os.path.join("output", thread_id, f)) ] results.append({"name": thread_id, "files": output_files}) # Render page progress/complete return render_template( "layout.html" if layout == "layout" else "ajax.html", action=page, data={ "breadcrumb": ["Co2mpas", title], "props": { "active": { "run": "active", "sync": "", "doc": "", "expert": "" } }, "thread_id": thread_id, "log": log, "result": result, "summary": summary[0] if summary is not None else None, "results": results if results is not None else None, "header": header, }, ) @app.route("/run/add-file", methods=["POST"]) def add_file(): f = request.files["file"] f.save(osp.join("input", secure_filename(f.filename))) files = {"file": f.read()} return redirect("/run/simulation-form", code=302) @app.route("/run/delete-file", methods=["GET"]) def delete_file(): fn = request.args.get("fn") inputs = [ f for f in listdir_inputs("input") if osp.isfile(osp.join("input", f)) ] os.remove(osp.join("input", inputs[int(fn) - 1])) return redirect("/run/simulation-form", code=302) @app.route("/run/view-results") def view_results(): dirpath = r"output" entries = (osp.join(dirpath, fn) for fn in os.listdir(dirpath)) entries = ((os.stat(path), path) for path in entries) entries = ((stat[ST_CTIME], path) for stat, path in entries if S_ISDIR(stat[ST_MODE])) results = [] for cdate, path in sorted(entries): dirname = os.path.basename(path) output_files = [ f for f in listdir_outputs(osp.join("output", dirname)) if osp.isfile(osp.join("output", dirname, f)) ] summary = get_summary(dirname) outcome = "KO" if (summary is None or len(summary[0].keys()) <= 2) else "OK" results.append({ "datetime": time.ctime(cdate), "name": dirname, "files": output_files, "outcome": outcome, }) return render_template( "layout.html", action="view_results", data={ "breadcrumb": ["Co2mpas", _("View results")], "props": { "active": { "run": "active", "sync": "", "doc": "", "expert": "" } }, "results": reversed(results), "texts": co2wui_texts, }, ) @app.route("/run/download-result/<runid>/<fnum>") def download_result(runid, fnum): files = list(listdir_outputs(osp.join("output", runid))) rf = osp.join("output", runid, files[int(fnum) - 1]) # Read from file data = None with open(rf, "rb") as result: data = result.read() # Output xls file iofile = io.BytesIO(data) iofile.seek(0) return send_file(iofile, attachment_filename=files[int(fnum) - 1], as_attachment=True) @app.route("/run/delete-results", methods=["POST"]) def delete_results(): for k in request.form.keys(): if re.match(r"select-[0-9]+", k): runid = k.rpartition("-")[2] shutil.rmtree(osp.join("output", runid)) return redirect("/run/view-results", code=302) @app.route("/run/download-log/<runid>") def download_log(runid): rf = osp.join("output", runid, "logfile.txt") # Read from file data = None with open(rf, "rb") as xlsx: data = xlsx.read() # Output xls file iofile = io.BytesIO(data) iofile.seek(0) return send_file(iofile, attachment_filename="logfile.txt", as_attachment=True) @app.route("/sync/template-form") def sync_template_form(): return render_template( "layout.html", action="synchronisation_template_form", data={ "breadcrumb": ["Co2mpas", _("Data synchronisation")], "props": { "active": { "run": "", "sync": "active", "doc": "", "expert": "" } }, "title": "Data synchronisation", }, ) @app.route("/sync/template-download") def sync_template_download(): # Parameters from request cycle_type = request.args.get("cycle") gear_box_type = request.args.get("gearbox") wltp_class = request.args.get("wltpclass") # Output temp file output_file = next(tempfile._get_candidate_names()) + ".xlsx" # Generate template import pandas as pd from co2mpas.core.model.physical import dsp theoretical = sh.selector( ["times", "velocities"], dsp( inputs=dict( cycle_type=cycle_type.upper(), gear_box_type=gear_box_type, wltp_class=wltp_class, downscale_factor=0, ), outputs=["times", "velocities"], shrink=True, ), ) base = dict.fromkeys( ( "times", "velocities", "target gears", "engine_speeds_out", "engine_coolant_temperatures", "co2_normalization_references", "alternator_currents", "battery_currents", "target fuel_consumptions", "target co2_emissions", "target engine_powers_out", ), [], ) data = dict(theoretical=theoretical, dyno=base, obd=base) with pd.ExcelWriter(output_file) as writer: for k, v in data.items(): pd.DataFrame(v).to_excel(writer, k, index=False) # Read from generated file data = None with open(output_file, "rb") as xlsx: data = xlsx.read() # Delete files os.remove(output_file) # Output xls file iofile = io.BytesIO(data) iofile.seek(0) return send_file(iofile, attachment_filename="datasync.xlsx", as_attachment=True) @app.route("/sync/synchronisation-form") def synchronisation_form(): inputs = [ f for f in listdir_inputs("sync/input") if osp.isfile(osp.join("sync/input", f)) ] return render_template( "layout.html", action="synchronisation_form", data={ "breadcrumb": ["Co2mpas", _("Run synchronisation")], "props": { "active": { "run": "", "sync": "active", "doc": "", "expert": "" } }, "interpolation_methods": [ "linear", "nearest", "zero", "slinear", "quadratic", "cubic", "pchip", "akima", "integral", "polynomial0", "polynomial1", "polynomial2", "polynomial3", "polynomial4", "spline5", "spline7", "spline9", ], "inputs": inputs, "texts": co2wui_texts, }, ) @app.route("/sync/add-sync-file", methods=["POST"]) def add_sync_file(): inputs = [ f for f in listdir_inputs("sync") if osp.isfile(osp.join("sync", f)) ] for file in inputs: os.remove(osp.join("sync/input", file)) f = request.files["file"] f.save(osp.join("sync/input", secure_filename(f.filename))) files = {"file": f.read()} return redirect("/sync/synchronisation-form", code=302) @app.route("/sync/run-synchronisation", methods=["POST"]) def run_synchronisation(): # Dedicated logging for this run fileh = logging.FileHandler("sync/logfile.txt", "w") logger = logging.getLogger() logger.setLevel(logging.INFO) frmt = "%(asctime)-15s:%(levelname)5.5s:%(name)s:%(message)s" logging.basicConfig(level=logging.INFO, format=frmt) logger.addHandler(fileh) # Input and output files inputs = [ f for f in listdir_inputs("sync/input") if osp.isfile(osp.join("sync/input", f)) ] input_file = osp.join("sync", "input", inputs[0]) output_file = osp.join("sync", "output", "datasync.sync.xlsx") # Arguments kwargs = { "x_label": request.form.get("x_label") if request.form.get("x_label") else "times", "y_label": request.form.get("y_label") if request.form.get("y_label") else "velocities", "interpolation_method": request.form.get("interpolation_method"), "header": request.form.get("header"), "reference_name": request.form.get("reference_name") if request.form.get("reference_name") else "theoretical", } kwargs = {k: v for k, v in kwargs.items() if v} try: # Dispatcher _process = sh.SubDispatch(syncing.dsp, ["written"], output_type="value") ret = _process( dict(input_fpath=input_file, output_fpath=output_file, **kwargs)) fileh.close() logger.removeHandler(fileh) return "OK" except Exception as e: logger.error(_("Synchronisation failed: ") + str(e)) fileh.close() logger.removeHandler(fileh) return "KO" @app.route("/sync/delete-file", methods=["GET"]) def delete_sync_file(): inputs = [ f for f in listdir_inputs("sync/input") if osp.isfile(osp.join("sync/input", f)) ] for file in inputs: os.remove(osp.join("sync/input", file)) return redirect("/sync/synchronisation-form", code=302) @app.route("/sync/load-log", methods=["GET"]) def load_sync_log(): log = "" loglines = [] with open("sync/logfile.txt") as f: loglines = f.readlines() for logline in loglines: log += logline return log @app.route("/sync/download-result") def sync_download_result(): resfile = "sync/output/datasync.sync.xlsx" # Read from file data = None with open(resfile, "rb") as xlsx: data = xlsx.read() # Output xls file iofile = io.BytesIO(data) iofile.seek(0) return send_file(iofile, attachment_filename="datasync.sync.xlsx", as_attachment=True) # Demo/download @app.route("/demo/download") def demo_download(): # Temporary output folder of = next(tempfile._get_candidate_names()) # Input parameters inputs = {"output_folder": of} # Dispatcher d = dsp.register() ret = d.dispatch(inputs, ["demo", "done"]) # List of demo files created demofiles = [f for f in listdir(of) if osp.isfile(osp.join(of, f))] # Create zip archive on the fly zip_subdir = of iofile = io.BytesIO() zf = zipfile.ZipFile(iofile, mode="w", compression=zipfile.ZIP_DEFLATED) # Adds demo files to archive for f in demofiles: # Add file, at correct path zf.write(os.path.abspath(osp.join(of, f)), f) # Close archive zf.close() # Remove temporary files shutil.rmtree(of) # Output zip file iofile.seek(0) return send_file(iofile, attachment_filename="co2mpas-demo.zip", as_attachment=True) @app.route("/plot/launched") def plot_launched(): return render_template( "content.html", action="launch_plot", data={ "breadcrumb": ["Co2mpas", "Plot launched"], "props": { "active": { "run": "", "sync": "", "doc": "", "expert": "active" } }, "title": "Plot launched", }, ) @app.route("/plot/model-graph") def plot_model_graph(): dsp( dict(plot_model=True, cache_folder="cache", host="127.0.0.1", port=4999), ["plot", "done"], ) return "" @app.route("/conf/configuration-form") def configuration_form(): files = [f for f in listdir_conf(".") if osp.isfile(osp.join(".", f))] return render_template( "layout.html", action="configuration_form", data={ "breadcrumb": ["Co2mpas", _("Configuration file")], "props": { "active": { "run": "", "sync": "active", "doc": "", "expert": "active", } }, "title": "Configuration form", "inputs": files, "texts": co2wui_texts, }, ) @app.route("/conf/add-conf-file", methods=["POST"]) def add_conf_file(): if os.path.exists("conf.yaml"): os.remove("conf.yaml") f = request.files["file"] f.save("conf.yaml") return redirect("/conf/configuration-form", code=302) @app.route("/conf/delete-file", methods=["GET"]) def delete_conf_file(): os.remove("conf.yaml") return redirect("/conf/configuration-form", code=302) @app.route("/keys/keys-form") def keys_form(): enc_keys = [ f for f in listdir_enc_keys("keys") if osp.isfile(osp.join("keys", f)) ] key_pass = [ f for f in listdir_key_pass("keys") if osp.isfile(osp.join("keys", f)) ] key_sign = [ f for f in listdir_key_sign("keys") if osp.isfile(osp.join("keys", f)) ] return render_template( "layout.html", action="keys_form", data={ "breadcrumb": ["Co2mpas", _("Load keys")], "props": { "active": { "run": "", "sync": "", "doc": "", "expert": "active" } }, "enc_keys": enc_keys, "key_pass": key_pass, "key_sign": key_sign, "texts": co2wui_texts, }, ) @app.route("/keys/add-key-file", methods=["POST"]) def add_key_file(): upload_type = request.form.get("upload_type") filenames = { "enc_keys": "dice.co2mpas.keys", "key_pass": "******", "key_sign": "sign.co2mpas.key", } filename = filenames.get(upload_type) if os.path.exists(filename): os.remove(filename) f = request.files["file"] f.save(osp.join("keys", filename)) return redirect("/keys/keys-form", code=302) @app.route("/keys/delete-file", methods=["GET"]) def delete_key_file(): upload_type = request.args.get("upload_type") filenames = { "enc_keys": "dice.co2mpas.keys", "key_pass": "******", "key_sign": "sign.co2mpas.key", } filename = filenames.get(upload_type) os.remove(osp.join("keys", filename)) return redirect("/keys/keys-form", code=302) @app.route("/not-implemented") def not_implemented(): return render_template( "layout.html", action="generic_message", data={ "breadcrumb": ["Co2mpas", _("Feature not implemented")], "props": { "active": { "run": "", "sync": "", "doc": "", "expert": "" } }, "title": "Feature not implemented", "message": "Please refer to future versions of the application or contact [email protected] for information.", }, ) @app.route("/conf/generate") def conf_generate(): # Conf file name of = "conf.yaml" # Input parameters inputs = {"output_file": of} # Dispatcher d = dsp.register() ret = d.dispatch(inputs, ["conf", "done"]) return redirect("/conf/configuration-form", code=302) # Demo/download @app.route("/conf/download") def conf_download(): # Conf file name of = "conf.yaml" # Read from file data = None with open(of, "rb") as conf_yaml: data = conf_yaml.read() # Output xls file iofile = io.BytesIO(data) iofile.seek(0) return send_file(iofile, attachment_filename="conf.yaml", as_attachment=True) @app.route("/contact-us") def contact_us(): return render_template( "layout.html", action="contact_us", data={ "breadcrumb": ["Co2mpas", "Contact us"], "props": { "active": { "run": "", "sync": "", "doc": "active", "expert": "" } }, "title": "Contact us", "texts": co2wui_texts, }, ) return app
# -*- coding: utf-8 -*- from werkzeug.contrib.fixers import ProxyFix from flask_babel import Babel, gettext as _ from app import create_app app.create_app() saasapp = app.app_ babel = Babel(app.app_) saasapp.wsgi_app = ProxyFix(saasapp.wsgi_app) if __name__ == '__main__': saasapp.run()
def init_app(app, test_config=None): # For automated tests # Setup Flask and read config from ConfigClass defined above app.config.from_object(__name__+'.ConfigClass') # Load local_settings.py if file exists # For automated tests try: app.config.from_object('local_settings') except: pass # Load optional test_config # For automated tests if test_config: app.config.update(test_config) # Initialize Flask extensions babel = Babel(app) # Initialize Flask-Babel mail = Mail(app) # Initialize Flask-Mail # Reset all the database tables db.create_all() # Setup Flask-User db_adapter = SQLAlchemyAdapter(db, User, UserInvitationClass=UserInvitation) user_manager = UserManager(db_adapter, app) # Create regular 'member' user if not User.query.filter(User.username=='member').first(): user = User(username='******', email='*****@*****.**', active=True, password=user_manager.hash_password('Password1'), confirmed_at=datetime.datetime.utcnow()) db.session.add(user) db.session.commit() # Create 'user007' user with 'secret' and 'agent' roles if not User.query.filter(User.username=='user007').first(): user1 = User(username='******', email='*****@*****.**', active=True, password=user_manager.hash_password('Password1')) user1.roles.append(Role(name='secret')) user1.roles.append(Role(name='agent')) db.session.add(user1) db.session.commit() # The '/' page is accessible to anyone @app.route('/') def home_page(): return render_template_string(""" {% extends "base.html" %} {% block content %} <h2>{%trans%}Home Page{%endtrans%}</h2> <p><a href="{{ url_for('user.login') }}">{%trans%}Sign in{%endtrans%}</a></p> {% endblock %} """) # The '/profile' page requires a logged-in user @app.route('/user/profile') @login_required # Use of @login_required decorator @confirm_email_required def user_profile_page(): return render_template_string(""" {% extends "base.html" %} {% block content %} <h2>{%trans%}Profile Page{%endtrans%}</h2> <p> {%trans%}Hello{%endtrans%} {{ current_user.username or current_user.email }},</p> <p> <a href="{{ url_for('user.change_username') }}"> {%trans%}Change username{%endtrans%}</a></p> <p> <a href="{{ url_for('user.change_password') }}"> {%trans%}Change password{%endtrans%}</a></p> <p> <a href="{{ url_for('user.invite') }}"> {%trans%}Invite User{%endtrans%}</a></p> <p> <a href="{{ url_for('user.logout') }}?next={{ url_for('user.login') }}"> {%trans%}Sign out{%endtrans%}</a></p> {% endblock %} """) # The '/special' page requires a user that has the 'special' AND ('sauce' OR 'agent') role. @app.route('/special') @roles_required('secret', ['sauce', 'agent']) # Use of @roles_required decorator def special_page(): return render_template_string(""" {% extends "base.html" %} {% block content %} <h2>{%trans%}Special Page{%endtrans%}</h2> {% endblock %} """) # For testing only app.db = db app.UserEmailClass = UserEmail return app
# -*- coding: utf-8 -*- from flask import Flask from flask_babel import Babel from flask_wtf import CSRFProtect from flask_bootstrap import Bootstrap babel = Babel(configure_jinja=False) bootstrap = Bootstrap() csrf = CSRFProtect() app = Flask('texrrow')
# coding:utf-8 from flask import Flask, request #request: para trabajar con parametros por rutas from bs4 import BeautifulSoup from flask_restful import Resource, Api from flask_babel import Babel, gettext import requests import json import warnings #ppp: re.sub launches the DeprecationWarning, I can not identify the reason. For this reason, this line is added. warnings.filterwarnings("ignore", category=DeprecationWarning) app = Flask(__name__) app.secret_key = 'html_secret_key' api = Api(app) babel = Babel(app) @babel.localeselector def get_locale(): #return request.accept_languages.best_match(['en', 'es', 'fi']) return 'en' def recomAbbr(contentJson, badEvaluations): contentBase = BeautifulSoup(contentJson, "lxml") images = contentBase.find_all('abbr') badEvalsPos = badEvaluations.get('positionsBadTags') recomendations = []
def create_app(main_module: bool = False): app = Flask(__name__, static_url_path='/static', static_folder='static') app.config['BABEL_TRANSLATION_DIRECTORIES'] = os.path.abspath( 'limonero/i18n/locales') app.json_encoder = LimoneroJSONEncoder babel = Babel(app) logging.config.fileConfig('logging_config.ini') app.secret_key = 'l3m0n4d1' # Cryptography key app.download_key = Fernet.generate_key() app.fernet = Fernet(app.download_key) # Cache cache.init_app(app) # CORS CORS(app, resources={r"/*": {"origins": "*"}}) api = Api(app) # Swagger swaggerui_blueprint = get_swaggerui_blueprint( '/api/docs', '/static/swagger.yaml', config={ # Swagger UI config overrides 'app_name': "Lemonade Caipirinha" }, # oauth_config={ # OAuth config. See https://github.com/swagger-api/swagger-ui#oauth2-configuration . # 'clientId': "your-client-id", # 'clientSecret': "your-client-secret-if-required", # 'realm': "your-realms", # 'appName': "your-app-name", # 'scopeSeparator': " ", # 'additionalQueryStringParams': {'test': "hello"} # } ) app.register_blueprint(swaggerui_blueprint) mappings = { '/datasources': DataSourceListApi, '/datasources/upload': DataSourceUploadApi, '/datasources/infer-schema/<int:data_source_id>': DataSourceInferSchemaApi, '/datasources/sample/<int:data_source_id>': DataSourceSampleApi, '/datasources/initialize/<status>/<int:data_source_id>': DataSourceInitializationApi, '/datasources/<int:data_source_id>': DataSourceDetailApi, '/datasources/<int:data_source_id>/permission/<int:user_id>': DataSourcePermissionApi, '/datasources/<int:data_source_id>/privacy': DataSourcePrivacyApi, '/privacy': GlobalPrivacyListApi, '/privacy/attribute-groups': AttributePrivacyGroupListApi, '/models': ModelListApi, '/models/<int:model_id>': ModelDetailApi, '/storages': StorageListApi, '/storages/<int:storage_id>': StorageDetailApi, '/storages/metadata/<int:storage_id>': StorageMetadataApi, } grouped_mappings = itertools.groupby(sorted(mappings.items()), key=lambda path: path[1]) for view, g in grouped_mappings: api.add_resource(view, *[x[0] for x in g], endpoint=view.__name__) app.add_url_rule('/datasources/public/<int:data_source_id>/download', methods=['GET'], endpoint='DataSourceDownload', view_func=DataSourceDownload.as_view('download')) app.add_url_rule('/models/<int:model_id>/download', methods=['GET'], endpoint='ModelDownloadApi', view_func=ModelDownloadApi.as_view('download_model')) migrate = Migrate(app, db) app.handle_exception @babel.localeselector def get_locale(): user = getattr(flask_g, 'user', None) if user is not None and user.locale: return user.locale else: return request.args.get( 'lang', request.accept_languages.best_match(['en', 'pt', 'es'])) sqlalchemy_utils.i18n.get_locale = get_locale config_file = None signal.signal(signal.SIGINT, exit_gracefully) if main_module: parser = argparse.ArgumentParser() parser.add_argument("-c", "--config", type=str, help="Config file", required=False) args = parser.parse_args() config_file = args.config if config_file is None: config_file = os.environ.get('LIMONERO_CONFIG') logger = logging.getLogger(__name__) if config_file: with open(config_file) as f: config = yaml.load(f, Loader=yaml.FullLoader)['limonero'] app.config['LIMONERO_CONFIG'] = config app.config["RESTFUL_JSON"] = {"cls": app.json_encoder} server_config = config.get('servers', {}) app.config['SQLALCHEMY_DATABASE_URI'] = server_config.get( 'database_url') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False is_mysql = 'mysql://' in app.config['SQLALCHEMY_DATABASE_URI'] if config.get('config') is not None and 'config' in config and is_mysql: app.config.update(config.get('config', {})) app.config['SQLALCHEMY_POOL_SIZE'] = 10 app.config['SQLALCHEMY_POOL_RECYCLE'] = 240 db.init_app(app) port = int(config.get('port', 5000)) logger.debug( gettext('Running in %(mode)s mode', mode=config.get('environment'))) init_jvm(app, logger) if main_module: # JVM, used to interact with HDFS. if config.get('environment', 'dev') == 'dev': app.run(debug=True, port=port, host='0.0.0.0') else: eventlet.wsgi.server(eventlet.listen(('', port)), app) else: return app else: logger.error( gettext('Please, set LIMONERO_CONFIG environment variable')) exit(1) return app