Commit 927873cd659432717a18d84d70b8bdd423107178

Authored by Luan
1 parent f10f7bf0

Avoiding raising an exception to user

Showing 2 changed files with 19 additions and 6 deletions   Show diff stats
src/accounts/errors.py 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +class XMPPChangePwdException(Exception):
  2 + """Error changing XMPP Account password"""
... ...
src/accounts/views.py
... ... @@ -25,6 +25,7 @@ from super_archives.utils.email import send_email_lists
25 25 from search.utils import trans
26 26 from .forms import (UserCreationForm, ListsForm, UserUpdateForm,
27 27 ChangeXMPPPasswordForm)
  28 +from .errors import XMPPChangePwdException
28 29 from .utils import mailman
29 30  
30 31  
... ... @@ -181,11 +182,6 @@ class ChangeXMPPPasswordView(UpdateView):
181 182 fields = ['password', ]
182 183 template_name = 'accounts/change_password.html'
183 184  
184   - @method_decorator(transaction.atomic)
185   - def dispatch(self, request, *args, **kwargs):
186   - return super(ChangeXMPPPasswordView, self).dispatch(request, *args,
187   - **kwargs)
188   -
189 185 def get_success_url(self):
190 186 return reverse('user_profile', kwargs={
191 187 'username': self.request.user.username
... ... @@ -197,6 +193,8 @@ class ChangeXMPPPasswordView(UpdateView):
197 193 return obj
198 194  
199 195 def form_valid(self, form):
  196 + transaction.set_autocommit(False)
  197 +
200 198 response = super(ChangeXMPPPasswordView, self).form_valid(form)
201 199  
202 200 changed = xmpp.change_password(
... ... @@ -204,6 +202,19 @@ class ChangeXMPPPasswordView(UpdateView):
204 202 self.old_password,
205 203 form.cleaned_data['password2']
206 204 )
  205 +
207 206 if not changed:
208   - raise Exception
  207 + messages.error(
  208 + self.request,
  209 + _(u'Could not change your password. Please, try again later.')
  210 + )
  211 + transaction.rollback()
  212 + return response
  213 + else:
  214 + transaction.commit()
  215 +
  216 + messages.success(
  217 + self.request,
  218 + _("You've changed your password successfully!")
  219 + )
209 220 return response
... ...