2024-02-28 17:33:16 +01:00
|
|
|
from .models.user_information import UserInformation
|
2024-02-28 13:45:21 +01:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
2024-02-28 14:00:53 +01:00
|
|
|
async def acreate_user_information(user: User, *, display_name: Optional[str] = None) -> UserInformation:
|
2024-02-28 13:56:38 +01:00
|
|
|
"""Async. Create a UserInformation object for the given user.
|
2024-02-28 13:45:21 +01:00
|
|
|
|
|
|
|
@param user: The user to create the UserInformation object for.
|
|
|
|
@param display_name: The display name for the user. If None, the user's username will be used.
|
|
|
|
@return: The created UserInformation object.
|
|
|
|
"""
|
2024-02-28 13:56:38 +01:00
|
|
|
user_information = await UserInformation.objects.acreate(user=user, display_name=user.username)
|
2024-02-28 13:45:21 +01:00
|
|
|
|
|
|
|
# Use the given display name if provided.
|
|
|
|
if display_name is not None:
|
|
|
|
user_information.display_name = display_name
|
2024-02-28 13:56:38 +01:00
|
|
|
await user_information.asave()
|
2024-02-28 13:45:21 +01:00
|
|
|
|
|
|
|
return user_information
|
|
|
|
|
|
|
|
|
2024-02-28 14:00:53 +01:00
|
|
|
async def aget_user_information(user: User) -> UserInformation:
|
2024-02-28 13:56:38 +01:00
|
|
|
"""Async. Get the UserInformation object for the given user.
|
2024-02-28 13:45:21 +01:00
|
|
|
|
|
|
|
@param user: The user to get the UserInformation object for.
|
|
|
|
@return: The UserInformation object for the given user.
|
|
|
|
"""
|
2024-02-28 13:56:38 +01:00
|
|
|
return await UserInformation.objects.aget(user)
|
2024-02-28 13:45:21 +01:00
|
|
|
|
|
|
|
|
2024-02-28 14:00:53 +01:00
|
|
|
async def aset_user_display_name(user: User, display_name: Optional[str] = None) -> UserInformation:
|
2024-02-28 13:56:38 +01:00
|
|
|
"""Async. Set the display name for the given user.
|
2024-02-28 13:45:21 +01:00
|
|
|
|
|
|
|
@param user: The user to set the display name for.
|
|
|
|
@param display_name: The display name to set for the user. If None, the user's username will be used.
|
|
|
|
@return: The UserInformation object for the given user.
|
|
|
|
"""
|
2024-02-28 13:56:38 +01:00
|
|
|
user_information = await UserInformation.objects.aget(user)
|
2024-02-28 13:45:21 +01:00
|
|
|
|
|
|
|
# Set the display name to the given display name, or the user's username if None.
|
|
|
|
if display_name is not None:
|
|
|
|
user_information.display_name = display_name
|
|
|
|
else:
|
|
|
|
user_information.display_name = user.username
|
|
|
|
|
2024-02-28 13:56:38 +01:00
|
|
|
await user_information.asave()
|
2024-02-28 13:45:21 +01:00
|
|
|
return user_information
|