VirtualMailManager/Relocated.py
branchv0.6.x
changeset 215 33f727efa7c4
parent 213 1a9fee6b93bc
child 216 0c8c053b451c
equal deleted inserted replaced
214:84e6e898e6c5 215:33f727efa7c4
    21 class Relocated(object):
    21 class Relocated(object):
    22     """Class to handle e-mail addresses of relocated users."""
    22     """Class to handle e-mail addresses of relocated users."""
    23     __slots__ = ('_addr', '_dest', '_gid', '_dbh')
    23     __slots__ = ('_addr', '_dest', '_gid', '_dbh')
    24 
    24 
    25     def __init__(self, dbh, address):
    25     def __init__(self, dbh, address):
    26         """Creates a new *Relocated* instance. The ``address`` is the
    26         """Creates a new *Relocated* instance.  The ``address`` is the
    27         old e-mail address of the user.
    27         old e-mail address of the user.
    28 
    28 
    29         Use `setDestination()` to set/update the new address, where the
    29         Use `setDestination()` to set/update the new address, where the
    30         user has moved to."""
    30         user has moved to.
       
    31 
       
    32         """
    31         assert isinstance(address, EmailAddress)
    33         assert isinstance(address, EmailAddress)
    32         self._addr = address
    34         self._addr = address
    33         self._dbh = dbh
    35         self._dbh = dbh
    34         self._gid = get_gid(self._dbh, self._addr.domainname)
    36         self._gid = get_gid(self._dbh, self._addr.domainname)
    35         self._dest = None
    37         self._dest = None
    36 
    38 
    37         self.__load()
    39         self.__load()
    38 
    40 
    39     def __load(self):
    41     def __load(self):
    40         """Loads the destination address from the database into the
    42         """Loads the destination address from the database into the
    41         `_dest` attribute."""
    43         `_dest` attribute.
       
    44 
       
    45         """
    42         dbc = self._dbh.cursor()
    46         dbc = self._dbh.cursor()
    43         dbc.execute(
    47         dbc.execute(
    44             'SELECT destination FROM relocated WHERE gid=%s AND address=%s',
    48             'SELECT destination FROM relocated WHERE gid=%s AND address=%s',
    45                     self._gid, self._addr.localpart)
    49                     self._gid, self._addr.localpart)
    46         destination = dbc.fetchone()
    50         destination = dbc.fetchone()
    47         dbc.close()
    51         dbc.close()
    48         if destination:
    52         if destination:
    49             self._dest = EmailAddress(destination[0])
    53             self._dest = EmailAddress(destination[0])
    50 
    54 
    51     def setDestination(self, destination):
    55     def set_destination(self, destination):
    52         """Sets/updates the new address of the relocated user."""
    56         """Sets/updates the new address of the relocated user."""
    53         update = False
    57         update = False
    54         assert isinstance(destination, EmailAddress)
    58         assert isinstance(destination, EmailAddress)
    55         if self._addr == destination:
    59         if self._addr == destination:
    56             raise VMMRE(_(u'Address and destination are identical.'),
    60             raise VMMRE(_(u'Address and destination are identical.'),
    74 WHERE gid=%s AND address=%s',
    78 WHERE gid=%s AND address=%s',
    75                         str(self._dest), self._gid, self._addr.localpart)
    79                         str(self._dest), self._gid, self._addr.localpart)
    76         self._dbh.commit()
    80         self._dbh.commit()
    77         dbc.close()
    81         dbc.close()
    78 
    82 
    79     def getInfo(self):
    83     def get_info(self):
    80         """Returns the address to which mails should be sent."""
    84         """Returns the address to which mails should be sent."""
    81         if not self._dest:
    85         if not self._dest:
    82             raise VMMRE(_(u"The relocated user %r doesn't exist.") %
    86             raise VMMRE(_(u"The relocated user %r doesn't exist.") %
    83                         self._addr, NO_SUCH_RELOCATED)
    87                         self._addr, NO_SUCH_RELOCATED)
    84         return self._dest
    88         return self._dest