VirtualMailManager/AliasDomain.py
author Pascal Volk <neverseen@users.sourceforge.net>
Thu, 15 Apr 2010 20:21:18 +0000
branchv0.6.x
changeset 250 73cd082cd724
parent 243 30aaf2bc079c
child 254 8aecc83a0d32
permissions -rw-r--r--
VMM/{Account,Alias,Relocated}: added a address property to the classes Account, Alias and Relocated.
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
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    11
from VirtualMailManager import check_domainname
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    12
from VirtualMailManager.Domain import Domain
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    13
from VirtualMailManager.constants.ERROR import \
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    14
     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
    15
     NO_SUCH_ALIASDOMAIN, NO_SUCH_DOMAIN
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    16
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
    17
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    18
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    19
_ = lambda msg: msg
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    20
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    22
class AliasDomain(object):
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
    """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
    24
    __slots__ = ('_gid', '_name', '_domain', '_dbh')
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    25
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    26
    def __init__(self, dbh, domainname):
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    27
        """Creates a new AliasDomain instance.
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    28
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    29
        Arguments:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    30
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    31
        `dbh` : pyPgSQL.PgSQL.Connection
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    32
          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
    33
        `domainname` : basestring
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    34
          the name of the AliasDomain"""
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
        self._dbh = dbh
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    36
        self._name = check_domainname(domainname)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    37
        self._gid = 0
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    38
        self._domain = None
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    39
        self._load()
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    41
    def _load(self):
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    42
        """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
    43
        domain name is marked as primary."""
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    44
        dbc = self._dbh.cursor()
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    45
        dbc.execute(
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    46
            '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
    47
                    self._name)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    48
        result = dbc.fetchone()
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    49
        dbc.close()
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    50
        if result:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    51
            if result[1]:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    52
                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
    53
                            self._name, ALIASDOMAIN_ISDOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    54
            self._gid = result[0]
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    55
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    56
    def set_destination(self, dest_domain):
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    57
        """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
    58
        destination of an existing AliasDomain.
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    59
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    60
        Argument:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    61
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    62
        `dest_domain` : VirtualMailManager.Domain.Domain
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    63
          the AliasDomain's destination domain
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    64
        """
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    65
        assert isinstance(dest_domain, Domain)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    66
        self._domain = dest_domain
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
    def save(self):
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    69
        """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
    70
        if self._gid > 0:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    71
            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
    72
                        self._name, ALIASDOMAIN_EXISTS)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    73
        if not self._domain:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    74
            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
    75
                        ALIASDOMAIN_NO_DOMDEST)
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    76
        if self._domain.gid < 1:
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    77
            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
    78
                        self._domain.name, NO_SUCH_DOMAIN)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    79
        dbc = self._dbh.cursor()
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    80
        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
    81
                    self._name, self._domain.gid)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    82
        self._dbh.commit()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    83
        dbc.close()
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()
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   128
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   129
    def delete(self):
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   130
        """Delete the AliasDomain's record form the database.
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   131
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   132
        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
   133
        """
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   134
        if self._gid < 1:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   135
            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
   136
                        self._name, NO_SUCH_ALIASDOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   137
        dbc = self._dbh.cursor()
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   138
        dbc.execute(
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   139
            '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
   140
                    self._name)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   141
        if dbc.rowcount > 0:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   142
            self._dbh.commit()
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   143
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   144
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   145
del _