VirtualMailManager/alias.py
changeset 618 d8736bb80bdc
parent 568 14abdd04ddf5
child 619 4ec5c015b7aa
--- a/VirtualMailManager/alias.py	Mon Sep 24 19:13:51 2012 +0000
+++ b/VirtualMailManager/alias.py	Thu Sep 27 19:15:09 2012 +0000
@@ -73,20 +73,21 @@
                                  'limit': limit, 'count_new': count_new},
                        ALIAS_EXCEEDS_EXPANSION_LIMIT)
 
-    def _delete(self, destination=None):
-        """Deletes a destination from the alias, if ``destination`` is
-        not ``None``.  If ``destination`` is None, the alias with all
+    def _delete(self, destinations=None):
+        """Deletes the *destinations* from the alias, if ``destinations``
+        is not ``None``.  If ``destinations`` is None, the alias with all
         its destination addresses will be deleted.
 
         """
         dbc = self._dbh.cursor()
-        if not destination:
+        if not destinations:
             dbc.execute('DELETE FROM alias WHERE gid = %s AND address = %s',
                         (self._gid, self._addr.localpart))
         else:
-            dbc.execute('DELETE FROM alias WHERE gid = %s AND address = %s '
-                        'AND destination = %s',
-                        (self._gid, self._addr.localpart, str(destination)))
+            dbc.executemany("DELETE FROM alias WHERE gid = %d AND address = "
+                            "'%s' AND destination = %%s" % (self._gid,
+                                                         self._addr.localpart),
+                            ((str(dest),) for dest in destinations))
         if dbc.rowcount > 0:
             self._dbh.commit()
         dbc.close()
@@ -135,18 +136,34 @@
         self._dests.extend(destinations)
         return destinations
 
-    def del_destination(self, destination):
-        """Deletes the specified ``destination`` address from the alias."""
-        assert isinstance(destination, EmailAddress)
+    def del_destinations(self, destinations, warnings=None):
+        """Delete the specified `EmailAddress`es of *destinations* from
+        the alias's destinations.
+
+        """
+        destinations = set(destinations)
+        assert destinations and \
+                all(isinstance(dest, EmailAddress) for dest in destinations)
+        if not warnings is None:
+            assert isinstance(warnings, list)
+        if self._addr in destinations:
+            destinations.remove(self._addr)
+            if not warnings is None:
+                warnings.append(self._addr)
         if not self._dests:
             raise AErr(_(u"The alias '%s' does not exist.") % self._addr,
                        NO_SUCH_ALIAS)
-        if not destination in self._dests:
-            raise AErr(_(u"The address '%(addr)s' is not a destination of "
-                         u"the alias '%(alias)s'.") % {'addr': destination,
-                       'alias': self._addr}, NO_SUCH_ALIAS)
-        self._delete(destination)
-        self._dests.remove(destination)
+        unknown = destinations.difference(set(self._dests))
+        if unknown:
+            destinations.intersection_update(set(self._dests))
+            if not warnings is None:
+                warnings.extend(unknown)
+        if not destinations:
+            raise AErr(_(u"No suitable destinations left to remove from alias "
+                         u"'%s'.") % self._addr, NO_SUCH_ALIAS)
+        self._delete(destinations)
+        for destination in destinations:
+            self._dests.remove(destination)
 
     def get_destinations(self):
         """Returns an iterator for all destinations of the alias."""