Rebranded UserInformation to Profile
This commit is contained in:
parent
0739545a41
commit
f7a631d543
8 changed files with 71 additions and 52 deletions
|
@ -1,6 +1,6 @@
|
|||
from django.contrib import admin
|
||||
from .user.models.user_information import UserInformation
|
||||
from api_v1.profile.profile import Profile
|
||||
|
||||
# Register your models here.
|
||||
admin.site.register(UserInformation)
|
||||
admin.site.register(Profile)
|
||||
|
||||
|
|
19
api_v1/migrations/0002_rebrandUserInformationToProfile.py
Normal file
19
api_v1/migrations/0002_rebrandUserInformationToProfile.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 5.0.2 on 2024-02-28 19:41
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('api_v1', '0001_createBasicUserInformation'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameModel(
|
||||
old_name='UserInformation',
|
||||
new_name='Profile',
|
||||
),
|
||||
]
|
|
@ -1 +1 @@
|
|||
from .user.models.user_information import UserInformation
|
||||
from api_v1.profile.profile import Profile
|
||||
|
|
|
@ -2,7 +2,7 @@ from django.contrib.auth.models import User
|
|||
from django.db import models
|
||||
|
||||
|
||||
class UserInformation(models.Model):
|
||||
class Profile(models.Model):
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
|
||||
|
||||
display_name = models.CharField(max_length=150, null=False)
|
48
api_v1/profile/profile_services.py
Normal file
48
api_v1/profile/profile_services.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
from api_v1.profile.profile 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
|
|
@ -1,48 +0,0 @@
|
|||
from .models.user_information import UserInformation
|
||||
from django.contrib.auth.models import User
|
||||
from typing import Optional
|
||||
|
||||
|
||||
async def acreate_user_information(user: User, *, display_name: Optional[str] = None) -> UserInformation:
|
||||
"""Async. Create a UserInformation object for the given user.
|
||||
|
||||
@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.
|
||||
"""
|
||||
user_information = await UserInformation.objects.acreate(user=user, display_name=user.username)
|
||||
|
||||
# Use the given display name if provided.
|
||||
if display_name is not None:
|
||||
user_information.display_name = display_name
|
||||
await user_information.asave()
|
||||
|
||||
return user_information
|
||||
|
||||
|
||||
async def aget_user_information(user: User) -> UserInformation:
|
||||
"""Async. Get the UserInformation object for the given user.
|
||||
|
||||
@param user: The user to get the UserInformation object for.
|
||||
@return: The UserInformation object for the given user.
|
||||
"""
|
||||
return await UserInformation.objects.aget(user)
|
||||
|
||||
|
||||
async def aset_user_display_name(user: User, display_name: Optional[str] = None) -> UserInformation:
|
||||
"""Async. Set the display name for the given user.
|
||||
|
||||
@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.
|
||||
"""
|
||||
user_information = await UserInformation.objects.aget(user)
|
||||
|
||||
# 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
|
||||
|
||||
await user_information.asave()
|
||||
return user_information
|
Loading…
Reference in a new issue