Another Step in the Quest to clean up the code base.

This commit is contained in:
2017-09-08 07:19:50 +02:00
parent ce218080b2
commit b3ab9798b5
229 changed files with 1915 additions and 15175 deletions

View File

@@ -3,20 +3,20 @@ Created on 03.10.2011
@author: Christian
"""
import sys
from captcha.fields import ReCaptchaField
from django import forms
from django.conf import settings
from django.contrib import auth
from django.contrib.sites.models import Site
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from utils.massmailer import MassMailer
from captcha.fields import ReCaptchaField
from . import models
class MembershipForm(forms.ModelForm):
"""Form for users to change their own user data."""
error_css_class = 'error'
required_css_class = 'required'
birthday = forms.DateField(
@@ -27,6 +27,7 @@ class MembershipForm(forms.ModelForm):
email = forms.EmailField(label=_('Email'), required=True)
class Meta:
"""get the user model dyamicly"""
model = auth.get_user_model()
fields = (
'username', 'gender', 'first_name', 'last_name', 'email', 'avatar',
@@ -35,26 +36,31 @@ class MembershipForm(forms.ModelForm):
)
def clean_birthday(self):
if self.cleaned_data['membership'] and not self.cleaned_data['birthday']:
"""If the user wants to be a member the birthday field is mandatory."""
if self.cleaned_data['membership'] \
and not self.cleaned_data['birthday']:
raise forms.ValidationError(_('For your membership, we need this. \
Please fill out this field yet.'))
return self.cleaned_data['birthday']
def clean_street_name(self):
if self.cleaned_data['membership'] and not self.cleaned_data[
'street_name']:
"""If the user wants to be a member the address is mandatory."""
if self.cleaned_data['membership'] \
and not self.cleaned_data['street_name']:
raise forms.ValidationError(_('For your membership, we need this. \
Please fill out this field yet.'))
return self.cleaned_data['street_name']
def clean_post_code(self):
if self.cleaned_data['membership'] and not self.cleaned_data[
'post_code']:
"""If the user wants to be a member the address is mandatory."""
if self.cleaned_data['membership'] \
and not self.cleaned_data['post_code']:
raise forms.ValidationError(_('For your membership, we need this. \
Please fill out this field yet.'))
return self.cleaned_data['post_code']
def clean_city(self):
"""If the user wants to be a member the address is mandatory."""
if self.cleaned_data['membership'] and not self.cleaned_data['city']:
raise forms.ValidationError(_('For your membership, we need this. \
Please fill out this field yet.'))
@@ -62,11 +68,11 @@ class MembershipForm(forms.ModelForm):
class RegistrationForm(MembershipForm):
"""
Form to register a new user account.
"""Form to register a new user account.
Validates that the requested username and email is not already in use,
requires the password to be entered twice to catch typos.
sends an activation request per mail, to validate the eMail.
Also sends an activation request per mail, to validate the email.
"""
password1 = forms.CharField(
widget=forms.PasswordInput(), label=_('password'))
@@ -75,6 +81,7 @@ class RegistrationForm(MembershipForm):
recaptcha = ReCaptchaField()
class Meta:
"""Metadata to localize and customize the ModelForm."""
model = auth.get_user_model()
fields = ('first_name', 'last_name', 'username', 'email',
'username', 'gender', 'first_name', 'last_name', 'email',
@@ -85,9 +92,7 @@ class RegistrationForm(MembershipForm):
)
def clean_username(self):
"""
Validate that the username is not already in use.
"""
"""Validate that the username is not already in use."""
try:
auth.get_user_model().objects.get(
username__iexact=self.cleaned_data['username']
@@ -98,9 +103,7 @@ class RegistrationForm(MembershipForm):
Please choose another.'))
def clean_email(self):
"""
Validate that the supplied email address is unique for the site.
"""
"""Validate that the supplied email address is unique for the site."""
if auth.get_user_model().objects.filter(
email__iexact=self.cleaned_data['email']):
raise forms.ValidationError(_(u'This email address is already in \
@@ -108,6 +111,7 @@ class RegistrationForm(MembershipForm):
return self.cleaned_data['email']
def clean_password2(self):
"""Check that the password has been entered identical for two times."""
password1 = self.cleaned_data.get("password1", "")
password2 = self.cleaned_data["password2"]
if password1 != password2:
@@ -115,30 +119,29 @@ class RegistrationForm(MembershipForm):
_("The two password fields didn't match."))
return password2
def send_email(self, activation):
mailer = MassMailer()
mailer.subject = 'Deine Anmeldung auf %s' % Site.objects.get_current()
mailer.txt_template = 'membership/email/activation_email.txt'
mailer.context = {
'user': activation.user,
'site': Site.objects.get_current(),
'activation_key': activation.activation_key,
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
}
mailer.add_recipient(activation.user)
mailer.send()
def save(self, commit=True):
"""
Creates the new ``User`` and ``RegistrationProfile`` and
returns the ``User``.
""" Create the new User, set him/her inactive, create an acitivation
request for the user and send him/her an activation email.
:param commit: commit the SQL and send the email if True
:return: the created User Object
"""
user = super(RegistrationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.is_active = False
if commit:
user.save()
activation = models.ActivationRequest.objects.create_pending_registration(
user) # @IgnorePep8
self.send_email(activation)
activation_request = models.ActivationRequest.objects \
.create_pending_registration(user)
mailer = MassMailer(
subject='Deine Anmeldung auf %s' % Site.objects.get_current(),
txt_template='membership/email/activation_email.txt',
context={
'user': activation_request.user,
'site': Site.objects.get_current(),
'activation_key': activation_request.activation_key,
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
}
)
mailer.send(recipients=[activation_request.user])
return user