Unverified Commit 03e9ec2a authored by Thom Wiggers's avatar Thom Wiggers 📐

Customize user creation form, send email

parent 69af5501
"""
This module registers admin pages for the models
"""
import datetime
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm as BaseUserCreationForm
from django.utils.translation import ugettext_lazy as _
import datetime
from . import models
from . import models, forms
class MembershipInline(admin.StackedInline):
model = models.Membership
extra = 0
extra = 1
class MemberInline(admin.StackedInline):
......@@ -86,7 +85,7 @@ class UserCreationForm(BaseUserCreationForm):
class UserAdmin(BaseUserAdmin):
add_form = UserCreationForm
add_form = forms.UserCreationForm
inlines = (MemberInline, MembershipInline)
# FIXME include proper filter for expiration
......@@ -99,7 +98,7 @@ class UserAdmin(BaseUserAdmin):
(None, {
'classes': ('wide',),
'fields': ('first_name', 'last_name', 'username', 'email',
'password1', 'password2'),
'send_welcome_email')
}),
)
......
from __future__ import unicode_literals
from django.forms import ModelForm
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from .models import Member
class MemberForm(ModelForm):
class MemberForm(forms.ModelForm):
class Meta:
fields = ['address_street', 'address_street2',
'address_postal_code', 'address_city', 'phone_number',
......@@ -14,3 +17,37 @@ class MemberForm(ModelForm):
'display_name_preference', 'photo', 'language',
'receive_optin', 'receive_newsletter']
model = Member
class UserCreationForm(forms.ModelForm):
# Don't forget to edit the formset in admin.py!
# This is a stupid quirk of the user admin.
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in ('email', 'first_name', 'last_name'):
self.fields[field].required = True
send_welcome_email = forms.BooleanField(
label=_('Send welcome email'),
help_text=_('This email will include the generated password'),
initial=True)
def save(self, commit=True):
password = User.objects.make_random_password(length=15)
user = super().save(commit=False)
user.set_password(password)
if commit:
user.save()
if self.cleaned_data['send_welcome_email']:
email_body = "new password: {}".format(password)
user.email_user(
_('Welkom bij Studievereniging Thalia'),
email_body)
return user
class Meta:
fields = ('username',
'first_name',
'last_name',
'send_welcome_email')
......@@ -112,6 +112,9 @@ LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'
# Email settings
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# Password validation
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment