VirtualMailManager/catchall.py
changeset 618 d8736bb80bdc
parent 611 8e9b0046bc8f
child 637 ca6621caff2f
child 703 58815c004a61
equal deleted inserted replaced
617:9eecf0160c39 618:d8736bb80bdc
    82         if failed:
    82         if failed:
    83             raise AErr(errmsg % {'domain': self._domain, 'count': dcount,
    83             raise AErr(errmsg % {'domain': self._domain, 'count': dcount,
    84                                  'limit': limit, 'count_new': count_new},
    84                                  'limit': limit, 'count_new': count_new},
    85                        ALIAS_EXCEEDS_EXPANSION_LIMIT)
    85                        ALIAS_EXCEEDS_EXPANSION_LIMIT)
    86 
    86 
    87     def _delete(self, destination=None):
    87     def _delete(self, destinations=None):
    88         """Deletes a destination from the catchall alias, if ``destination``
    88         """Delete one ore multiple destinations from the catchall alias, if
    89         is not ``None``.  If ``destination`` is None, the catchall alias with
    89         ``destinations`` is not ``None``.  If ``destinations`` is None, the
    90         all its destination addresses will be deleted.
    90         catchall alias with all its destination addresses will be deleted.
    91 
    91 
    92         """
    92         """
    93         dbc = self._dbh.cursor()
    93         dbc = self._dbh.cursor()
    94         if not destination:
    94         if not destinations:
    95             dbc.execute('DELETE FROM catchall WHERE gid = %s', (self._gid,))
    95             dbc.execute('DELETE FROM catchall WHERE gid = %s', (self._gid,))
    96         else:
    96         else:
    97             dbc.execute('DELETE FROM catchall WHERE gid = %s '
    97             dbc.executemany('DELETE FROM catchall WHERE gid = %d AND '
    98                         'AND destination = %s', (self._gid, str(destination)))
    98                             'destination = %%s' % self._gid,
       
    99                             ((str(dest),) for dest in destinations))
    99         if dbc.rowcount > 0:
   100         if dbc.rowcount > 0:
   100             self._dbh.commit()
   101             self._dbh.commit()
   101         dbc.close()
   102         dbc.close()
   102 
   103 
   103     def __len__(self):
   104     def __len__(self):
   137         self._dbh.commit()
   138         self._dbh.commit()
   138         dbc.close()
   139         dbc.close()
   139         self._dests.extend(destinations)
   140         self._dests.extend(destinations)
   140         return destinations
   141         return destinations
   141 
   142 
   142     def del_destination(self, destination):
   143     def del_destinations(self, destinations, warnings=None):
   143         """Deletes the specified ``destination`` address from the catchall
   144         """Deletes the specified ``destinations`` from the catchall alias."""
   144         alias."""
   145         destinations = set(destinations)
   145         assert isinstance(destination, EmailAddress)
   146         assert destinations and \
       
   147                 all(isinstance(dest, EmailAddress) for dest in destinations)
       
   148         if not warnings is None:
       
   149             assert isinstance(warnings, list)
   146         if not self._dests:
   150         if not self._dests:
   147             raise AErr(_(u"There are no catch-all aliases defined for "
   151             raise AErr(_(u"There are no catch-all aliases defined for "
   148                          u"domain '%s'.") % self._domain, NO_SUCH_ALIAS)
   152                          u"domain '%s'.") % self._domain, NO_SUCH_ALIAS)
   149         if not destination in self._dests:
   153         unknown = destinations.difference(set(self._dests))
   150             raise AErr(_(u"The address '%(addr)s' is not a destination of "
   154         if unknown:
   151                          u"the catch-all alias for domain '%(domain)s'.")
   155             destinations.intersection_update(set(self._dests))
   152                        % {'addr': destination, 'domain': self._domain},
   156             if not warnings is None:
       
   157                 warnings.extend(unknown)
       
   158         if not destinations:
       
   159             raise AErr(_(u"No suitable destinations left to remove from the "
       
   160                          u"catch-all alias of domain '%s'.") % self._domain,
   153                        NO_SUCH_ALIAS)
   161                        NO_SUCH_ALIAS)
   154         self._delete(destination)
   162         self._delete(destinations)
   155         self._dests.remove(destination)
   163         for destination in destinations:
       
   164             self._dests.remove(destination)
   156 
   165 
   157     def get_destinations(self):
   166     def get_destinations(self):
   158         """Returns an iterator for all destinations of the catchall alias."""
   167         """Returns an iterator for all destinations of the catchall alias."""
   159         if not self._dests:
   168         if not self._dests:
   160             raise AErr(_(u"There are no catch-all aliases defined for "
   169             raise AErr(_(u"There are no catch-all aliases defined for "