mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-19 05:58:34 +02:00
97 lines
3.0 KiB
Python
97 lines
3.0 KiB
Python
"""Models related to domain aliases management."""
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from django.db import models
|
|
from django.utils.encoding import python_2_unicode_compatible, smart_text
|
|
from django.utils.translation import ugettext as _, ugettext_lazy
|
|
|
|
from django.contrib.contenttypes.fields import GenericRelation
|
|
|
|
from reversion import revisions as reversion
|
|
|
|
from modoboa.core import models as core_models
|
|
from modoboa.core import signals as core_signals
|
|
from modoboa.lib.exceptions import BadRequest, Conflict
|
|
|
|
from .base import AdminObject
|
|
from .domain import Domain
|
|
|
|
|
|
class DomainAliasManager(models.Manager):
|
|
|
|
def get_for_admin(self, admin):
|
|
"""Return the domain aliases belonging to this admin.
|
|
|
|
The result is a ``QuerySet`` object, so this function can be used
|
|
to fill ``ModelChoiceField`` objects.
|
|
"""
|
|
if admin.is_superuser:
|
|
return self.get_queryset()
|
|
return self.get_queryset().filter(owners__user=admin)
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
class DomainAlias(AdminObject):
|
|
|
|
"""Domain aliases."""
|
|
|
|
name = models.CharField(ugettext_lazy("name"), max_length=100, unique=True,
|
|
help_text=ugettext_lazy("The alias name"))
|
|
target = models.ForeignKey(
|
|
Domain, verbose_name=ugettext_lazy('target'),
|
|
help_text=ugettext_lazy("The domain this alias points to")
|
|
)
|
|
enabled = models.BooleanField(
|
|
ugettext_lazy('enabled'),
|
|
help_text=ugettext_lazy("Check to activate this alias"),
|
|
default=True
|
|
)
|
|
|
|
owners = GenericRelation(core_models.ObjectAccess)
|
|
|
|
objects = DomainAliasManager()
|
|
|
|
class Meta:
|
|
permissions = (
|
|
("view_domaliases", "View domain aliases"),
|
|
)
|
|
app_label = "admin"
|
|
|
|
def __str__(self):
|
|
return smart_text(self.name)
|
|
|
|
def from_csv(self, user, row):
|
|
"""Create a domain alias from a CSV row
|
|
|
|
Expected format: ["domainalias", domain alias name, targeted domain, enabled]
|
|
|
|
:param user: a ``User`` object
|
|
:param row: a list containing the alias definition
|
|
"""
|
|
if len(row) < 4:
|
|
raise BadRequest(_("Invalid line"))
|
|
self.name = row[1].strip()
|
|
for model in [DomainAlias, Domain]:
|
|
if model.objects.filter(name=self.name).exists():
|
|
raise Conflict
|
|
domname = row[2].strip()
|
|
try:
|
|
self.target = Domain.objects.get(name=domname)
|
|
except Domain.DoesNotExist:
|
|
raise BadRequest(_("Unknown domain %s") % domname)
|
|
core_signals.can_create_object.send(
|
|
sender="import", context=self.target, object_type="domain_aliases")
|
|
self.enabled = row[3].strip().lower() in ["true", "1", "yes", "y"]
|
|
self.save(creator=user)
|
|
|
|
def to_csv(self, csvwriter):
|
|
"""Export a domain alias using CSV format
|
|
|
|
:param csvwriter: a ``csv.writer`` object
|
|
"""
|
|
csvwriter.writerow(["domainalias", self.name,
|
|
self.target.name, self.enabled])
|
|
|
|
reversion.register(DomainAlias)
|