implement mail address target editing

- extract common code into managemails.forms.MailAddressFieldMixin
- move code from forms into managemails.models.MailAddress
- implement managemails.models.MailboxManager.unused and unused_or_own
- implement managemails.forms.EditMailAddressForm
- add managemails.views.EditMailAddress
- add URL pattern 'edit_mailaddress' to managemails.urls
- add template managemails/mailaddress_edit.html
- add changelog entry
This commit is contained in:
Jan Dittberner 2015-01-25 22:07:54 +01:00
parent bebcad8c86
commit 5429055f0d
6 changed files with 269 additions and 26 deletions

View file

@ -61,6 +61,30 @@ class MailboxManager(models.Manager):
break
return mailboxname
def unused_or_own(self, mailaddress, osuser):
"""
Returns a queryset that fetches those mailboxes that belong to the
given operating system user and are either not used by any mail address
or used by the given mailaddress itself.
"""
return self.filter(
models.Q(mailaddressmailbox__isnull=True) |
models.Q(mailaddressmailbox__mailaddress=mailaddress),
active=True, osuser=osuser,
)
def unused(self, osuser):
"""
Returns a queryset that fetches unused mailboxes belonging to the
given operating system user.
"""
return self.filter(
mailaddressmailbox__isnull=True,
active=True, osuser=osuser,
)
@python_2_unicode_compatible
class Mailbox(ActivateAbleMixin, TimeStampedModel):
@ -130,6 +154,70 @@ class MailAddress(ActivateAbleMixin, TimeStampedModel, models.Model):
def __str__(self):
return "{0}@{1}".format(self.localpart, self.domain)
def set_mailbox(self, mailbox, commit=True):
"""
Set the target for this mail address to the given mailbox. This method
takes care of removing forwarding addresses or changing existing
mailbox relations.
:param mailbox: :py:class:`Mailbox`
:param boolean commit: whether to persist changes or not
:return: :py:class:`MailAddressMailbox` instance
"""
if self.pk is not None:
if MailAddressMailbox.objects.filter(mailaddress=self).exists():
mabox = MailAddressMailbox.objects.get(mailaddress=self)
mabox.mailbox = mailbox
else:
mabox = MailAddressMailbox(mailaddress=self, mailbox=mailbox)
if commit:
mabox.save()
for mafwd in MailAddressForward.objects.filter(mailaddress=self):
mafwd.delete()
else:
mabox = MailAddressMailbox(mailaddress=self, mailbox=mailbox)
if commit:
mabox.save()
return mabox
def set_forward_addresses(self, addresses, commit=True):
"""
Set the forwarding addresses for this mail address to the given
addresses. This method takes care of removing other mail forwards and
mailboxes.
:param addresses: list of email address strings
:param boolean commit: whether to persist changes or not
:return: list of :py:class:`MailAddressForward` instances
"""
retval = []
if self.pk is not None:
if MailAddressMailbox.objects.filter(mailaddress=self).exists():
mabox = MailAddressMailbox.objects.get(mailaddress=self)
mabox.delete()
forwards = MailAddressForward.objects.filter(
mailaddress=self).all()
for item in forwards:
if not item.target in addresses:
item.delete()
else:
retval.append(item)
for target in addresses:
if not target in [item.target for item in forwards]:
mafwd = MailAddressForward(mailaddress=self, target=target)
if commit:
mafwd.save()
retval.append(item)
else:
for target in addresses:
mafwd = MailAddressForward(mailaddress=self, target=target)
if commit:
mafwd.save()
retval.append(item)
return retval
@python_2_unicode_compatible
class MailAddressMailbox(TimeStampedModel, models.Model):