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-24 22:50:43 +02:00
|
|
|
from osusers.models import User as OsUser
|
|
|
|
|
2014-05-19 22:28:25 +02:00
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
2014-05-24 21:56:30 +02:00
|
|
|
class MailDomain(TimeStampedModel, models.Model):
|
2014-05-19 22:28:25 +02:00
|
|
|
domain = models.CharField(max_length=128, unique=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Mail domain')
|
|
|
|
verbose_name_plural = _('Mail domains')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.domain
|
2014-05-18 20:02:39 +02:00
|
|
|
|
2014-05-18 21:35:00 +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
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
2014-05-24 21:56:30 +02:00
|
|
|
class Mailbox(ActivateAbleMixin, TimeStampedModel, models.Model):
|
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)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Mailbox')
|
|
|
|
verbose_name_plural = _('Mailboxes')
|
|
|
|
|
|
|
|
def set_password(self, password):
|
|
|
|
self.password = sha512_crypt.encrypt(password)
|
|
|
|
|
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):
|
2014-05-19 22:28:25 +02:00
|
|
|
localpart = models.CharField(max_length=128)
|
|
|
|
domain = models.ForeignKey(MailDomain)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('localpart', 'domain')
|
|
|
|
verbose_name = _('Mail address')
|
|
|
|
verbose_name_plural = _('Mail addresses')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{0}@{1}".format(self.localpart, self.domain)
|
|
|
|
|
|
|
|
|
2014-05-24 21:56:30 +02:00
|
|
|
class MailAddressMailbox(TimeStampedModel, models.Model):
|
2014-05-24 20:57:17 +02:00
|
|
|
mailaddress = models.OneToOneField(MailAddress, primary_key=True)
|
2014-05-19 22:28:25 +02:00
|
|
|
mailbox = models.ForeignKey(Mailbox)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('mailaddress', 'mailbox')
|
|
|
|
|
|
|
|
|
2014-05-24 21:56:30 +02:00
|
|
|
class MailAddressForward(TimeStampedModel, models.Model):
|
2014-05-19 22:28:25 +02:00
|
|
|
mailaddress = models.ForeignKey(MailAddress)
|
|
|
|
target = models.EmailField(max_length=254)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('mailaddress', 'target')
|