VirtualMailManager/Relocated.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sat, 27 Feb 2010 10:51:04 +0000
branchv0.6.x
changeset 219 0b6ce895e1dc
parent 216 0c8c053b451c
child 222 d0c16e70a9fb
permissions -rw-r--r--
VMM/pycompat: added to the repository. Provides all() for Py24.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
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) 2008 - 2010, Pascal Volk
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     5
"""
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     6
    VirtualMailManager.Relocated
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     8
    Virtual Mail Manager's Relocated class to handle relocated users.
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     9
"""
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
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: 197
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 RelocatedError as RErr
198
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 197
diff changeset
    14
from VirtualMailManager.constants.ERROR import \
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    15
     NO_SUCH_RELOCATED, RELOCATED_ADDR_DEST_IDENTICAL, RELOCATED_EXISTS
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    16
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    17
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    18
_ = lambda msg: msg
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    19
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    21
class Relocated(object):
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    22
    """Class to handle e-mail addresses of relocated users."""
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    23
    __slots__ = ('_addr', '_dest', '_gid', '_dbh')
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    24
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    25
    def __init__(self, dbh, address):
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    26
        """Creates a new *Relocated* instance.  The ``address`` is the
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    27
        old e-mail address of the user.
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    28
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    29
        Use `setDestination()` to set/update the new address, where the
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    30
        user has moved to.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    31
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    32
        """
213
1a9fee6b93bc VMM:/{Alias,EmailAddress,Relocated}: use assertions for argument checks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 202
diff changeset
    33
        assert isinstance(address, EmailAddress)
202
43e7c8b440da VMM/Relocated: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    34
        self._addr = address
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
        self._dbh = dbh
198
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 197
diff changeset
    36
        self._gid = get_gid(self._dbh, self._addr.domainname)
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    37
        self._dest = None
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    39
        self.__load()
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    41
    def __load(self):
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    42
        """Loads the destination address from the database into the
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    43
        `_dest` attribute.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    44
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    45
        """
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    46
        dbc = self._dbh.cursor()
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    47
        dbc.execute(
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    48
            'SELECT destination FROM relocated WHERE gid=%s AND address=%s',
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    49
                    self._gid, self._addr.localpart)
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    50
        destination = dbc.fetchone()
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    51
        dbc.close()
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    52
        if destination:
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    53
            self._dest = EmailAddress(destination[0])
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    55
    def set_destination(self, destination):
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    56
        """Sets/updates the new address of the relocated user."""
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    57
        update = False
213
1a9fee6b93bc VMM:/{Alias,EmailAddress,Relocated}: use assertions for argument checks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 202
diff changeset
    58
        assert isinstance(destination, EmailAddress)
202
43e7c8b440da VMM/Relocated: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    59
        if self._addr == destination:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    60
            raise RErr(_(u'Address and destination are identical.'),
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    61
                       RELOCATED_ADDR_DEST_IDENTICAL)
202
43e7c8b440da VMM/Relocated: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    62
        if self._dest:
43e7c8b440da VMM/Relocated: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    63
            if self._dest == destination:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    64
                raise RErr(_(u'The relocated user %r already exists.') %
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    65
                           self._addr, RELOCATED_EXISTS)
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    66
            else:
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    67
                self._dest = destination
202
43e7c8b440da VMM/Relocated: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    68
                update = True
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
        else:
202
43e7c8b440da VMM/Relocated: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    70
            self._dest = destination
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    71
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    72
        dbc = self._dbh.cursor()
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    73
        if not update:
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    74
            dbc.execute('INSERT INTO relocated VALUES (%s, %s, %s)',
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    75
                        self._gid, self._addr.localpart, str(self._dest))
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    76
        else:
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    77
            dbc.execute('UPDATE relocated SET destination=%s \
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    78
WHERE gid=%s AND address=%s',
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    79
                        str(self._dest), self._gid, self._addr.localpart)
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    80
        self._dbh.commit()
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    81
        dbc.close()
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    83
    def get_info(self):
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    84
        """Returns the address to which mails should be sent."""
202
43e7c8b440da VMM/Relocated: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    85
        if not self._dest:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    86
            raise RErr(_(u"The relocated user %r doesn't exist.") %
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    87
                       self._addr, NO_SUCH_RELOCATED)
202
43e7c8b440da VMM/Relocated: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    88
        return self._dest
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
    def delete(self):
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    91
        """Deletes the relocated entry from the database."""
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    92
        if not self._dest:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    93
            raise RErr(_(u"The relocated user %r doesn't exist.") % self._addr,
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    94
                       NO_SUCH_RELOCATED)
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
        dbc = self._dbh.cursor()
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
        dbc.execute("DELETE FROM relocated WHERE gid = %s AND address = %s",
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    97
                    self._gid, self._addr.localpart)
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    98
        if dbc.rowcount > 0:
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
            self._dbh.commit()
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   100
        dbc.close()
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   101
        self._dest = None
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
197
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   103
d2712e8c724e VMM/{Relocated,Handler}: reworked Relocated class, adjusted Handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   104
del _