Log results into the database.

see #482
This commit is contained in:
Antoine Nguyen
2013-09-14 16:46:28 +02:00
parent ec8aba2aaa
commit 08b0fd0e5c
4 changed files with 28 additions and 24 deletions
+2 -2
View File
@@ -197,11 +197,11 @@ class GeneralParametersForm(parameters.AdminParametersForm):
self.fields["default_top_redirection"].choices = enabled_applications()
hide_fields = False
try:
code, version = exec_cmd("dovecot --version")
code, output = exec_cmd("which doveadm")
except OSError, e:
hide_fields = True
else:
if code or not version.strip().startswith("2"):
if code:
hide_fields = True
if hide_fields:
del self.fields["handle_mailboxes"]
+7 -7
View File
@@ -78,7 +78,7 @@ class DomainFormGeneral(forms.ModelForm, DynamicForm):
def update_mailbox_quotas(self, domain):
"""Update all quota records associated to this domain
This methid must be called only when a domain gets renamed. As
This method must be called only when a domain gets renamed. As
the primary key used for a quota is an email address, rename a
domain will change all associated email addresses, so it will
change the primary keys used for quotas. The consequence is we
@@ -90,8 +90,6 @@ class DomainFormGeneral(forms.ModelForm, DynamicForm):
perfomant at all as it will generate one query per quota
record to update.
"""
Mailbox.objects.filter(domain=domain, use_domain_quota=True) \
.update(quota=d.quota)
for q in Quota.objects.filter(username__contains="@%s" % self.oldname).values('username'):
username = q['username'].replace('@%s' % self.oldname, '@%s' % domain.name)
Quota.objects.filter(username=q['username']).update(username=username)
@@ -108,9 +106,13 @@ class DomainFormGeneral(forms.ModelForm, DynamicForm):
if commit:
old_mail_homes = None
if self.oldname is not None and d.name != self.oldname:
d.name = self.oldname
old_mail_homes = \
dict((mb.id, mb.mail_home) for mb in d.mailbox_set.all())
d.name = self.cleaned_data['name']
d.save()
Mailbox.objects.filter(domain=d, use_domain_quota=True) \
.update(quota=d.quota)
for k, v in self.cleaned_data.iteritems():
if not k.startswith("aliases"):
continue
@@ -131,12 +133,10 @@ class DomainFormGeneral(forms.ModelForm, DynamicForm):
self.cleaned_data.keys())):
dalias.delete()
self.update_mailbox_quotas(d)
if old_mail_homes is not None:
self.update_mailbox_quotas(d)
for mb in d.mailbox_set.all():
MailboxOperation.objects.create(
mailbox=mb, type='rename', argument=old_mail_homes[mb.id]
)
mb.rename_dir()
return d
@@ -3,6 +3,8 @@ import logging
from optparse import make_option
from django.core.management.base import BaseCommand
from modoboa.lib import parameters
from modoboa.lib.sysutils import exec_cmd
from modoboa.admin import AdminConsole
from modoboa.admin.models import MailboxOperation
@@ -16,22 +18,18 @@ class Command(BaseCommand):
def __init__(self, *args, **kwargs):
super(Command, self).__init__(*args, **kwargs)
self.logger = logging.getLogger('modoboa.cron')
self.logger = logging.getLogger('modoboa.admin')
def rename_mailbox(self, operation):
if not os.path.exists(operation.argument):
return
new_mail_home = operation.mailbox.mail_home.strip(os.sep)
self.logger.info("Renaming %s to %s", operation.argument, new_mail_home)
curpath = '/'
for part in new_mail_home.split(os.sep)[:-1]:
if not part:
continue
curpath = os.path.join(curpath, part)
if not os.path.exists(curpath):
os.mkdir(curpath)
new_mail_home = operation.mailbox.mail_home
try:
os.makedirs(os.path.dirname(new_mail_home))
except os.error as e:
raise OperationError(str(e))
code, output = exec_cmd(
"mv %s %s" % (old_mail_home, new_mail_home)
"mv %s %s" % (ope.argument, new_mail_home)
)
if code:
raise OperationError(output)
@@ -39,24 +37,25 @@ class Command(BaseCommand):
def delete_mailbox(self, operation):
if not os.path.exists(operation.argument):
return
self.logger.info("Deleting %s", operation.argument)
code, output = exec_cmd(
"rm -r %s" % operation.argument
)
if code:
raise OperationError(output)
def handle(self):
def handle(self, *args, **options):
AdminConsole().load()
if parameters.get_admin("HANDLE_MAILBOXES") == 'no':
return
for ope in MailboxOperation.objects.all():
try:
f = getattr(self, '%s_mailbox' % ope.optype)
f = getattr(self, '%s_mailbox' % ope.type)
except AttributeError:
continue
try:
f(ope)
except OperationError as e:
self.logger.critical('Operation failed: %s', str(e))
self.logger.critical('Operation failed: %s. Reason: %s', ope, str(e))
else:
self.logger.info('Operation succeed: %s', ope)
ope.delete()
+5
View File
@@ -1013,6 +1013,11 @@ class MailboxOperation(models.Model):
)
argument = models.TextField()
def __str__(self):
if self.type == 'rename':
return 'Rename %s -> %s' % (self.argument, self.mailbox.full_address)
return 'Delete %s' % self.argument
class Alias(DatesAware):
address = models.CharField(