[Migration] fix issues with users from Django Auth
[mygpo.git] / mygpo / users / views / registration.py
blobf5e4a643399ce68f036bc2ef03b2c3d8e040bafb
1 import uuid
3 from django import forms
4 from django.db import IntegrityError, transaction
5 from django.http import HttpResponseRedirect
6 from django.views.generic.edit import FormView
7 from django.utils.translation import ugettext as _
8 from django.template.loader import render_to_string
9 from django.core.urlresolvers import reverse, reverse_lazy
10 from django.views.generic import TemplateView
11 from django.views.generic.base import View
12 from django.contrib import messages
13 from django.contrib.auth import get_user_model
14 from django.contrib.sites.shortcuts import get_current_site
16 from mygpo.utils import random_token
17 from mygpo.users.models import UserProxy
20 USERNAME_MAXLEN = get_user_model()._meta.get_field('username').max_length
23 class RegistrationForm(forms.Form):
24 """ Form that is used to register a new user """
25 username = forms.CharField(max_length=USERNAME_MAXLEN)
26 email = forms.EmailField()
27 password1 = forms.CharField(widget=forms.PasswordInput())
28 password2 = forms.CharField(widget=forms.PasswordInput())
30 def clean(self):
31 cleaned_data = super(RegistrationForm, self).clean()
32 password1 = cleaned_data.get('password1')
33 password2 = cleaned_data.get('password2')
35 if not password1 or password1 != password2:
36 raise forms.ValidationError('Passwords do not match')
39 class RegistrationView(FormView):
40 """ View to register a new user """
41 template_name = 'registration/registration_form.html'
42 form_class = RegistrationForm
43 success_url = reverse_lazy('registration-complete')
45 def form_valid(self, form):
46 """ called whene the form was POSTed and its contents were valid """
48 try:
49 user = self.create_user(form)
51 except IntegrityError:
52 messages.error(self.request,
53 _('Username or email address already in use'))
54 return HttpResponseRedirect(reverse('register'))
56 send_activation_email(user, self.request)
57 return super(RegistrationView, self).form_valid(form)
59 @transaction.atomic
60 def create_user(self, form):
61 User = get_user_model()
62 user = User()
63 user.username = form.cleaned_data['username']
64 user.email = form.cleaned_data['email']
65 user.set_password(form.cleaned_data['password1'])
66 user.is_active = False
67 user.save()
69 user.profile.uuid == uuid.uuid1()
70 user.profile.activation_key = random_token()
71 user.profile.save()
73 return user
76 class ActivationView(TemplateView):
77 """ Activates an already registered user """
79 template_name = 'registration/activation_failed.html'
81 def get(self, request, activation_key):
82 User = get_user_model()
84 try:
85 user = UserProxy.objects.get(
86 profile__activation_key=activation_key,
87 is_active=False,
89 except UserProxy.DoesNotExist:
90 messages.error(request, _('The activation link is either not '
91 'valid or has already expired.'))
92 return super(ActivationView, self).get(request, activation_key)
94 user.activate()
95 messages.success(request, _('Your user has been activated. '
96 'You can log in now.'))
97 return HttpResponseRedirect(reverse('login'))
100 class ResendActivationForm(forms.Form):
101 """ Form for resending the activation email """
103 username = forms.CharField(max_length=USERNAME_MAXLEN, required=False)
104 email = forms.EmailField(required=False)
106 def clean(self):
107 cleaned_data = super(ResendActivationForm, self).clean()
108 username = cleaned_data.get('username')
109 email = cleaned_data.get('email')
111 if not username and not email:
112 raise forms.ValidationError(_('Either username or email address '
113 'are required.'))
116 class ResendActivationView(FormView):
117 """ View to resend the activation email """
118 template_name = 'registration/resend_activation.html'
119 form_class = ResendActivationForm
120 success_url = reverse_lazy('resent-activation')
122 def form_valid(self, form):
123 """ called whene the form was POSTed and its contents were valid """
125 try:
126 user = UserProxy.objects.all().by_username_or_email(
127 form.cleaned_data['username'],
128 form.cleaned_data['email'],
131 except UserProxy.DoesNotExist:
132 messages.error(self.request, _('User does not exist.'))
133 return HttpResponseRedirect(reverse('resend-activation'))
135 if user.profile.activation_key is None:
136 messages.success(self.request, _('Your account already has been '
137 'activated. Go ahead and log in.'))
139 send_activation_email(user, self.request)
140 return super(ResendActivationView, self).form_valid(form)
143 class ResentActivationView(TemplateView):
144 template_name = 'registration/resent_activation.html'
147 def send_activation_email(user, request):
148 """ Sends the activation email for the given user """
150 subj = render_to_string('registration/activation_email_subject.txt')
151 # remove trailing newline added by render_to_string
152 subj = subj.strip()
154 msg = render_to_string('registration/activation_email.txt', {
155 'site': get_current_site(request),
156 'activation_key': user.profile.activation_key,
158 user.email_user(subj, msg)