1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add api_user_avatar.py

This commit is contained in:
Steffo 2020-07-05 10:16:11 +02:00
parent e58860f347
commit dadf5b94d9
Signed by: steffo
GPG key ID: 896A80F55F7C97F0
2 changed files with 40 additions and 0 deletions

View file

@ -12,6 +12,7 @@ from .api_cvstats_latest import ApiCvstatsLatestStar
from .api_cvstats_avg import ApiCvstatsAvgStar
from .api_user_ryg import ApiUserRygStar
from .api_user_ryg_list import ApiUserRygListStar
from .api_user_avatar import ApiUserAvatarStar
# Enter the PageStars of your Pack here!
available_page_stars = [
@ -28,6 +29,7 @@ available_page_stars = [
ApiCvstatsAvgStar,
ApiUserRygStar,
ApiUserRygListStar,
ApiUserAvatarStar,
]
# Don't change this, it should automatically generate __all__

View file

@ -0,0 +1,38 @@
import re
import royalnet.utils as ru
import royalnet.constellation.api as rca
url_validation = re.compile(r'^(?:http|ftp)s?://'
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'
r'localhost|'
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
r'(?::\d+)?'
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
class ApiUserAvatarStar(rca.ApiStar):
path = "/api/user/avatar/v2"
parameters = {
"put": {
"avatar_url": "The url that the user wants to set as avatar."
}
}
auth = {
"put": True,
}
tags = ["user"]
@rca.magic
async def put(self, data: rca.ApiData) -> ru.JSON:
"""Set the avatar of current user."""
avatar_url = data["avatar_url"]
user = await data.user()
if not re.match(url_validation, avatar_url):
raise rca.InvalidParameterError("avatar_url is not a valid url.")
user.avatar_url = avatar_url
await data.session_commit()
return user.json()