Commit d011b43a authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg

Merge branch '23-1-signup-form' into 'master'

Administratietrucjes: part 1: create user form.

Relates to #23 part 1

See merge request !99
parents 69af5501 a76b36a5
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):
......@@ -68,11 +68,11 @@ class AgeListFilter(admin.SimpleListFilter):
if self.value() == 'unknown':
elif (user.member.birthday <= eightteen_years_ago
and self.value() == '18+'):
elif (user.member.birthday <= eightteen_years_ago and
self.value() == '18+'):
elif (user.member.birthday > eightteen_years_ago
and self.value() == '18-'):
elif (user.member.birthday > eightteen_years_ago and
self.value() == '18-'):
except models.Member.DoesNotExist:
# The superuser does not have a .member object attached.
......@@ -86,7 +86,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 +99,7 @@ class UserAdmin(BaseUserAdmin):
(None, {
'classes': ('wide',),
'fields': ('first_name', 'last_name', 'username', 'email',
'password1', 'password2'),
from __future__ import unicode_literals
from django.forms import ModelForm
from django import forms
from django.contrib.auth.models import User
from django.template import loader
from django.utils import translation
from django.utils.translation import ugettext_lazy as _, ugettext as t
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 +19,44 @@ 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!
# 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'),
def save(self, commit=True):
password = User.objects.make_random_password(length=15)
user = super().save(commit=False)
if commit:
if self.cleaned_data['send_welcome_email']:
# Ugly way to get the language since member isn't available
language = str('member-0-language', 'en'))
if language not in ('nl', 'en'):
language = 'en'
with translation.override(language):
email_body = loader.render_to_string(
{'user': user, 'password': password})
t('Welkom bij Studievereniging Thalia'),
return user
class Meta:
fields = ('username',
{% load i18n %}{% blocktrans trimmed %}Dear {{ user.get_full_name }},
Welcome to Study Association Thalia! You now have an account and can
log in at This is your username and password:
Your username is: {{ user.username }}
Your password is: {{ password }}
Please also check the information on your profile.
{% endblocktrans %}
......@@ -112,6 +112,9 @@ LOGIN_URL = '/login/'
# Email settings
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# Password validation
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