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
6 class OpenStreetMapLoginForm(forms.Form):
7 """ osm account signin form """
9 username = UserNameField(required=False, skip_clean=True)
10 password = forms.CharField(max_length=128,
11 widget=forms.widgets.PasswordInput(attrs={'class':'required login'}),
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,
18 self.user_details = None
20 def _clean_nonempty_field(self, field):
22 if field in self.cleaned_data:
23 value = self.cleaned_data[field].strip()
26 self.cleaned_data[field] = value
29 def clean_username(self):
30 return self._clean_nonempty_field('username')
32 def clean_password(self):
33 return self._clean_nonempty_field('password')
37 username = self.cleaned_data['username']
38 password = self.cleaned_data['password']
40 self.user_details = None
41 if username and password:
42 api = OpenStreetMapAPI(username, password)
45 self.user_details = api.user_details()
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.'))
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)
62 return self.cleaned_data
65 """ get authenticated user """
66 return "http://www.openstreetmap.org/user/%s" % self.user_details["id"]
68 def get_user_data(self):
69 """ get user data for authenticated user """
71 "username": self.user_details["display_name"]