102 lines
3 KiB
Python
102 lines
3 KiB
Python
from django.db import models
|
|
from django.utils.encoding import python_2_unicode_compatible
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from passlib.hash import sha512_crypt
|
|
from model_utils.models import TimeStampedModel
|
|
|
|
from domains.models import MailDomain
|
|
from osusers.models import User as OsUser
|
|
from osusers.tasks import create_file_mailbox, delete_file_mailbox
|
|
|
|
|
|
class ActivateAbleMixin(models.Model):
|
|
"""
|
|
Mixin for model classes that can be active or inactive.
|
|
|
|
"""
|
|
active = models.BooleanField(default=True)
|
|
|
|
class Meta:
|
|
abstract = True
|
|
|
|
|
|
class MailboxManager(models.Manager):
|
|
|
|
def get_next_mailbox_name(self, osuser):
|
|
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
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
class Mailbox(ActivateAbleMixin, TimeStampedModel, models.Model):
|
|
osuser = models.ForeignKey(OsUser)
|
|
username = models.CharField(max_length=128, unique=True)
|
|
password = models.CharField(max_length=255)
|
|
|
|
objects = MailboxManager()
|
|
|
|
class Meta:
|
|
ordering = ['osuser', 'username']
|
|
verbose_name = _('Mailbox')
|
|
verbose_name_plural = _('Mailboxes')
|
|
|
|
def set_password(self, password):
|
|
self.password = sha512_crypt.encrypt(password)
|
|
|
|
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)
|
|
|
|
def __str__(self):
|
|
return self.username
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
class MailAddress(ActivateAbleMixin, TimeStampedModel, models.Model):
|
|
localpart = models.CharField(max_length=128)
|
|
domain = models.ForeignKey(MailDomain)
|
|
|
|
class Meta:
|
|
ordering = ['domain', 'localpart']
|
|
unique_together = ('localpart', 'domain')
|
|
verbose_name = _('Mail address')
|
|
verbose_name_plural = _('Mail addresses')
|
|
|
|
def __str__(self):
|
|
return "{0}@{1}".format(self.localpart, self.domain)
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
class MailAddressMailbox(TimeStampedModel, models.Model):
|
|
mailaddress = models.OneToOneField(MailAddress, primary_key=True)
|
|
mailbox = models.ForeignKey(Mailbox)
|
|
|
|
class Meta:
|
|
unique_together = ('mailaddress', 'mailbox')
|
|
|
|
def __str__(self):
|
|
return self.mailbox.username
|
|
|
|
|
|
class MailAddressForward(TimeStampedModel, models.Model):
|
|
mailaddress = models.ForeignKey(MailAddress)
|
|
target = models.EmailField(max_length=254)
|
|
|
|
class Meta:
|
|
unique_together = ('mailaddress', 'target')
|