Fixed errors with systemuser form and serializer

This commit is contained in:
Marc 2014-11-14 23:06:14 +00:00
parent fd6c7975a0
commit 66fb997f65
4 changed files with 25 additions and 21 deletions

View File

@ -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,

View File

@ -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():

View File

@ -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("""\

View File

@ -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.")