login.py
5.09 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from datetime import datetime
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.httpexceptions import (
HTTPFound,
HTTPForbidden,
)
from pyramid.security import (
remember,
forget,
authenticated_userid,
)
from pyramid.i18n import TranslationStringFactory
import transaction
import colander
from deform import (
Form,
ValidationFailure,
widget,
Button,
)
from ziggurat_foundations.models.services.user import UserService
from ..tools.waktu import create_now
from ..models import DBSession
from ..models.ziggurat import User
_ = TranslationStringFactory('login')
class Login(colander.Schema):
username = colander.SchemaNode(colander.String(), title=_('Username'))
password = colander.SchemaNode(
colander.String(), widget=widget.PasswordWidget(),
title=_('Password'))
# http://deformdemo.repoze.org/interfield/
def login_validator(form, value):
user = form.user
if not user or \
not user.status or \
not user.user_password or \
not UserService.check_password(user, value['password']):
raise colander.Invalid(form, _('Login failed'))
def get_login_headers(request, user):
headers = remember(request, user.id)
user.last_login_date = create_now()
DBSession.add(user)
DBSession.flush()
transaction.commit()
return headers
@view_config(context=HTTPForbidden, renderer='templates/login.pt')
@view_config(route_name='login', renderer='templates/login.pt')
def view_login(request):
if authenticated_userid(request):
return HTTPFound(location=request.route_url('home'))
if '_LOCALE_' in request.GET:
resp = Response()
resp.set_cookie('_LOCALE_', request.GET['_LOCALE_'], 31536000)
return HTTPFound(
location=request.route_url('login'), headers=resp.headers)
resp = dict(title=_('Login'))
schema = Login(validator=login_validator)
btn_submit = Button('submit', _('Submit'))
form = Form(schema, buttons=(btn_submit,))
if 'submit' in request.POST:
controls = request.POST.items()
identity = request.POST.get('username')
if identity.find('@') > -1:
q = DBSession.query(User).filter_by(email=identity)
else:
q = DBSession.query(User).filter_by(user_name=identity)
schema.user = user = q.first()
try:
c = form.validate(controls)
except ValidationFailure:
resp['form'] = form.render()
return resp
headers = get_login_headers(request, user)
return HTTPFound(location=request.route_url('home'),
headers=headers)
resp['form'] = form.render()
return resp
@view_config(route_name='logout')
def view_logout(request):
headers = forget(request)
return HTTPFound(location = request.route_url('home'),
headers = headers)
###################
# Change password #
###################
class Password(colander.Schema):
old_password = colander.SchemaNode(colander.String(),
title="Kata Sandi Lama",
widget=widget.PasswordWidget())
new_password = colander.SchemaNode(colander.String(),
title="Kata Sandi Baru",
widget=widget.PasswordWidget())
retype_password = colander.SchemaNode(colander.String(),
title="Ketik Ulang Kata Sandi",
widget=widget.PasswordWidget())
def password_validator(form, value):
if not form.request.user.check_password(value['old_password']):
raise colander.Invalid(form, 'Invalid old password.')
if value['new_password'] != value['retype_password']:
raise colander.Invalid(form, 'Retype mismatch.')
@view_config(route_name='password', renderer='templates/password.pt',
permission='view')
def view_password(request):
schema = Password(validator=password_validator)
form = Form(schema, buttons=('simpan','batal'))
if request.POST:
if 'simpan' in request.POST:
schema.request = request
controls = request.POST.items()
try:
c = form.validate(controls)
except ValidationFailure as e:
request.session['invalid password'] = e.render()
return HTTPFound(location=request.route_url('password'))
user = request.user
user.password = c['new_password']
DBSession.add(user)
DBSession.flush()
transaction.commit()
#request.session.flash('Your password has been changed.')
request.session.flash('Password telah berhasil diubah.')
return HTTPFound(location=request.route_url('home'))
elif 'invalid password' in request.session:
r = dict(form=request.session['invalid password'])
del request.session['invalid password']
return r
return dict(form=form.render())