added support for user profiles

This commit is contained in:
Edward Tirado Jr 2025-07-08 00:39:58 -05:00
parent eeb74027eb
commit 1f314f1c20
8 changed files with 137 additions and 32 deletions

View file

@ -0,0 +1,25 @@
# Generated by Django 5.2.2 on 2025-07-08 02:37
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='UserProfile',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, max_length=100, null=True)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View file

@ -1,3 +1,12 @@
from django.contrib.auth.models import User
from django.db import models
# Create your models here.
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=100, null=True, blank=True)
@property
def lists(self):
return self.user.movielist_set.all()

6
users/permissions.py Normal file
View file

@ -0,0 +1,6 @@
from rest_framework import permissions
from rest_framework.permissions import SAFE_METHODS
class ReadOnly(permissions.BasePermission):
def has_permission(self, request, view):
return request.method in SAFE_METHODS

View file

@ -1,12 +1,35 @@
from django.contrib.auth import authenticate
from rest_framework import serializers
from django.contrib.auth.models import User, Group
from rest_framework import serializers
from movie_manager.serializers import MovieListSerializer
from users.models import UserProfile
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ["url", "username", "email", "password", "groups"]
fields = ["url", "username", "email", "groups"]
class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
name = serializers.SerializerMethodField()
username = serializers.SerializerMethodField()
date_joined = serializers.SerializerMethodField()
lists = MovieListSerializer(many=True, read_only=True)
class Meta:
model = UserProfile
fields = ["name", "username", "date_joined", "lists"]
def get_name(self, obj):
return obj.name or ""
def get_username(self, obj):
return obj.user.username
def get_date_joined(self, obj):
return obj.user.date_joined
class GroupSerializer(serializers.HyperlinkedModelSerializer):

View file

@ -1,10 +1,13 @@
from django.contrib.auth.models import User, Group
from django.contrib.auth.models import User
from django.http import JsonResponse
from knox.auth import TokenAuthentication
from rest_framework import viewsets, permissions, status
from rest_framework.decorators import api_view
from rest_framework.decorators import api_view, action
from rest_framework.response import Response
from users.serializers import UserSerializer, GroupSerializer
from users.models import UserProfile
from users.permissions import ReadOnly
from users.serializers import UserSerializer, UserProfileSerializer
class UserViewSet(viewsets.ModelViewSet):
@ -15,6 +18,51 @@ class UserViewSet(viewsets.ModelViewSet):
serializer_class = UserSerializer
class UserProfileViewSet(viewsets.ModelViewSet):
authentication_classes = [TokenAuthentication]
permission_classes = [permissions.IsAuthenticated | ReadOnly]
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
lookup_field = "user__username"
@action(detail=False)
def current_user_profile(self, request, *args, **kwargs):
try:
user = request.user
except User.DoesNotExist:
return Response([], status=status.HTTP_404_NOT_FOUND)
try:
user_profile = UserProfile.objects.get(user=user)
except UserProfile.DoesNotExist:
user_profile = UserProfile(
user=user,
)
user_profile.save()
return JsonResponse(UserProfileSerializer(user_profile).data)
def retrieve(self, request, pk=None, *args, **kwargs):
try:
username = kwargs.get('user__username')
user = User.objects.get(username=username)
except User.DoesNotExist:
return Response([], status=status.HTTP_404_NOT_FOUND)
try:
user_profile = UserProfile.objects.get(user=user)
except UserProfile.DoesNotExist:
user_profile = UserProfile(
user=user,
)
user_profile.save()
return JsonResponse(UserProfileSerializer(user_profile).data)
@api_view(["POST"])
def register(request):
user_data = UserSerializer(data=request.data)