48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
from api_v1.profile.model import Profile
|
|
from django.contrib.auth.models import User
|
|
from typing import Optional
|
|
|
|
|
|
async def acreate_profile(user: User, *, display_name: Optional[str] = None) -> Profile:
|
|
"""Async. Create a Profile object for the given profile.
|
|
|
|
@param user: The profile to create the Profile object for.
|
|
@param display_name: The display name for the profile. If None, the profile's username will be used.
|
|
@return: The created Profile object.
|
|
"""
|
|
profile = await Profile.objects.acreate(user=user, display_name=user.username)
|
|
|
|
# Use the given display name if provided.
|
|
if display_name is not None:
|
|
profile.display_name = display_name
|
|
await profile.asave()
|
|
|
|
return profile
|
|
|
|
|
|
async def aget_profile(user: User) -> Profile:
|
|
"""Async. Get the Profile object for the given profile.
|
|
|
|
@param user: The profile to get the Profile object for.
|
|
@return: The Profile object for the given profile.
|
|
"""
|
|
return await Profile.objects.aget(user)
|
|
|
|
|
|
async def aset_user_display_name(user: User, display_name: Optional[str] = None) -> Profile:
|
|
"""Async. Set the display name for the given profile.
|
|
|
|
@param user: The profile to set the display name for.
|
|
@param display_name: The display name to set for the profile. If None, the profile's username will be used.
|
|
@return: The Profile object for the given profile.
|
|
"""
|
|
profile = await Profile.objects.aget(user)
|
|
|
|
# Set the display name to the given display name, or the profile's username if None.
|
|
if display_name is not None:
|
|
profile.display_name = display_name
|
|
else:
|
|
profile.display_name = user.username
|
|
|
|
await profile.asave()
|
|
return profile
|