Skip to content

Commit

Permalink
Merge pull request #178 from Cadasta/bugs/user-model
Browse files Browse the repository at this point in the history
Replace first_name/last_name with full_name
  • Loading branch information
ian-ross committed May 5, 2016
2 parents 9f453af + 2e563ce commit f0484be
Show file tree
Hide file tree
Showing 27 changed files with 133 additions and 172 deletions.
4 changes: 2 additions & 2 deletions cadasta/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RegisterForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2',
'first_name', 'last_name']
'full_name']

def clean_password1(self):
password = self.data.get('password1')
Expand All @@ -39,7 +39,7 @@ def save(self, *args, **kwargs):
class ProfileForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'email', 'first_name', 'last_name']
fields = ['username', 'email', 'full_name']

def clean_username(self):
username = self.data.get('username')
Expand Down
8 changes: 3 additions & 5 deletions cadasta/accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2016-04-13 12:02
# Generated by Django 1.9.4 on 2016-05-04 06:50
from __future__ import unicode_literals

import accounts.manager
Expand All @@ -26,21 +26,19 @@ class Migration(migrations.Migration):
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=30, unique=True, validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$', 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.')], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=30, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('full_name', models.CharField(blank=True, max_length=130, verbose_name='full name')),
('email_verified', models.BooleanField(default=False)),
('verify_email_by', models.DateTimeField(default=accounts.models.now_plus_48_hours)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name_plural': 'users',
'verbose_name': 'user',
'abstract': False,
'verbose_name_plural': 'users',
},
managers=[
('objects', accounts.manager.UserManager()),
Expand Down
32 changes: 29 additions & 3 deletions cadasta/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from django.db import models
from django.dispatch import receiver
from django.utils.translation import ugettext as _
from django.contrib.auth.models import AbstractUser
import django.contrib.auth.models as auth
import django.contrib.auth.base_user as auth_base
from tutelary.models import Policy
from tutelary.decorators import permissioned_model

Expand All @@ -17,12 +18,31 @@ def now_plus_48_hours():
return datetime.now(tz=timezone.utc) + timedelta(hours=48)


def abstract_user_field(name):
for f in auth.AbstractUser._meta.fields:
if f.name == name:
return f


@permissioned_model
class User(AbstractUser):
class User(auth_base.AbstractBaseUser, auth.PermissionsMixin):
username = abstract_user_field('username')
full_name = models.CharField(_('full name'), max_length=130, blank=True)
email = abstract_user_field('email')
is_staff = abstract_user_field('is_staff')
is_active = abstract_user_field('is_active')
date_joined = abstract_user_field('date_joined')
email_verified = models.BooleanField(default=False)
verify_email_by = models.DateTimeField(default=now_plus_48_hours)

REQUIRED_FIELDS = ['email', 'first_name', 'last_name']
objects = UserManager()

USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email', 'full_name']

class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')

objects = UserManager()

Expand All @@ -37,6 +57,12 @@ class TutelaryMeta:
{'error_message':
_("You don't have permission to update user details")})]

def get_full_name(self):
"""
Returns the full_name.
"""
return self.full_name


@receiver(models.signals.post_save, sender=User)
def assign_default_policy(sender, instance, **kwargs):
Expand Down
6 changes: 2 additions & 4 deletions cadasta/accounts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class Meta:
model = User
fields = (
'username',
'first_name',
'last_name',
'full_name',
'email',
'password',
'email_verified',
Expand All @@ -48,8 +47,7 @@ class Meta:
model = User
fields = (
'username',
'first_name',
'last_name',
'full_name',
'email',
'email_verified',
'last_login',
Expand Down
21 changes: 7 additions & 14 deletions cadasta/accounts/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def test_valid_data(self):
'email': '[email protected]',
'password1': 'iloveyoko79',
'password2': 'iloveyoko79',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}
form = RegisterForm(data)
form.save()
Expand All @@ -32,8 +31,7 @@ def test_passwords_do_not_match(self):
'email': '[email protected]',
'password1': 'iloveyoko79',
'password2': 'iloveyoko68',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}
form = RegisterForm(data)

Expand All @@ -48,8 +46,7 @@ def test_signup_with_existing_email(self):
'email': '[email protected]',
'password1': 'iloveyoko79',
'password2': 'iloveyoko79',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}
form = RegisterForm(data)

Expand All @@ -66,15 +63,13 @@ def test_update_user(self):
data = {
'username': 'imagine71',
'email': '[email protected]',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}
form = ProfileForm(data, instance=user)
form.save()

user.refresh_from_db()
assert user.first_name == 'John'
assert user.last_name == 'Lennon'
assert user.full_name == 'John Lennon'

def test_update_user_with_existing_username(self):
UserFactory.create(username='existing')
Expand All @@ -83,8 +78,7 @@ def test_update_user_with_existing_username(self):
data = {
'username': 'existing',
'email': '[email protected]',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}
form = ProfileForm(data, instance=user)
assert form.is_valid() is False
Expand All @@ -96,8 +90,7 @@ def test_update_user_with_existing_email(self):
data = {
'username': 'imagine71',
'email': '[email protected]',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}
form = ProfileForm(data, instance=user)
assert form.is_valid() is False
12 changes: 4 additions & 8 deletions cadasta/accounts/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
'username': 'imagine71',
'email': '[email protected]',
'password': 'iloveyoko79',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}


Expand Down Expand Up @@ -49,8 +48,7 @@ def test_create_without_email(self):
'username': 'imagine71',
'password': 'iloveyoko79',
'password_repeat': 'iloveyoko79',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}

serializer = RegistrationSerializer(data=data)
Expand All @@ -67,8 +65,7 @@ def test_create_with_existing_email(self):
'email': '[email protected]',
'password': 'iloveyoko79',
'password_repeat': 'iloveyoko79',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}

serializer = RegistrationSerializer(data=data)
Expand All @@ -89,8 +86,7 @@ def test_create_with_valid_data(self):
'username': 'imagine71',
'email': '[email protected]',
'password': 'iloveyoko79',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
'last_login': '2016-01-01 23:00:00'
}
serializer = UserSerializer(data=data)
Expand Down
6 changes: 2 additions & 4 deletions cadasta/accounts/tests/test_views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ def test_user_signs_up(self):
'username': 'imagine71',
'email': '[email protected]',
'password': 'iloveyoko79',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}
self._post(data, status=201, count=1)

Expand All @@ -95,8 +94,7 @@ def test_user_signs_up_with_invalid(self):
data = {
'username': 'imagine71',
'password': 'iloveyoko79',
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
}
self._post(data, status=400, count=0)

Expand Down
12 changes: 4 additions & 8 deletions cadasta/accounts/tests/test_views_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ def test_update_profile(self):
setattr(self.request, 'POST', {
'username': 'John',
'email': user.email,
'first_name': 'John',
'last_name': 'Lennon',
'full_name': 'John Lennon',
})

self.view(self.request)

user.refresh_from_db()
assert user.first_name == 'John'
assert user.last_name == 'Lennon'
assert user.full_name == 'John Lennon'

def test_get_profile_when_no_user_is_signed_in(self):
response = self.view(self.request)
Expand All @@ -70,16 +68,14 @@ def test_update_profile_when_no_user_is_signed_in(self):

def test_update_profile_duplicate_email(self):
user1 = UserFactory.create(username='John',
first_name='John',
last_name='Lennon')
full_name='John Lennon')
user2 = UserFactory.create(username='Bill')
setattr(self.request, 'user', user2)
setattr(self.request, 'method', 'POST')
setattr(self.request, 'POST', {
'username': 'Bill',
'email': user1.email,
'first_name': 'Bill',
'last_name': 'Bloggs',
'full_name': 'Bill Bloggs',
})

self.view(self.request)
Expand Down
14 changes: 5 additions & 9 deletions cadasta/core/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os.path
import factory
from faker import Factory
from django.contrib.gis.geos import GEOSGeometry
from django.conf import settings
Expand All @@ -20,20 +19,18 @@ def add_test_users(self):
# the first two named users will have superuser access
named_users = [
{'username': 'iross', 'email': '[email protected]',
'first_name': 'Ian', 'last_name': 'Ross'},
'full_name': 'Ian Ross'},
{'username': 'oroick', 'email': '[email protected]',
'first_name': 'Oliver', 'last_name': 'Roick'}]
'full_name': 'Oliver Roick'}]
# add user's with names in languages that need to be tested.
languages = ['el_GR', 'ja_JP', 'hi_IN', 'hr_HR', 'lt_LT']
named_users.append({
'first_name': 'עזרא',
'last_name': 'ברש'
'full_name': 'עזרא ברש'
})
for lang in languages:
fake = Factory.create(lang)
named_users.append({
'first_name': fake.first_name(),
'last_name': fake.last_name()
'full_name': fake.name()
})
for n in range(20):
if n < len(named_users):
Expand All @@ -48,8 +45,7 @@ def add_test_users(self):
users.append(UserFactory.create(
password='password',
is_active=(n < 8),
first_name=factory.Faker('first_name'),
last_name=factory.Faker('last_name'),
full_name=fake.name(),
))
print('Successfully added test users.')
return users
Expand Down
Loading

0 comments on commit f0484be

Please sign in to comment.