profile: rename multiple var and change to use

django rest frameword
This commit is contained in:
starnakin 2023-11-12 00:03:08 +01:00
parent a7d9471d59
commit b9728dcb06
5 changed files with 22 additions and 23 deletions

View File

@ -1,6 +1,6 @@
from django.contrib import admin from django.contrib import admin
from .models import Profile from .models import ProfileModel
# Register your models here. # Register your models here.
admin.site.register(Profile) admin.site.register(ProfileModel)

View File

@ -5,12 +5,12 @@ from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
# Create your models here. # Create your models here.
class Profile(models.Model): class ProfileModel(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=40) title = models.CharField(max_length=40)
@receiver(post_save, sender=User) @receiver(post_save, sender=User)
def on_user_created(sender, instance, created, **kwargs): def on_user_created(sender, instance, created, **kwargs):
if created: if created:
profile: Profile = Profile.objects.create(pk = instance.pk, user = instance) profile: ProfileModel = ProfileModel.objects.create(pk = instance.pk, user = instance)
profile.save() profile.save()

View File

@ -7,11 +7,12 @@ class ProfileTest(TestCase):
def setUp(self): def setUp(self):
self.user: User = User.objects.create(username='bozo', password='password') self.user: User = User.objects.create(username='bozo', password='password')
self.user.save() self.user.save()
self.expected_response = {"username": "bozo", self.expected_response = {"name": "bozo",
"title": ""} "title": ""}
self.url = "/profiles/"
def test_profile_create_on_user_created(self): def test_profile_create_on_user_created(self):
response: HttpResponse = self.client.get(f"/api/profiles/{self.user.pk}") response: HttpResponse = self.client.get(self.url + str(self.user.pk))
response_dict: dict = eval(response.content) response_dict: dict = eval(response.content)
self.assertEqual(self.expected_response, response_dict) self.assertDictEqual(self.expected_response, response_dict)

View File

@ -3,5 +3,5 @@ from django.urls import path
from . import views from . import views
urlpatterns = [ urlpatterns = [
path("<int:id>", views.ProfilePage.as_view(), name="profile_page"), path("<int:pk>", views.ProfileView.as_view(), name="profile_page"),
] ]

View File

@ -1,21 +1,19 @@
from django.shortcuts import render from django.http import HttpRequest
from django.views import View from rest_framework.views import APIView
from django.http import HttpResponse, HttpRequest from rest_framework.response import Response
from django.contrib.auth.models import User from rest_framework import permissions, status
from django.http import JsonResponse
from .status_code import * from .models import ProfileModel
from .models import Profile
# Create your views here. # Create your views here.
class ProfilePage(View): class ProfileView(APIView):
def get(self, request: HttpRequest, id: int): permission_classes = (permissions.AllowAny,)
def get(self, request: HttpRequest, pk: int):
query: QuerySet = Profile.objects.filter(pk=id) profile: ProfileModel = ProfileModel.objects.get(pk=pk)
if (not query.exists()): if (profile is None):
return HttpResponse(PROFILE_NOT_FOUND) return Response(status=status.HTTP_404_NOT_FOUND)
profile: Profile = Profile.objects.get(pk=id) return Response(status=status.HTTP_200_OK, data={'name': profile.user.username,
return JsonResponse({'username': profile.user.username,
'title': profile.title}) 'title': profile.title})