2015-01-19 21:44:57 +01:00
|
|
|
"""
|
|
|
|
This module defines the database models for mail handling.
|
|
|
|
|
|
|
|
"""
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-05-18 20:02:39 +02:00
|
|
|
from django.db import models
|
2014-05-19 22:28:25 +02:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
|
|
|
from django.utils.translation import ugettext as _
|
2014-05-24 21:56:30 +02:00
|
|
|
|
2014-05-19 22:28:25 +02:00
|
|
|
from passlib.hash import sha512_crypt
|
2014-05-24 21:56:30 +02:00
|
|
|
from model_utils.models import TimeStampedModel
|
2014-05-19 22:28:25 +02:00
|
|
|
|
2014-05-25 15:17:08 +02:00
|
|
|
from domains.models import MailDomain
|
2014-05-24 22:50:43 +02:00
|
|
|
from osusers.models import User as OsUser
|
2015-01-19 21:44:57 +01:00
|
|
|
|
|
|
|
from fileservertasks.tasks import (
|
|
|
|
create_file_mailbox,
|
|
|
|
delete_file_mailbox,
|
|
|
|
)
|
2014-05-24 22:50:43 +02:00
|
|
|
|
2014-05-19 22:28:25 +02:00
|
|
|
|
2014-05-24 14:58:54 +02:00
|
|
|
class ActivateAbleMixin(models.Model):
|
|
|
|
"""
|
|
|
|
Mixin for model classes that can be active or inactive.
|
|
|
|
|
|
|
|
"""
|
|
|
|
active = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
2014-12-27 00:24:05 +01:00
|
|
|
class MailboxManager(models.Manager):
|
2015-01-19 21:44:57 +01:00
|
|
|
"""
|
|
|
|
This is the default manager class for :py:class:`Mailbox`.
|
|
|
|
"""
|
2014-12-27 00:24:05 +01:00
|
|
|
|
|
|
|
def get_next_mailbox_name(self, osuser):
|
2015-01-19 23:00:01 +01:00
|
|
|
"""
|
|
|
|
Get the next available mailbox name for the given operating system
|
|
|
|
user.
|
|
|
|
|
|
|
|
:param osuser: a :py:class:`operating system user <osuser.models.User>`
|
|
|
|
instance
|
|
|
|
:return: name of the mailbox
|
|
|
|
:rtype: str
|
|
|
|
|
|
|
|
"""
|
2014-12-27 00:24:05 +01:00
|
|
|
count = 1
|
|
|
|
mailboxformat = "{0}p{1:02d}"
|
|
|
|
mailboxname = mailboxformat.format(osuser.username, count)
|
|
|
|
|
|
|
|
for box in self.values('username').filter(osuser=osuser).order_by(
|
|
|
|
'username'
|
|
|
|
):
|
|
|
|
if box['username'] == mailboxname:
|
|
|
|
count += 1
|
|
|
|
mailboxname = mailboxformat.format(osuser.username, count)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
return mailboxname
|
|
|
|
|
2015-01-25 22:07:54 +01:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2014-12-27 00:24:05 +01:00
|
|
|
|
2014-05-24 14:58:54 +02:00
|
|
|
@python_2_unicode_compatible
|
2015-01-24 20:58:20 +01:00
|
|
|
class Mailbox(ActivateAbleMixin, TimeStampedModel):
|
2015-01-19 23:00:01 +01:00
|
|
|
"""
|
|
|
|
This is the model class for a mailbox.
|
|
|
|
|
|
|
|
"""
|
2014-05-24 22:50:43 +02:00
|
|
|
osuser = models.ForeignKey(OsUser)
|
2014-05-19 22:28:25 +02:00
|
|
|
username = models.CharField(max_length=128, unique=True)
|
|
|
|
password = models.CharField(max_length=255)
|
|
|
|
|
2014-12-27 00:24:05 +01:00
|
|
|
objects = MailboxManager()
|
|
|
|
|
2014-05-19 22:28:25 +02:00
|
|
|
class Meta:
|
2015-01-17 12:25:54 +01:00
|
|
|
ordering = ['osuser', 'username']
|
2014-05-19 22:28:25 +02:00
|
|
|
verbose_name = _('Mailbox')
|
|
|
|
verbose_name_plural = _('Mailboxes')
|
|
|
|
|
|
|
|
def set_password(self, password):
|
2015-01-19 23:00:01 +01:00
|
|
|
"""
|
|
|
|
Set the hashed password for the mailbox.
|
|
|
|
|
|
|
|
:param str password: the clear text password
|
|
|
|
|
|
|
|
"""
|
2014-05-19 22:28:25 +02:00
|
|
|
self.password = sha512_crypt.encrypt(password)
|
|
|
|
|
2014-12-27 16:31:43 +01:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
create_file_mailbox.delay(self.osuser.username, self.username).get()
|
|
|
|
super(Mailbox, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
delete_file_mailbox.delay(self.osuser.username, self.username).get()
|
|
|
|
super(Mailbox, self).delete(*args, **kwargs)
|
|
|
|
|
2015-01-24 20:58:20 +01:00
|
|
|
def get_mailaddresses(self):
|
|
|
|
"""
|
|
|
|
Get a list of mail addresses assigned to this mailbox.
|
|
|
|
|
|
|
|
"""
|
|
|
|
addrs = [
|
|
|
|
mbadr.mailaddress for mbadr in
|
|
|
|
self.mailaddressmailbox_set.all()
|
|
|
|
]
|
|
|
|
return addrs
|
|
|
|
mailaddresses = property(get_mailaddresses)
|
|
|
|
|
2014-05-24 14:58:54 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.username
|
|
|
|
|
2014-05-19 22:28:25 +02:00
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
2014-05-24 21:56:30 +02:00
|
|
|
class MailAddress(ActivateAbleMixin, TimeStampedModel, models.Model):
|
2015-01-19 23:00:01 +01:00
|
|
|
"""
|
|
|
|
This is the model class for a mail address.
|
|
|
|
|
|
|
|
"""
|
2015-01-25 19:01:01 +01:00
|
|
|
localpart = models.CharField(_('local part'), max_length=128)
|
|
|
|
domain = models.ForeignKey(MailDomain, verbose_name=_('domain'))
|
2014-05-19 22:28:25 +02:00
|
|
|
|
|
|
|
class Meta:
|
2015-01-17 12:25:54 +01:00
|
|
|
ordering = ['domain', 'localpart']
|
2014-05-19 22:28:25 +02:00
|
|
|
unique_together = ('localpart', 'domain')
|
|
|
|
verbose_name = _('Mail address')
|
|
|
|
verbose_name_plural = _('Mail addresses')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{0}@{1}".format(self.localpart, self.domain)
|
|
|
|
|
2015-01-25 22:07:54 +01:00
|
|
|
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
|
|
|
|
|
2014-05-19 22:28:25 +02:00
|
|
|
|
2015-01-17 12:25:54 +01:00
|
|
|
@python_2_unicode_compatible
|
2014-05-24 21:56:30 +02:00
|
|
|
class MailAddressMailbox(TimeStampedModel, models.Model):
|
2015-01-19 23:00:01 +01:00
|
|
|
"""
|
|
|
|
This is the model class to assign a mail address to a mailbox.
|
|
|
|
|
|
|
|
"""
|
2015-01-24 20:58:20 +01:00
|
|
|
mailaddress = models.OneToOneField(
|
|
|
|
MailAddress, verbose_name=_('mailaddress'), primary_key=True)
|
|
|
|
mailbox = models.ForeignKey(Mailbox, verbose_name=_('mailbox'))
|
2014-05-19 22:28:25 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('mailaddress', 'mailbox')
|
|
|
|
|
2015-01-17 12:25:54 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.mailbox.username
|
|
|
|
|
2014-05-19 22:28:25 +02:00
|
|
|
|
2014-05-24 21:56:30 +02:00
|
|
|
class MailAddressForward(TimeStampedModel, models.Model):
|
2015-01-19 23:00:01 +01:00
|
|
|
"""
|
|
|
|
This is a model class to map mail addresses to forwarding addresses.
|
|
|
|
|
|
|
|
"""
|
2014-05-19 22:28:25 +02:00
|
|
|
mailaddress = models.ForeignKey(MailAddress)
|
|
|
|
target = models.EmailField(max_length=254)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('mailaddress', 'target')
|