From 66fb997f65675d03be44b934b1c9f86dadb83386 Mon Sep 17 00:00:00 2001 From: Marc Date: Fri, 14 Nov 2014 23:06:14 +0000 Subject: [PATCH] Fixed errors with systemuser form and serializer --- orchestra/admin/options.py | 12 ++++++------ orchestra/apps/mailboxes/models.py | 28 ++++++++++++++++------------ orchestra/apps/systemusers/forms.py | 4 ++-- orchestra/apps/systemusers/models.py | 2 +- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/orchestra/admin/options.py b/orchestra/admin/options.py index 330c6f62..78bec761 100644 --- a/orchestra/admin/options.py +++ b/orchestra/admin/options.py @@ -194,12 +194,13 @@ class ChangePasswordAdminMixin(object): related.append(user.account) else: account = user - for rel in account.get_related_passwords(): - if not isinstance(user, type(rel)): - related.append(rel) + if account.username == username: + for rel in account.get_related_passwords(): + if not isinstance(user, type(rel)): + related.append(rel) if request.method == 'POST': - form = self.change_password_form(user, request.POST, related) + form = self.change_password_form(user, request.POST, related=related) if form.is_valid(): form.save() change_message = self.construct_change_message(request, form, None) @@ -209,7 +210,7 @@ class ChangePasswordAdminMixin(object): update_session_auth_hash(request, form.user) # This is safe return HttpResponseRedirect('..') else: - form = self.change_password_form(user, related) + form = self.change_password_form(user, related=related) fieldsets = [ (user._meta.verbose_name.capitalize(), { @@ -224,7 +225,6 @@ class ChangePasswordAdminMixin(object): })) adminForm = admin.helpers.AdminForm(form, fieldsets, {}) - context = { 'title': _('Change password: %s') % escape(username), 'adminform': adminForm, diff --git a/orchestra/apps/mailboxes/models.py b/orchestra/apps/mailboxes/models.py index d58d2419..508484cc 100644 --- a/orchestra/apps/mailboxes/models.py +++ b/orchestra/apps/mailboxes/models.py @@ -13,16 +13,18 @@ class Mailbox(models.Model): CUSTOM = 'CUSTOM' name = models.CharField(_("name"), max_length=64, unique=True, - help_text=_("Required. 30 characters or fewer. Letters, digits and " - "@/./+/-/_ only."), - validators=[RegexValidator(r'^[\w.@+-]+$', - _("Enter a valid mailbox name."), 'invalid')]) + help_text=_("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."), + validators=[ + RegexValidator(r'^[\w.@+-]+$', _("Enter a valid mailbox name.")) + ]) password = models.CharField(_("password"), max_length=128) account = models.ForeignKey('accounts.Account', verbose_name=_("account"), related_name='mailboxes') filtering = models.CharField(max_length=16, - choices=[(k, v[0]) for k,v in settings.MAILBOXES_MAILBOX_FILTERINGS.iteritems()], - default=settings.MAILBOXES_MAILBOX_DEFAULT_FILTERING) + default=settings.MAILBOXES_MAILBOX_DEFAULT_FILTERING, + choices=[ + (k, v[0]) for k,v in settings.MAILBOXES_MAILBOX_FILTERINGS.iteritems() + ]) custom_filtering = models.TextField(_("filtering"), blank=True, validators=[validators.validate_sieve], help_text=_("Arbitrary email filtering in sieve language. " @@ -110,15 +112,17 @@ class Address(models.Model): # return ' '.join(destinations) def clean(self): - if self.account: - errors = [] + if self.account_id: + forward_errors = [] for mailbox in self.get_forward_mailboxes(): - if mailbox.account == self.account: - errors.append(ValidationError( + if mailbox.account_id == self.account_id: + forward_errors.append(ValidationError( _("Please use mailboxes field for '%s' mailbox.") % mailbox )) - if errors: - raise ValidationError({'forward': errors}) + if forward_errors: + raise ValidationError({ + 'forward': forward_errors + }) def get_forward_mailboxes(self): for forward in self.forward.split(): diff --git a/orchestra/apps/systemusers/forms.py b/orchestra/apps/systemusers/forms.py index 30667b6a..04cf3300 100644 --- a/orchestra/apps/systemusers/forms.py +++ b/orchestra/apps/systemusers/forms.py @@ -31,8 +31,8 @@ class SystemUserFormMixin(object): self.fields['home'].widget = forms.HiddenInput() self.fields['directory'].widget = forms.HiddenInput() elif self.instance.pk and (self.instance.get_base_home() == self.instance.home): - self.fields['directory'].widget = forms.HiddenInput() - if self.instance.pk and not self.instance.is_main: + self.fields['directory'].widget = forms.HiddenInput() + if not self.instance.pk or not self.instance.is_main: # Some javascript for hidde home/directory inputs when convinient self.fields['shell'].widget.attrs = { 'onChange': textwrap.dedent("""\ diff --git a/orchestra/apps/systemusers/models.py b/orchestra/apps/systemusers/models.py index b0f922ae..b2264ee3 100644 --- a/orchestra/apps/systemusers/models.py +++ b/orchestra/apps/systemusers/models.py @@ -79,7 +79,7 @@ class SystemUser(models.Model): directory_error = None if self.has_shell: directory_error = _("Directory with shell users can not be specified.") - elif self.pk and self.is_main: + elif self.account_id and self.is_main: directory_error = _("Directory with main system users can not be specified.") elif self.home == self.get_base_home(): directory_error = _("Directory on the user's base home is not allowed.")