[misc] Reformat code to match black styleguide
This commit is contained in:
parent
f1db68d1c8
commit
bedcaa940e
|
@ -8,9 +8,9 @@ from pytoml import load
|
||||||
|
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
'database_connection': 'sqlite:////tmp/kibicara.sqlite',
|
'database_connection': 'sqlite:////tmp/kibicara.sqlite',
|
||||||
'frontend_path': None,
|
'frontend_path': None,
|
||||||
}
|
}
|
||||||
|
|
||||||
parser = OptionParser()
|
parser = OptionParser()
|
||||||
parser.add_option('-f', dest='configfile', default='/etc/kibicara.conf')
|
parser.add_option('-f', dest='configfile', default='/etc/kibicara.conf')
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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'])
|
||||||
|
|
|
@ -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):
|
||||||
|
@ -57,9 +58,10 @@ async def get_admin(access_token=Depends(oauth2_scheme)):
|
||||||
admin = await get_auth(**from_token(access_token))
|
admin = await get_auth(**from_token(access_token))
|
||||||
except (CryptoError, ValueError):
|
except (CryptoError, ValueError):
|
||||||
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,
|
||||||
# XXX create real confirm link
|
subject='Confirm Account',
|
||||||
body=register_token)
|
# XXX create real confirm link
|
||||||
|
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)
|
||||||
|
@ -98,13 +102,15 @@ async def admin_login(form_data: OAuth2PasswordRequestForm = Depends()):
|
||||||
await get_auth(form_data.username, form_data.password)
|
await get_auth(form_data.username, form_data.password)
|
||||||
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()
|
||||||
|
)
|
||||||
|
|
|
@ -27,8 +27,9 @@ async def get_hood(hood_id: int, admin=Depends(get_admin)):
|
||||||
await AdminHoodRelation.objects.get(admin=admin, hood=hood)
|
await AdminHoodRelation.objects.get(admin=admin, hood=hood)
|
||||||
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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue