Created basic User Information and user services
This commit is contained in:
parent
63cb2d7a8e
commit
f3daf83cb9
7 changed files with 76 additions and 5 deletions
|
@ -1,6 +1,6 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from .user.models import *
|
from .user.models import *
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
admin.site.register(UserInformation)
|
admin.site.register(UserInformation)
|
||||||
|
|
||||||
# Register your models here.
|
|
||||||
|
|
24
api_v1/migrations/0001_createBasicUserInformation.py
Normal file
24
api_v1/migrations/0001_createBasicUserInformation.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# Generated by Django 5.0.2 on 2024-02-28 12:44
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('auth', '0012_alter_user_first_name_max_length'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='UserInformation',
|
||||||
|
fields=[
|
||||||
|
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
|
||||||
|
('display_name', models.CharField(max_length=150)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,4 +1 @@
|
||||||
from django.db import models
|
|
||||||
from .user.models import *
|
from .user.models import *
|
||||||
|
|
||||||
# Create your models here.
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
from .models import *
|
||||||
|
from .user_services import *
|
|
@ -5,4 +5,4 @@ from django.db import models
|
||||||
class UserInformation(models.Model):
|
class UserInformation(models.Model):
|
||||||
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
|
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
|
||||||
|
|
||||||
display_name = models.CharField(max_length=150, null=True)
|
display_name = models.CharField(max_length=150, null=False)
|
||||||
|
|
48
api_v1/user/user_services.py
Normal file
48
api_v1/user/user_services.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
from .models import UserInformation
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
def create_user_information(user: User, *, display_name: Optional[str] = None) -> UserInformation:
|
||||||
|
"""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 = UserInformation.objects.create(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
|
||||||
|
user_information.save()
|
||||||
|
|
||||||
|
return user_information
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_information(user: User) -> UserInformation:
|
||||||
|
"""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 UserInformation.objects.get(user)
|
||||||
|
|
||||||
|
|
||||||
|
def set_user_display_name(user: User, display_name: Optional[str] = None) -> UserInformation:
|
||||||
|
"""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 = UserInformation.objects.get(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
|
||||||
|
|
||||||
|
user_information.save()
|
||||||
|
return user_information
|
Loading…
Reference in a new issue