Пример #1
0
    def test_via_routes_name_app_config(self, _include):
        via = Via()
        self.app.config['VIA_ROUTES_MODULE'] = 'foo.bar'
        self.app.config['VIA_ROUTES_NAME'] = 'urls'

        via.init_app(self.app)

        _include.assert_called_once_with('foo.bar', 'urls')
Пример #2
0
def create_app(config_name):
    global user_datastore
    app = Flask(__name__)

    app.config.from_object(app_config[config_name])

    csrf = CSRFProtect()
    csrf.init_app(app)

    assets = Environment(app)
    create_assets(assets)

    via = Via()
    via.init_app(app)

    # Code for desmostration the flask upload in several models - - - -

    from user import user_photo
    from restaurant import restaurant_photo
    from food import food_photo

    configure_uploads(app, (restaurant_photo, food_photo, user_photo))

    engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
    if not database_exists(engine.url):
        create_database(engine.url)

    security = Security(app, user_datastore, register_form=SecurityRegisterForm)

    create_security_admin(app=app, path=os.path.join(os.path.dirname(__file__)))

    with app.app_context():
        db.init_app(app)
        db.create_all()
        user_datastore.find_or_create_role(name='admin', description='Administrator')
        db.session.commit()
        user_datastore.find_or_create_role(name='end-user', description='End user')
        db.session.commit()

    @app.route('/', methods=['GET'])
    @app.route('/home', methods=['GET'])
    def index():
        return render_template('index.html')

    @app.errorhandler(403)
    def forbidden(error):
        return render_template('error/403.html', title='Forbidden'), 403

    @app.errorhandler(404)
    def page_not_found(error):
        return render_template('error/404.html', title='Page Not Found'), 404

    @app.errorhandler(500)
    def internal_server_error(error):
        db.session.rollback()
        return render_template('error/500.html', title='Server Error'), 500

    return app
Пример #3
0
    def test_init_app_raises_not_implemented(self):
        via = Via()

        with self.assertRaises(ImproperlyConfigured) as e:
            via.init_app(self.app)

        self.assertEqual(
            str(e.exception),
            'VIA_ROUTES_MODULE is not defined in application configuration.')
Пример #4
0
    def test_init_app_iterates_over_routes(self, import_module):
        routes = [
            mock.MagicMock(),
            mock.MagicMock()
        ]
        import_module.return_value = mock.MagicMock(routes=routes)

        via = Via()
        via.init_app(self.app, routes_module='foo.bar')

        for instance in routes:
            instance.add_to_app.assert_called_once_with(self.app)
Пример #5
0
    def test_init_app_raises_attribute_error(self, import_module):

        class Module(object):
            pass

        import_module.return_value = Module()

        via = Via()

        with self.assertRaises(AttributeError) as e:
            via.init_app(self.app, routes_module='foo.bar')

        self.assertEqual(
            str(e.exception),
            "'Module' object has no attribute 'routes'")
Пример #6
0
def create_app():
    app = Flask(__name__)
    app.config['VIA_ROUTES_MODULE'] = 'app.routes'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
    if not database_exists(engine.url):
        create_database(engine.url)

    db.init_app(app)
    with app.app_context():
        db.create_all()

    via = Via()
    via.init_app(app)
    return app
Пример #7
0
    def test_init_app_raises_import_error(self):
        via = Via()
        self.app.config['VIA_ROUTES_MODULE'] = 'foo.bar'

        with self.assertRaises(ImportError):
            via.init_app(self.app)
Пример #8
0
def create_app(config_name):
    global user_datastore
    app = Flask(__name__)

    app.config.from_object(app_config[config_name])

    csrf = CSRFProtect()
    csrf.init_app(app)

    assets = Environment(app)
    create_assets(assets)

    via = Via()
    via.init_app(app)

    # Ipload in several models - - - -

    from app.user import user_photo
    from app.restaurant import restaurant_photo
    from app.food import food_photo

    configure_uploads(app, (restaurant_photo, food_photo, user_photo))

    engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
    if not database_exists(engine.url):
        create_database(engine.url)

    security = Security(app, user_datastore, register_form=SecurityRegisterForm)

    create_security_admin(app=app, path=os.path.join(os.path.dirname(__file__)))

    with app.app_context():
        db.init_app(app)
        #Conditionally create admin/end_user
        db.create_all()
        user_datastore.find_or_create_role(name='admin', description='Administrator')

        db.session.commit()
        user_datastore.find_or_create_role(name='end-user', description='End user')
        db.session.commit()

        # Create two Users for testing purposes -- unless they already exists.
        # In each case, use Flask-Security utility function to encrypt the password.
        encrypted_password = utils.encrypt_password('password')
        if not user_datastore.get_user('*****@*****.**'):
            user_datastore.create_user(email='*****@*****.**', password=encrypted_password)
        if not user_datastore.get_user('*****@*****.**'):
            user_datastore.create_user(email='*****@*****.**', password=encrypted_password)

        # Commit any database changes; the User and Roles must exist before we can add a Role to the User
        db.session.commit()

        # Give one User has the "end-user" role, while the other has the "admin" role. (This will have no effect if the
        # Users already have these Roles.) Again, commit any database changes.
        user_datastore.add_role_to_user('*****@*****.**', 'end-user')
        user_datastore.add_role_to_user('*****@*****.**', 'admin')
        db.session.commit()


    @app.route('/', methods=['GET'])
    @app.route('/home', methods=['GET'])    
    def index():
        return render_template('index.html')

    @app.errorhandler(403)
    def forbidden(error):
        return render_template('error/403.html', title='Forbidden'), 403

    @app.errorhandler(404)
    def page_not_found(error):
        return render_template('error/404.html', title='Page Not Found'), 404

    @app.errorhandler(500)
    def internal_server_error(error):
        db.session.rollback()
        return render_template('error/500.html', title='Server Error'), 500

    return app