VirtualMailManager/AliasDomain.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 08 Sep 2008 05:37:56 +0000
changeset 77 9e5af7210c29
parent 76 14c0a092d7d2
child 78 8f1e501b1bb1
permissions -rw-r--r--
* 'VirtualMailManager/__init__.py' * 'VirtualMailManager/constants/__init__.py' * 'VirtualMailManager/constants/VERSION.py' - Removed shebang
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 -*-
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     2
# Copyright 2008 VEB IT
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
# $Id$
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
     6
"""Virtual Mail Manager's AliasDomain class to manage alias domains."""
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
from constants.VERSION import VERSION
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
__author__ = 'Pascal Volk <p.volk@veb-it.de>'
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
__version__ = VERSION
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
__revision__ = 'rev '+'$Rev$'.split()[1]
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
__date__ = '$Date$'.split()[1]
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    15
from Exceptions import VMMAliasDomainException as VADE
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
import constants.ERROR as ERR
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    17
import VirtualMailManager as VMM
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    19
class AliasDomain:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
    """Class to manage e-mail alias domains."""
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    21
    def __init__(self, dbh, domainname, targetDomain=None):
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
        self._dbh = dbh
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    23
        self.__name = VMM.VirtualMailManager.chkDomainname(domainname)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    24
        self.__gid = 0
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    25
        self._domain = targetDomain
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    26
        self._exists()
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
    def _exists(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    29
        dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    30
        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
    31
 = %s', self.__name)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    32
        alias = dbc.fetchone()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    33
        dbc.close()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    34
        if alias is not None:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    35
            self.__gid, primary = alias
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    36
            if primary:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    37
                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
    38
                        self.__name, ERR.ALIASDOMAIN_ISDOMAIN)
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
    def save(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    41
        if self.__gid > 0:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    42
            raise VADE(_(u'The alias domain »%s« already exists.') %self.__name,
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 55
diff changeset
    43
                ERR.ALIASDOMAIN_EXISTS)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    44
        if self._domain is None:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    45
            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
    46
                    ERR.ALIASDOMAIN_NO_DOMDEST)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    47
        if self._domain._id < 1:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    48
            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
    49
                    self._domain._name, ERR.NO_SUCH_DOMAIN)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    50
        dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    51
        dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary)\
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    52
 VALUES (%s, %s, FALSE)', self.__name, self._domain._id)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    53
        self._dbh.commit()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    54
        dbc.close()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    55
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
    def info(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    58
        if self.__gid > 0:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    59
            dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    60
            dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s\
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    61
 AND is_primary', self.__gid)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    62
            domain = dbc.fetchone()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    63
            dbc.close()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    64
            if domain is not None:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    65
                return {'alias': self.__name, 'domain': domain[0]}
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    66
            else:# an almost unlikely case, isn't it?
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    67
                raise VADE(
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    68
                    _(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
    69
                            % self.__name, ERR.NO_SUCH_DOMAIN)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    70
        else:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    71
            raise VADE(
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    72
                  _(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
    73
                  ERR.NO_SUCH_ALIASDOMAIN)
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
    
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
    def delete(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    76
        if self.__gid > 0:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    77
            dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    78
            dbc.execute('DELETE FROM domain_name WHERE domainname = %s \
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    79
 AND NOT is_primary', self.__name)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    80
            if dbc.rowcount > 0:
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
        else:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    83
            raise VADE(
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    84
                  _(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
    85
                  ERR.NO_SUCH_ALIASDOMAIN)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    86