[misc] Reformat code to match black styleguide

This commit is contained in:
Cathy Hu 2020-07-02 14:31:53 +02:00
parent f1db68d1c8
commit bedcaa940e
7 changed files with 38 additions and 32 deletions

View file

@ -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')

View file

@ -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)

View file

@ -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'])

View file

@ -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()
)

View file

@ -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)

View file

@ -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

View file

@ -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