diff --git a/profiles/admin.py b/profiles/admin.py index 9bb175b..124b2a0 100644 --- a/profiles/admin.py +++ b/profiles/admin.py @@ -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) \ No newline at end of file +admin.site.register(ProfileModel) \ No newline at end of file diff --git a/profiles/models.py b/profiles/models.py index 045c11f..f34eb29 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -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() \ No newline at end of file diff --git a/profiles/tests.py b/profiles/tests.py index 38085ea..b850238 100644 --- a/profiles/tests.py +++ b/profiles/tests.py @@ -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) \ No newline at end of file diff --git a/profiles/urls.py b/profiles/urls.py index d9975c3..9dc15a8 100644 --- a/profiles/urls.py +++ b/profiles/urls.py @@ -3,5 +3,5 @@ from django.urls import path from . import views urlpatterns = [ - path("", views.ProfilePage.as_view(), name="profile_page"), + path("", views.ProfileView.as_view(), name="profile_page"), ] \ No newline at end of file diff --git a/profiles/views.py b/profiles/views.py index 23d42ab..2a67b8e 100644 --- a/profiles/views.py +++ b/profiles/views.py @@ -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}) \ No newline at end of file