VirtualMailManager/Domain.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sun, 09 May 2010 06:20:52 +0000
branchv0.6.x
changeset 290 e2785e04f92e
parent 254 8aecc83a0d32
child 294 7d1bafc6fa30
permissions -rw-r--r--
VMM/…: re-indented long queries and error messages.
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
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
     5
"""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
     6
    VirtualMailManager.Domain
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
     8
    Virtual Mail Manager's Domain class to manage e-mail domains.
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
     9
"""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    10
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    11
import os
254
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
    12
import re
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
    13
from encodings.idna import ToASCII, ToUnicode
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
from random import choice
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    16
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
    17
     ACCOUNT_AND_ALIAS_PRESENT, ACCOUNT_PRESENT, ALIAS_PRESENT, \
254
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
    18
     DOMAIN_ALIAS_EXISTS, DOMAIN_EXISTS, DOMAIN_INVALID, DOMAIN_TOO_LONG, \
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
    19
     NO_SUCH_DOMAIN
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 199
diff changeset
    20
from VirtualMailManager.errors import DomainError as DomErr
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    21
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
    22
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
254
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
    25
RE_DOMAIN = re.compile(r"^(?:[a-z0-9-]{1,63}\.){1,}[a-z]{2,6}$")
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
    26
_ = lambda msg: msg
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 110
diff changeset
    29
class Domain(object):
3
a9b44e04bf01 * VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    30
    """Class to manage e-mail domains."""
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    31
    __slots__ = ('_directory', '_gid', '_name', '_transport', '_dbh', '_new')
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 222
diff changeset
    32
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    33
    def __init__(self, dbh, domainname):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
        """Creates a new Domain instance.
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 110
diff changeset
    35
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    36
        Loads all relevant data from the database, if the domain could be
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    37
        found.  To create a new domain call the methods set_directory() and
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    38
        set_transport() before save().
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    39
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    40
        A DomainError will be thrown when the *domainname* is the name of
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    41
        an alias domain.
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    42
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    43
        Arguments:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    44
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    45
        `dbh` : pyPgSQL.PgSQL.Connection
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    46
          a database connection for the database access
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    47
        `domainname` : basestring
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    48
          The name of the domain
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
        """
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    50
        self._name = check_domainname(domainname)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
        self._dbh = dbh
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    52
        self._gid = 0
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    53
        self._transport = None
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    54
        self._directory = None
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    55
        self._new = True
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    56
        self._load()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    58
    def _load(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    59
        """Load information from the database and checks if the domain name
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    60
        is the primary one.
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    62
        Raises a DomainError if Domain._name isn't the primary name of the
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    63
        domain.
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    66
        dbc.execute('SELECT dd.gid, tid, domaindir, is_primary FROM '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    67
                    'domain_data dd, domain_name dn WHERE domainname = %s AND '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    68
                    'dn.gid = dd.gid', self._name)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
        result = dbc.fetchone()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
        dbc.close()
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    71
        if result:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    72
            if not result[3]:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    73
                raise DomErr(_(u"The domain '%s' is an alias domain.") %
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    74
                             self._name, DOMAIN_ALIAS_EXISTS)
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    75
            self._gid, self._directory = result[0], result[2]
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    76
            self._transport = Transport(self._dbh, tid=result[1])
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    77
            self._new = False
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    79
    def _set_gid(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    80
        """Sets the ID of the domain - if not set yet."""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    81
        assert self._gid == 0
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
        dbc = self._dbh.cursor()
43
92a6132940f5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 35
diff changeset
    83
        dbc.execute("SELECT nextval('domain_gid')")
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    84
        self._gid = dbc.fetchone()[0]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        dbc.close()
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
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    93
        Argument:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    94
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    95
        `what` : basestring
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    96
            "alias" or "users"
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
        """
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    98
        assert what in ('alias', 'users')
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
        if what == 'users':
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   101
            dbc.execute("SELECT count(gid) FROM users WHERE gid=%s", self._gid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
        else:
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   103
            dbc.execute("SELECT count(gid) FROM alias WHERE gid=%s", self._gid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
        count = dbc.fetchone()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
        dbc.close()
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   106
        return count[0] > 0
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   108
    def _chk_delete(self, deluser, delalias):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   109
        """Checks dependencies for deletion.
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 110
diff changeset
   110
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   111
        Arguments:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   112
        deluser -- ignore available accounts (bool)
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   113
        delalias -- ignore available aliases (bool)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
        """
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   115
        if not deluser:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   116
            hasuser = self._has('users')
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   117
        else:
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   118
            hasuser = False
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   119
        if not delalias:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   120
            hasalias = self._has('alias')
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   121
        else:
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   122
            hasalias = False
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   123
        if hasuser and hasalias:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 199
diff changeset
   124
            raise DomErr(_(u'There are accounts and aliases.'),
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 199
diff changeset
   125
                         ACCOUNT_AND_ALIAS_PRESENT)
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   126
        elif hasuser:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 199
diff changeset
   127
            raise DomErr(_(u'There are accounts.'), ACCOUNT_PRESENT)
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   128
        elif hasalias:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 199
diff changeset
   129
            raise DomErr(_(u'There are aliases.'), ALIAS_PRESENT)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   130
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   131
    def _chk_state(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   132
        """Throws a DomainError if the Domain is new - not saved in the
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   133
        database."""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   134
        if self._new:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   135
            raise DomErr(_(u"The domain '%s' doesn't exist.") % self._name,
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   136
                         NO_SUCH_DOMAIN)
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   137
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   138
    @property
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   139
    def gid(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   140
        """The GID of the Domain."""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   141
        return self._gid
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   142
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   143
    @property
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   144
    def name(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   145
        """The Domain's name."""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   146
        return self._name
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   147
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   148
    @property
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   149
    def directory(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   150
        """The Domain's directory."""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   151
        return self._directory
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   152
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   153
    def set_directory(self, basedir):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   154
        """Set the path value of the Domain's directory, inside *basedir*.
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   155
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   156
        Argument:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   157
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   158
        `basedir` : basestring
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   159
          The base directory of all domains
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   160
        """
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   161
        assert self._new and self._directory is None
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   162
        self._set_gid()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   163
        self._directory = os.path.join(basedir, choice(MAILDIR_CHARS),
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   164
                                       str(self._gid))
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   165
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   166
    @property
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   167
    def transport(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   168
        """The Domain's transport."""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   169
        return self._transport
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   170
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   171
    def set_transport(self, transport):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   172
        """Set the transport for the new Domain.
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   173
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   174
        Argument:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   175
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   176
        `transport` : VirtualMailManager.Transport
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   177
          The transport of the new Domain
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   178
        """
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   179
        assert self._new and isinstance(transport, Transport)
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   180
        self._transport = transport
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   181
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   182
    def save(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   183
        """Stores the new domain in the database."""
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   184
        if not self._new:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   185
            raise DomErr(_(u"The domain '%s' already exists.") % self._name,
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 199
diff changeset
   186
                         DOMAIN_EXISTS)
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   187
        assert self._directory is not None and self._transport is not None
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   188
        dbc = self._dbh.cursor()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   189
        dbc.execute("INSERT INTO domain_data VALUES (%s, %s, %s)", self._gid,
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   190
                    self._transport.tid, self._directory)
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   191
        dbc.execute("INSERT INTO domain_name VALUES (%s, %s, %s)", self._name,
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   192
                    self._gid, True)
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   193
        self._dbh.commit()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   194
        dbc.close()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   195
        self._new = False
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   196
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   197
    def delete(self, deluser=False, delalias=False):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   198
        """Deletes the domain.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   199
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   200
        Arguments:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   201
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   202
        `deluser` : bool
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   203
          force deletion of all available accounts, default `False`
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   204
        `delalias` : bool
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   205
          force deletion of all available aliases, default `False`
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   206
        """
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   207
        self._chk_state()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   208
        self._chk_delete(deluser, delalias)
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   209
        dbc = self._dbh.cursor()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   210
        for tbl in ('alias', 'users', 'relocated', 'domain_name',
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   211
                    'domain_data'):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   212
            dbc.execute("DELETE FROM %s WHERE gid = %d" % (tbl, self._gid))
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   213
        self._dbh.commit()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   214
        dbc.close()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   215
        self._gid = 0
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   216
        self._directory = self._transport = None
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   217
        self._new = True
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   218
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   219
    def update_transport(self, transport, force=False):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   220
        """Sets a new transport for the Domain.
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   221
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   222
        If *force* is `True` the new *transport* will be assigned to all
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   223
        existing accounts.  Otherwise the *transport* will be only used for
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   224
        accounts created from now on.
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   225
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   226
        Arguments:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   227
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   228
        `transport` : VirtualMailManager.Transport
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   229
          the new transport
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   230
        `force` : bool
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   231
          enforce new transport setting for all accounts, default `False`
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   232
        """
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   233
        self._chk_state()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   234
        assert isinstance(transport, Transport)
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   235
        if transport == self._transport:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   236
            return
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   237
        dbc = self._dbh.cursor()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   238
        dbc.execute("UPDATE domain_data SET tid = %s WHERE gid = %s",
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   239
                    transport.tid, self._gid)
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   240
        if dbc.rowcount > 0:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   241
            self._dbh.commit()
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   242
        if force:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   243
            dbc.execute("UPDATE users SET tid = %s WHERE gid = %s",
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   244
                        transport.tid, self._gid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   245
            if dbc.rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   246
                self._dbh.commit()
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   247
        dbc.close()
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   248
        self._transport = transport
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   249
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   250
    def get_info(self):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   251
        """Returns a dictionary with information about the domain."""
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   252
        self._chk_state()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   253
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   254
        dbc.execute('SELECT gid, domainname, transport, domaindir, '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   255
                    'aliasdomains accounts, aliases, relocated FROM '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   256
                    'vmm_domain_info WHERE gid = %s', self._gid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   257
        info = dbc.fetchone()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   258
        dbc.close()
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   259
        keys = ('gid', 'domainname', 'transport', 'domaindir', 'aliasdomains',
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   260
                'accounts', 'aliases', 'relocated')
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   261
        return dict(zip(keys, info))
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   262
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   263
    def get_accounts(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   264
        """Returns a list with all accounts of the domain."""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   265
        self._chk_state()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   266
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   267
        dbc.execute('SELECT local_part from users where gid = %s ORDER BY '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   268
                    'local_part', self._gid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   269
        users = dbc.fetchall()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   270
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   271
        accounts = []
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   272
        if users:
142
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   273
            addr = u'@'.join
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   274
            _dom = self._name
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   275
            accounts = [addr((account[0], _dom)) for account in users]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   276
        return accounts
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   277
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   278
    def get_aliases(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   279
        """Returns a list with all aliases e-mail addresses of the domain."""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   280
        self._chk_state()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   281
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   282
        dbc.execute('SELECT DISTINCT address FROM alias WHERE gid = %s ORDER '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   283
                    'BY address', self._gid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   284
        addresses = dbc.fetchall()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   285
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   286
        aliases = []
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   287
        if addresses:
142
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   288
            addr = u'@'.join
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   289
            _dom = self._name
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   290
            aliases = [addr((alias[0], _dom)) for alias in addresses]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   291
        return aliases
35
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   292
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   293
    def get_relocated(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   294
        """Returns a list with all addresses of relocated users."""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   295
        self._chk_state()
80
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   296
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   297
        dbc.execute('SELECT address FROM relocated WHERE gid = %s ORDER BY '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   298
                    'address', self._gid)
80
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   299
        addresses = dbc.fetchall()
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   300
        dbc.close()
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   301
        relocated = []
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   302
        if addresses:
142
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   303
            addr = u'@'.join
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   304
            _dom = self._name
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   305
            relocated = [addr((address[0], _dom)) for address in addresses]
80
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   306
        return relocated
5dedc673524e * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
   307
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   308
    def get_aliase_names(self):
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   309
        """Returns a list with all alias domain names of the domain."""
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   310
        self._chk_state()
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   311
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   312
        dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s AND '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   313
                    'NOT is_primary ORDER BY domainname', self._gid)
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   314
        anames = dbc.fetchall()
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   315
        dbc.close()
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   316
        aliasdomains = []
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   317
        if anames:
142
28f26f7f3d8f Reduced the mixing/concatenating of str and unicode objects.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   318
            aliasdomains = [aname[0] for aname in anames]
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   319
        return aliasdomains
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   320
198
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   321
254
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   322
def ace2idna(domainname):
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   323
    """Converts the domain name `domainname` from ACE according to IDNA."""
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   324
    return u'.'.join([ToUnicode(lbl) for lbl in domainname.split('.') if lbl])
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   325
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   326
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   327
def check_domainname(domainname):
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   328
    """Returns the validated domain name `domainname`.
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   329
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   330
    It also converts the name of the domain from IDN to ASCII, if
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   331
    necessary.
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   332
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   333
    Throws an `DomainError`, if the domain name is too long or doesn't
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   334
    look like a valid domain name (label.label.label).
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   335
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   336
    """
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   337
    if not RE_DOMAIN.match(domainname):
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   338
        domainname = idn2ascii(domainname)
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   339
    if len(domainname) > 255:
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   340
        raise DomErr(_(u'The domain name is too long'), DOMAIN_TOO_LONG)
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   341
    if not RE_DOMAIN.match(domainname):
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   342
        raise DomErr(_(u"The domain name '%s' is invalid") % domainname,
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   343
                     DOMAIN_INVALID)
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   344
    return domainname
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   345
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   346
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   347
def get_gid(dbh, domainname):
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   348
    """Returns the group id of the domain *domainname*.
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   349
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   350
    If the domain couldn't be found in the database 0 will be returned.
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   351
    """
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   352
    domainname = check_domainname(domainname)
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   353
    dbc = dbh.cursor()
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   354
    dbc.execute('SELECT gid FROM domain_name WHERE domainname=%s', domainname)
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   355
    gid = dbc.fetchone()
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   356
    dbc.close()
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   357
    if gid:
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   358
        return gid[0]
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   359
    return 0
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   360
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   361
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   362
def idn2ascii(domainname):
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   363
    """Converts the idn domain name `domainname` into punycode."""
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   364
    return '.'.join([ToASCII(lbl) for lbl in domainname.split('.') if lbl])
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   365
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 242
diff changeset
   366
35
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   367
def search(dbh, pattern=None, like=False):
236
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   368
    """'Search' for domains by *pattern* in the database.
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   369
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   370
    *pattern* may be a domain name or a partial domain name - starting
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   371
    and/or ending with a '%' sign.  When the *pattern* starts or ends with
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   372
    a '%' sign *like* has to be `True` to perform a wildcard search.
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   373
    To retrieve all available domains use the arguments' default values.
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   374
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   375
    This function returns a tuple with a list and a dict: (order, domains).
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   376
    The order list contains the domains' gid, alphabetical sorted by the
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   377
    primary domain name.  The domains dict's keys are the gids of the
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   378
    domains. The value of item is a list.  The first list element contains
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   379
    the primary domain name or `None`.  The elements [1:] contains the
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   380
    names of alias domains.
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   381
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   382
    Arguments:
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   383
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   384
    `pattern` : basestring
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   385
      a (partial) domain name (starting and/or ending with a "%" sign)
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   386
    `like` : bool
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   387
      should be `True` when *pattern* starts/ends with a "%" sign
084331dd1e4c VMM/Domain: reworked Domain class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
   388
    """
242
496099847480 VMM/Domain: search() small code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 240
diff changeset
   389
    if pattern and not like:
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   390
        pattern = check_domainname(pattern)
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   391
    sql = 'SELECT gid, domainname, is_primary FROM domain_name'
242
496099847480 VMM/Domain: search() small code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 240
diff changeset
   392
    if pattern:
496099847480 VMM/Domain: search() small code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 240
diff changeset
   393
        if like:
496099847480 VMM/Domain: search() small code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 240
diff changeset
   394
            sql += " WHERE domainname LIKE '%s'" % pattern
496099847480 VMM/Domain: search() small code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 240
diff changeset
   395
        else:
496099847480 VMM/Domain: search() small code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 240
diff changeset
   396
            sql += " WHERE domainname = '%s'" % pattern
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   397
    sql += ' ORDER BY is_primary DESC, domainname'
35
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   398
    dbc = dbh.cursor()
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   399
    dbc.execute(sql)
239
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   400
    result = dbc.fetchall()
35
22cc616aef61 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   401
    dbc.close()
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   402
239
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   403
    gids = [domain[0] for domain in result if domain[2]]
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   404
    domains = {}
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   405
    for gid, domain, is_primary in result:
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   406
        if is_primary:
239
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   407
            if not gid in domains:
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   408
                domains[gid] = [domain]
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   409
            else:
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   410
                domains[gid].insert(0, domain)
44
c9ab6900ede9 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 43
diff changeset
   411
        else:
239
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   412
            if gid in gids:
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   413
                if gid in domains:
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   414
                    domains[gid].append(domain)
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   415
                else:
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   416
                    domains[gid] = [domain]
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   417
            else:
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   418
                gids.append(gid)
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   419
                domains[gid] = [None, domain]
184970fd7486 VMM/Domain: search() lists now all matching domains, also when
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   420
    return gids, domains
45
9e66138aad0b * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 44
diff changeset
   421
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 222
diff changeset
   422
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 222
diff changeset
   423
del _