Server IP : 85.214.239.14 / Your IP : 3.149.255.239 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /srv/modoboa/env/lib/python3.5/site-packages/modoboa/admin/models/ |
Upload File : |
"""Models related to domain aliases management.""" from reversion import revisions as reversion from django.contrib.contenttypes.fields import GenericRelation 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 modoboa.core import models as core_models, 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"), on_delete=models.CASCADE ) 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: 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().lower() 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)