VirtualMailManager/Alias.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sat, 27 Feb 2010 21:36:55 +0000
branchv0.6.x
changeset 221 371ae0b4443d
parent 216 0c8c053b451c
child 222 d0c16e70a9fb
permissions -rw-r--r--
VMM/Alias: renamed Alias.add_destination() to add_destinations(). Now it's possible to add one ore more destinations to the alias with a single method call. VMM/Handler: adjusted Handler.aliasAdd() to the API changes of the Alias class. Also use get_gid from the Domain module to get the gid of a domain. We don't need complete Domain object, only the gid. Handler.getWarnings(): no longer return the __warnings list. Return a copy instead and empty the Handler.__warnings list.
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
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     5
"""
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     6
    VirtualMailManager.Alias
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     8
    Virtual Mail Manager's Alias class to manage e-mail aliases.
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     9
"""
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    10
198
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 196
diff changeset
    11
from VirtualMailManager.Domain import get_gid
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    12
from VirtualMailManager.EmailAddress import EmailAddress
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    13
from VirtualMailManager.errors import AliasError as AErr
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    14
from VirtualMailManager.pycompat import all
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    15
from VirtualMailManager.constants.ERROR import ALIAS_ADDR_DEST_IDENTICAL, \
198
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 196
diff changeset
    16
     ALIAS_EXCEEDS_EXPANSION_LIMIT, ALIAS_EXISTS, NO_SUCH_ALIAS
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    17
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    18
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    19
_ = lambda msg: msg
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    20
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    22
class Alias(object):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    23
    """Class to manage e-mail aliases."""
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    24
    __slots__ = ('_addr', '_dests', '_gid', '_dbh')
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    25
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    26
    def __init__(self, dbh, address):
213
1a9fee6b93bc VMM:/{Alias,EmailAddress,Relocated}: use assertions for argument checks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 203
diff changeset
    27
        assert isinstance(address, EmailAddress)
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    28
        self._addr = address
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
        self._dbh = dbh
198
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 196
diff changeset
    30
        self._gid = get_gid(self._dbh, self._addr.domainname)
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    31
        self._dests = []
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    33
        self.__load_dests()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    35
    def __load_dests(self):
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    36
        """Loads all known destination addresses into the _dests list."""
88
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    37
        dbc = self._dbh.cursor()
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    38
        dbc.execute(
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    39
                'SELECT destination FROM alias WHERE gid=%s AND address=%s',
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    40
                    self._gid, self._addr.localpart)
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    41
        dests = iter(dbc.fetchall())
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    42
        if dbc.rowcount > 0:
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    43
            dest_add = self._dests.append
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    44
            for dest in dests:
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    45
                dest_add(EmailAddress(dest[0]))
88
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    46
        dbc.close()
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    47
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    48
    def __check_expansion(self, count_new, limit):
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    49
        """Checks the current expansion limit of the alias."""
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    50
        dcount = len(self._dests)
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    51
        failed = False
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    52
        if dcount == limit or dcount + count_new > limit:
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    53
            failed = True
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    54
            errmsg = _(
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    55
u"""Can't add %(count_new)i new destination(s) to alias %(address)r.
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    56
Currently this alias expands into %(count)i/%(limit)i recipients.
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    57
%(count_new)i additional destination(s) will render this alias unusable.
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    58
Hint: Increase Postfix' virtual_alias_expansion_limit""")
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    59
        elif dcount > limit:
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    60
            failed = True
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    61
            errmsg = _(
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    62
u"""Can't add %(count_new)i new destination(s) to alias %(address)r.
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    63
This alias already exceeds it's expansion limit (%(count)i/%(limit)i).
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    64
So its unusable, all messages addressed to this alias will be bounced.
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    65
Hint: Delete some destination addresses.""")
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    66
        if failed:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    67
            raise AErr(errmsg % {'address': str(self._addr), 'count': dcount,
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    68
                                 'limit': limit, 'count_new': count_new},
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    69
                       ALIAS_EXCEEDS_EXPANSION_LIMIT)
88
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    70
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    71
    def __delete(self, destination=None):
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    72
        """Deletes a destination from the alias, if ``destination`` is
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    73
        not ``None``.  If ``destination`` is None, the alias with all
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    74
        it's destination addresses will be deleted.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    75
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    76
        """
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    77
        dbc = self._dbh.cursor()
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    78
        if not destination:
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    79
            dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    80
                        self._gid, self._addr.localpart)
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    81
        else:
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    82
            dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s AND \
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    83
 destination=%s",
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    84
                        self._gid, self._addr.localpart, str(destination))
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    85
        if dbc.rowcount > 0:
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    86
            self._dbh.commit()
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    87
        dbc.close()
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    88
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    89
    def __len__(self):
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    90
        """Returns the number of destinations of the alias."""
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    91
        return len(self._dests)
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    92
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    93
    def add_destinations(self, destinations, expansion_limit, warnings=None):
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    94
        """Adds the `EmailAddress`es from *destinations* list to the
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    95
        destinations of the alias.
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    96
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    97
        Destinations, that are already assigned to the alias, will be
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    98
        removed from *destinations*.  When done, this method will return
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    99
        a set with all destinations, that was saved in the database.
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   100
        """
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   101
        destinations = set(destinations)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   102
        assert destinations and \
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   103
                all(isinstance(dest, EmailAddress) for dest in destinations)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   104
        if not warnings is None:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   105
            assert isinstance(warnings, list)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   106
        if self._addr in destinations:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   107
            destinations.remove(self._addr)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   108
            if not warnings is None:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   109
                warnings.append(self._addr)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   110
        duplicates = destinations.intersection(set(self._dests))
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   111
        if duplicates:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   112
            destinations.difference_update(set(self._dests))
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   113
            if not warnings is None:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   114
                warnings.extend(duplicates)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   115
        if not destinations:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   116
            return destinations
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   117
        self.__check_expansion(len(destinations), expansion_limit)
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   118
        dbc = self._dbh.cursor()
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   119
        dbc.executemany("INSERT INTO alias VALUES (%d, '%s', %%s)" %
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   120
                        (self._gid, self._addr.localpart),
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   121
                        (str(destination) for destination in destinations))
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   122
        self._dbh.commit()
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   123
        dbc.close()
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   124
        self._dests.extend(destinations)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   125
        return destinations
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   126
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
   127
    def del_destination(self, destination):
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   128
        """Deletes the specified ``destination`` address from the alias."""
213
1a9fee6b93bc VMM:/{Alias,EmailAddress,Relocated}: use assertions for argument checks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 203
diff changeset
   129
        assert isinstance(destination, EmailAddress)
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   130
        if not self._dests:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   131
            raise AErr(_(u"The alias %r doesn't exist.") % str(self._addr),
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   132
                       NO_SUCH_ALIAS)
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   133
        if not destination in self._dests:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   134
            raise AErr(_(u"The address %(d)r isn't a destination of \
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   135
the alias %(a)r.") %
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   136
                       {'a': str(self._addr), 'd': str(destination)},
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   137
                       NO_SUCH_ALIAS)
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   138
        self.__delete(destination)
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   139
        self._dests.remove(destination)
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   140
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
   141
    def get_destinations(self):
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   142
        """Returns an iterator for all destinations of the alias."""
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   143
        if not self._dests:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   144
            raise AErr(_(u"The alias %r doesn't exist.") % str(self._addr),
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   145
                       NO_SUCH_ALIAS)
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   146
        return iter(self._dests)
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):
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   149
        """Deletes the alias with all its destinations."""
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   150
        if not self._dests:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   151
            raise AErr(_(u"The alias %r doesn't exist.") % str(self._addr),
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   152
                       NO_SUCH_ALIAS)
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   153
        self.__delete()
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   154
        del self._dests[:]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   155
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   156
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   157
del _