From a5100880340546d44309923bccfec2a41c071663 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Tue, 10 Sep 2013 22:46:36 +0200 Subject: [PATCH] Don't forget aliases :p see #459 --- modoboa/admin/forms.py | 37 +++++++++++++++---- .../extensions/postfix_autoreply/__init__.py | 5 +++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/modoboa/admin/forms.py b/modoboa/admin/forms.py index 8c8cd0daf..db716e541 100644 --- a/modoboa/admin/forms.py +++ b/modoboa/admin/forms.py @@ -75,17 +75,40 @@ class DomainFormGeneral(forms.ModelForm, DynamicForm): return cleaned_data + 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 + 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 + can't issue regular UPDATE queries using the .save() method of + a Quota instance (it will trigger an INSERT as the primary key + has changed). + + So, we use this ugly hack to bypass this behaviour. It is not + perfomant at all as it will generate one query per quota + record to update. + """ + 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) + def save(self, user, commit=True): + """Custom save method + + Updating a domain may have consequences on other objects + (domain alias, mailbox, quota). The most tricky part concerns + quotas update. + + + """ d = super(DomainFormGeneral, self).save(commit=False) if commit: old_mail_homes = None - hm = parameters.get_admin("HANDLE_MAILBOXES", raise_error=False) - if hm == "yes": - if self.oldname is not None and d.name != self.oldname: - for q in Quota.objects.filter(username__contains="@%s" % self.oldname): - q.username = q.username.replace('@%s' % self.oldname, '@%s' % d.name) - q.save() - old_mail_homes = dict((mb.id, mb.mail_home) for mb in d.mailbox_set.all()) + if self.oldname is not None and d.name != self.oldname: + self.update_mailbox_quotas(d) + old_mail_homes = dict((mb.id, mb.mail_home) for mb in d.mailbox_set.all()) d.save() Mailbox.objects.filter(domain=d, use_domain_quota=True).update(quota=d.quota) if old_mail_homes is not None: diff --git a/modoboa/extensions/postfix_autoreply/__init__.py b/modoboa/extensions/postfix_autoreply/__init__.py index 6434a339e..b86855897 100644 --- a/modoboa/extensions/postfix_autoreply/__init__.py +++ b/modoboa/extensions/postfix_autoreply/__init__.py @@ -94,6 +94,11 @@ def onDomainModified(domain): return Transport.objects.filter(domain="autoreply.%s" % domain.oldname) \ .update(domain="autoreply.%s" % domain.name) + for al in Alias.objects.filter(full_address__contains="@%s" % domain.oldname): + new_address = al.full_address.replace("@%s" % domain.oldname, "@%s" % domain.name) + al.full_address = new_address + al.autoreply_address = "%s@autoreply.%s" % (new_address, domain.name) + al.save() @events.observe("DeleteDomain")