Compare commits

..

2 Commits

Author SHA1 Message Date
54cc1b1705 profiles: init 2023-10-29 21:09:38 +01:00
246213c5b8 core: add anotation type 2023-10-29 21:00:18 +01:00
8 changed files with 52 additions and 3 deletions

View File

@ -23,7 +23,7 @@ class RegisterView(View):
if User.objects.filter(username=username).exists():
return HttpResponse(USERNAME_ALREADY_USED)
user = User.objects.create_user(username, password=password)
user: User = User.objects.create_user(username, password=password)
user.save()
return HttpResponse(USER_ADDED)

View File

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

View File

@ -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()

1
profiles/status_code.py Normal file
View File

@ -0,0 +1 @@
PROFILE_NOT_FOUND = "Profile Not Found"

View File

@ -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)

View File

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

View File

@ -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})

View File

@ -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')),
]