profiles: init
This commit is contained in:
parent
246213c5b8
commit
54cc1b1705
@ -1,3 +1,6 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
|
from .models import Profile
|
||||||
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
|
admin.site.register(Profile)
|
@ -1,3 +1,16 @@
|
|||||||
from django.db import models
|
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.
|
# 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()
|
1
profiles/status_code.py
Normal file
1
profiles/status_code.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
PROFILE_NOT_FOUND = "Profile Not Found"
|
@ -1,3 +1,17 @@
|
|||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
from django.http import HttpResponse, HttpRequest
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
# Create your tests here.
|
# 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)
|
||||||
|
|
@ -3,5 +3,5 @@ from django.urls import path
|
|||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("<str:uuid>", views.ProfilePage.as_view(), name="profile_page"),
|
path("<int:id>", views.ProfilePage.as_view(), name="profile_page"),
|
||||||
]
|
]
|
@ -1,3 +1,21 @@
|
|||||||
from django.shortcuts import render
|
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.
|
# 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})
|
@ -20,5 +20,5 @@ from django.urls import path, include
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
path('api/accounts/', include('accounts.urls')),
|
path('api/accounts/', include('accounts.urls')),
|
||||||
path('api/profiles/', include('accounts.urls')),
|
path('api/profiles/', include('profiles.urls')),
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user