from starlette.routing import Route, Router from starlette.responses import JSONResponse from starlette.authentication import requires, AuthenticationError from webargs_starlette import use_annotations from sparrow.database.models import User from sparrow.context import get_sparrow_app, get_database from sparrow.logs import get_logger log = get_logger(__name__) def get_backend(): app = get_sparrow_app() return app.plugins.get("auth").backend def UnauthorizedResponse(**kwargs): return JSONResponse( dict(login=False, username=None, message="user is not authenticated"), **kwargs) @use_annotations(location="json") async def login(request, username: str, password: str): db = get_database() backend = get_backend() current_user = db.session.query(User).get(username) log.debug(current_user) if current_user is not None and current_user.is_correct_password(password):
#!/usr/bin/env python """ Initialize database and dump data into it. """ from sparrow.app import App from sparrow_tests.helpers.database import testing_database, connection_args from sparrow_tests.test_dz_import import import_dz_test_data from sparrow.util import run from sys import stderr, stdout from contextlib import redirect_stdout from sparrow.logs import console_handler, get_logger from sparrow_tests.fixtures import basic_project import logging log = get_logger(level=logging.WARNING, handler=console_handler) conn = "postgresql://postgres@db:5432/sparrow_test_1" with redirect_stdout(stderr), testing_database(conn) as engine: # Re-initialize app app = App(__name__) app.database.initialize() app.load() app.load_phase_2() logging.getLogger("sparrow.interface.schema").setLevel(logging.ERROR) project = app.database.load_data("project", basic_project, session=app.database.session) dz_data = import_dz_test_data()