VirtualMailManager/alias.py
branchv0.6.x
changeset 322 94bd10e237e5
parent 320 011066435e6f
child 352 22d115376e4d
equal deleted inserted replaced
321:883d5cd66498 322:94bd10e237e5
    33         if not self._gid:
    33         if not self._gid:
    34             raise AErr(_(u"The domain '%s' doesn't exist.") %
    34             raise AErr(_(u"The domain '%s' doesn't exist.") %
    35                        self._addr.domainname, NO_SUCH_DOMAIN)
    35                        self._addr.domainname, NO_SUCH_DOMAIN)
    36         self._dests = []
    36         self._dests = []
    37 
    37 
    38         self.__load_dests()
    38         self._load_dests()
    39 
    39 
    40     def __load_dests(self):
    40     def _load_dests(self):
    41         """Loads all known destination addresses into the _dests list."""
    41         """Loads all known destination addresses into the _dests list."""
    42         dbc = self._dbh.cursor()
    42         dbc = self._dbh.cursor()
    43         dbc.execute('SELECT destination FROM alias WHERE gid = %s AND '
    43         dbc.execute('SELECT destination FROM alias WHERE gid = %s AND '
    44                     'address = %s', self._gid, self._addr.localpart)
    44                     'address = %s', self._gid, self._addr.localpart)
    45         dests = dbc.fetchall()
    45         dests = dbc.fetchall()
    46         if dbc.rowcount > 0:
    46         if dbc.rowcount > 0:
    47             self._dests.extend(EmailAddress(dest[0]) for dest in dests)
    47             self._dests.extend(EmailAddress(dest[0]) for dest in dests)
    48         dbc.close()
    48         dbc.close()
    49 
    49 
    50     def __check_expansion(self, count_new):
    50     def _check_expansion(self, count_new):
    51         """Checks the current expansion limit of the alias."""
    51         """Checks the current expansion limit of the alias."""
    52         postconf = Postconf(cfg_dget('bin.postconf'))
    52         postconf = Postconf(cfg_dget('bin.postconf'))
    53         limit = long(postconf.read('virtual_alias_expansion_limit'))
    53         limit = long(postconf.read('virtual_alias_expansion_limit'))
    54         dcount = len(self._dests)
    54         dcount = len(self._dests)
    55         failed = False
    55         failed = False
    70         if failed:
    70         if failed:
    71             raise AErr(errmsg % {'address': self._addr, 'count': dcount,
    71             raise AErr(errmsg % {'address': self._addr, 'count': dcount,
    72                                  'limit': limit, 'count_new': count_new},
    72                                  'limit': limit, 'count_new': count_new},
    73                        ALIAS_EXCEEDS_EXPANSION_LIMIT)
    73                        ALIAS_EXCEEDS_EXPANSION_LIMIT)
    74 
    74 
    75     def __delete(self, destination=None):
    75     def _delete(self, destination=None):
    76         """Deletes a destination from the alias, if ``destination`` is
    76         """Deletes a destination from the alias, if ``destination`` is
    77         not ``None``.  If ``destination`` is None, the alias with all
    77         not ``None``.  If ``destination`` is None, the alias with all
    78         its destination addresses will be deleted.
    78         its destination addresses will be deleted.
    79 
    79 
    80         """
    80         """
   121             destinations.difference_update(set(self._dests))
   121             destinations.difference_update(set(self._dests))
   122             if not warnings is None:
   122             if not warnings is None:
   123                 warnings.extend(duplicates)
   123                 warnings.extend(duplicates)
   124         if not destinations:
   124         if not destinations:
   125             return destinations
   125             return destinations
   126         self.__check_expansion(len(destinations))
   126         self._check_expansion(len(destinations))
   127         dbc = self._dbh.cursor()
   127         dbc = self._dbh.cursor()
   128         dbc.executemany("INSERT INTO alias VALUES (%d, '%s', %%s)" %
   128         dbc.executemany("INSERT INTO alias VALUES (%d, '%s', %%s)" %
   129                         (self._gid, self._addr.localpart),
   129                         (self._gid, self._addr.localpart),
   130                         (str(destination) for destination in destinations))
   130                         (str(destination) for destination in destinations))
   131         self._dbh.commit()
   131         self._dbh.commit()
   141                        NO_SUCH_ALIAS)
   141                        NO_SUCH_ALIAS)
   142         if not destination in self._dests:
   142         if not destination in self._dests:
   143             raise AErr(_(u"The address '%(addr)s' isn't a destination of "
   143             raise AErr(_(u"The address '%(addr)s' isn't a destination of "
   144                          u"the alias '%(alias)s'.") % {'addr': self._addr,
   144                          u"the alias '%(alias)s'.") % {'addr': self._addr,
   145                        'alias': destination}, NO_SUCH_ALIAS)
   145                        'alias': destination}, NO_SUCH_ALIAS)
   146         self.__delete(destination)
   146         self._delete(destination)
   147         self._dests.remove(destination)
   147         self._dests.remove(destination)
   148 
   148 
   149     def get_destinations(self):
   149     def get_destinations(self):
   150         """Returns an iterator for all destinations of the alias."""
   150         """Returns an iterator for all destinations of the alias."""
   151         if not self._dests:
   151         if not self._dests:
   156     def delete(self):
   156     def delete(self):
   157         """Deletes the alias with all its destinations."""
   157         """Deletes the alias with all its destinations."""
   158         if not self._dests:
   158         if not self._dests:
   159             raise AErr(_(u"The alias '%s' doesn't exist.") % self._addr,
   159             raise AErr(_(u"The alias '%s' doesn't exist.") % self._addr,
   160                        NO_SUCH_ALIAS)
   160                        NO_SUCH_ALIAS)
   161         self.__delete()
   161         self._delete()
   162         del self._dests[:]
   162         del self._dests[:]
   163 
   163 
   164 del _, cfg_dget
   164 del _, cfg_dget