docker setup
This commit is contained in:
0
srcs/profiles/__init__.py
Normal file
0
srcs/profiles/__init__.py
Normal file
6
srcs/profiles/admin.py
Normal file
6
srcs/profiles/admin.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import ProfileModel
|
||||
|
||||
# Register your models here.
|
||||
admin.site.register(ProfileModel)
|
6
srcs/profiles/apps.py
Normal file
6
srcs/profiles/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ProfilesConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'profiles'
|
16
srcs/profiles/models.py
Normal file
16
srcs/profiles/models.py
Normal file
@ -0,0 +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 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: ProfileModel = ProfileModel.objects.create(pk = instance.pk, user = instance)
|
||||
profile.save()
|
1
srcs/profiles/status_code.py
Normal file
1
srcs/profiles/status_code.py
Normal file
@ -0,0 +1 @@
|
||||
PROFILE_NOT_FOUND = "Profile Not Found"
|
18
srcs/profiles/tests.py
Normal file
18
srcs/profiles/tests.py
Normal file
@ -0,0 +1,18 @@
|
||||
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 = {"name": "bozo",
|
||||
"title": ""}
|
||||
self.url = "/profiles/"
|
||||
|
||||
def test_profile_create_on_user_created(self):
|
||||
response: HttpResponse = self.client.get(self.url + str(self.user.pk))
|
||||
response_dict: dict = eval(response.content)
|
||||
self.assertDictEqual(self.expected_response, response_dict)
|
||||
|
7
srcs/profiles/urls.py
Normal file
7
srcs/profiles/urls.py
Normal file
@ -0,0 +1,7 @@
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path("<int:pk>", views.ProfileView.as_view(), name="profile_page"),
|
||||
]
|
19
srcs/profiles/views.py
Normal file
19
srcs/profiles/views.py
Normal file
@ -0,0 +1,19 @@
|
||||
from django.http import HttpRequest
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import permissions, status
|
||||
|
||||
from .models import ProfileModel
|
||||
|
||||
# Create your views here.
|
||||
class ProfileView(APIView):
|
||||
permission_classes = (permissions.AllowAny,)
|
||||
|
||||
def get(self, request: HttpRequest, pk: int):
|
||||
|
||||
profile: ProfileModel = ProfileModel.objects.get(pk=pk)
|
||||
if (profile is None):
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
return Response(status=status.HTTP_200_OK, data={'name': profile.user.username,
|
||||
'title': profile.title})
|
Reference in New Issue
Block a user