VirtualMailManager/Domain.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 22 Feb 2010 10:44:13 +0000
branchv0.6.x
changeset 211 0b129678cfe1
parent 199 0684790fff7c
child 216 0c8c053b451c
permissions -rw-r--r--
VMM/Config: LazyConfig.getboolean(), convert the value in our getboolean. So RawConfigParser hasn't to do the whole get() stuff again.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
bb0aa2102206 Initial import @sf.net
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) 2007 - 2010, Pascal Volk
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
3
a9b44e04bf01 * VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
     5
"""Virtual Mail Manager's Domain class to manage e-mail domains."""
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
from random import choice
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
     9
from VirtualMailManager import check_domainname
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    10
from VirtualMailManager.constants.ERROR import \
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    11
     ACCOUNT_AND_ALIAS_PRESENT, ACCOUNT_PRESENT, ALIAS_PRESENT, \
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    12
     DOMAIN_ALIAS_EXISTS, DOMAIN_EXISTS, NO_SUCH_DOMAIN
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    13
from VirtualMailManager.Exceptions import VMMDomainException as VMMDE
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    14
from VirtualMailManager.Transport import Transport
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    15
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    19
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 110
diff changeset
    20
class Domain(object):
3
a9b44e04bf01 * VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    21
    """Class to manage e-mail domains."""
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 110
diff changeset
    22
    __slots__ = ('_basedir','_domaindir','_id','_name','_transport','_dbh')
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    23
    def __init__(self, dbh, domainname, basedir=None, transport=None):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
        """Creates a new Domain instance.
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 110
diff changeset
    25
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
        Keyword arguments:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
        dbh -- a pyPgSQL.PgSQL.connection
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
        domainname -- name of the domain (str)
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    29
        transport -- default vmm.cfg/misc/transport  (str)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
        self._dbh = dbh
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    32
        self._name = check_domainname(domainname)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        self._basedir = basedir
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    34
        if transport is not None:
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    35
            self._transport = Transport(self._dbh, transport=transport)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
        else:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
            self._transport = transport
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
        self._id = 0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        self._domaindir = None
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    40
        if not self._exists() and self._isAlias():
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    41
            raise VMMDE(_(u"The domain “%s” is an alias domain.") % self._name,
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    42
                        DOMAIN_ALIAS_EXISTS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
    def _exists(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
        """Checks if the domain already exists.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
        If the domain exists _id will be set and returns True, otherwise False
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
        will be returned.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
        dbc = self._dbh.cursor()
43
92a6132940f5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 35
diff changeset
    51
        dbc.execute("SELECT gid, tid, domaindir FROM domain_data WHERE gid =\
45
9e66138aad0b * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 44
diff changeset
    52
 (SELECT gid FROM domain_name WHERE domainname = %s AND is_primary)",
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    53
                self._name)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
        result = dbc.fetchone()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
        if result is not None:
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    57
            self._id, self._domaindir = result[0], result[2]
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    58
            self._transport = Transport(self._dbh, tid=result[1])
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
            return True
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        else:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
            return False
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    63
    def _isAlias(self):
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    64
        """Checks if self._name is known for an alias domain."""
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    65
        dbc = self._dbh.cursor()
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    66
        dbc.execute('SELECT is_primary FROM domain_name WHERE domainname = %s',
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    67
                self._name)
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    68
        result = dbc.fetchone()
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    69
        dbc.close()
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    70
        if result is not None and not result[0]:
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    71
            return True
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    72
        else:
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    73
            return False
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    74
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
    def _setID(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
        """Sets the ID of the domain."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
        dbc = self._dbh.cursor()
43
92a6132940f5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 35
diff changeset
    78
        dbc.execute("SELECT nextval('domain_gid')")
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
        self._id = dbc.fetchone()[0]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    def _prepare(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
        self._setID()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
        self._domaindir = "%s/%s/%i" % (self._basedir, choice(MAILDIR_CHARS),
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
                self._id)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
    def _has(self, what):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        """Checks if aliases or accounts are assigned to the domain.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        If there are assigned accounts or aliases True will be returned,
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        otherwise False will be returned.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        Keyword arguments:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
        what -- 'alias' or 'users' (strings)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
        if what not in ['alias', 'users']:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
            return False
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
        if what == 'users':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
            dbc.execute("SELECT count(gid) FROM users WHERE gid=%s", self._id)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
        else:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
            dbc.execute("SELECT count(gid) FROM alias WHERE gid=%s", self._id)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   103
        count = dbc.fetchone()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
        if count[0] > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   106
            return True
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
        else:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   108
            return False
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   109
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   110
    def _chkDelete(self, delUser, delAlias):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   111
        """Checks dependencies for deletion.
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 110
diff changeset
   112
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   113
        Keyword arguments:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
        delUser -- ignore available accounts (bool)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   115
        delAlias -- ignore available aliases (bool)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   116
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   117
        if not delUser:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   118
            hasUser = self._has('users')
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   119
        else:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   120
            hasUser = False
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   121
        if not delAlias:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   122
            hasAlias = self._has('alias')
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   123
        else:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   124
            hasAlias = False
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   125
        if hasUser and hasAlias:
50
927b0705d31a * 'VirtualMailManager/Domain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   126
            raise VMMDE(_(u'There are accounts and aliases.'),
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   127
                ACCOUNT_AND_ALIAS_PRESENT)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   128
        elif hasUser:
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   129
            raise VMMDE(_(u'There are accounts.'), ACCOUNT_PRESENT)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   130
        elif hasAlias:
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   131
            raise VMMDE(_(u'There are aliases.'), ALIAS_PRESENT)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   132
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   133
    def save(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   134
        """Stores the new domain in the database."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   135
        if self._id < 1:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   136
            self._prepare()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   137
            dbc = self._dbh.cursor()
43
92a6132940f5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 35
diff changeset
   138
            dbc.execute("INSERT INTO domain_data (gid, tid, domaindir)\
92a6132940f5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 35
diff changeset
   139
 VALUES (%s, %s, %s)", self._id, self._transport.getID(), self._domaindir)
92a6132940f5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 35
diff changeset
   140
            dbc.execute("INSERT INTO domain_name (domainname, gid, is_primary)\
92a6132940f5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 35
diff changeset
   141
 VALUES (%s, %s, %s)", self._name, self._id, True)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   142
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   143
            dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   144
        else:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
   145
            raise VMMDE(_(u'The domain “%s” already exists.') % self._name,
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   146
                        DOMAIN_EXISTS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   147
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   148
    def delete(self, delUser=False, delAlias=False):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   149
        """Deletes the domain.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   150
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   151
        Keyword arguments:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   152
        delUser -- force deletion of available accounts (bool)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   153
        delAlias -- force deletion of available aliases (bool)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   154
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   155
        if self._id > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   156
            self._chkDelete(delUser, delAlias)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   157
            dbc = self._dbh.cursor()
45
9e66138aad0b * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 44
diff changeset
   158
            for t in ('alias','users','relocated','domain_name','domain_data'):
9e66138aad0b * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 44
diff changeset
   159
                dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   160
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   161
            dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   162
        else:
155
eb866ebb9f2e Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 142
diff changeset
   163
            raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   164
                        NO_SUCH_DOMAIN)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   165
50
927b0705d31a * 'VirtualMailManager/Domain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   166
    def updateTransport(self, transport, force=False):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   167
        """Sets a new transport for the domain.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   168
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   169
        Keyword arguments:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   170
        transport -- the new transport (str)
18
c98e08791ee8 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   171
        force -- True/False force new transport for all accounts (bool)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   172
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   173
        if self._id > 0:
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
   174
            if transport == self._transport.getTransport():
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
   175
                return
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   176
            trsp = Transport(self._dbh, transport=transport)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   177
            dbc = self._dbh.cursor()
43
92a6132940f5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 35
diff changeset
   178
            dbc.execute("UPDATE domain_data SET tid = %s WHERE gid = %s",
92a6132940f5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 35
diff changeset
   179
                    trsp.getID(), self._id)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   180
            if dbc.rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   181
                self._dbh.commit()
18
c98e08791ee8 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   182
            if force:
43
92a6132940f5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 35
diff changeset
   183
                dbc.execute("UPDATE users SET tid = %s WHERE gid = %s",
18
c98e08791ee8 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   184
                        trsp.getID(), self._id)
c98e08791ee8 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   185
                if dbc.rowcount > 0:
c98e08791ee8 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   186
                    self._dbh.commit()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   187
            dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   188
        else:
155
eb866ebb9f2e Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 142
diff changeset
   189
            raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   190
                        NO_SUCH_DOMAIN)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   191
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   192
    def getID(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   193
        """Returns the ID of the domain."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   194
        return self._id
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   195
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   196
    def getDir(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   197
        """Returns the directory of the domain."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   198
        return self._domaindir
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   199
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   200
    def getTransport(self):
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   201
        """Returns domain's transport."""
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   202
        return self._transport.getTransport()
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   203
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   204
    def getTransportID(self):
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   205
        """Returns the ID from the domain's transport."""
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   206
        return self._transport.getID()
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   207
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   208
    def getInfo(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   209
        """Returns a dictionary with information about the domain."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   210
        sql = """\
80
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   211
SELECT gid, domainname, transport, domaindir, aliasdomains, accounts,
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   212
       aliases, relocated
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   213
  FROM vmm_domain_info
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   214
 WHERE gid = %i""" % self._id
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   215
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   216
        dbc.execute(sql)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   217
        info = dbc.fetchone()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   218
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   219
        if info is None:
155
eb866ebb9f2e Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 142
diff changeset
   220
            raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   221
                        NO_SUCH_DOMAIN)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   222
        else:
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   223
            keys = ['gid', 'domainname', 'transport', 'domaindir',
80
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   224
                    'aliasdomains', 'accounts', 'aliases', 'relocated']
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   225
            return dict(zip(keys, info))
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   226
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   227
    def getAccounts(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   228
        """Returns a list with all accounts from the domain."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   229
        dbc = self._dbh.cursor()
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   230
        dbc.execute("SELECT local_part from users where gid = %s ORDER BY\
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   231
 local_part", self._id)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   232
        users = dbc.fetchall()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   233
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   234
        accounts = []
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   235
        if len(users) > 0:
142
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   236
            addr = u'@'.join
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   237
            _dom = self._name
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   238
            accounts = [addr((account[0], _dom)) for account in users]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   239
        return accounts
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   240
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   241
    def getAliases(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   242
        """Returns a list with all aliases from the domain."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   243
        dbc = self._dbh.cursor()
58
1692da96ec17 * 'VirtualMailManager/Domain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 56
diff changeset
   244
        dbc.execute("SELECT DISTINCT address FROM alias WHERE gid = %s\
1692da96ec17 * 'VirtualMailManager/Domain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 56
diff changeset
   245
 ORDER BY address",  self._id)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   246
        addresses = dbc.fetchall()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   247
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   248
        aliases = []
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   249
        if len(addresses) > 0:
142
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   250
            addr = u'@'.join
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   251
            _dom = self._name
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   252
            aliases = [addr((alias[0], _dom)) for alias in addresses]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   253
        return aliases
35
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   254
80
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   255
    def getRelocated(self):
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   256
        """Returns a list with all addresses from relocated users."""
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   257
        dbc = self._dbh.cursor()
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   258
        dbc.execute("SELECT address FROM relocated WHERE gid = %s\
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   259
 ORDER BY address", self._id)
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   260
        addresses = dbc.fetchall()
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   261
        dbc.close()
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   262
        relocated = []
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   263
        if len(addresses) > 0:
142
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   264
            addr = u'@'.join
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   265
            _dom = self._name
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   266
            relocated = [addr((address[0], _dom)) for address in addresses]
80
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   267
        return relocated
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   268
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   269
    def getAliaseNames(self):
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   270
        """Returns a list with all alias names from the domain."""
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   271
        dbc = self._dbh.cursor()
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   272
        dbc.execute("SELECT domainname FROM domain_name WHERE gid = %s\
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
   273
 AND NOT is_primary ORDER BY domainname", self._id)
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   274
        anames = dbc.fetchall()
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   275
        dbc.close()
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   276
        aliasdomains = []
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   277
        if len(anames) > 0:
142
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   278
            aliasdomains = [aname[0] for aname in anames]
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   279
        return aliasdomains
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   280
198
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   281
35
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   282
def search(dbh, pattern=None, like=False):
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 110
diff changeset
   283
    if pattern is not None and like is False:
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   284
        pattern = check_domainname(pattern)
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   285
    sql = 'SELECT gid, domainname, is_primary FROM domain_name'
35
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   286
    if pattern is None:
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   287
        pass
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   288
    elif like:
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   289
        sql += " WHERE domainname LIKE '%s'" % pattern
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   290
    else:
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   291
        sql += " WHERE domainname = '%s'" % pattern
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   292
    sql += ' ORDER BY is_primary DESC, domainname'
35
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   293
    dbc = dbh.cursor()
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   294
    dbc.execute(sql)
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   295
    doms = dbc.fetchall()
35
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   296
    dbc.close()
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   297
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   298
    domdict = {}
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   299
    order = [dom[0] for dom in doms if dom[2]]
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   300
    if len(order) == 0:
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   301
        for dom in doms:
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   302
            if dom[0] not in order:
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   303
                order.append(dom[0])
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   304
    for gid, dom, is_primary in doms:
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   305
        if is_primary:
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   306
            domdict[gid] = [dom]
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   307
        else:
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   308
            try:
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   309
                domdict[gid].append(dom)
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   310
            except KeyError:
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   311
                domdict[gid] = [None, dom]
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   312
    del doms
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   313
    return order, domdict
45
9e66138aad0b * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 44
diff changeset
   314
198
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   315
def get_gid(dbh, domainname):
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   316
    """Returns the *GID* of the domain *domainname*.
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   317
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   318
    Raises an `VMMDomainException` if the domain does not exist.
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   319
    """
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   320
    domainname = check_domainname(domainname)
198
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   321
    dbc = dbh.cursor()
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   322
    dbc.execute('SELECT gid FROM domain_name WHERE domainname=%s', domainname)
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   323
    gid = dbc.fetchone()
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   324
    dbc.close()
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   325
    if gid:
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   326
        return gid[0]
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   327
    else:
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   328
        raise VMMDE(_(u"The domain “%s” doesn't exist.") % domainname,
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   329
                    NO_SUCH_DOMAIN)