From 08b0fd0e5c187eed525c04276a9fdb241b35e5cf Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Sat, 14 Sep 2013 16:46:28 +0200 Subject: [PATCH] Log results into the database. see #482 --- modoboa/admin/app_settings.py | 4 +-- modoboa/admin/forms.py | 14 ++++----- .../commands/handle_mailbox_operations.py | 29 +++++++++---------- modoboa/admin/models.py | 5 ++++ 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/modoboa/admin/app_settings.py b/modoboa/admin/app_settings.py index 1f02db899..7b183b15c 100644 --- a/modoboa/admin/app_settings.py +++ b/modoboa/admin/app_settings.py @@ -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"] diff --git a/modoboa/admin/forms.py b/modoboa/admin/forms.py index f5e2a217d..75ec95f28 100644 --- a/modoboa/admin/forms.py +++ b/modoboa/admin/forms.py @@ -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 diff --git a/modoboa/admin/management/commands/handle_mailbox_operations.py b/modoboa/admin/management/commands/handle_mailbox_operations.py index 419766023..e606be97d 100644 --- a/modoboa/admin/management/commands/handle_mailbox_operations.py +++ b/modoboa/admin/management/commands/handle_mailbox_operations.py @@ -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() diff --git a/modoboa/admin/models.py b/modoboa/admin/models.py index 8619781b4..8c03ee7e9 100644 --- a/modoboa/admin/models.py +++ b/modoboa/admin/models.py @@ -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(