diff --git a/opentech/apply/users/forms.py b/opentech/apply/users/forms.py new file mode 100644 index 0000000000000000000000000000000000000000..68cc0575f984fae35d179ade41f59b8b1061f9e9 --- /dev/null +++ b/opentech/apply/users/forms.py @@ -0,0 +1,20 @@ +from django import forms + +from wagtail.wagtailusers.forms import UserEditForm, UserCreationForm + + +class CustomUserAdminFormBase(): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # HACK: Wagtail admin doesn't work with custom User models that do not have first/last name. + self.fields['first_name'].widget = forms.HiddenInput(attrs={'value': f"Not used - see full_name"}) + self.fields['last_name'].widget = forms.HiddenInput(attrs={'value': f"Not used - see full_name"}) + + +class CustomUserEditForm(CustomUserAdminFormBase, UserEditForm): + pass + + +class CustomUserCreationForm(CustomUserAdminFormBase, UserCreationForm): + pass diff --git a/opentech/apply/users/migrations/0004_drop_first_last_names.py b/opentech/apply/users/migrations/0004_drop_first_last_names.py new file mode 100644 index 0000000000000000000000000000000000000000..19a541f53a5bc945fc4b06d3c243549481e9abb2 --- /dev/null +++ b/opentech/apply/users/migrations/0004_drop_first_last_names.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.8 on 2018-02-28 11:04 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0003_make_email_username'), + ] + + operations = [ + migrations.RemoveField( + model_name='user', + name='first_name', + ), + migrations.RemoveField( + model_name='user', + name='last_name', + ), + migrations.AddField( + model_name='user', + name='full_name', + field=models.CharField(blank=True, max_length=255, verbose_name='Full name'), + ), + ] diff --git a/opentech/apply/users/models.py b/opentech/apply/users/models.py index 26a0c2c355a20b12d08cdc30126ebbd9073ac29c..9e867ab9b60c9d430a1e0f79825514b20d043249 100644 --- a/opentech/apply/users/models.py +++ b/opentech/apply/users/models.py @@ -5,19 +5,6 @@ from django.utils.translation import gettext_lazy as _ from .utils import send_activation_email -def convert_full_name_to_parts(defaults): - full_name = defaults.pop('full_name', ' ') - if not full_name: - # full_name was None - full_name = ' ' - first_name, *last_name = full_name.split(' ') - if first_name: - defaults.update(first_name=first_name) - if last_name: - defaults.update(last_name=' '.join(last_name)) - return defaults - - class UserManager(BaseUserManager): use_in_migrations = True @@ -28,7 +15,6 @@ class UserManager(BaseUserManager): if not email: raise ValueError('The given email must be set') email = self.normalize_email(email) - extra_fields = convert_full_name_to_parts(extra_fields) user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) @@ -50,11 +36,6 @@ class UserManager(BaseUserManager): return self._create_user(email, password, **extra_fields) - def get_or_create(self, defaults, **kwargs): - # Allow passing of 'full_name' but replace it with actual database fields - defaults = convert_full_name_to_parts(defaults) - return super().get_or_create(defaults=defaults, **kwargs) - def get_or_create_and_notify(self, defaults=dict(), site=None, **kwargs): defaults.update(is_active=False) user, created = self.get_or_create(defaults=defaults, **kwargs) @@ -64,14 +45,24 @@ class UserManager(BaseUserManager): class User(AbstractUser): - USERNAME_FIELD = 'email' email = models.EmailField(_('email address'), unique=True) + full_name = models.CharField(verbose_name='Full name', max_length=255, blank=True) + + USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] - # Remove the username field which is no longer used + # Remove the username/first/last name field which is no longer used. username = None + first_name = None + last_name = None objects = UserManager() def __str__(self): return self.get_full_name() + + def get_full_name(self): + return self.full_name.strip() + + def get_short_name(self): + return self.email diff --git a/opentech/apply/users/templates/wagtailusers/users/create.html b/opentech/apply/users/templates/wagtailusers/users/create.html new file mode 100644 index 0000000000000000000000000000000000000000..8d546b4a2d56e2ef170f66d7e4675713868a2628 --- /dev/null +++ b/opentech/apply/users/templates/wagtailusers/users/create.html @@ -0,0 +1,26 @@ +{% extends "wagtailusers/users/create.html" %} + +{% block fields %} + {% if form.separate_username_field %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.username_field %} + {% endif %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.email %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.full_name %} + + {% comment %} + First/last name hidden input with dummy values because.. Wagtail admin + See opentech.apply.users.forms.CustomUserCreationForm + {% endcomment %} + {{ form.first_name }} + {{ form.last_name }} + + {% if form.password1 %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.password1 %} + {% endif %} + {% if form.password2 %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.password2 %} + {% endif %} + {% if form.is_active %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.is_active %} + {% endif %} +{% endblock fields %} diff --git a/opentech/apply/users/templates/wagtailusers/users/edit.html b/opentech/apply/users/templates/wagtailusers/users/edit.html new file mode 100644 index 0000000000000000000000000000000000000000..8eb3ddb49673db595cff97275eec9e7dcce1a95b --- /dev/null +++ b/opentech/apply/users/templates/wagtailusers/users/edit.html @@ -0,0 +1,26 @@ +{% extends "wagtailusers/users/edit.html" %} + +{% block fields %} + {% if form.separate_username_field %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.username_field %} + {% endif %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.email %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.full_name %} + + {% comment %} + First/last name hidden input with dummy values because.. Wagtail admin + See opentech.apply.users.forms.CustomUserEditForm + {% endcomment %} + {{ form.first_name }} + {{ form.last_name }} + + {% if form.password1 %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.password1 %} + {% endif %} + {% if form.password2 %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.password2 %} + {% endif %} + {% if form.is_active %} + {% include "wagtailadmin/shared/field_as_li.html" with field=form.is_active %} + {% endif %} +{% endblock fields %} diff --git a/opentech/settings/base.py b/opentech/settings/base.py index afcbf80bcd413edbb96719cf3b61a26506721e35..3e9b3885934f75879296b689ce3bfd3dd62a1b74 100644 --- a/opentech/settings/base.py +++ b/opentech/settings/base.py @@ -206,6 +206,10 @@ MEDIA_URL = '/media/' AUTH_USER_MODEL = 'users.User' +WAGTAIL_USER_EDIT_FORM = 'opentech.apply.users.forms.CustomUserEditForm' +WAGTAIL_USER_CREATION_FORM = 'opentech.apply.users.forms.CustomUserCreationForm' +WAGTAIL_USER_CUSTOM_FIELDS = ['full_name'] + LOGIN_URL = 'users:login' LOGIN_REDIRECT_URL = 'dashboard:dashboard'