def setUp(self): self.db_name = 'testing' app = flask.Flask(__name__) app.config['MONGODB_DB'] = self.db_name app.config['TESTING'] = True db = MongoEngine(app) app.session_interface = MongoEngineSessionInterface(db) @app.route('/') def index(): session["a"] = "hello session" return session["a"] @app.route('/check-session') def check_session(): return "session: %s" % session["a"] @app.route('/check-session-database') def check_session_database(): sessions = self.app.session_interface.cls.objects.count() return "sessions: %s" % sessions self.app = app self.db = db
def configure_app(app, db, login_service=None): # Choose configuration environment. config_name = os.environ.get("SERVER_ENV", "development") print("Config Name: " + config_name) app.config.from_pyfile( os.path.join(os.getcwd() + "/config/" + ("%s.py" % config_name))) # Apply any changes from environment variable. app.config.from_envvar("SERVER_CONFIG", silent=True) # Configure database, and set the session to be stored in MongoDB. db.init_app(app) app.session_interface = MongoEngineSessionInterface(db) mandrill_serv.initialize(app.config) # Saves the user (Employee or User in our case) that requested something # from our server into global g of flask. @app.before_request def before_request(): # Set up the Employee. g.login = current_user # Copy over config details. g.secret_key = app.config["SECRET_KEY"]
def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) app.session_interface = MongoEngineSessionInterface(db) bootstrap.init_app(app) moment.init_app(app) db.init_app(app) login_manager.init_app(app) mail.init_app(app) create_admins(config[config_name].ADMINS) #errorhandlers @app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 @app.errorhandler(500) def internal_server_error(e): return render_template('500.html'), 500 #routes @app.route('/login', methods=['GET','POST']) def loginpage(): return redirect(url_for('users.login')) @app.route('/') @app.route('/home') def homepage(): return redirect(url_for('wall.list')) #filters @app.template_filter('boolswitch') def switchboolean(s): var = {True:'DA',False:'NU'} return var[s] from .users.views import mod as users_blueprint app.register_blueprint(users_blueprint, url_prefix='/membri') from .messages.views import mod as msgs_blueprint app.register_blueprint(msgs_blueprint, url_prefix='/mesagerie') from .wall.views import mod as wall_blueprint app.register_blueprint(wall_blueprint, url_prefix='/home') from .projects.views import mod as proj_blueprint app.register_blueprint(proj_blueprint, url_prefix='/proiecte') from .inventory.views import mod as inv_blueprint app.register_blueprint(inv_blueprint, url_prefix='/inventar') return app
def configure_extensions(app): # flask-mongoengine db.init_app(app) app.session_interface = MongoEngineSessionInterface(db) # flask-mail mail.init_app(app) # flask-babel babel.init_app(app) # Bcrypt for hashing passwords bcrypt.init_app(app) # the Gravatar service gravatar.init_app(app) # Markdown md.init_app(app) # Caching cache.init_app(app) # Debug Toolbar if app.debug: from flask_debugtoolbar import DebugToolbarExtension DebugToolbarExtension(app) @babel.localeselector def get_locale(): """ Get the current request locale. returns String """ if not hasattr(g, 'lang'): g.lang = 'en' accept_languages = app.config.get('ACCEPT_LANGUAGES') return g.lang or request.accept_languages.best_match(accept_languages) # flask-login login_manager.login_view = 'frontend.login' login_manager.refresh_view = 'frontend.reauth' @login_manager.user_loader def load_user(user_id): return User.objects(id=user_id).first() login_manager.setup_app(app)
def __call__(self, name, **kwargs): self.app = Flask(name, **kwargs) self.app.config.from_object(self.config) self.bind_extensions(db, mail, celery, csrf, cache, htmlmin, toolbar) register_connection(DB_TEMP_NAME, DB_TEMP_NAME) self.app.session_interface = MongoEngineSessionInterface(db) self.app.permanent_session_lifetime = timedelta(weeks=1) self.app.user_datastore = MongoEngineUserDatastore(db, User, Role) self.app.security = Security(self.app, self.app.user_datastore, login_form=ExtLoginForm) @self.app.security.send_mail_task def delay_security_email(msg): send_security_mail.delay(msg) # @self.app.errorhandler(Exception) # def validation_error(err): # flash(err.message, 'error') # return redirect(request.path) self.register_blueprints(BLUEPRINTS) self.register_template_filters(time_distance, is_list, smart_round, pretty_date, phonofize) self.register_context_processors(categories, cart, visited_offers, pages) self.app.jinja_env.globals['get_plural'] = get_plural self.app.add_url_rule('/media/<filename>', 'media', build_only=True) self.app.wsgi_app = SharedDataMiddleware( self.app.wsgi_app, {'/media': self.app.config['MEDIA_DIR']}) return self.app
def setUp(self): super(SessionTestCase, self).setUp() self.db_name = 'testing' self.app.config['MONGODB_DB'] = self.db_name self.app.config['TESTING'] = True db = MongoEngine(self.app) self.app.session_interface = MongoEngineSessionInterface(db) @self.app.route('/') def index(): session["a"] = "hello session" return session["a"] @self.app.route('/check-session') def check_session(): return "session: %s" % session["a"] @self.app.route('/check-session-database') def check_session_database(): sessions = self.app.session_interface.cls.objects.count() return "sessions: %s" % sessions self.db = db
from flask.ext.mongoengine import MongoEngine, MongoEngineSessionInterface #from flask.ext.mongo_sessions import MongoDBSessionInterface import os import flask.ext.login as flask_login from flask_bootstrap import Bootstrap #from flask.ext.login import LoginManager app = Flask(__name__) app.config.from_object('config') Session(app) Bootstrap(app) app.jinja_env.line_statement_prefix = '#' db = MongoEngine(app) app.session_interface = MongoEngineSessionInterface(db) #db.init_app(app) login_manager = flask_login.LoginManager() login_manager.init_app(app) login_manager.login_view = 'login' #oid = OpenID(app, safe_roots=[], extension_responses=[pape.Response]) #oid = OpenID() #oid.init_app(app) @app.errorhandler(404) def not_found(error): return render_template('404.html'), 404 from app.mod_search.controllers import mod_search as search_module from app.mod_search.controllers import mod_search_res as search_res_module
return render_template('404.html'), 404 #return "Oops!! It's 404. Page doesn't exist." @app.route("/", methods=['GET', 'POST']) def redirect_home(): return redirect(url_for('home.home_page')) # # database connection # app.config['MONGODB_SETTINGS'] = {'HOST':os.environ.get('MONGOLAB_URI'),'DB': 'FlaskLogin'} # app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY') # app.debug = os.environ.get('DEBUG',False) db = MongoEngine(app) # connect MongoEngine with Flask App app.session_interface = MongoEngineSessionInterface( db) # sessions w/ mongoengine # Flask BCrypt will be used to salt the user password flask_bcrypt = Bcrypt(app) # Associate Flask-Login manager with current app login_manager = LoginManager() login_manager.session_protection = 'strong' login_manager.login_view = 'login' login_manager.init_app(app) # Import a module / component using its blueprint handler variable (mod_auth) from app.mod_home.controllers import mod_home as auth_home # Register blueprint(s) app.register_blueprint(auth_home)
def configure_extensions(app): # flask-mongoengine db.init_app(app) app.session_interface = MongoEngineSessionInterface(db) # flask-mail mail.init_app(app) # flask-babel babel.init_app(app) # Bcrypt for hashing passwords bcrypt.init_app(app) # the Gravatar service gravatar.init_app(app) # Markdown md.init_app(app) # Caching cache.init_app(app) # Debug Toolbar if app.debug and app.config['DEBUG_TB_PROFILER_ENABLED']: from flask_debugtoolbar import DebugToolbarExtension DebugToolbarExtension(app) @babel.localeselector def get_locale(): """ Get the current request locale. returns String """ if not hasattr(g, 'lang'): g.lang = app.config.get('BABEL_DEFAULT_LOCALE') accept_languages = app.config.get('ACCEPT_LANGUAGES') # Try to guess the language from the user accept # header the browser transmits. We support ar/fr/en, # The best match wins. g.lang = session.get('language', None) or \ request.accept_languages.best_match(accept_languages) or \ g.lang if g.lang == 'ar': g.lang_dir = 'rtl' g.layout_dir_class = 'right-to-left' g.language_name = u'العربية' else: g.lang_dir = 'ltr' g.layout_dir_class = 'left-to-right' g.language_name = 'English' if g.lang == 'en' else u'Français' return g.lang # flask-login login_manager.login_view = 'frontend.login' login_manager.refresh_view = 'frontend.reauth' @login_manager.user_loader def load_user(user_id): return User.objects(id=user_id).first() login_manager.setup_app(app)
def create_app(config_name): app = Flask(__name__, template_folder='templates') CORS(app) app.config.from_object(config[config_name]) moment = Moment(app) '''app.config['MONGODB_SETTINGS'] = { 'db': config[config_name].MONGO_DATABASE_NAME, 'host': config[config_name].MONGO_DATABASE_SERVER, 'port': config[config_name].MONGO_DATABASE_PORT }''' app.config['MONGODB_SETTINGS'] = [{ 'alias': 'default', 'db': config[config_name].MONGO_DATABASE_SECURITY_NAME, 'host': config[config_name].MONGO_DATABASE_SECURITY_SERVER, 'port': config[config_name].MONGO_DATABASE_SECURITY_PORT }, { 'alias': config[config_name].MONGO_DATABASE_ARCHIVIED_NAME, 'db': config[config_name].MONGO_DATABASE_ARCHIVIED_NAME, 'host': config[config_name].MONGO_DATABASE_ARCHIVIED_SERVER, 'port': config[config_name].MONGO_DATABASE_ARCHIVIED_PORT }, { 'alias': config[config_name].MONGO_DATABASE_BLANKSPIDER_NAME, 'db': config[config_name].MONGO_DATABASE_BLANKSPIDER_NAME, 'host': config[config_name].MONGO_DATABASE_BLANKSPIDER_SERVER, 'port': config[config_name].MONGO_DATABASE_BLANKSPIDER_PORT }] config[config_name].init_app(app) bootstrap.init_app(app) login_manager.session_protection = 'strong' login_manager.login_view = 'auth.login' login_manager.init_app(app) app.session_interface = MongoEngineSessionInterface(db) db.init_app(app) # Specify the debug panels you want '''app.config['DEBUG_TB_PANELS'] = [ 'flask_debugtoolbar.panels.versions.VersionDebugPanel', 'flask_debugtoolbar.panels.timer.TimerDebugPanel', 'flask_debugtoolbar.panels.headers.HeaderDebugPanel', 'flask_debugtoolbar.panels.request_vars.RequestVarsDebugPanel', 'flask_debugtoolbar.panels.template.TemplateDebugPanel', 'flask_debugtoolbar.panels.sqlalchemy.SQLAlchemyDebugPanel', 'flask_debugtoolbar.panels.logger.LoggingPanel', 'flask_debugtoolbar.panels.profiler.ProfilerDebugPanel', # Add the MongoDB panel #'flask.ext.mongoengine.panels.MongoDebugPanel', ] app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False toolbar = DebugToolbarExtension(app)''' ############### begin blueprint ############################ from app.controllers.auth import auth as auth_blueprint from app.controllers.home import home as home_blueprint from app.controllers.source import source as source_blueprint from app.controllers.repository import repository as repository_blueprint from app.controllers.ajax import ajax as ajax_blueprint app.register_blueprint(home_blueprint, url_prefix='/') app.register_blueprint(auth_blueprint, url_prefix='/auth') app.register_blueprint(source_blueprint, url_prefix='/source') app.register_blueprint(repository_blueprint, url_prefix='/repository') app.register_blueprint(ajax_blueprint, url_prefix='/ajax') ############### end blueprint ############################ return app