VirtualMailManager/aliasdomain.py
author Pascal Volk <user@localhost.localdomain.org>
Mon, 10 Feb 2014 22:54:43 +0000
branchv0.7.x
changeset 718 65a29a373c19
parent 711 2a75058fc064
permissions -rw-r--r--
doc/web: Replaced vmm.cfg by a symbolic link.
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 -*-
703
58815c004a61 Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents: 675
diff changeset
     2
# Copyright (c) 2008 - 2014, Pascal Volk
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     4
"""
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     5
    VirtualMailManager.aliasdomain
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243
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
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    11
from VirtualMailManager.domain import Domain, check_domainname
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    12
from VirtualMailManager.constants import \
243
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
694
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    30
        `dbh` : psycopg2._psycopg.connection
243
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()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 257
diff changeset
    44
        dbc.execute('SELECT gid, is_primary FROM domain_name WHERE '
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    45
                    'domainname = %s', (self._name,))
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    46
        result = dbc.fetchone()
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    47
        dbc.close()
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    48
        if result:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    49
            if result[1]:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    50
                raise ADErr(_("The domain '%s' is a primary domain.") %
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    51
                            self._name, ALIASDOMAIN_ISDOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    52
            self._gid = result[0]
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    53
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    54
    def set_destination(self, dest_domain):
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    55
        """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
    56
        destination of an existing AliasDomain.
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    57
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    58
        Argument:
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
        `dest_domain` : VirtualMailManager.Domain.Domain
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    61
          the AliasDomain's destination domain
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    62
        """
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    63
        assert isinstance(dest_domain, Domain)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    64
        self._domain = dest_domain
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
    def save(self):
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    67
        """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
    68
        if self._gid > 0:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    69
            raise ADErr(_("The alias domain '%s' already exists.") %
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    70
                        self._name, ALIASDOMAIN_EXISTS)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    71
        if not self._domain:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    72
            raise ADErr(_('No destination domain set for the alias domain.'),
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    73
                        ALIASDOMAIN_NO_DOMDEST)
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    74
        if self._domain.gid < 1:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    75
            raise ADErr(_("The target domain '%s' does not exist.") %
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    76
                        self._domain.name, NO_SUCH_DOMAIN)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    77
        dbc = self._dbh.cursor()
379
7518d927d443 VMM/*: Use target column names in all INSERT statements.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    78
        dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary) '
7518d927d443 VMM/*: Use target column names in all INSERT statements.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    79
                    'VALUES (%s, %s, FALSE)', (self._name, self._domain.gid))
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    80
        self._dbh.commit()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    81
        dbc.close()
257
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 254
diff changeset
    82
        self._gid = self._domain.gid
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    83
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
    def info(self):
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    85
        """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
    86
        AliasDomain and its primary domain."""
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    87
        if self._gid < 1:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    88
            raise ADErr(_("The alias domain '%s' does not exist.") %
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    89
                        self._name, NO_SUCH_ALIASDOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    90
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 257
diff changeset
    91
        dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s AND '
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    92
                    'is_primary', (self._gid,))
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    93
        domain = dbc.fetchone()
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    94
        dbc.close()
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    95
        if domain:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    96
            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
    97
        else:  # an almost unlikely case, isn't it?
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    98
            raise ADErr(_('There is no primary domain for the alias domain '
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    99
                          "'%s'.") % self._name, NO_SUCH_DOMAIN)
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   100
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   101
    def switch(self):
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   102
        """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
   103
        set with the method `set_destination()`.
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   104
        """
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   105
        if not self._domain:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   106
            raise ADErr(_('No destination domain set for the alias domain.'),
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   107
                        ALIASDOMAIN_NO_DOMDEST)
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   108
        if self._domain.gid < 1:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   109
            raise ADErr(_("The target domain '%s' does not exist.") %
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   110
                        self._domain.name, NO_SUCH_DOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   111
        if self._gid < 1:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   112
            raise ADErr(_("The alias domain '%s' does not exist.") %
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   113
                        self._name, NO_SUCH_ALIASDOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   114
        if self._gid == self._domain.gid:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   115
            raise ADErr(_("The alias domain '%(alias)s' is already assigned "
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   116
                          "to the domain '%(domain)s'.") %
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   117
                        {'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
   118
                        ALIASDOMAIN_EXISTS)
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   119
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 257
diff changeset
   120
        dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s AND '
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   121
                    'domainname = %s AND NOT is_primary', (self._domain.gid,
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   122
                    self._gid, self._name))
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   123
        self._dbh.commit()
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   124
        dbc.close()
257
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 254
diff changeset
   125
        self._gid = self._domain.gid
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   126
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   127
    def delete(self):
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   128
        """Delete the AliasDomain's record form the database.
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   129
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   130
        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
   131
        """
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   132
        if self._gid < 1:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   133
            raise ADErr(_("The alias domain '%s' does not exist.") %
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   134
                        self._name, NO_SUCH_ALIASDOMAIN)
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   135
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 257
diff changeset
   136
        dbc.execute('DELETE FROM domain_name WHERE domainname = %s AND NOT '
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   137
                    'is_primary', (self._name,))
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   138
        if dbc.rowcount > 0:
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   139
            self._dbh.commit()
257
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 254
diff changeset
   140
            self._gid = 0
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 254
diff changeset
   141
        dbc.close()
243
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   142
30aaf2bc079c VMM/AliasDomain: some cleanups in class AliasDomain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   143
del _