]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/osmauth/forms.py
Add an OSM authentication provider
[osqa.git] / forum_modules / osmauth / forms.py
1 from osm import OpenStreetMapAPI
2 from forum.forms import NextUrlField, UserNameField, SetPasswordForm
3 from django.utils.translation import ugettext as _
4 from django import forms
5
6 class OpenStreetMapLoginForm(forms.Form):
7     """ osm account signin form """
8     next = NextUrlField()
9     username = UserNameField(required=False, skip_clean=True)
10     password = forms.CharField(max_length=128,
11                                widget=forms.widgets.PasswordInput(attrs={'class':'required login'}),
12                                required=False)
13
14     def __init__(self, data=None, files=None, auto_id='id_%s',
15                  prefix=None, initial=None):
16         super(OpenStreetMapLoginForm, self).__init__(data, files, auto_id,
17                                                      prefix, initial)
18         self.user_details = None
19
20     def _clean_nonempty_field(self, field):
21         value = None
22         if field in self.cleaned_data:
23             value = self.cleaned_data[field].strip()
24             if value == '':
25                 value = None
26         self.cleaned_data[field] = value
27         return value
28
29     def clean_username(self):
30         return self._clean_nonempty_field('username')
31
32     def clean_password(self):
33         return self._clean_nonempty_field('password')
34
35     def clean(self):
36         error_list = []
37         username = self.cleaned_data['username']
38         password = self.cleaned_data['password']
39
40         self.user_details = None
41         if username and password:
42             api = OpenStreetMapAPI(username, password)
43
44             try:
45                 self.user_details = api.user_details()
46             except:
47                 del self.cleaned_data['username']
48                 del self.cleaned_data['password']
49                 error_list.insert(0, (_("Please enter valid username and password "
50                 "(both are case-sensitive).")))
51                 error_list.insert(0, _('Login failed.'))
52
53         elif password == None and username == None:
54             error_list.append(_('Please enter username and password'))
55         elif password == None:
56             error_list.append(_('Please enter your password'))
57         elif username == None:
58             error_list.append(_('Please enter user name'))
59         if len(error_list) > 0:
60             self._errors['__all__'] = forms.util.ErrorList(error_list)
61
62         return self.cleaned_data
63
64     def get_user(self):
65         """ get authenticated user """
66         return "http://www.openstreetmap.org/user/%s" % self.user_details["id"]
67
68     def get_user_data(self):
69         """ get user data for authenticated user """
70         return {
71             "username": self.user_details["display_name"]
72         }