diff --git a/admin/forms.py b/admin/forms.py index 451ef249f..3bf1d3b22 100644 --- a/admin/forms.py +++ b/admin/forms.py @@ -267,14 +267,21 @@ class AccountFormGeneral(forms.ModelForm): choices=[('', ugettext_lazy("Choose"))], help_text=ugettext_lazy("What level of permission this user will have") ) + password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput) + password2 = forms.CharField( + label=ugettext_lazy("Confirmation"), + widget=forms.PasswordInput, + help_text=ugettext_lazy("Enter the same password as above, for verification.") + ) class Meta: model = User - fields = ("first_name", "last_name", "role", "is_active") + fields = ("username", "first_name", "last_name", "role", "is_active") def __init__(self, user, *args, **kwargs): super(AccountFormGeneral, self).__init__(*args, **kwargs) - + self.fields.keyOrder = ['role', 'username', 'first_name', 'last_name', + 'password1', 'password2', 'is_active'] self.fields["is_active"].label = _("Enabled") self.user = user if user.group == "DomainAdmins": @@ -284,6 +291,12 @@ class AccountFormGeneral(forms.ModelForm): self.fields["role"].choices = \ [('', ugettext_lazy("Choose"))] + get_account_roles(user) if kwargs.has_key("instance"): + if len(args) \ + and (args[0].get("password1", "") == "" \ + and args[0].get("password2", "") == ""): + self.fields["password1"].required = False + self.fields["password2"].required = False + u = kwargs["instance"] if u.is_superuser: role = "SuperAdmins" @@ -294,10 +307,27 @@ class AccountFormGeneral(forms.ModelForm): pass self.fields["role"].initial = role + def clean_username(self): + from django.core.validators import validate_email + + if self.cleaned_data["role"] != "SimpleUsers": + return self.cleaned_data + validate_email(self.cleaned_data["username"]) + return self.cleaned_data["username"] + + def clean_password2(self): + password1 = self.cleaned_data.get("password1", "") + password2 = self.cleaned_data["password2"] + if password1 != password2: + raise forms.ValidationError(_("The two password fields didn't match.")) + return password2 + + def save(self, commit=True): account = super(AccountFormGeneral, self).save(commit=False) - account.username = self.cleaned_data["username"] if commit: + if self.cleaned_data.has_key("password1"): + account.set_password(self.cleaned_data["password1"]) account.save() role = None if self.cleaned_data.has_key("role"): @@ -314,34 +344,6 @@ class AccountFormGeneral(forms.ModelForm): account.save() return account -class AccountFormGeneralPwd(AccountFormGeneral): - password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput) - password2 = forms.CharField( - label=ugettext_lazy("Confirmation"), - widget=forms.PasswordInput, - help_text=ugettext_lazy("Enter the same password as above, for verification.") - ) - - class Meta: - model = User - fields = ("username", "first_name", "last_name", - "password1", "password2", "role", "is_active") - - def clean_password2(self): - password1 = self.cleaned_data.get("password1", "") - password2 = self.cleaned_data["password2"] - if password1 != password2: - raise forms.ValidationError(_("The two password fields didn't match.")) - return password2 - - def save(self, commit=True, **kwargs): - user = super(AccountFormGeneralPwd, self).save(**kwargs) - if self.cleaned_data.has_key("password1"): - user.set_password(self.cleaned_data["password1"]) - if commit: - user.save() - return user - class AccountFormMail(forms.Form, DynamicForm): email = forms.EmailField(label=ugettext_lazy("E-mail"), required=False) quota = forms.IntegerField(label=ugettext_lazy("Quota"), required=False) @@ -479,7 +481,7 @@ class AccountForm(TabForms): def __init__(self, user, *args, **kwargs): self.user = user self.forms = [ - dict(id="general", title=_("General"), cls=AccountFormGeneralPwd, + dict(id="general", title=_("General"), cls=AccountFormGeneral, new_args=[user], mandatory=True), dict(id="mail", title=_("Mail"), formtpl="admin/mailform.html", cls=AccountFormMail), diff --git a/admin/views.py b/admin/views.py index 4a5e3e4c0..b7ed1596e 100644 --- a/admin/views.py +++ b/admin/views.py @@ -350,7 +350,7 @@ def newaccount(request, tplname='admin/newaccount.html'): submit_label=_("Create") ) cwizard = CreationWizard(create_account) - cwizard.add_step(AccountFormGeneralPwd, _("General"), + cwizard.add_step(AccountFormGeneral, _("General"), [dict(classes="btn-inverse next", label=_("Next"))], new_args=[request.user]) cwizard.add_step(AccountFormMail, _("Mail"), @@ -363,7 +363,7 @@ def newaccount(request, tplname='admin/newaccount.html'): raise AdminError(data) if retcode == 1: return ajax_simple_response(dict( - status="ok", title=cwizard.get_title(data + 1) + status="ok", title=cwizard.get_title(data + 1), stepid=data )) if retcode == 2: messages.info(request, _("Account created")) @@ -406,9 +406,6 @@ def editaccount(request, accountid, tplname="common/tabforms.html"): if request.method == "POST": classes = {} - if request.POST.get("password1", "") == "" \ - and request.POST.get("password2", "") == "": - classes["general"] = AccountFormGeneral form = AccountForm(request.user, request.POST, instances=instances, classes=classes) account.oldgroup = account.group diff --git a/static/js/admin.js b/static/js/admin.js index 7a5117ccc..df1b9db28 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -21,23 +21,62 @@ function domainform_cb() { }); } -function accountform_init() { +function generalform_init() { + $("#id_role").change(function(e) { + if ($(this).attr("value") == "SimpleUsers") { + $("#id_username").autocompleter({ + from_character: "@", + choices: get_domains_list + }); + $("#id_email").addClass("disabled") + .attr("readonly", "") + .autocompleter("unbind"); + } else { + $("#id_email").removeClass("disabled") + .attr("readonly", null) + .autocompleter("listen"); + } + }); +} + +function mailform_init() { $("#id_aliases").dynamic_input(); $("#id_email").autocompleter({ from_character: "@", choices: get_domains_list }); + $("#id_role").trigger("change"); $("#id_domains") .autocompleter({ choices: get_domains_list }) .dynamic_input(); + +} + +function accountform_init() { + generalform_init(); + mailform_init(); +} + +function mailform_prefill() { + if ($("#id_role").attr("value") == "SimpleUsers") { + $("#id_email").attr("value", $("#id_username").attr("value")); + } } function newaccount_cb() { - $('input:text:visible:first').focus(); accountform_init(); - $("#wizard").cwizard({formid: "newaccount_form"}); + $("#wizard").cwizard({ + formid: "newaccount_form", + transition_callbacks: { + 1: mailform_prefill + }, + error_callbacks: { + 1: generalform_init, + 2: mailform_init + } + }); } function editaccount_cb() { diff --git a/static/js/autocompleter.js b/static/js/autocompleter.js index 15f3f1a8c..b50570d72 100644 --- a/static/js/autocompleter.js +++ b/static/js/autocompleter.js @@ -1,29 +1,41 @@ (function($) { var Autocompleter = function(element, options) { - this.$element = $(element); - this.options = $.extend({}, $.fn.autocompleter.defaults, options); - this.choices = { - value: null - }; - if (typeof this.options.choices === "function") { - this.options.choices(this.choices); - } else { - this.choices.value = this.options.choices(); - } - this.$menu = - $('
'); - $("body").append(this.$menu); - this.$menu.on("click", $.proxy(this.select_entry, this)); - - this.$element.attr("autocomplete", "off"); - this.$element.keydown($.proxy(this.keydown, this)); - this.$element.keyup($.proxy(this.keyup, this)); - this.$element.blur($.proxy(this.hide_menu, this)); + this.initialize(element, options); }; Autocompleter.prototype = { constructor: Autocompleter, + initialize: function(element, options) { + this.$element = $(element); + this.options = $.extend({}, $.fn.autocompleter.defaults, options); + this.choices = { + value: null + }; + if (typeof this.options.choices === "function") { + this.options.choices(this.choices); + } else { + this.choices.value = this.options.choices(); + } + this.$menu = + $(''); + $("body").append(this.$menu); + this.$menu.on("click", $.proxy(this.select_entry, this)); + + this.$element.attr("autocomplete", "off"); + this.listen(); + }, + + listen: function() { + this.$element.keydown($.proxy(this.keydown, this)); + this.$element.keyup($.proxy(this.keyup, this)); + this.$element.blur($.proxy(this.hide_menu, this)); + }, + + unbind: function() { + this.$element.off("keydown keyup blur"); + }, + check_user_input: function() { var value = this.$element.attr("value"); var start = -1; diff --git a/static/js/creation_wizard.js b/static/js/creation_wizard.js index 90714042f..44252a75e 100644 --- a/static/js/creation_wizard.js +++ b/static/js/creation_wizard.js @@ -28,8 +28,12 @@ $.post($form.attr("action"), data, $.proxy(function(resp) { if (resp.status == "ok") { if (!last) { + var stepid = resp.stepid + 1; $('input:text:visible:first').focus(); $(".modal-header").find("small").html(resp.title); + if (this.options.transition_callbacks[stepid] != undefined) { + this.options.transition_callbacks[stepid](); + } this.$element.carousel('next'); } else { $("#modalbox").modal('hide').remove(); @@ -38,12 +42,17 @@ return; } if (resp.stepid != undefined && resp.form != undefined) { - $("#step" + (resp.stepid + 1)).html(resp.form); + var stepid = resp.stepid + 1; + $("#step" + stepid).html(resp.form); + if (this.options.error_callbacks[stepid] != undefined) { + this.options.error_callbacks[stepid](); + } + $('input:text:visible:first').focus(); } if (resp.respmsg) { $(".modal-body").prepend(build_error_alert(resp.respmsg)); } - this.listen(); + //this.listen(); }, this)); },