[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

@ -35,12 +35,14 @@ class Main:
if response.status_code == 404: if response.status_code == 404:
response = await super().get_response('.', scope) response = await super().get_response('.', scope)
return response return response
app = FastAPI() app = FastAPI()
server_config = Config() server_config = Config()
server_config.accesslog = '-' server_config.accesslog = '-'
app.include_router(router, prefix='/api') app.include_router(router, prefix='/api')
if config['frontend_path'] is not None: if config['frontend_path'] is not None:
app.mount('/', app=SinglePageApplication( app.mount(
directory=config['frontend_path'], '/',
html=True)) app=SinglePageApplication(directory=config['frontend_path'], html=True),
)
await serve(app, server_config) await serve(app, server_config)

View file

@ -15,6 +15,5 @@ router = APIRouter()
router.include_router(admin_router, prefix='/admin', tags=['admin']) router.include_router(admin_router, prefix='/admin', tags=['admin'])
hoods_router.include_router(triggers_router, prefix='/{hood_id}/triggers') hoods_router.include_router(triggers_router, prefix='/{hood_id}/triggers')
hoods_router.include_router(badwords_router, prefix='/{hood_id}/badwords') hoods_router.include_router(badwords_router, prefix='/{hood_id}/badwords')
hoods_router.include_router( hoods_router.include_router(test_router, prefix='/{hood_id}/test', tags=['test'])
test_router, prefix='/{hood_id}/test', tags=['test'])
router.include_router(hoods_router, prefix='/hoods', tags=['hoods']) 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): def to_token(**kwargs):
return secret_box.encrypt(dumps(kwargs), encoder=URLSafeBase64Encoder) \ return secret_box.encrypt(dumps(kwargs), encoder=URLSafeBase64Encoder).decode(
.decode('ascii') 'ascii'
)
def from_token(token): def from_token(token):
return loads(secret_box.decrypt( return loads(
token.encode('ascii'), secret_box.decrypt(token.encode('ascii'), encoder=URLSafeBase64Encoder)
encoder=URLSafeBase64Encoder)) )
async def get_auth(email, password): async def get_auth(email, password):
@ -59,7 +60,8 @@ async def get_admin(access_token=Depends(oauth2_scheme)):
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
detail='Invalid authentication credentials', detail='Invalid authentication credentials',
headers={'WWW-Authenticate': 'Bearer'}) headers={'WWW-Authenticate': 'Bearer'},
)
return admin return admin
@ -72,9 +74,11 @@ async def admin_register(values: BodyAdmin):
logger.debug(register_token) logger.debug(register_token)
try: try:
send_email( send_email(
to=values.email, subject='Confirm Account', to=values.email,
subject='Confirm Account',
# XXX create real confirm link # XXX create real confirm link
body=register_token) body=register_token,
)
except ConnectionRefusedError: except ConnectionRefusedError:
logger.exception('Email sending failed') logger.exception('Email sending failed')
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY) raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY)
@ -99,12 +103,14 @@ async def admin_login(form_data: OAuth2PasswordRequestForm = Depends()):
except ValueError: except ValueError:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
detail='Incorrect email or password') detail='Incorrect email or password',
)
token = to_token(email=form_data.username, password=form_data.password) token = to_token(email=form_data.username, password=form_data.password)
return {'access_token': token, 'token_type': 'bearer'} return {'access_token': token, 'token_type': 'bearer'}
@router.get('/hoods/') @router.get('/hoods/')
async def admin_hood_read_all(admin=Depends(get_admin)): async def admin_hood_read_all(admin=Depends(get_admin)):
return await AdminHoodRelation.objects.select_related('hood') \ return (
.filter(admin=admin).all() await AdminHoodRelation.objects.select_related('hood').filter(admin=admin).all()
)

View file

@ -28,7 +28,8 @@ async def get_hood(hood_id: int, admin=Depends(get_admin)):
except NoMatch: except NoMatch:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
headers={'WWW-Authenticate': 'Bearer'}) headers={'WWW-Authenticate': 'Bearer'},
)
return hood return hood
@ -41,9 +42,7 @@ async def hood_read_all():
@router.post('/', status_code=status.HTTP_201_CREATED) @router.post('/', status_code=status.HTTP_201_CREATED)
async def hood_create( async def hood_create(values: BodyHood, response: Response, admin=Depends(get_admin)):
values: BodyHood, response: Response,
admin=Depends(get_admin)):
try: try:
hood = await Hood.objects.create(**values.__dict__) hood = await Hood.objects.create(**values.__dict__)
await AdminHoodRelation.objects.create(admin=admin.id, hood=hood.id) 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) @router.post('/', status_code=status.HTTP_201_CREATED)
async def badword_create( async def badword_create(
values: BodyBadWord, response: Response, values: BodyBadWord, response: Response, hood=Depends(get_hood)
hood=Depends(get_hood)): ):
try: try:
badword = await BadWord.objects.create(hood=hood, **values.__dict__) badword = await BadWord.objects.create(hood=hood, **values.__dict__)
response.headers['Location'] = '%d' % badword.id 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) @router.post('/', status_code=status.HTTP_201_CREATED)
async def trigger_create( async def trigger_create(
values: BodyTrigger, response: Response, values: BodyTrigger, response: Response, hood=Depends(get_hood)
hood=Depends(get_hood)): ):
try: try:
trigger = await Trigger.objects.create(hood=hood, **values.__dict__) trigger = await Trigger.objects.create(hood=hood, **values.__dict__)
response.headers['Location'] = '%d' % trigger.id response.headers['Location'] = '%d' % trigger.id