VirtualMailManager/AliasDomain.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 31 Aug 2009 04:58:45 +0000
changeset 136 fb61f64e6351
parent 133 2d5c4745efec
child 138 617f27715b01
permissions -rw-r--r--
Reorganized imports, eliminated a potential UnicodeEncodeError. Removed double definition of function w_std() in Config class.
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 -*-
102
485d3f7d6981 Removed svn keywords, more POSIX compliant shell scripts
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
     2
# Copyright (c) 2008 - 2009, VEB IT
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
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
     5
"""Virtual Mail Manager's AliasDomain class to manage alias domains."""
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
102
485d3f7d6981 Removed svn keywords, more POSIX compliant shell scripts
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
     7
from constants.VERSION import *
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
     9
from Exceptions import VMMAliasDomainException as VADE
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
import constants.ERROR as ERR
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    11
import VirtualMailManager as VMM
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    13
class AliasDomain(object):
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
    """Class to manage e-mail alias domains."""
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    15
    __slots__ = ('__gid', '__name', '_domain', '_dbh')
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    16
    def __init__(self, dbh, domainname, targetDomain=None):
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
        self._dbh = dbh
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    18
        self.__name = VMM.VirtualMailManager.chkDomainname(domainname)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    19
        self.__gid = 0
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    20
        self._domain = targetDomain
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    21
        self._exists()
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
    def _exists(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    24
        dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    25
        dbc.execute('SELECT gid, is_primary FROM domain_name WHERE domainname\
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    26
 = %s', self.__name)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    27
        alias = dbc.fetchone()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    28
        dbc.close()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    29
        if alias is not None:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    30
            self.__gid, primary = alias
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    31
            if primary:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
    32
                raise VADE(_(u"The domain “%s” is a primary domain.") %
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 55
diff changeset
    33
                        self.__name, ERR.ALIASDOMAIN_ISDOMAIN)
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
    def save(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    36
        if self.__gid > 0:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
    37
            raise VADE(_(u'The alias domain “%s” already exists.') %self.__name,
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    38
                    ERR.ALIASDOMAIN_EXISTS)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    39
        if self._domain is None:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    40
            raise VADE(_(u'No destination domain for alias domain denoted.'),
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 55
diff changeset
    41
                    ERR.ALIASDOMAIN_NO_DOMDEST)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    42
        if self._domain._id < 1:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
    43
            raise VADE (_(u"The target domain “%s” doesn't exist yet.") %
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    44
                    self._domain._name, ERR.NO_SUCH_DOMAIN)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    45
        dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    46
        dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary)\
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    47
 VALUES (%s, %s, FALSE)', self.__name, self._domain._id)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    48
        self._dbh.commit()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    49
        dbc.close()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    50
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
    def info(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    52
        if self.__gid > 0:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    53
            dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    54
            dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s\
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    55
 AND is_primary', self.__gid)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    56
            domain = dbc.fetchone()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    57
            dbc.close()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    58
            if domain is not None:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    59
                return {'alias': self.__name, 'domain': domain[0]}
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    60
            else:# an almost unlikely case, isn't it?
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    61
                raise VADE(
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
    62
                    _(u'There is no primary domain for the alias domain “%s”.')\
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    63
                            % self.__name, ERR.NO_SUCH_DOMAIN)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    64
        else:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
    65
            raise VADE(_(u"The alias domain “%s” doesn't exist yet.") %
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    66
                    self.__name, ERR.NO_SUCH_ALIASDOMAIN)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    67
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    68
    def switch(self):
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    69
        if self._domain is None:
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    70
            raise VADE(_(u'No destination domain for alias domain denoted.'),
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    71
                    ERR.ALIASDOMAIN_NO_DOMDEST)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    72
        if self._domain._id < 1:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
    73
            raise VADE (_(u"The target domain “%s” doesn't exist yet.") %
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    74
                    self._domain._name, ERR.NO_SUCH_DOMAIN)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    75
        if self.__gid < 1:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
    76
            raise VADE(_(u"The alias domain “%s” doesn't exist yet.") %
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    77
                    self.__name, ERR.NO_SUCH_ALIASDOMAIN)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    78
        if self.__gid == self._domain._id:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
    79
            raise VADE(_(u"The alias domain “%(alias)s” is already assigned to\
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
    80
 the domain “%(domain)s”.") %
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    81
                    {'alias': self.__name, 'domain': self._domain._name},
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    82
                    ERR.ALIASDOMAIN_EXISTS)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    83
        dbc = self._dbh.cursor()
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    84
        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
    85
 AND domainname = %s AND NOT is_primary',
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    86
                self._domain._id, self.__gid, self.__name)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    87
        self._dbh.commit()
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    88
        dbc.close()
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    89
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
    def delete(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    91
        if self.__gid > 0:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    92
            dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    93
            dbc.execute('DELETE FROM domain_name WHERE domainname = %s \
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    94
 AND NOT is_primary', self.__name)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    95
            if dbc.rowcount > 0:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    96
                self._dbh.commit()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    97
        else:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    98
            raise VADE(
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
    99
                  _(u"The alias domain “%s” doesn't exist yet.") % self.__name,
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 55
diff changeset
   100
                  ERR.NO_SUCH_ALIASDOMAIN)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   101