Revision 890b0eaf astakos/im/target/local.py
b/astakos/im/target/local.py | ||
---|---|---|
37 | 37 |
from django.shortcuts import render_to_response |
38 | 38 |
from django.template import RequestContext |
39 | 39 |
from django.contrib.auth import authenticate |
40 |
from django.contrib.auth.forms import AuthenticationForm |
|
41 |
from django.contrib import messages |
|
40 | 42 |
from django.utils.translation import ugettext as _ |
41 | 43 |
|
42 | 44 |
from astakos.im.target.util import prepare_response |
43 | 45 |
from astakos.im.models import AstakosUser |
44 |
from astakos.im.forms import LoginForm |
|
45 | 46 |
|
46 | 47 |
from urllib import unquote |
47 | 48 |
|
... | ... | |
51 | 52 |
""" |
52 | 53 |
on_failure: whatever redirect accepts as to |
53 | 54 |
""" |
54 |
form = LoginForm(request.POST) |
|
55 |
|
|
55 |
form = AuthenticationForm(data=request.POST) |
|
56 | 56 |
if not form.is_valid(): |
57 | 57 |
return render_to_response(on_failure, |
58 | 58 |
{'form':form}, |
59 | 59 |
context_instance=RequestContext(request)) |
60 |
# get the user from the cash |
|
61 |
user = form.user_cache |
|
60 | 62 |
|
61 |
user = authenticate(**form.cleaned_data) |
|
62 |
status = 'success' |
|
63 |
message = None |
|
63 | 64 |
if not user: |
64 |
status = 'error' |
|
65 | 65 |
message = _('Cannot authenticate account') |
66 | 66 |
elif not user.is_active: |
67 |
status = 'error' |
|
68 | 67 |
message = _('Inactive account') |
69 |
|
|
70 |
if status == 'error':
|
|
68 |
if message: |
|
69 |
messages.add_message(request, message.ERROR, message)
|
|
71 | 70 |
return render_to_response(on_failure, |
72 |
{'form':form, |
|
73 |
'message': _('Unverified account')}, |
|
71 |
{'form':form}, |
|
74 | 72 |
context_instance=RequestContext(request)) |
75 | 73 |
|
76 | 74 |
next = request.POST.get('next') |
... | ... | |
87 | 85 |
user.is_active = True |
88 | 86 |
user.save() |
89 | 87 |
return prepare_response(request, user, next, renew=True) |
90 |
|
|
91 |
def reset_password(request): |
|
92 |
if request.method == 'GET': |
|
93 |
cookie_value = unquote(request.COOKIES.get('_pithos2_a', '')) |
|
94 |
if cookie_value and '|' in cookie_value: |
|
95 |
token = cookie_value.split('|', 1)[1] |
|
96 |
else: |
|
97 |
token = request.GET.get('auth') |
|
98 |
next = request.GET.get('next') |
|
99 |
username = request.GET.get('username') |
|
100 |
kwargs = {'auth': token, |
|
101 |
'next': next, |
|
102 |
'username' : username} |
|
103 |
if not token: |
|
104 |
kwargs.update({'status': 'error', |
|
105 |
'message': 'Missing token'}) |
|
106 |
html = render_to_string('reset.html', kwargs) |
|
107 |
return HttpResponse(html) |
|
108 |
elif request.method == 'POST': |
|
109 |
token = request.POST.get('auth') |
|
110 |
username = request.POST.get('username') |
|
111 |
password = request.POST.get('password') |
|
112 |
next = request.POST.get('next') |
|
113 |
if not token: |
|
114 |
status = 'error' |
|
115 |
message = 'Bad Request: missing token' |
|
116 |
try: |
|
117 |
user = AstakosUser.objects.get(auth_token=token) |
|
118 |
if username != user.username: |
|
119 |
status = 'error' |
|
120 |
message = 'Bad Request: username mismatch' |
|
121 |
else: |
|
122 |
user.password = password |
|
123 |
user.status = 'NORMAL' |
|
124 |
user.save() |
|
125 |
return prepare_response(request, user, next, renew=True) |
|
126 |
except AstakosUser.DoesNotExist: |
|
127 |
status = 'error' |
|
128 |
message = 'Bad Request: invalid token' |
|
129 |
|
|
130 |
html = render_to_string('reset.html', { |
|
131 |
'status': status, |
|
132 |
'message': message}) |
|
133 |
return HttpResponse(html) |
Also available in: Unified diff