takahe/api/views/apps.py

28 lines
734 B
Python
Raw Normal View History

2022-12-11 04:03:14 +00:00
import secrets
2023-03-09 17:47:33 +00:00
from hatchway import QueryOrBody, api_view
2022-12-11 04:03:14 +00:00
2022-12-11 07:25:48 +00:00
from .. import schemas
2022-12-11 04:03:14 +00:00
from ..models import Application
@api_view.post
2023-03-09 17:47:33 +00:00
def add_app(
request,
client_name: QueryOrBody[str],
redirect_uris: QueryOrBody[str],
scopes: QueryOrBody[None | str] = None,
website: QueryOrBody[None | str] = None,
) -> schemas.Application:
2022-12-11 04:03:14 +00:00
client_id = "tk-" + secrets.token_urlsafe(16)
client_secret = secrets.token_urlsafe(40)
application = Application.objects.create(
2023-03-09 17:47:33 +00:00
name=client_name,
website=website,
2022-12-11 04:03:14 +00:00
client_id=client_id,
client_secret=client_secret,
2023-03-09 17:47:33 +00:00
redirect_uris=redirect_uris,
scopes=scopes or "read",
2022-12-11 04:03:14 +00:00
)
return schemas.Application.from_orm(application)