diff --git a/static/js/utils.js b/static/js/utils.js index 79b3fa0e2..9560a3ab4 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -88,7 +88,7 @@ if (!Object.keys) { */ function simple_ajax_form_post(e, options) { e.preventDefault(); - var $form = $("form"); + var $form = (options.formid != undefined) ? $("#" + options.formid) : $("form"); var defaults = {reload_on_success: true, reload_mode: 'full'}; var opts = $.extend({}, defaults, options); var args = $form.serialize(); @@ -96,7 +96,6 @@ function simple_ajax_form_post(e, options) { if (options.extradata != undefined) { args += "&" + options.extradata; } - $.post($form.attr("action"), args, function(data) { if (data.status == "ok") { if (opts.success_cb != undefined) { @@ -163,6 +162,18 @@ function parse_qs(raw) { return res; } +/* + * Return the target associated to an event object. + */ +function get_target(e, tag) { + var $target = $(e.target); + + if (tag === undefined || $target.is(tag)) { + return $target; + } + return $target.parent(); +} + /* * Simple wrapper around Request.JSON. We just ensure that * redirections are correctly catched. (on session timeout for @@ -179,4 +190,15 @@ function parse_qs(raw) { // $(document.body).set("html", xhr.responseText); // } // } -// }); \ No newline at end of file +// }); + +/* + * 'Change password' form initialization. + */ +function chpasswordform_cb() { + $(".submit").one('click', function(e) { + simple_ajax_form_post(e, { + error_cb: chpasswordform_cb + }); + }); +} \ No newline at end of file diff --git a/templates/userprefs/chpassword.html b/templates/userprefs/chpassword.html deleted file mode 100644 index d54b10cf8..000000000 --- a/templates/userprefs/chpassword.html +++ /dev/null @@ -1,17 +0,0 @@ -{% load i18n %} - - - diff --git a/userprefs/templatetags/uprefs_extras.py b/userprefs/templatetags/uprefs_extras.py index bf2287291..e5200edb1 100644 --- a/userprefs/templatetags/uprefs_extras.py +++ b/userprefs/templatetags/uprefs_extras.py @@ -37,7 +37,8 @@ def user_menu(user): "url" : reverse("modoboa.userprefs.views.changepassword"), "img" : "icon-edit", "label" : _("Change password"), - "modal" : True}, + "modal" : True, + "modalcb" : "chpasswordform_cb"}, {"name" : "preferences", "img" : "icon-list", "label" : _("Preferences"), diff --git a/userprefs/views.py b/userprefs/views.py index 4d9413e31..82c3355b2 100644 --- a/userprefs/views.py +++ b/userprefs/views.py @@ -1,3 +1,4 @@ +# coding: utf-8 import copy from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect @@ -18,12 +19,20 @@ def index(request): return HttpResponseRedirect(reverse(preferences)) @login_required -def changepassword(request, tplname="userprefs/chpassword.html"): +def changepassword(request, tplname="common/generic_modal_form.html"): res = events.raiseQueryEvent("PasswordChange", request.user) if True in res: ctx = dict(error=_("Password change is disabled for this user")) return _render_error(request, errortpl="error_simple", user_context=ctx) + ctx = dict( + title=_("Change password"), + formid="chpasswordform", + action=reverse(changepassword), + action_label=_("Update"), + action_classes="submit", + ) + error = None if request.method == "POST": form = ChangePasswordForm(request.user, request.POST) @@ -38,14 +47,11 @@ def changepassword(request, tplname="userprefs/chpassword.html"): request.user.save() if error is None: return ajax_response(request, respmsg=_("Password changed")) + ctx.update(form=form, error=error) + return ajax_response(request, status="ko", template=tplname, **ctx) - return ajax_response(request, status="ko", template=tplname, - form=form, error=error) - - form = ChangePasswordForm(request.user) - return _render(request, 'userprefs/chpassword.html', { - "form" : form - }) + ctx.update(form=ChangePasswordForm(request.user)) + return _render(request, tplname, ctx) @login_required @user_passes_test(lambda u: u.belongs_to_group('SimpleUsers'))