##// END OF EJS Templates
login: switch to re-use the baseApp pyramid view.
marcink -
r1536:995f91ba default
parent child Browse files
Show More
@@ -56,13 +56,14 b' class BaseAppView(object):'
56 setattr(c, k, v)
56 setattr(c, k, v)
57
57
58 def _get_template_context(self, tmpl_args):
58 def _get_template_context(self, tmpl_args):
59
60 self._register_global_c(tmpl_args)
59 self._register_global_c(tmpl_args)
61
60
62 return {
61 local_tmpl_args = {
63 'defaults': {},
62 'defaults': {},
64 'errors': {},
63 'errors': {},
65 }
64 }
65 local_tmpl_args.update(tmpl_args)
66 return local_tmpl_args
66
67
67 def load_default_context(self):
68 def load_default_context(self):
68 """
69 """
@@ -30,6 +30,7 b' from pyramid.httpexceptions import HTTPF'
30 from pyramid.view import view_config
30 from pyramid.view import view_config
31 from recaptcha.client.captcha import submit
31 from recaptcha.client.captcha import submit
32
32
33 from rhodecode.apps._base import BaseAppView
33 from rhodecode.authentication.base import authenticate, HTTP_TYPE
34 from rhodecode.authentication.base import authenticate, HTTP_TYPE
34 from rhodecode.events import UserRegistered
35 from rhodecode.events import UserRegistered
35 from rhodecode.lib import helpers as h
36 from rhodecode.lib import helpers as h
@@ -105,20 +106,13 b' def get_came_from(request):'
105 return came_from or url('home')
106 return came_from or url('home')
106
107
107
108
108 class LoginView(object):
109 class LoginView(BaseAppView):
109
110
110 def __init__(self, context, request):
111 def load_default_context(self):
111 self.request = request
112 c = self._get_local_tmpl_context()
112 self.context = context
113 c.came_from = get_came_from(self.request)
113 self.session = request.session
114 self._register_global_c(c)
114 self._rhodecode_user = request.user
115 return c
115
116 def _get_template_context(self):
117 return {
118 'came_from': get_came_from(self.request),
119 'defaults': {},
120 'errors': {},
121 }
122
116
123 def _get_captcha_data(self):
117 def _get_captcha_data(self):
124 settings = SettingsModel().get_all_settings()
118 settings = SettingsModel().get_all_settings()
@@ -132,12 +126,13 b' class LoginView(object):'
132 route_name='login', request_method='GET',
126 route_name='login', request_method='GET',
133 renderer='rhodecode:templates/login.mako')
127 renderer='rhodecode:templates/login.mako')
134 def login(self):
128 def login(self):
135 came_from = get_came_from(self.request)
129 c = self.load_default_context()
136 user = self.request.user
130 auth_user = self._rhodecode_user
137
131
138 # redirect if already logged in
132 # redirect if already logged in
139 if user.is_authenticated and not user.is_default and user.ip_allowed:
133 if (auth_user.is_authenticated and
140 raise HTTPFound(came_from)
134 not auth_user.is_default and auth_user.ip_allowed):
135 raise HTTPFound(c.came_from)
141
136
142 # check if we use headers plugin, and try to login using it.
137 # check if we use headers plugin, and try to login using it.
143 try:
138 try:
@@ -147,18 +142,18 b' class LoginView(object):'
147 if auth_info:
142 if auth_info:
148 headers = _store_user_in_session(
143 headers = _store_user_in_session(
149 self.session, auth_info.get('username'))
144 self.session, auth_info.get('username'))
150 raise HTTPFound(came_from, headers=headers)
145 raise HTTPFound(c.came_from, headers=headers)
151 except UserCreationError as e:
146 except UserCreationError as e:
152 log.error(e)
147 log.error(e)
153 self.session.flash(e, queue='error')
148 self.session.flash(e, queue='error')
154
149
155 return self._get_template_context()
150 return self._get_template_context(c)
156
151
157 @view_config(
152 @view_config(
158 route_name='login', request_method='POST',
153 route_name='login', request_method='POST',
159 renderer='rhodecode:templates/login.mako')
154 renderer='rhodecode:templates/login.mako')
160 def login_post(self):
155 def login_post(self):
161 came_from = get_came_from(self.request)
156 c = self.load_default_context()
162
157
163 login_form = LoginForm()()
158 login_form = LoginForm()()
164
159
@@ -170,13 +165,13 b' class LoginView(object):'
170 self.session,
165 self.session,
171 username=form_result['username'],
166 username=form_result['username'],
172 remember=form_result['remember'])
167 remember=form_result['remember'])
173 log.debug('Redirecting to "%s" after login.', came_from)
168 log.debug('Redirecting to "%s" after login.', c.came_from)
174 raise HTTPFound(came_from, headers=headers)
169 raise HTTPFound(c.came_from, headers=headers)
175 except formencode.Invalid as errors:
170 except formencode.Invalid as errors:
176 defaults = errors.value
171 defaults = errors.value
177 # remove password from filling in form again
172 # remove password from filling in form again
178 defaults.pop('password', None)
173 defaults.pop('password', None)
179 render_ctx = self._get_template_context()
174 render_ctx = self._get_template_context(c)
180 render_ctx.update({
175 render_ctx.update({
181 'errors': errors.error_dict,
176 'errors': errors.error_dict,
182 'defaults': defaults,
177 'defaults': defaults,
@@ -189,13 +184,13 b' class LoginView(object):'
189 # with user creation, explanation should be provided in
184 # with user creation, explanation should be provided in
190 # Exception itself
185 # Exception itself
191 self.session.flash(e, queue='error')
186 self.session.flash(e, queue='error')
192 return self._get_template_context()
187 return self._get_template_context(c)
193
188
194 @CSRFRequired()
189 @CSRFRequired()
195 @view_config(route_name='logout', request_method='POST')
190 @view_config(route_name='logout', request_method='POST')
196 def logout(self):
191 def logout(self):
197 user = self.request.user
192 auth_user = self._rhodecode_user
198 log.info('Deleting session for user: `%s`', user)
193 log.info('Deleting session for user: `%s`', auth_user)
199 self.session.delete()
194 self.session.delete()
200 return HTTPFound(url('home'))
195 return HTTPFound(url('home'))
201
196
@@ -205,6 +200,7 b' class LoginView(object):'
205 route_name='register', request_method='GET',
200 route_name='register', request_method='GET',
206 renderer='rhodecode:templates/register.mako',)
201 renderer='rhodecode:templates/register.mako',)
207 def register(self, defaults=None, errors=None):
202 def register(self, defaults=None, errors=None):
203 c = self.load_default_context()
208 defaults = defaults or {}
204 defaults = defaults or {}
209 errors = errors or {}
205 errors = errors or {}
210
206
@@ -214,7 +210,7 b' class LoginView(object):'
214 auto_active = 'hg.register.auto_activate' in User.get_default_user()\
210 auto_active = 'hg.register.auto_activate' in User.get_default_user()\
215 .AuthUser.permissions['global']
211 .AuthUser.permissions['global']
216
212
217 render_ctx = self._get_template_context()
213 render_ctx = self._get_template_context(c)
218 render_ctx.update({
214 render_ctx.update({
219 'defaults': defaults,
215 'defaults': defaults,
220 'errors': errors,
216 'errors': errors,
General Comments 0
You need to be logged in to leave comments. Login now