]> git.openstreetmap.org Git - osqa.git/blob - forum/views/auth.py
deleting the test file
[osqa.git] / forum / views / auth.py
1 from django.shortcuts import render_to_response\r
2 from django.template import RequestContext\r
3 from django.core.urlresolvers import reverse\r
4 from django.contrib.auth.models import User\r
5 from django.http import HttpResponseRedirect\r
6 from django.utils.safestring import mark_safe\r
7 from django.utils.translation import ugettext as _\r
8 from django.contrib.auth.decorators import login_required\r
9 from django.contrib.auth import login,  logout\r
10 from django.http import get_host\r
11 import types\r
12 \r
13 from forum.models import AuthKeyUserAssociation\r
14 from forum.authentication.forms import SimpleRegistrationForm, SimpleEmailSubscribeForm\r
15 \r
16 from forum.authentication.base import InvalidAuthentication\r
17 from forum.authentication import AUTH_PROVIDERS\r
18 \r
19 from forum.models import Question, Answer\r
20 \r
21 def signin_page(request, action=None):\r
22     if action is None:\r
23         request.session['on_signin_url'] = request.META.get('HTTP_REFERER', '/')\r
24     else:\r
25         request.session['on_signin_action'] = action\r
26 \r
27     all_providers = [provider.context for provider in AUTH_PROVIDERS.values()]\r
28 \r
29     sort = lambda c1, c2: c1.weight - c2.weight\r
30     can_show = lambda c: not request.user.is_authenticated() or c.show_to_logged_in_user\r
31 \r
32     bigicon_providers = sorted([\r
33         context for context in all_providers if context.mode == 'BIGICON' and can_show(context)\r
34     ], sort)\r
35 \r
36     smallicon_providers = sorted([\r
37         context for context in all_providers if context.mode == 'SMALLICON' and can_show(context)\r
38     ], sort)\r
39 \r
40     stackitem_providers = sorted([\r
41         context for context in all_providers if context.mode == 'STACK_ITEM' and can_show(context)\r
42     ], sort)\r
43 \r
44     try:\r
45         msg = request.session['auth_error']\r
46         del request.session['auth_error']\r
47     except:\r
48         msg = None\r
49 \r
50     return render_to_response(\r
51             'auth/signin.html',\r
52             {\r
53                 'msg': msg,\r
54                 'all_providers': all_providers,\r
55                 'bigicon_providers': bigicon_providers,\r
56                 'stackitem_providers': stackitem_providers,\r
57                 'smallicon_providers': smallicon_providers,\r
58             },\r
59             RequestContext(request))\r
60 \r
61 def prepare_provider_signin(request, provider):\r
62     force_email_request = request.REQUEST.get('validate_email', 'yes') == 'yes'\r
63     request.session['force_email_request'] = force_email_request\r
64     \r
65     if provider in AUTH_PROVIDERS:\r
66         provider_class = AUTH_PROVIDERS[provider].consumer\r
67 \r
68         try:\r
69             request_url = provider_class.prepare_authentication_request(request,\r
70                     reverse('auth_provider_done', kwargs={'provider': provider}))\r
71 \r
72             return HttpResponseRedirect(request_url)\r
73         except NotImplementedError, e:\r
74             return process_provider_signin(request, provider)\r
75         except InvalidAuthentication, e:\r
76             request.session['auth_error'] = e.message\r
77 \r
78         return HttpResponseRedirect(reverse('auth_signin'))    \r
79 \r
80 \r
81 def process_provider_signin(request, provider):\r
82     if provider in AUTH_PROVIDERS:\r
83         provider_class = AUTH_PROVIDERS[provider].consumer\r
84 \r
85         try:\r
86             assoc_key = provider_class.process_authentication_request(request)\r
87         except InvalidAuthentication, e:\r
88             request.session['auth_error'] = e.message\r
89             return HttpResponseRedirect(reverse('auth_signin'))\r
90 \r
91         if request.user.is_authenticated():\r
92             if isinstance(assoc_key, (type, User)):\r
93                 if request.user != assoc_key:\r
94                     request.session['auth_error'] = _("Sorry, these login credentials belong to anoother user. Plese terminate your current session and try again.")\r
95                 else:\r
96                     request.session['auth_error'] = _("You are already logged in with that user.")\r
97             else:\r
98                 try:\r
99                     assoc = AuthKeyUserAssociation.objects.get(key=assoc_key)\r
100                     if assoc.user == request.user:\r
101                         request.session['auth_error'] = _("These login credentials are already associated with your account.")\r
102                     else:\r
103                         request.session['auth_error'] = _("Sorry, these login credentials belong to anoother user. Plese terminate your current session and try again.")\r
104                 except:\r
105                     uassoc = AuthKeyUserAssociation(user=request.user, key=assoc_key, provider=provider)\r
106                     uassoc.save()\r
107                     request.session['auth_error'] = _("These new credentials are now associated with your account.")                    \r
108             return HttpResponseRedirect(reverse('auth_signin'))\r
109 \r
110         try:\r
111             assoc = AuthKeyUserAssociation.objects.get(key=assoc_key)\r
112             user_ = assoc.user\r
113             return login_and_forward(request, user_)\r
114         except:\r
115             request.session['assoc_key'] = assoc_key\r
116             request.session['auth_provider'] = provider\r
117             return HttpResponseRedirect(reverse('auth_external_register'))\r
118 \r
119     return HttpResponseRedirect(reverse('auth_signin'))\r
120 \r
121 def external_register(request):\r
122     if request.method == 'POST' and 'bnewaccount' in request.POST:\r
123         form1 = SimpleRegistrationForm(request.POST)\r
124         email_feeds_form = SimpleEmailSubscribeForm(request.POST)\r
125 \r
126         if (form1.is_valid() and email_feeds_form.is_valid()):\r
127             tmp_pwd = User.objects.make_random_password()\r
128             user_ = User.objects.create_user(form1.cleaned_data['username'],\r
129                      form1.cleaned_data['email'], tmp_pwd)\r
130 \r
131             user_.set_unusable_password()\r
132 \r
133             uassoc = AuthKeyUserAssociation(user=user_, key=request.session['assoc_key'], provider=request.session['auth_provider'])\r
134             uassoc.save()\r
135 \r
136             email_feeds_form.save(user_)\r
137 \r
138             del request.session['assoc_key']\r
139             del request.session['auth_provider']\r
140             return login_and_forward(request, user_)\r
141     else:\r
142         provider_class = AUTH_PROVIDERS[request.session['auth_provider']].consumer\r
143         user_data = provider_class.get_user_data(request.session['assoc_key'])\r
144 \r
145         username = user_data.get('username', '')\r
146         email = user_data.get('email', '')\r
147 \r
148         if not email:\r
149             email = request.session.get('auth_email_request', '')\r
150 \r
151         form1 = SimpleRegistrationForm(initial={\r
152             'next': '/',\r
153             'username': username,\r
154             'email': email,\r
155         })\r
156         email_feeds_form = SimpleEmailSubscribeForm()\r
157 \r
158     provider_context = AUTH_PROVIDERS[request.session['auth_provider']].context\r
159 \r
160     return render_to_response('auth/complete.html', {\r
161         'form1': form1,\r
162         'email_feeds_form': email_feeds_form,\r
163         'provider':mark_safe(provider_context.human_name),\r
164         'login_type':provider_context.id,\r
165         'gravatar_faq_url':reverse('faq') + '#gravatar',\r
166     }, context_instance=RequestContext(request))\r
167 \r
168 def newquestion_signin_action(user):\r
169     question = Question.objects.filter(author=user).order_by('-added_at')[0]\r
170     return question.get_absolute_url()\r
171 \r
172 def newanswer_signin_action(user):\r
173     answer = Answer.objects.filter(author=user).order_by('-added_at')[0]\r
174     return answer.get_absolute_url()\r
175 \r
176 POST_SIGNIN_ACTIONS = {\r
177     'newquestion': newquestion_signin_action,\r
178     'newanswer': newanswer_signin_action,\r
179 }\r
180 \r
181 def login_and_forward(request,  user):\r
182     old_session = request.session.session_key\r
183     user.backend = "django.contrib.auth.backends.ModelBackend"\r
184     login(request,  user)\r
185 \r
186     from forum.models import user_logged_in\r
187     user_logged_in.send(user=user,session_key=old_session,sender=None)\r
188 \r
189     redirect = request.session.get('on_signin_url', None)\r
190 \r
191     if not redirect:\r
192         signin_action = request.session.get('on_signin_action', None)\r
193         if not signin_action:\r
194             redirect = reverse('index')\r
195         else:\r
196             try:\r
197                 redirect = POST_SIGNIN_ACTIONS[signin_action](user)\r
198             except:\r
199                 redirect = reverse('index')\r
200 \r
201     return HttpResponseRedirect(redirect)\r
202 \r
203 @login_required\r
204 def signout(request):\r
205     """\r
206     signout from the website. Remove openid from session and kill it.\r
207 \r
208     url : /signout/"\r
209     """\r
210 \r
211     logout(request)\r
212     return HttpResponseRedirect(reverse('index'))