forked from justjkk/cseismic2kx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
127 lines (109 loc) · 5.3 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
# Copyright 2007, 2008,2009 by Benoît Chesneau <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import re
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from django.utils.translation import ugettext as _
from django.conf import settings
# needed for some linux distributions like debian
try:
from openid.yadis import xri
except ImportError:
from yadis import xri
from django_authopenid.models import UserAssociation
class OpenidSigninForm(forms.Form):
""" signin form """
openid_url = forms.CharField(max_length=255,
widget=forms.widgets.TextInput(attrs={'class': 'required openid'}))
def clean_openid_url(self):
""" test if openid is accepted """
if 'openid_url' in self.cleaned_data:
openid_url = self.cleaned_data['openid_url']
if xri.identifierScheme(openid_url) == 'XRI' and getattr(
settings, 'OPENID_DISALLOW_INAMES', False
):
raise forms.ValidationError(_('i-names are not supported'))
return self.cleaned_data['openid_url']
attrs_dict = { 'class': 'required login' }
username_re = re.compile(r'^\w+$')
class OpenidRegisterForm(forms.Form):
""" openid signin form """
username = forms.CharField(max_length=30,
widget=forms.widgets.TextInput(attrs=attrs_dict))
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=200)), label=u'Email address')
def __init__(self, *args, **kwargs):
super(OpenidRegisterForm, self).__init__(*args, **kwargs)
self.user = None
def clean_username(self):
""" test if username is valid and exist in database """
if 'username' in self.cleaned_data:
if not username_re.search(self.cleaned_data['username']):
raise forms.ValidationError(_("Usernames can only contain \
letters, numbers and underscores"))
try:
user = User.objects.get(
username__exact = self.cleaned_data['username']
)
except User.DoesNotExist:
return self.cleaned_data['username']
except User.MultipleObjectsReturned:
raise forms.ValidationError(u'There is already more than one \
account registered with that username. Please try \
another.')
self.user = user
raise forms.ValidationError(_("This username is already \
taken. Please choose another."))
def clean_email(self):
"""For security reason one unique email in database"""
if 'email' in self.cleaned_data:
try:
user = User.objects.get(email = self.cleaned_data['email'])
except User.DoesNotExist:
return self.cleaned_data['email']
except User.MultipleObjectsReturned:
raise forms.ValidationError(u'There is already more than one \
account registered with that e-mail address. Please try \
another.')
raise forms.ValidationError(_("This email is already \
registered in our database. Please choose another."))
class AssociateOpenID(forms.Form):
""" new openid association form """
openid_url = forms.CharField(max_length=255,
widget=forms.widgets.TextInput(attrs={'class': 'required openid'}))
def __init__(self, user, *args, **kwargs):
super(AssociateOpenID, self).__init__(*args, **kwargs)
self.user = user
def clean_openid_url(self):
""" test if openid is accepted """
if 'openid_url' in self.cleaned_data:
openid_url = self.cleaned_data['openid_url']
if xri.identifierScheme(openid_url) == 'XRI' and getattr(
settings, 'OPENID_DISALLOW_INAMES', False
):
raise forms.ValidationError(_('i-names are not supported'))
try:
rel = UserAssociation.objects.get(openid_url__exact=openid_url)
except UserAssociation.DoesNotExist:
return self.cleaned_data['openid_url']
if rel.user != self.user:
raise forms.ValidationError(_("This openid is already \
registered in our database by another account. Please choose another."))
raise forms.ValidationError(_("You already associated this openid to your account."))
class OpenidDissociateForm(OpenidSigninForm):
""" form used to dissociate an openid. """
openid_url = forms.CharField(max_length=255, widget=forms.widgets.HiddenInput())