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