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':
users.add(user.pk)
continue
elif (user.member.birthday <= eightteen_years_ago
and self.value() == '18+'):
elif (user.member.birthday <= eightteen_years_ago and
self.value() == '18+'):
users.add(user.pk)
elif (user.member.birthday > eightteen_years_ago
and self.value() == '18-'):
elif (user.member.birthday > eightteen_years_ago and
self.value() == '18-'):
users.add(user.pk)
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'),
'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.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 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']:
# Ugly way to get the language since member isn't available
language = str(self.data.get('member-0-language', 'en'))
if language not in ('nl', 'en'):
language = 'en'
with translation.override(language):
email_body = loader.render_to_string(
'members/email/welcome.txt',
{'user': user, 'password': password})
user.email_user(
t('Welkom bij Studievereniging Thalia'),
email_body)
return user
class Meta:
fields = ('username',
'first_name',
'last_name',
'send_welcome_email')
{% load i18n %}{% blocktrans trimmed %}Dear {{ user.get_full_name }},
Welcome to Study Association Thalia! You now have an account and can
log in at https://thalia.nu/. This is your username and password:
Your username is: {{ user.username }}
Your password is: {{ password }}
Please also check the information on your profile.
Cheers,
Thalia
{% endblocktrans %}
......@@ -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