mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-26 06:08:38 +01:00
fixed #107
Removed useless fields (full_address) and added associated migrations. SQL queries used by postfix or dovecot will need to be updated. (as the documentation) see #103 Big refactoring of objects creation and edition. (javascript cleanup, forms factoring, etc.)
This commit is contained in:
+54
-11
@@ -1,3 +1,4 @@
|
||||
# coding: utf8
|
||||
from django import forms
|
||||
from modoboa.admin.models import *
|
||||
from django.utils.translation import ugettext as _
|
||||
@@ -14,12 +15,26 @@ class DomainForm(forms.ModelForm):
|
||||
for f in ['name', 'quota']:
|
||||
self.fields[f].widget.attrs['size'] = 14
|
||||
|
||||
class DomainAliasForm(forms.ModelForm):
|
||||
class ProxyForm(forms.ModelForm):
|
||||
def __init__(self, user, *args, **kwargs):
|
||||
super(ProxyForm, self).__init__(*args, **kwargs)
|
||||
|
||||
if user.is_superuser:
|
||||
self._domain = None
|
||||
else:
|
||||
self._domain = user.mailbox_set.all()[0].domain
|
||||
|
||||
class DomainAliasForm(ProxyForm):
|
||||
class Meta:
|
||||
model = DomainAlias
|
||||
fields = ('name', 'target', 'enabled')
|
||||
|
||||
class MailboxForm(forms.ModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(DomainAliasForm, self).__init__(*args, **kwargs)
|
||||
if self._domain is not None:
|
||||
self.fields["target"].queryset = Domain.objects.filter(pk=self._domain.id)
|
||||
|
||||
class MailboxForm(ProxyForm):
|
||||
quota = forms.IntegerField(label=_("Quota"), required=False,
|
||||
help_text=_("Mailbox quota in MB (default to domain quota if blank)"))
|
||||
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput,
|
||||
@@ -38,6 +53,15 @@ class MailboxForm(forms.ModelForm):
|
||||
super(MailboxForm, self).__init__(*args, **kwargs)
|
||||
for f in ['name', 'address', 'quota', 'password1', 'password2']:
|
||||
self.fields[f].widget.attrs['size'] = 14
|
||||
if self._domain is not None:
|
||||
self.fields["domain"].queryset = Domain.objects.filter(pk=self._domain.id)
|
||||
self.fields["domain"].initial = self._domain
|
||||
|
||||
if kwargs.has_key("instance"):
|
||||
self.fields['quota'].initial = kwargs["instance"].quota
|
||||
self.fields['enabled'].initial = kwargs["instance"].user.is_active
|
||||
self.fields['password1'].initial = "é"
|
||||
self.fields['password2'].initial = "é"
|
||||
|
||||
def clean_address(self):
|
||||
if self.cleaned_data["address"].find('@') != -1:
|
||||
@@ -57,17 +81,37 @@ class MailboxForm(forms.ModelForm):
|
||||
quota=self.cleaned_data["quota"])
|
||||
return m
|
||||
|
||||
class AliasForm(forms.ModelForm):
|
||||
class AliasForm(ProxyForm):
|
||||
domain = forms.ModelChoiceField(queryset=Domain.objects.all(), label=_("Domain"),
|
||||
required=True,
|
||||
empty_label=_("Select a domain"),
|
||||
help_text=_("Select a domain in the list"))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
domain = None
|
||||
if kwargs.has_key("domain"):
|
||||
domain = kwargs["domain"]
|
||||
del kwargs["domain"]
|
||||
super(AliasForm, self).__init__(*args, **kwargs)
|
||||
self.fields['address'].widget.attrs['size'] = 14
|
||||
self.fields['mboxes'].widget.attrs['size'] = 5
|
||||
if domain:
|
||||
self.fields['mboxes'].queryset = Mailbox.objects.filter(domain=domain.id)
|
||||
|
||||
# This part is a little bit tricky. As 'domain' is not a
|
||||
# member of Alias, we need to fill it manually. if
|
||||
# self._domain exists, it means a DomainAdmin is going to
|
||||
# create a new alias, we know how to fill the fields. Else,
|
||||
# leave them empty (super admin mode).
|
||||
if self._domain is not None:
|
||||
self.fields["domain"].queryset = Domain.objects.filter(pk=self._domain.id)
|
||||
elif len(args) < 2 or args[1]["domain"] == '':
|
||||
self.fields['mboxes'].queryset = Mailbox.objects.none()
|
||||
|
||||
# if 'instance' exists, it means we are modifying an existing
|
||||
# record, we need to select the right value for
|
||||
# 'domain'. Finally, if super user is doing the job, we need
|
||||
# to fill 'mboxes' because it's currently empty (see upper).
|
||||
if kwargs.has_key("instance") or self._domain is not None:
|
||||
domain = self._domain is None and kwargs["instance"].domain or self._domain
|
||||
self.fields["domain"].initial = domain
|
||||
self.fields['mboxes'].queryset = Mailbox.objects.filter(domain=domain)
|
||||
|
||||
self.fields.keyOrder = ['domain', 'address', 'mboxes', 'enabled']
|
||||
|
||||
class Meta:
|
||||
model = Alias
|
||||
@@ -81,8 +125,7 @@ class AliasForm(forms.ModelForm):
|
||||
def save(self, force_insert=False, force_update=False, commit=True):
|
||||
a = super(AliasForm, self).save(commit=False)
|
||||
if commit:
|
||||
domain = self.cleaned_data["mboxes"][0].domain
|
||||
a.full_address = "%s@%s" % (a.address, domain.name)
|
||||
domain = self.cleaned_data["domain"].name
|
||||
a.save()
|
||||
return a
|
||||
|
||||
|
||||
Reference in New Issue
Block a user