VirtualMailManager/alias.py
changeset 618 d8736bb80bdc
parent 568 14abdd04ddf5
child 619 4ec5c015b7aa
equal deleted inserted replaced
617:9eecf0160c39 618:d8736bb80bdc
    71         if failed:
    71         if failed:
    72             raise AErr(errmsg % {'address': self._addr, 'count': dcount,
    72             raise AErr(errmsg % {'address': self._addr, 'count': dcount,
    73                                  'limit': limit, 'count_new': count_new},
    73                                  'limit': limit, 'count_new': count_new},
    74                        ALIAS_EXCEEDS_EXPANSION_LIMIT)
    74                        ALIAS_EXCEEDS_EXPANSION_LIMIT)
    75 
    75 
    76     def _delete(self, destination=None):
    76     def _delete(self, destinations=None):
    77         """Deletes a destination from the alias, if ``destination`` is
    77         """Deletes the *destinations* from the alias, if ``destinations``
    78         not ``None``.  If ``destination`` is None, the alias with all
    78         is not ``None``.  If ``destinations`` is None, the alias with all
    79         its destination addresses will be deleted.
    79         its destination addresses will be deleted.
    80 
    80 
    81         """
    81         """
    82         dbc = self._dbh.cursor()
    82         dbc = self._dbh.cursor()
    83         if not destination:
    83         if not destinations:
    84             dbc.execute('DELETE FROM alias WHERE gid = %s AND address = %s',
    84             dbc.execute('DELETE FROM alias WHERE gid = %s AND address = %s',
    85                         (self._gid, self._addr.localpart))
    85                         (self._gid, self._addr.localpart))
    86         else:
    86         else:
    87             dbc.execute('DELETE FROM alias WHERE gid = %s AND address = %s '
    87             dbc.executemany("DELETE FROM alias WHERE gid = %d AND address = "
    88                         'AND destination = %s',
    88                             "'%s' AND destination = %%s" % (self._gid,
    89                         (self._gid, self._addr.localpart, str(destination)))
    89                                                          self._addr.localpart),
       
    90                             ((str(dest),) for dest in destinations))
    90         if dbc.rowcount > 0:
    91         if dbc.rowcount > 0:
    91             self._dbh.commit()
    92             self._dbh.commit()
    92         dbc.close()
    93         dbc.close()
    93 
    94 
    94     def __len__(self):
    95     def __len__(self):
   133         self._dbh.commit()
   134         self._dbh.commit()
   134         dbc.close()
   135         dbc.close()
   135         self._dests.extend(destinations)
   136         self._dests.extend(destinations)
   136         return destinations
   137         return destinations
   137 
   138 
   138     def del_destination(self, destination):
   139     def del_destinations(self, destinations, warnings=None):
   139         """Deletes the specified ``destination`` address from the alias."""
   140         """Delete the specified `EmailAddress`es of *destinations* from
   140         assert isinstance(destination, EmailAddress)
   141         the alias's destinations.
       
   142 
       
   143         """
       
   144         destinations = set(destinations)
       
   145         assert destinations and \
       
   146                 all(isinstance(dest, EmailAddress) for dest in destinations)
       
   147         if not warnings is None:
       
   148             assert isinstance(warnings, list)
       
   149         if self._addr in destinations:
       
   150             destinations.remove(self._addr)
       
   151             if not warnings is None:
       
   152                 warnings.append(self._addr)
   141         if not self._dests:
   153         if not self._dests:
   142             raise AErr(_(u"The alias '%s' does not exist.") % self._addr,
   154             raise AErr(_(u"The alias '%s' does not exist.") % self._addr,
   143                        NO_SUCH_ALIAS)
   155                        NO_SUCH_ALIAS)
   144         if not destination in self._dests:
   156         unknown = destinations.difference(set(self._dests))
   145             raise AErr(_(u"The address '%(addr)s' is not a destination of "
   157         if unknown:
   146                          u"the alias '%(alias)s'.") % {'addr': destination,
   158             destinations.intersection_update(set(self._dests))
   147                        'alias': self._addr}, NO_SUCH_ALIAS)
   159             if not warnings is None:
   148         self._delete(destination)
   160                 warnings.extend(unknown)
   149         self._dests.remove(destination)
   161         if not destinations:
       
   162             raise AErr(_(u"No suitable destinations left to remove from alias "
       
   163                          u"'%s'.") % self._addr, NO_SUCH_ALIAS)
       
   164         self._delete(destinations)
       
   165         for destination in destinations:
       
   166             self._dests.remove(destination)
   150 
   167 
   151     def get_destinations(self):
   168     def get_destinations(self):
   152         """Returns an iterator for all destinations of the alias."""
   169         """Returns an iterator for all destinations of the alias."""
   153         if not self._dests:
   170         if not self._dests:
   154             raise AErr(_(u"The alias '%s' does not exist.") % self._addr,
   171             raise AErr(_(u"The alias '%s' does not exist.") % self._addr,