VirtualMailManager/relocated.py
branchv0.7.x
changeset 643 df1e3b67882a
parent 568 14abdd04ddf5
child 676 2bc11dada296
equal deleted inserted replaced
642:4cd9d0a9f42f 643:df1e3b67882a
    34         assert isinstance(address, EmailAddress)
    34         assert isinstance(address, EmailAddress)
    35         self._addr = address
    35         self._addr = address
    36         self._dbh = dbh
    36         self._dbh = dbh
    37         self._gid = get_gid(self._dbh, self._addr.domainname)
    37         self._gid = get_gid(self._dbh, self._addr.domainname)
    38         if not self._gid:
    38         if not self._gid:
    39             raise RErr(_(u"The domain '%s' does not exist.") %
    39             raise RErr(_("The domain '%s' does not exist.") %
    40                        self._addr.domainname, NO_SUCH_DOMAIN)
    40                        self._addr.domainname, NO_SUCH_DOMAIN)
    41         self._dest = None
    41         self._dest = None
    42 
    42 
    43         self._load()
    43         self._load()
    44 
    44 
    45     def __nonzero__(self):
    45     def __bool__(self):
    46         """Returns `True` if the Relocated is known, `False` if it's new."""
    46         """Returns `True` if the Relocated is known, `False` if it's new."""
    47         return self._dest is not None
    47         return self._dest is not None
    48 
    48 
    49     def _load(self):
    49     def _load(self):
    50         """Loads the destination address from the database into the
    50         """Loads the destination address from the database into the
    57         destination = dbc.fetchone()
    57         destination = dbc.fetchone()
    58         dbc.close()
    58         dbc.close()
    59         if destination:
    59         if destination:
    60             destination = DestinationEmailAddress(destination[0], self._dbh)
    60             destination = DestinationEmailAddress(destination[0], self._dbh)
    61             if destination.at_localhost:
    61             if destination.at_localhost:
    62                 raise RErr(_(u"The destination address' domain name must not "
    62                 raise RErr(_("The destination address' domain name must not "
    63                              u"be localhost."), DOMAIN_INVALID)
    63                              "be localhost."), DOMAIN_INVALID)
    64             self._dest = destination
    64             self._dest = destination
    65 
    65 
    66     @property
    66     @property
    67     def address(self):
    67     def address(self):
    68         """The Relocated's EmailAddress instance."""
    68         """The Relocated's EmailAddress instance."""
    71     def set_destination(self, destination):
    71     def set_destination(self, destination):
    72         """Sets/updates the new address of the relocated user."""
    72         """Sets/updates the new address of the relocated user."""
    73         update = False
    73         update = False
    74         assert isinstance(destination, DestinationEmailAddress)
    74         assert isinstance(destination, DestinationEmailAddress)
    75         if destination.at_localhost:
    75         if destination.at_localhost:
    76             raise RErr(_(u"The destination address' domain name must not be "
    76             raise RErr(_("The destination address' domain name must not be "
    77                          u"localhost."), DOMAIN_INVALID)
    77                          "localhost."), DOMAIN_INVALID)
    78         if self._addr == destination:
    78         if self._addr == destination:
    79             raise RErr(_(u'Address and destination are identical.'),
    79             raise RErr(_('Address and destination are identical.'),
    80                        RELOCATED_ADDR_DEST_IDENTICAL)
    80                        RELOCATED_ADDR_DEST_IDENTICAL)
    81         if self._dest:
    81         if self._dest:
    82             if self._dest == destination:
    82             if self._dest == destination:
    83                 raise RErr(_(u"The relocated user '%s' already exists.") %
    83                 raise RErr(_("The relocated user '%s' already exists.") %
    84                            self._addr, RELOCATED_EXISTS)
    84                            self._addr, RELOCATED_EXISTS)
    85             else:
    85             else:
    86                 self._dest = destination
    86                 self._dest = destination
    87                 update = True
    87                 update = True
    88         else:
    88         else:
   101         dbc.close()
   101         dbc.close()
   102 
   102 
   103     def get_info(self):
   103     def get_info(self):
   104         """Returns the address to which mails should be sent."""
   104         """Returns the address to which mails should be sent."""
   105         if not self._dest:
   105         if not self._dest:
   106             raise RErr(_(u"The relocated user '%s' does not exist.") %
   106             raise RErr(_("The relocated user '%s' does not exist.") %
   107                        self._addr, NO_SUCH_RELOCATED)
   107                        self._addr, NO_SUCH_RELOCATED)
   108         return self._dest
   108         return self._dest
   109 
   109 
   110     def delete(self):
   110     def delete(self):
   111         """Deletes the relocated entry from the database."""
   111         """Deletes the relocated entry from the database."""
   112         if not self._dest:
   112         if not self._dest:
   113             raise RErr(_(u"The relocated user '%s' does not exist.") %
   113             raise RErr(_("The relocated user '%s' does not exist.") %
   114                        self._addr, NO_SUCH_RELOCATED)
   114                        self._addr, NO_SUCH_RELOCATED)
   115         dbc = self._dbh.cursor()
   115         dbc = self._dbh.cursor()
   116         dbc.execute('DELETE FROM relocated WHERE gid = %s AND address = %s',
   116         dbc.execute('DELETE FROM relocated WHERE gid = %s AND address = %s',
   117                     (self._gid, self._addr.localpart))
   117                     (self._gid, self._addr.localpart))
   118         if dbc.rowcount > 0:
   118         if dbc.rowcount > 0: