mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-19 05:58:34 +02:00
wip: Per-domain policy support
* The domain edition form was converted to a "TabsForm" * New events added : "ExtraDomainForm" and "FillDomainInstances" * Creation and modification of policies work see #260
This commit is contained in:
+30
-4
@@ -10,7 +10,7 @@ from modoboa.lib.permissions import get_account_roles
|
||||
from modoboa.lib.formutils import *
|
||||
from modoboa.lib.permissions import *
|
||||
|
||||
class DomainForm(forms.ModelForm, DynamicForm):
|
||||
class DomainFormGeneral(forms.ModelForm, DynamicForm):
|
||||
aliases = DomainNameField(
|
||||
label=ugettext_lazy("Alias(es)"),
|
||||
required=False,
|
||||
@@ -25,7 +25,7 @@ class DomainForm(forms.ModelForm, DynamicForm):
|
||||
self.oldname = None
|
||||
if kwargs.has_key("instance"):
|
||||
self.oldname = kwargs["instance"].name
|
||||
super(DomainForm, self).__init__(*args, **kwargs)
|
||||
super(DomainFormGeneral, self).__init__(*args, **kwargs)
|
||||
|
||||
if len(args) and isinstance(args[0], QueryDict):
|
||||
self._load_from_qdict(args[0], "aliases", DomainNameField)
|
||||
@@ -36,7 +36,7 @@ class DomainForm(forms.ModelForm, DynamicForm):
|
||||
self._create_field(forms.CharField, name, dalias.name, 3)
|
||||
|
||||
def clean(self):
|
||||
super(DomainForm, self).clean()
|
||||
super(DomainFormGeneral, self).clean()
|
||||
if len(self._errors):
|
||||
raise forms.ValidationError(self._errors)
|
||||
|
||||
@@ -69,7 +69,7 @@ class DomainForm(forms.ModelForm, DynamicForm):
|
||||
return cleaned_data
|
||||
|
||||
def save(self, user, commit=True):
|
||||
d = super(DomainForm, self).save(commit=False)
|
||||
d = super(DomainFormGeneral, self).save(commit=False)
|
||||
if commit:
|
||||
if self.oldname is not None and d.name != self.oldname:
|
||||
if not d.rename_dir(self.oldname):
|
||||
@@ -98,6 +98,32 @@ class DomainForm(forms.ModelForm, DynamicForm):
|
||||
dalias.delete()
|
||||
return d
|
||||
|
||||
class DomainForm(TabForms):
|
||||
def __init__(self, user, *args, **kwargs):
|
||||
self.user = user
|
||||
self.forms = [
|
||||
dict(id="general", title=_("General"), formtpl="admin/domain_general_form.html",
|
||||
cls=DomainFormGeneral, mandatory=True),
|
||||
]
|
||||
cbargs = [user]
|
||||
if kwargs.has_key("instances"):
|
||||
cbargs += [kwargs["instances"]["general"]]
|
||||
self.forms += events.raiseQueryEvent("ExtraDomainForm", *cbargs)
|
||||
|
||||
super(DomainForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def save(self, user):
|
||||
"""Custom save method
|
||||
|
||||
As forms interact with each other, it is simpler to make
|
||||
custom code to save them.
|
||||
"""
|
||||
self.forms[0]["instance"].save(user)
|
||||
if len(self.forms) <= 1:
|
||||
return
|
||||
for f in self.forms[1:]:
|
||||
f["instance"].save()
|
||||
|
||||
class DlistForm(forms.ModelForm, DynamicForm):
|
||||
email = forms.EmailField(
|
||||
label=ugettext_lazy("Email address"),
|
||||
|
||||
+14
-11
@@ -45,7 +45,7 @@ def domains_list(request):
|
||||
return ajax_simple_response(doms)
|
||||
|
||||
def _validate_domain(request, form, successmsg, commonctx, tplname,
|
||||
callback=None):
|
||||
callback=None, tpl_form_name="form"):
|
||||
"""Domain form validation
|
||||
|
||||
Common function shared between creation and modification actions.
|
||||
@@ -56,15 +56,15 @@ def _validate_domain(request, form, successmsg, commonctx, tplname,
|
||||
domain = form.save(request.user)
|
||||
except AdminError, e:
|
||||
error = str(e)
|
||||
except IntegrityError:
|
||||
error = _("An alias with this name already exists")
|
||||
# except IntegrityError:
|
||||
# error = _("An alias with this name already exists")
|
||||
else:
|
||||
if callback is not None:
|
||||
callback(request.user, domain)
|
||||
messages.info(request, successmsg, fail_silently=True)
|
||||
return ajax_response(request, url=reverse(admin.views.domains))
|
||||
|
||||
commonctx["form"] = form
|
||||
commonctx[tpl_form_name] = form
|
||||
commonctx["error"] = error
|
||||
return ajax_response(request, status="ko", template=tplname, **commonctx)
|
||||
|
||||
@@ -83,10 +83,10 @@ def newdomain(request, tplname="admin/newdomainform.html"):
|
||||
"action" : reverse(newdomain),
|
||||
"formid" : "domform"}
|
||||
if request.method == "POST":
|
||||
form = DomainForm(request.POST)
|
||||
form = DomainFormGeneral(request.POST)
|
||||
return _validate_domain(request, form, _("Domain created"), commonctx,
|
||||
tplname, callback=newdomain_cb)
|
||||
form = DomainForm()
|
||||
form = DomainFormGeneral()
|
||||
commonctx["form"] = form
|
||||
return _render(request, tplname, commonctx)
|
||||
|
||||
@@ -103,7 +103,10 @@ def editdomain(request, dom_id, tplname="admin/editdomainform.html"):
|
||||
)
|
||||
if not request.user.is_superuser:
|
||||
domadmins = filter(lambda u: u.group == "DomainAdmins", domadmins)
|
||||
|
||||
|
||||
instances = dict(general=domain)
|
||||
events.raiseEvent("FillDomainInstances", request.user, domain, instances)
|
||||
|
||||
commonctx = {"title" : domain.name,
|
||||
"action_label" : _("Update"),
|
||||
"action_classes" : "submit",
|
||||
@@ -111,11 +114,11 @@ def editdomain(request, dom_id, tplname="admin/editdomainform.html"):
|
||||
"formid" : "domform",
|
||||
"domain" : domain}
|
||||
if request.method == "POST":
|
||||
form = DomainForm(request.POST, instance=domain)
|
||||
return _validate_domain(request, form, _("Domain modified"), commonctx, tplname)
|
||||
form = DomainForm(request.user, request.POST, instances=instances)
|
||||
return _validate_domain(request, form, _("Domain modified"), commonctx, tplname,
|
||||
tpl_form_name="tabs")
|
||||
|
||||
form = DomainForm(instance=domain)
|
||||
commonctx["form"] = form
|
||||
commonctx["tabs"] = DomainForm(request.user, instances=instances)
|
||||
commonctx["domadmins"] = domadmins
|
||||
return _render(request, tplname, commonctx)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user