-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- GET user details - GET all user profiles - GET specific user profile - Add unit tests for feature above - Add .DS_Store to gitignore [starts #161967022]
- Loading branch information
1 parent
9ff6fff
commit c30fc49
Showing
19 changed files
with
292 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DATABASE_URL=postgres://emmanuelbeja:12345678@localhost/ahjumanji | ||
DEBUG=on |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.test import TestCase | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
from ..models import User | ||
|
||
|
||
def register_user( | ||
username="Thanos", | ||
email="thanos@avengers.com", | ||
password="avengersassemble"): | ||
"""Creating a test user""" | ||
user = User.objects.create_user(username, email, password) | ||
return user | ||
|
||
|
||
class TestProfile(TestCase): | ||
"""Test profile.""" | ||
|
||
def setUp(self): | ||
"""Test Setup.""" | ||
self.client = APIClient() | ||
self.login_credentials = { | ||
'user': { | ||
'email': "thanos@avengers.com", | ||
'password': "avengersassemble" | ||
} | ||
} | ||
|
||
def login_user(self): | ||
"""Login A user""" | ||
register_user() | ||
response = self.client.post( | ||
'/api/users/login', | ||
data=self.login_credentials, | ||
format='json', | ||
) | ||
return response.data['token'] | ||
|
||
def test_get_all_users(self): | ||
"""Test get all authors""" | ||
token = self.login_user() | ||
response = self.client.get( | ||
'/api/users/', | ||
HTTP_AUTHORIZATION='Token ' + token | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
from django.urls import path | ||
|
||
from .views import ( | ||
LoginAPIView, RegistrationAPIView, UserRetrieveUpdateAPIView | ||
) | ||
LoginAPIView, | ||
RegistrationAPIView, | ||
UserRetrieveUpdateAPIView, | ||
ListUsersAPIView) | ||
|
||
urlpatterns = [ | ||
path('user/', UserRetrieveUpdateAPIView.as_view(), name='user-detail'), | ||
path('users/', ListUsersAPIView.as_view(), name='users-List'), | ||
path('users/register', RegistrationAPIView.as_view(), name='register'), | ||
path('users/login', LoginAPIView.as_view(), name='login'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. | ||
from .models import Profile | ||
|
||
admin.site.register(Profile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,61 @@ | ||
from django.db import models | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
|
||
# Local imports | ||
from ..authentication.models import User | ||
|
||
# Create your models here. | ||
|
||
|
||
class Profile(models.Model): | ||
""" | ||
Models a user's profile. | ||
Is an extension of app's User model | ||
""" | ||
# Link profile to a User thro one-to-one rlship | ||
user = models.OneToOneField( | ||
User, on_delete=models.CASCADE, primary_key=True) | ||
|
||
# Add fields onto `Profile` model | ||
username = models.CharField(max_length=50, blank=True) | ||
email = models.EmailField(blank=True) | ||
full_name = models.CharField(max_length=50, blank=True) | ||
bio = models.CharField(max_length=200, blank=True) | ||
image = models.URLField(blank=True) | ||
country = models.CharField(max_length=3, blank=True) | ||
phone_number = models.CharField(max_length=15, blank=True) | ||
|
||
def __str__(self): | ||
""" | ||
Schema for representation of a Profile object in Terminal | ||
""" | ||
return self.email | ||
|
||
def update_profile(self, **kwargs): | ||
""" | ||
Update profile with any **kwarg provided, if **kwarg | ||
is in list of fields on Profile model | ||
""" | ||
|
||
|
||
# Recieve a signal whenever a User is created | ||
# and initialize a Profile tied to the User | ||
@receiver(post_save, sender=User) | ||
def init_profile(sender, instance, created, **kwargs): | ||
""" | ||
Initialize a `Profile` whenever an | ||
instance of a `User` is initialized | ||
""" | ||
if created: | ||
Profile.objects.create( | ||
user=instance, username=instance.username, email=instance.email) | ||
|
||
|
||
@receiver(post_save, sender=User) | ||
def save_profile(sender, instance, **kwargs): | ||
""" | ||
Save the profile initialized above | ||
if User is saved | ||
""" | ||
instance.profile.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from rest_framework import serializers | ||
from .models import Profile | ||
|
||
|
||
class ProfileSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Profile | ||
fields = ("__all__") |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.