diff --git a/profiles/admin.py b/profiles/admin.py index 8c38f3f..9bb175b 100644 --- a/profiles/admin.py +++ b/profiles/admin.py @@ -1,3 +1,6 @@ from django.contrib import admin +from .models import Profile + # Register your models here. +admin.site.register(Profile) \ No newline at end of file diff --git a/profiles/models.py b/profiles/models.py index 71a8362..045c11f 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -1,3 +1,16 @@ from django.db import models +from django.contrib.auth.models import User +from django.db.models.signals import post_save +from django.dispatch import receiver + # Create your models here. +class Profile(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.save() \ No newline at end of file diff --git a/profiles/status_code.py b/profiles/status_code.py new file mode 100644 index 0000000..187f78f --- /dev/null +++ b/profiles/status_code.py @@ -0,0 +1 @@ +PROFILE_NOT_FOUND = "Profile Not Found" \ No newline at end of file diff --git a/profiles/tests.py b/profiles/tests.py index 7ce503c..38085ea 100644 --- a/profiles/tests.py +++ b/profiles/tests.py @@ -1,3 +1,17 @@ from django.test import TestCase +from django.http import HttpResponse, HttpRequest +from django.contrib.auth.models import User # Create your tests here. +class ProfileTest(TestCase): + def setUp(self): + self.user: User = User.objects.create(username='bozo', password='password') + self.user.save() + self.expected_response = {"username": "bozo", + "title": ""} + + def test_profile_create_on_user_created(self): + response: HttpResponse = self.client.get(f"/api/profiles/{self.user.pk}") + response_dict: dict = eval(response.content) + self.assertEqual(self.expected_response, response_dict) + \ No newline at end of file diff --git a/profiles/urls.py b/profiles/urls.py index 6334a8f..d9975c3 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.ProfilePage.as_view(), name="profile_page"), ] \ No newline at end of file diff --git a/profiles/views.py b/profiles/views.py index 91ea44a..23d42ab 100644 --- a/profiles/views.py +++ b/profiles/views.py @@ -1,3 +1,21 @@ 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 .status_code import * +from .models import Profile # Create your views here. +class ProfilePage(View): + def get(self, request: HttpRequest, id: int): + + query: QuerySet = Profile.objects.filter(pk=id) + if (not query.exists()): + return HttpResponse(PROFILE_NOT_FOUND) + + profile: Profile = Profile.objects.get(pk=id) + + return JsonResponse({'username': profile.user.username, + 'title': profile.title}) \ No newline at end of file diff --git a/trancendence/urls.py b/trancendence/urls.py index 26c6912..6a8b20a 100644 --- a/trancendence/urls.py +++ b/trancendence/urls.py @@ -20,5 +20,5 @@ from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('api/accounts/', include('accounts.urls')), - path('api/profiles/', include('accounts.urls')), + path('api/profiles/', include('profiles.urls')), ]