Statistics
| Branch: | Tag: | Revision:

root / astakos / im / forms.py @ 64cd4730

History | View | Annotate | Download (5.7 kB)

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