diff --git a/kibicara/config.py b/kibicara/config.py index 840aa2e..32954d0 100644 --- a/kibicara/config.py +++ b/kibicara/config.py @@ -8,9 +8,9 @@ from pytoml import load config = { - 'database_connection': 'sqlite:////tmp/kibicara.sqlite', - 'frontend_path': None, - } + 'database_connection': 'sqlite:////tmp/kibicara.sqlite', + 'frontend_path': None, +} parser = OptionParser() parser.add_option('-f', dest='configfile', default='/etc/kibicara.conf') diff --git a/kibicara/kibicara.py b/kibicara/kibicara.py index fe45099..e0c240e 100644 --- a/kibicara/kibicara.py +++ b/kibicara/kibicara.py @@ -35,12 +35,14 @@ class Main: if response.status_code == 404: response = await super().get_response('.', scope) return response + app = FastAPI() server_config = Config() server_config.accesslog = '-' app.include_router(router, prefix='/api') if config['frontend_path'] is not None: - app.mount('/', app=SinglePageApplication( - directory=config['frontend_path'], - html=True)) + app.mount( + '/', + app=SinglePageApplication(directory=config['frontend_path'], html=True), + ) await serve(app, server_config) diff --git a/kibicara/webapi/__init__.py b/kibicara/webapi/__init__.py index 8a8a81c..5350889 100644 --- a/kibicara/webapi/__init__.py +++ b/kibicara/webapi/__init__.py @@ -15,6 +15,5 @@ router = APIRouter() router.include_router(admin_router, prefix='/admin', tags=['admin']) hoods_router.include_router(triggers_router, prefix='/{hood_id}/triggers') hoods_router.include_router(badwords_router, prefix='/{hood_id}/badwords') -hoods_router.include_router( - test_router, prefix='/{hood_id}/test', tags=['test']) +hoods_router.include_router(test_router, prefix='/{hood_id}/test', tags=['test']) router.include_router(hoods_router, prefix='/hoods', tags=['hoods']) diff --git a/kibicara/webapi/admin.py b/kibicara/webapi/admin.py index 9fe7b0d..731e2c6 100644 --- a/kibicara/webapi/admin.py +++ b/kibicara/webapi/admin.py @@ -32,14 +32,15 @@ secret_box = SecretBox(random(SecretBox.KEY_SIZE)) def to_token(**kwargs): - return secret_box.encrypt(dumps(kwargs), encoder=URLSafeBase64Encoder) \ - .decode('ascii') + return secret_box.encrypt(dumps(kwargs), encoder=URLSafeBase64Encoder).decode( + 'ascii' + ) def from_token(token): - return loads(secret_box.decrypt( - token.encode('ascii'), - encoder=URLSafeBase64Encoder)) + return loads( + secret_box.decrypt(token.encode('ascii'), encoder=URLSafeBase64Encoder) + ) async def get_auth(email, password): @@ -57,9 +58,10 @@ async def get_admin(access_token=Depends(oauth2_scheme)): admin = await get_auth(**from_token(access_token)) except (CryptoError, ValueError): raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail='Invalid authentication credentials', - headers={'WWW-Authenticate': 'Bearer'}) + status_code=status.HTTP_401_UNAUTHORIZED, + detail='Invalid authentication credentials', + headers={'WWW-Authenticate': 'Bearer'}, + ) return admin @@ -72,9 +74,11 @@ async def admin_register(values: BodyAdmin): logger.debug(register_token) try: send_email( - to=values.email, subject='Confirm Account', - # XXX create real confirm link - body=register_token) + to=values.email, + subject='Confirm Account', + # XXX create real confirm link + body=register_token, + ) except ConnectionRefusedError: logger.exception('Email sending failed') raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY) @@ -98,13 +102,15 @@ async def admin_login(form_data: OAuth2PasswordRequestForm = Depends()): await get_auth(form_data.username, form_data.password) except ValueError: raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail='Incorrect email or password') + status_code=status.HTTP_400_BAD_REQUEST, + detail='Incorrect email or password', + ) token = to_token(email=form_data.username, password=form_data.password) return {'access_token': token, 'token_type': 'bearer'} @router.get('/hoods/') async def admin_hood_read_all(admin=Depends(get_admin)): - return await AdminHoodRelation.objects.select_related('hood') \ - .filter(admin=admin).all() + return ( + await AdminHoodRelation.objects.select_related('hood').filter(admin=admin).all() + ) diff --git a/kibicara/webapi/hoods/__init__.py b/kibicara/webapi/hoods/__init__.py index fff0e17..fd5d7c8 100644 --- a/kibicara/webapi/hoods/__init__.py +++ b/kibicara/webapi/hoods/__init__.py @@ -27,8 +27,9 @@ async def get_hood(hood_id: int, admin=Depends(get_admin)): await AdminHoodRelation.objects.get(admin=admin, hood=hood) except NoMatch: raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - headers={'WWW-Authenticate': 'Bearer'}) + status_code=status.HTTP_401_UNAUTHORIZED, + headers={'WWW-Authenticate': 'Bearer'}, + ) return hood @@ -41,9 +42,7 @@ async def hood_read_all(): @router.post('/', status_code=status.HTTP_201_CREATED) -async def hood_create( - values: BodyHood, response: Response, - admin=Depends(get_admin)): +async def hood_create(values: BodyHood, response: Response, admin=Depends(get_admin)): try: hood = await Hood.objects.create(**values.__dict__) await AdminHoodRelation.objects.create(admin=admin.id, hood=hood.id) diff --git a/kibicara/webapi/hoods/badwords.py b/kibicara/webapi/hoods/badwords.py index 3a1ba85..bf3aa51 100644 --- a/kibicara/webapi/hoods/badwords.py +++ b/kibicara/webapi/hoods/badwords.py @@ -32,8 +32,8 @@ async def badword_read_all(hood=Depends(get_hood)): @router.post('/', status_code=status.HTTP_201_CREATED) async def badword_create( - values: BodyBadWord, response: Response, - hood=Depends(get_hood)): + values: BodyBadWord, response: Response, hood=Depends(get_hood) +): try: badword = await BadWord.objects.create(hood=hood, **values.__dict__) response.headers['Location'] = '%d' % badword.id diff --git a/kibicara/webapi/hoods/triggers.py b/kibicara/webapi/hoods/triggers.py index 2efa956..4776aa8 100644 --- a/kibicara/webapi/hoods/triggers.py +++ b/kibicara/webapi/hoods/triggers.py @@ -32,8 +32,8 @@ async def trigger_read_all(hood=Depends(get_hood)): @router.post('/', status_code=status.HTTP_201_CREATED) async def trigger_create( - values: BodyTrigger, response: Response, - hood=Depends(get_hood)): + values: BodyTrigger, response: Response, hood=Depends(get_hood) +): try: trigger = await Trigger.objects.create(hood=hood, **values.__dict__) response.headers['Location'] = '%d' % trigger.id