VirtualMailManager/AliasDomain.py
author Pascal Volk <neverseen@users.sourceforge.net>
Fri, 30 Apr 2010 00:01:15 +0000
branchv0.6.x
changeset 280 db35d2eec518
parent 257 5b8fde01e4f0
child 290 e2785e04f92e
permissions -rw-r--r--
VMM/{Config,Handler}: moved Config.install() to Handler.cfg_install(). Handler.cfg_install() must be called explicitly. To avoid 'strange' problems.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
162
0ac9ef587769 Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 155
diff changeset
     2
# Copyright (c) 2008 - 2010, Pascal Volk
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     5
"""
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     6
    VirtualMailManager.AliasDomain
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     7
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     8
    Virtual Mail Manager's AliasDomain class to manage alias domains.
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     9
"""
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
254
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 243
diff changeset
    11
from VirtualMailManager.Domain import Domain, check_domainname
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    12
from VirtualMailManager.constants.ERROR import \
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    13
     ALIASDOMAIN_EXISTS, ALIASDOMAIN_ISDOMAIN, ALIASDOMAIN_NO_DOMDEST, \
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    14
     NO_SUCH_ALIASDOMAIN, NO_SUCH_DOMAIN
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    15
from VirtualMailManager.errors import AliasDomainError as ADErr
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    16
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    17
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    18
_ = lambda msg: msg
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    19
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    21
class AliasDomain(object):
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
    """Class to manage e-mail alias domains."""
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    23
    __slots__ = ('_gid', '_name', '_domain', '_dbh')
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    24
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    25
    def __init__(self, dbh, domainname):
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    26
        """Creates a new AliasDomain instance.
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    27
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    28
        Arguments:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    29
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    30
        `dbh` : pyPgSQL.PgSQL.Connection
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    31
          a database connection for the database access
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    32
        `domainname` : basestring
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    33
          the name of the AliasDomain"""
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
        self._dbh = dbh
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    35
        self._name = check_domainname(domainname)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    36
        self._gid = 0
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    37
        self._domain = None
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    38
        self._load()
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    40
    def _load(self):
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    41
        """Loads the AliasDomain's GID from the database and checks if the
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    42
        domain name is marked as primary."""
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    43
        dbc = self._dbh.cursor()
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    44
        dbc.execute(
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    45
            'SELECT gid, is_primary FROM domain_name WHERE domainname = %s',
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    46
                    self._name)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    47
        result = dbc.fetchone()
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    48
        dbc.close()
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    49
        if result:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    50
            if result[1]:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    51
                raise ADErr(_(u"The domain '%s' is a primary domain.") %
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    52
                            self._name, ALIASDOMAIN_ISDOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    53
            self._gid = result[0]
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    54
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    55
    def set_destination(self, dest_domain):
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    56
        """Set the destination of a new AliasDomain or updates the
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    57
        destination of an existing AliasDomain.
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    58
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    59
        Argument:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    60
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    61
        `dest_domain` : VirtualMailManager.Domain.Domain
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    62
          the AliasDomain's destination domain
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    63
        """
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    64
        assert isinstance(dest_domain, Domain)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    65
        self._domain = dest_domain
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
    def save(self):
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    68
        """Stores information about the new AliasDomain in the database."""
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    69
        if self._gid > 0:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    70
            raise ADErr(_(u"The alias domain '%s' already exists.") %
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    71
                        self._name, ALIASDOMAIN_EXISTS)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    72
        if not self._domain:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    73
            raise ADErr(_(u'No destination domain set for the alias domain.'),
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    74
                        ALIASDOMAIN_NO_DOMDEST)
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    75
        if self._domain.gid < 1:
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    76
            raise ADErr(_(u"The target domain '%s' doesn't exist.") %
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    77
                        self._domain.name, NO_SUCH_DOMAIN)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    78
        dbc = self._dbh.cursor()
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    79
        dbc.execute('INSERT INTO domain_name VALUES (%s, %s, FALSE)',
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    80
                    self._name, self._domain.gid)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    81
        self._dbh.commit()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    82
        dbc.close()
257
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 254
diff changeset
    83
        self._gid = self._domain.gid
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    84
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
    def info(self):
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    86
        """Returns a dict (keys: "alias" and "domain") with the names of the
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    87
        AliasDomain and its primary domain."""
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    88
        if self._gid < 1:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    89
            raise ADErr(_(u"The alias domain '%s' doesn't exist.") %
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    90
                        self._name, NO_SUCH_ALIASDOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    91
        dbc = self._dbh.cursor()
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    92
        dbc.execute(
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    93
            'SELECT domainname FROM domain_name WHERE gid = %s AND is_primary',
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    94
                    self._gid)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    95
        domain = dbc.fetchone()
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    96
        dbc.close()
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    97
        if domain:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    98
            return {'alias': self._name, 'domain': domain[0]}
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    99
        else:  # an almost unlikely case, isn't it?
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   100
            raise ADErr(
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   101
                   _(u"There is no primary domain for the alias domain '%s'.")\
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   102
                        % self._name, NO_SUCH_DOMAIN)
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   103
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   104
    def switch(self):
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   105
        """Switch the destination of the AliasDomain to the new destination,
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   106
        set with the method `set_destination()`.
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   107
        """
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   108
        if not self._domain:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   109
            raise ADErr(_(u'No destination domain set for the alias domain.'),
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   110
                        ALIASDOMAIN_NO_DOMDEST)
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   111
        if self._domain.gid < 1:
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   112
            raise ADErr(_(u"The target domain '%s' doesn't exist.") %
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   113
                        self._domain.name, NO_SUCH_DOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   114
        if self._gid < 1:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   115
            raise ADErr(_(u"The alias domain '%s' doesn't exist.") %
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   116
                        self._name, NO_SUCH_ALIASDOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   117
        if self._gid == self._domain.gid:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   118
            raise ADErr(_(u"The alias domain '%(alias)s' is already assigned\
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   119
 to the domain '%(domain)s'.") %
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   120
                        {'alias': self._name, 'domain': self._domain.name},
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   121
                        ALIASDOMAIN_EXISTS)
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   122
        dbc = self._dbh.cursor()
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   123
        dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s\
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   124
 AND domainname = %s AND NOT is_primary',
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   125
                    self._domain.gid, self._gid, self._name)
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   126
        self._dbh.commit()
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   127
        dbc.close()
257
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 254
diff changeset
   128
        self._gid = self._domain.gid
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   129
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   130
    def delete(self):
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   131
        """Delete the AliasDomain's record form the database.
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   132
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   133
        Raises an AliasDomainError if the AliasDomain doesn't exist.
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   134
        """
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   135
        if self._gid < 1:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   136
            raise ADErr(_(u"The alias domain '%s' doesn't exist.") %
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   137
                        self._name, NO_SUCH_ALIASDOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   138
        dbc = self._dbh.cursor()
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   139
        dbc.execute(
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   140
            'DELETE FROM domain_name WHERE domainname = %s AND NOT is_primary',
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   141
                    self._name)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   142
        if dbc.rowcount > 0:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   143
            self._dbh.commit()
257
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 254
diff changeset
   144
            self._gid = 0
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 254
diff changeset
   145
        dbc.close()
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   146
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   147
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   148
del _