first draft of a registration form + email
[myslice.git] / portal / forms.py
1 # -*- coding: utf-8 -*-
2 #
3 # portal/forms.py: forms for the portal application
4 # This file is part of the Manifold project.
5 #
6 # Authors:
7 #   Jordan AugĂ© <jordan.auge@lip6.fr>
8 # Copyright 2013, UPMC Sorbonne UniversitĂ©s / LIP6
9 #
10 # This program is free software; you can redistribute it and/or modify it under
11 # the terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 3, or (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful, but WITHOUT
15 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17 # details.
18
19 # You should have received a copy of the GNU General Public License along with
20 # this program; see the file COPYING.  If not, write to the Free Software
21 # Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 from django import forms
24 from portal.models import PendingUser, PendingSlice
25 #from crispy_forms.helper import FormHelper
26 #from crispy_forms.layout import Submit
27 from django.utils.translation import ugettext_lazy as _
28
29 class UserRegisterForm(forms.Form): # Not ModelForm
30     """
31     Form for registering a new user account.
32     
33     Validates that the requested username is not already in use, and
34     requires the password to be entered twice to catch typos.
35     
36     Subclasses should feel free to add any additional validation they
37     need, but should avoid defining a ``save()`` method -- the actual
38     saving of collected user data is delegated to the active
39     registration backend.
40
41     """
42     required_css_class = 'required'
43     
44     first_name = forms.RegexField(regex=r'^[\w.@+-]+$',
45                                  max_length=30,
46                                  label=_("First name"),
47                                  error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
48     last_name = forms.RegexField(regex=r'^[\w.@+-]+$',
49                                  max_length=30,
50                                  label=_("Last name"),
51                                  error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
52     email = forms.EmailField(label=_("E-mail"))
53     password1 = forms.CharField(widget=forms.PasswordInput,
54                                 label=_("Password"))
55     password2 = forms.CharField(widget=forms.PasswordInput,
56                                 label=_("Password (again)"))
57     keypair    = forms.CharField( widget=forms.FileInput )
58
59     tos = forms.BooleanField(widget=forms.CheckboxInput,
60                              label=_(u'I have read and agree to the Terms of Service'),
61                              error_messages={'required': _("You must agree to the terms to register")})
62
63 #    def clean_username(self):
64 #        """
65 #        Validate that the username is alphanumeric and is not already
66 #        in use.
67 #        
68 #        """
69 #        existing = User.objects.filter(username__iexact=self.cleaned_data['username'])
70 #        if existing.exists():
71 #            raise forms.ValidationError(_("A user with that username already exists."))
72 #        else:
73 #            return self.cleaned_data['username']
74
75     def clean_email(self):
76         """
77         Validate that the supplied email address is unique for the
78         site.
79         
80         """
81         if PendingUser.objects.filter(email__iexact=self.cleaned_data['email']):
82             raise forms.ValidationError(_("This email address is already in use. Please supply a different email address."))
83         return self.cleaned_data['email']
84
85     def clean(self):
86         """
87         Verifiy that the values entered into the two password fields
88         match. Note that an error here will end up in
89         ``non_field_errors()`` because it doesn't apply to a single
90         field.
91         
92         """
93         if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
94             if self.cleaned_data['password1'] != self.cleaned_data['password2']:
95                 raise forms.ValidationError(_("The two password fields didn't match."))
96         return self.cleaned_data
97
98 # DEPRECATED #    class Meta:
99 # DEPRECATED #        model = PendingUser
100
101 class SliceRequestForm(forms.ModelForm):
102     slice_name = forms.CharField( widget=forms.TextInput )
103     class Meta:
104         model = PendingSlice
105
106 # DEPRECATED #class RegisterUserStep2Form(forms.ModelForm):
107 # DEPRECATED #    class Meta:
108 # DEPRECATED #        model = PendingUser