Skip to content

Commit

Permalink
Merge pull request #14 from andela/ft-user-profiles-159082310
Browse files Browse the repository at this point in the history
#159082310 Add profile update
  • Loading branch information
pluwum committed Aug 3, 2018
2 parents 945bd6e + 4d95f0d commit 445e9a9
Show file tree
Hide file tree
Showing 24 changed files with 423 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ omit =
*admin*
*articles/models*
*/migrations/*

*/venv/*


2 changes: 1 addition & 1 deletion authors/apps/authentication/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class UserAdmin(BaseUserAdmin):
list_display = ('email','is_staff')
list_filter = ('is_staff',)
fieldsets = (
(None, {'fields': ('username','email', 'password')}),
(None, {'fields': ('email', 'password','username','bio','image')}),
('Permissions', {'fields': ('is_staff',)}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
Expand Down
23 changes: 23 additions & 0 deletions authors/apps/authentication/migrations/0002_auto_20180731_0734.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.0.7 on 2018-07-31 07:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='user',
name='bio',
field=models.CharField(default=' ', max_length=255),
),
migrations.AddField(
model_name='user',
name='image',
field=models.CharField(default=' ', max_length=255),
),
]
23 changes: 23 additions & 0 deletions authors/apps/authentication/migrations/0003_auto_20180731_1252.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.0.7 on 2018-07-31 12:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0002_auto_20180731_0734'),
]

operations = [
migrations.AlterField(
model_name='user',
name='bio',
field=models.CharField(blank=True, max_length=255),
),
migrations.AlterField(
model_name='user',
name='image',
field=models.ImageField(upload_to=''),
),
]
18 changes: 18 additions & 0 deletions authors/apps/authentication/migrations/0004_auto_20180731_1657.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.7 on 2018-07-31 16:57

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0003_auto_20180731_1252'),
]

operations = [
migrations.AlterField(
model_name='user',
name='image',
field=models.ImageField(blank=True, upload_to='profile_image'),
),
]
24 changes: 24 additions & 0 deletions authors/apps/authentication/migrations/0005_auto_20180801_0714.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.0.7 on 2018-08-01 07:14

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('authentication', '0004_auto_20180731_1657'),
]

operations = [
migrations.AlterField(
model_name='user',
name='created_at',
field=models.DateTimeField(default=django.utils.timezone.now),
),
migrations.AlterField(
model_name='user',
name='updated_at',
field=models.DateTimeField(default=django.utils.timezone.now),
),
]
24 changes: 24 additions & 0 deletions authors/apps/authentication/migrations/0006_auto_20180801_0717.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.0.7 on 2018-08-01 07:17

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('authentication', '0005_auto_20180801_0714'),
]

operations = [
migrations.AlterField(
model_name='user',
name='created_at',
field=models.DateField(default=django.utils.timezone.now),
),
migrations.AlterField(
model_name='user',
name='updated_at',
field=models.DateField(default=django.utils.timezone.now),
),
]
23 changes: 23 additions & 0 deletions authors/apps/authentication/migrations/0007_auto_20180801_0730.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.0.7 on 2018-08-01 07:30

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0006_auto_20180801_0717'),
]

operations = [
migrations.AlterField(
model_name='user',
name='created_at',
field=models.DateTimeField(auto_now_add=True),
),
migrations.AlterField(
model_name='user',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
]
11 changes: 10 additions & 1 deletion authors/apps/authentication/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import jwt

from datetime import datetime, timedelta

from django.utils import timezone
from django.conf import settings
from django.contrib.auth.models import (
AbstractBaseUser, BaseUserManager, PermissionsMixin
Expand Down Expand Up @@ -60,6 +60,13 @@ def create_superuser(self, username, email, password):

return user

def update(self,email,bio):
user = User.objects.filter(email=email).update(bio=bio)
# user.bio = bio
# user.save()

return user


class User(AbstractBaseUser, PermissionsMixin):
# Each `User` needs a human-readable unique identifier that we can use to
Expand Down Expand Up @@ -93,6 +100,8 @@ class User(AbstractBaseUser, PermissionsMixin):
updated_at = models.DateTimeField(auto_now=True)

# More fields required by Django when specifying a custom user model.
bio = models.CharField(max_length=255,blank = True)
image = models.ImageField(upload_to='profile_image',blank = True)

# The `USERNAME_FIELD` property tells us which field we will use to log in.
# In this case, we want that to be the email field.
Expand Down
1 change: 1 addition & 0 deletions authors/apps/authentication/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ def test_user_not_recognised(self):
with self.assertRaises(Exception) as context:
self.auth_obj._authenticate_credentials(request, token)
self.assertIn('User not recognised!', str(context.exception))

55 changes: 31 additions & 24 deletions authors/apps/authentication/tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,52 @@ class LoginTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.user_to_login = {
'user': {
'username': 'rutale',
'email': 'rutale@gmail.com',
'password': 'rutaleivan#'
"user":{
"email": "rutale@gmail.com",
"password": "rutale1234*",
"username":"rutale"
}
}

self.headers = {
'HTTP_AUTHORIZATION': 'Token ' + self.make_token(self.user_to_login)
}


def make_token(self, user):
request = self.factory.post(
"/api/users/", data=json.dumps(self.user_to_login), content_type='application/json')
RegistrationAPIView.as_view()(request)
'/api/users/', data=json.dumps(user), content_type='application/json')
response = RegistrationAPIView.as_view()(request)
return response.data['token']

def test_normal_login(self):
request=self.factory.post(
"/api/users/login", data=json.dumps(self.user_to_login), content_type='application/json')
"/api/users/login", **self.headers, data=json.dumps(self.user_to_login), content_type='application/json')
response = LoginAPIView.as_view()(request)
self.assertEqual(response.status_code,200)

def test_login_wrong_email(self):
user = {
'user': {
'username': 'rutale',
'email': 'rut@gmail.com',
'password': 'rutaleivan#'
'password': 'rutale1234*'
}
}
request=self.factory.post(
"/api/users/login", data=json.dumps(user), content_type='application/json')
"/api/users/login", **self.headers, data=json.dumps(user), content_type='application/json')
response = LoginAPIView.as_view()(request)
self.assertIn('A user with this email and password was not found.',response.data["errors"]["error"][0])
self.assertEqual(response.status_code,400)

def test_login_wrong_password(self):
user = {
'user': {
'username': 'rutale',
'email': 'rutale@gmail.com',
'password': 'rutale'
'password': 'rutale123'
}
}
request=self.factory.post(
"/api/users/login", data=json.dumps(user), content_type='application/json')
"/api/users/login", **self.headers, data=json.dumps(user), content_type='application/json')
response = LoginAPIView.as_view()(request)
self.assertEqual(response.status_code,400)

Expand All @@ -54,11 +60,11 @@ def test_login_missing_email(self):
'user': {
'username': 'rutale',
'email': '',
'password': 'rutaleivan#'
'password': 'rutale1234*'
}
}
request=self.factory.post(
"/api/users/login", data=json.dumps(user), content_type='application/json')
"/api/users/login", **self.headers, data=json.dumps(user), content_type='application/json')
response = LoginAPIView.as_view()(request)
self.assertIn('This field may not be blank.',response.data["errors"]["email"][0])
self.assertEqual(response.status_code,400)
Expand All @@ -72,7 +78,7 @@ def test_login_missing_password(self):
}
}
request=self.factory.post(
"/api/users/login", data=json.dumps(user), content_type='application/json')
"/api/users/login", **self.headers, data=json.dumps(user), content_type='application/json')
response = LoginAPIView.as_view()(request)
self.assertIn('This field may not be blank.',response.data["errors"]["password"][0])
self.assertEqual(response.status_code,400)
Expand All @@ -82,11 +88,11 @@ def test_datastructure_user_error_missing_email_index(self):
'user': {
'username': 'rutale',
'': 'rut@gmail.com',
'password': ''
'password': 'rutale1234*'
}
}
request=self.factory.post(
"/api/users/login", data=json.dumps(user), content_type='application/json')
"/api/users/login", **self.headers, data=json.dumps(user), content_type='application/json')
response = LoginAPIView.as_view()(request)
self.assertIn('This field is required.',response.data["errors"]["email"][0])
self.assertEqual(response.status_code,400)
Expand All @@ -100,7 +106,7 @@ def test_datastructure_user_error_missing_password_index(self):
}
}
request=self.factory.post(
"/api/users/login", data=json.dumps(user), content_type='application/json')
"/api/users/login", **self.headers, data=json.dumps(user), content_type='application/json')
response = LoginAPIView.as_view()(request)
self.assertIn('This field is required.',response.data["errors"]["password"][0])
self.assertEqual(response.status_code,400)
Expand All @@ -113,7 +119,7 @@ def test_datastructure_user_error_missing_password_field(self):
}
}
request=self.factory.post(
"/api/users/login", data=json.dumps(user), content_type='application/json')
"/api/users/login", **self.headers, data=json.dumps(user), content_type='application/json')
response = LoginAPIView.as_view()(request)
self.assertIn('This field is required.',response.data["errors"]["password"][0])
self.assertEqual(response.status_code,400)
Expand All @@ -122,11 +128,11 @@ def test_datastructure_user_error_missing_email_field(self):
user = {
'user': {
'username': 'rutale',
'password': 'rutaleivan#'
'password': 'rutale1234*'
}
}
request=self.factory.post(
"/api/users/login", data=json.dumps(user), content_type='application/json')
"/api/users/login", **self.headers, data=json.dumps(user), content_type='application/json')
response = LoginAPIView.as_view()(request)
self.assertIn('This field is required.',response.data["errors"]["email"][0])
self.assertEqual(response.status_code,400)
Expand All @@ -135,11 +141,12 @@ def test_datastructure_user_error_missing_username_field(self):
user = {
'user': {
'email': 'rutale@gmail.com',
'password': 'rutaleivan#'
'password': 'rutale1234*'
}
}
request=self.factory.post(
"/api/users/login", data=json.dumps(user), content_type='application/json')
"/api/users/login", **self.headers, data=json.dumps(user), content_type='application/json')
response = LoginAPIView.as_view()(request)

self.assertEqual(response.status_code,200)

3 changes: 3 additions & 0 deletions authors/apps/profiles/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
Empty file.
3 changes: 3 additions & 0 deletions authors/apps/profiles/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
29 changes: 29 additions & 0 deletions authors/apps/profiles/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.contrib.auth import authenticate
from authors.apps.authentication.models import User
from rest_framework import serializers
from django.utils import timezone


class UpdateSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'

def update(self,instance,validated_data):
instance.username = validated_data.get('username',instance.username)
instance.bio = validated_data.get('bio', instance.bio)
instance.image = validated_data.get('file',instance.image)
instance.updated_at = timezone.now
instance.save()
return instance


class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'

def retrieve(self,instance):
return instance


Empty file.
Loading

0 comments on commit 445e9a9

Please sign in to comment.