-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-4: Persist users in SQLite in-memory database
- Loading branch information
1 parent
f1cfb5a
commit 19ef404
Showing
4 changed files
with
50 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,11 @@ | |
from flask import Flask, Response, render_template, request, url_for | ||
from inject import Binder | ||
from jinja2 import ChoiceLoader, FileSystemLoader, PackageLoader, PrefixLoader | ||
from sqlalchemy import Engine, MetaData, create_engine | ||
|
||
from schemes import api, auth, home, start | ||
from schemes.config import DevConfig | ||
from schemes.users import DatabaseUserRepository, User, UserRepository | ||
from schemes.users import DatabaseUserRepository, User, UserRepository, add_tables | ||
|
||
|
||
def create_app(test_config: Mapping[str, Any] | None = None) -> Flask: | ||
|
@@ -27,20 +28,23 @@ def create_app(test_config: Mapping[str, Any] | None = None) -> Flask: | |
_configure_basic_auth(app) | ||
_configure_govuk_frontend(app) | ||
_configure_oidc(app) | ||
if not app.testing: | ||
_configure_users() | ||
|
||
app.register_blueprint(start.bp) | ||
app.register_blueprint(auth.bp, url_prefix="/auth") | ||
app.register_blueprint(home.bp, url_prefix="/home") | ||
if app.testing: | ||
app.register_blueprint(api.bp, url_prefix="/api") | ||
|
||
_create_database() | ||
if not app.testing: | ||
_configure_users() | ||
|
||
return app | ||
|
||
|
||
def _bindings(binder: Binder) -> None: | ||
binder.bind(UserRepository, DatabaseUserRepository()) | ||
binder.bind(Engine, create_engine("sqlite+pysqlite:///file::memory:?uri=true")) | ||
binder.bind_to_constructor(UserRepository, DatabaseUserRepository) | ||
|
||
|
||
def _configure_error_pages(app: Flask) -> None: | ||
|
@@ -94,6 +98,14 @@ def _configure_oidc(app: Flask) -> None: | |
) | ||
|
||
|
||
def _create_database() -> None: | ||
metadata = MetaData() | ||
add_tables(metadata) | ||
|
||
engine = inject.instance(Engine) | ||
metadata.create_all(engine) | ||
|
||
|
||
def _configure_users() -> None: | ||
users = inject.instance(UserRepository) | ||
users.add(User("[email protected]")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters