diff --git a/api_v1/admin.py b/api_v1/admin.py index 5fe1ec9..c818c3f 100644 --- a/api_v1/admin.py +++ b/api_v1/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin from .user.models import * +# Register your models here. admin.site.register(UserInformation) -# Register your models here. diff --git a/api_v1/migrations/0001_createBasicUserInformation.py b/api_v1/migrations/0001_createBasicUserInformation.py new file mode 100644 index 0000000..56a2538 --- /dev/null +++ b/api_v1/migrations/0001_createBasicUserInformation.py @@ -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)), + ], + ), + ] diff --git a/api_v1/models.py b/api_v1/models.py index 15377ca..152f52c 100644 --- a/api_v1/models.py +++ b/api_v1/models.py @@ -1,4 +1 @@ -from django.db import models from .user.models import * - -# Create your models here. diff --git a/api_v1/user/__init__.py b/api_v1/user/__init__.py index e69de29..1e010ce 100644 --- a/api_v1/user/__init__.py +++ b/api_v1/user/__init__.py @@ -0,0 +1,2 @@ +from .models import * +from .user_services import * \ No newline at end of file diff --git a/api_v1/user/models/user_information.py b/api_v1/user/models/user_information.py index a5816f9..0dbfc5a 100644 --- a/api_v1/user/models/user_information.py +++ b/api_v1/user/models/user_information.py @@ -5,4 +5,4 @@ from django.db import models class UserInformation(models.Model): 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) diff --git a/api_v1/user/models/user_services.py b/api_v1/user/models/user_services.py deleted file mode 100644 index e69de29..0000000 diff --git a/api_v1/user/user_services.py b/api_v1/user/user_services.py new file mode 100644 index 0000000..22316c8 --- /dev/null +++ b/api_v1/user/user_services.py @@ -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