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 .models import Profile
from .models import ProfileModel
# 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
# Create your models here.
class Profile(models.Model):
class ProfileModel(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=40)
@receiver(post_save, sender=User)
def on_user_created(sender, instance, created, **kwargs):
if created:
profile: Profile = Profile.objects.create(pk = instance.pk, user = instance)
profile: ProfileModel = ProfileModel.objects.create(pk = instance.pk, user = instance)
profile.save()

View File

@ -7,11 +7,12 @@ class ProfileTest(TestCase):
def setUp(self):
self.user: User = User.objects.create(username='bozo', password='password')
self.user.save()
self.expected_response = {"username": "bozo",
self.expected_response = {"name": "bozo",
"title": ""}
self.url = "/profiles/"
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)
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
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.views import View
from django.http import HttpResponse, HttpRequest
from django.contrib.auth.models import User
from django.http import JsonResponse
from django.http import HttpRequest
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions, status
from .status_code import *
from .models import Profile
from .models import ProfileModel
# Create your views here.
class ProfilePage(View):
def get(self, request: HttpRequest, id: int):
class ProfileView(APIView):
permission_classes = (permissions.AllowAny,)
def get(self, request: HttpRequest, pk: int):
query: QuerySet = Profile.objects.filter(pk=id)
if (not query.exists()):
return HttpResponse(PROFILE_NOT_FOUND)
profile: ProfileModel = ProfileModel.objects.get(pk=pk)
if (profile is None):
return Response(status=status.HTTP_404_NOT_FOUND)
profile: Profile = Profile.objects.get(pk=id)
return JsonResponse({'username': profile.user.username,
return Response(status=status.HTTP_200_OK, data={'name': profile.user.username,
'title': profile.title})