Don't forget aliases :p

see #459
This commit is contained in:
Antoine Nguyen
2013-09-10 22:46:36 +02:00
parent d86733911a
commit a510088034
2 changed files with 35 additions and 7 deletions
+30 -7
View File
@@ -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:
@@ -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")