Initial commit. Move from pithos repository.
[astakos] / astakos / im / forms.py
1 # Copyright 2011 GRNET S.A. All rights reserved.
2
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10
11 #   2. Redistributions in binary form must reproduce the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer in the documentation and/or other materials
14 #      provided with the distribution.
15
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
33
34 from django import forms
35 from django.utils.translation import ugettext as _
36 from django.conf import settings
37
38 from astakos.im.models import User
39
40 openid_providers = (
41 ('Google','https://www.google.com/accounts/o8/id'),
42 ('Yahoo', 'http://yahoo.com/'),
43 ('AOL','http://openid.aol.com/%s/'),
44 ('OpenID', None),
45 ('MyOpenID','http://%s.myopenid.com/'),
46 ('LiveJournal', 'http://%s.livejournal.com/'),
47 ('Flickr', 'http://flickr.com/%s/'),
48 ('Technorati', 'http://technorati.com/people/technorati/%s/'),
49 ('Wordpress', 'http://%s.wordpress.com/'),
50 ('Blogger', 'http://%s.blogspot.com/'),
51 ('Verisign', 'http://%s.pip.verisignlabs.com/'),
52 ('Vidoop', 'http://%s.myvidoop.com/'),
53 ('ClaimID','http://claimid.com/%s')    
54 )
55
56 class RegisterForm(forms.Form):
57     uniq = forms.CharField(widget=forms.widgets.TextInput())
58     provider = forms.CharField(widget=forms.TextInput(),
59                                 label=u'Identity Provider')
60     email = forms.EmailField(widget=forms.TextInput(),
61                              label=_('Email address'))
62     realname = forms.CharField(widget=forms.TextInput(),
63                                 label=u'Real Name')
64     
65     def __init__(self, *args, **kwargs):
66         super(forms.Form, self).__init__(*args, **kwargs)
67         
68         #set readonly form fields
69         self.fields['provider'].widget.attrs['readonly'] = True
70     
71     def clean_uniq(self):
72         """
73         Validate that the uniq is alphanumeric and is not already
74         in use.
75         
76         """
77         try:
78             user = User.objects.get(uniq__iexact=self.cleaned_data['uniq'])
79         except User.DoesNotExist:
80             return self.cleaned_data['uniq']
81         raise forms.ValidationError(_("A user with that uniq already exists."))
82
83 class ShibbolethRegisterForm(RegisterForm):
84     pass
85
86 class TwitterRegisterForm(RegisterForm):
87     pass
88
89 class OpenidRegisterForm(RegisterForm):
90     openidurl = forms.ChoiceField(widget=forms.Select,
91                                   choices=((url, l) for l, url in openid_providers))
92
93 class LocalRegisterForm(RegisterForm):
94     """ local signup form"""
95     password = forms.CharField(widget=forms.PasswordInput(render_value=False),
96                                 label=_('Password'))
97     password2 = forms.CharField(widget=forms.PasswordInput(render_value=False),
98                                 label=_('Confirm Password'))
99     
100     def __init__(self, *args, **kwargs):
101         super(LocalRegisterForm, self).__init__(*args, **kwargs)
102     
103     def clean_uniq(self):
104         """
105         Validate that the uniq is alphanumeric and is not already
106         in use.
107         
108         """
109         try:
110             user = User.objects.get(uniq__iexact=self.cleaned_data['uniq'])
111         except User.DoesNotExist:
112             return self.cleaned_data['uniq']
113         raise forms.ValidationError(_("A user with that uniq already exists."))
114     
115     def clean(self):
116         """
117         Verifiy that the values entered into the two password fields
118         match. Note that an error here will end up in
119         ``non_field_errors()`` because it doesn't apply to a single
120         field.
121         
122         """
123         if 'password' in self.cleaned_data and 'password2' in self.cleaned_data:
124             if self.cleaned_data['password'] != self.cleaned_data['password2']:
125                 raise forms.ValidationError(_("The two password fields didn't match."))
126         return self.cleaned_data
127
128 class InvitedRegisterForm(RegisterForm):
129     inviter = forms.CharField(widget=forms.TextInput(),
130                                 label=_('Inviter Real Name'))
131     
132     def __init__(self, *args, **kwargs):
133         super(RegisterForm, self).__init__(*args, **kwargs)
134         
135         #set readonly form fields
136         self.fields['uniq'].widget.attrs['readonly'] = True
137         self.fields['inviter'].widget.attrs['readonly'] = True
138         self.fields['provider'].widget.attrs['provider'] = True
139
140 class InvitedLocalRegisterForm(LocalRegisterForm, InvitedRegisterForm):
141     pass
142
143 class InvitedOpenidRegisterForm(OpenidRegisterForm, InvitedRegisterForm):
144     pass
145
146 class InvitedTwitterRegisterForm(TwitterRegisterForm, InvitedRegisterForm):
147     pass
148
149 class InvitedShibbolethRegisterForm(ShibbolethRegisterForm, InvitedRegisterForm):
150     pass