1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-12-18 07:34:20 +00:00
royalnet/royalpack/stars/api_bio.py

46 lines
1.2 KiB
Python
Raw Normal View History

2020-06-22 17:27:11 +00:00
import royalnet.utils as ru
2020-06-26 14:13:11 +00:00
import royalnet.backpack.tables as rbt
import royalnet.constellation.api as rca
2020-06-22 17:27:11 +00:00
from ..tables import Bio
2020-06-26 14:13:11 +00:00
class ApiBioSetStar(rca.ApiStar):
2020-06-22 17:27:11 +00:00
path = "/api/bio/v2"
parameters = {
"get": {
"uid": "The id of the user to get the bio of."
},
"put": {
"contents": "The contents of the bio."
}
}
auth = {
"get": False,
"put": True,
}
tags = ["bio"]
2020-06-26 14:13:11 +00:00
@rca.magic
async def get(self, data: rca.ApiData) -> ru.JSON:
2020-06-22 17:27:11 +00:00
"""Get the bio of a specific user."""
2020-06-26 14:13:11 +00:00
user = await rbt.User.find(self.alchemy, data.session, data.int("uid"))
2020-06-22 17:27:11 +00:00
return user.bio.json() if user.bio else None
2020-06-26 14:13:11 +00:00
@rca.magic
async def put(self, data: rca.ApiData) -> ru.JSON:
2020-06-22 17:27:11 +00:00
"""Set the bio of current user."""
contents = data["contents"]
BioT = self.alchemy.get(Bio)
user = await data.user()
bio = user.bio
if bio is None:
bio = BioT(user=user, contents=contents)
data.session.add(bio)
else:
bio.contents = contents
await data.session_commit()
return bio.json()