VirtualMailManager/Alias.py
branchv0.6.x
changeset 257 5b8fde01e4f0
parent 250 73cd082cd724
child 281 59ff7c719697
equal deleted inserted replaced
256:ae80282301a3 257:5b8fde01e4f0
    27         assert isinstance(address, EmailAddress)
    27         assert isinstance(address, EmailAddress)
    28         self._addr = address
    28         self._addr = address
    29         self._dbh = dbh
    29         self._dbh = dbh
    30         self._gid = get_gid(self._dbh, self._addr.domainname)
    30         self._gid = get_gid(self._dbh, self._addr.domainname)
    31         if not self._gid:
    31         if not self._gid:
    32             raise AErr(_(u"The domain %r doesn't exist.") %
    32             raise AErr(_(u"The domain '%s' doesn't exist.") %
    33                        self._addr.domainname, NO_SUCH_DOMAIN)
    33                        self._addr.domainname, NO_SUCH_DOMAIN)
    34         self._dests = []
    34         self._dests = []
    35 
    35 
    36         self.__load_dests()
    36         self.__load_dests()
    37 
    37 
    51         dcount = len(self._dests)
    51         dcount = len(self._dests)
    52         failed = False
    52         failed = False
    53         if dcount == limit or dcount + count_new > limit:
    53         if dcount == limit or dcount + count_new > limit:
    54             failed = True
    54             failed = True
    55             errmsg = _(
    55             errmsg = _(
    56 u"""Can't add %(count_new)i new destination(s) to alias %(address)r.
    56 u"""Can't add %(count_new)i new destination(s) to alias '%(address)s'.
    57 Currently this alias expands into %(count)i/%(limit)i recipients.
    57 Currently this alias expands into %(count)i/%(limit)i recipients.
    58 %(count_new)i additional destination(s) will render this alias unusable.
    58 %(count_new)i additional destination(s) will render this alias unusable.
    59 Hint: Increase Postfix' virtual_alias_expansion_limit""")
    59 Hint: Increase Postfix' virtual_alias_expansion_limit""")
    60         elif dcount > limit:
    60         elif dcount > limit:
    61             failed = True
    61             failed = True
    62             errmsg = _(
    62             errmsg = _(
    63 u"""Can't add %(count_new)i new destination(s) to alias %(address)r.
    63 u"""Can't add %(count_new)i new destination(s) to alias '%(address)s'.
    64 This alias already exceeds it's expansion limit (%(count)i/%(limit)i).
    64 This alias already exceeds its expansion limit (%(count)i/%(limit)i).
    65 So its unusable, all messages addressed to this alias will be bounced.
    65 So its unusable, all messages addressed to this alias will be bounced.
    66 Hint: Delete some destination addresses.""")
    66 Hint: Delete some destination addresses.""")
    67         if failed:
    67         if failed:
    68             raise AErr(errmsg % {'address': str(self._addr), 'count': dcount,
    68             raise AErr(errmsg % {'address': self._addr, 'count': dcount,
    69                                  'limit': limit, 'count_new': count_new},
    69                                  'limit': limit, 'count_new': count_new},
    70                        ALIAS_EXCEEDS_EXPANSION_LIMIT)
    70                        ALIAS_EXCEEDS_EXPANSION_LIMIT)
    71 
    71 
    72     def __delete(self, destination=None):
    72     def __delete(self, destination=None):
    73         """Deletes a destination from the alias, if ``destination`` is
    73         """Deletes a destination from the alias, if ``destination`` is
    74         not ``None``.  If ``destination`` is None, the alias with all
    74         not ``None``.  If ``destination`` is None, the alias with all
    75         it's destination addresses will be deleted.
    75         its destination addresses will be deleted.
    76 
    76 
    77         """
    77         """
    78         dbc = self._dbh.cursor()
    78         dbc = self._dbh.cursor()
    79         if not destination:
    79         if not destination:
    80             dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
    80             dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
   100         """Adds the `EmailAddress`es from *destinations* list to the
   100         """Adds the `EmailAddress`es from *destinations* list to the
   101         destinations of the alias.
   101         destinations of the alias.
   102 
   102 
   103         Destinations, that are already assigned to the alias, will be
   103         Destinations, that are already assigned to the alias, will be
   104         removed from *destinations*.  When done, this method will return
   104         removed from *destinations*.  When done, this method will return
   105         a set with all destinations, that was saved in the database.
   105         a set with all destinations, that were saved in the database.
   106         """
   106         """
   107         destinations = set(destinations)
   107         destinations = set(destinations)
   108         assert destinations and \
   108         assert destinations and \
   109                 all(isinstance(dest, EmailAddress) for dest in destinations)
   109                 all(isinstance(dest, EmailAddress) for dest in destinations)
   110         if not warnings is None:
   110         if not warnings is None:
   132 
   132 
   133     def del_destination(self, destination):
   133     def del_destination(self, destination):
   134         """Deletes the specified ``destination`` address from the alias."""
   134         """Deletes the specified ``destination`` address from the alias."""
   135         assert isinstance(destination, EmailAddress)
   135         assert isinstance(destination, EmailAddress)
   136         if not self._dests:
   136         if not self._dests:
   137             raise AErr(_(u"The alias %r doesn't exist.") % str(self._addr),
   137             raise AErr(_(u"The alias '%s' doesn't exist.") % self._addr,
   138                        NO_SUCH_ALIAS)
   138                        NO_SUCH_ALIAS)
   139         if not destination in self._dests:
   139         if not destination in self._dests:
   140             raise AErr(_(u"The address %(d)r isn't a destination of \
   140             raise AErr(_(u"The address '%(d)s' isn't a destination of \
   141 the alias %(a)r.") %
   141 the alias '%(a)s'.") %
   142                        {'a': str(self._addr), 'd': str(destination)},
   142                        {'a': self._addr, 'd': destination},
   143                        NO_SUCH_ALIAS)
   143                        NO_SUCH_ALIAS)
   144         self.__delete(destination)
   144         self.__delete(destination)
   145         self._dests.remove(destination)
   145         self._dests.remove(destination)
   146 
   146 
   147     def get_destinations(self):
   147     def get_destinations(self):
   148         """Returns an iterator for all destinations of the alias."""
   148         """Returns an iterator for all destinations of the alias."""
   149         if not self._dests:
   149         if not self._dests:
   150             raise AErr(_(u"The alias %r doesn't exist.") % str(self._addr),
   150             raise AErr(_(u"The alias '%s' doesn't exist.") % self._addr,
   151                        NO_SUCH_ALIAS)
   151                        NO_SUCH_ALIAS)
   152         return iter(self._dests)
   152         return iter(self._dests)
   153 
   153 
   154     def delete(self):
   154     def delete(self):
   155         """Deletes the alias with all its destinations."""
   155         """Deletes the alias with all its destinations."""
   156         if not self._dests:
   156         if not self._dests:
   157             raise AErr(_(u"The alias %r doesn't exist.") % str(self._addr),
   157             raise AErr(_(u"The alias '%s' doesn't exist.") % self._addr,
   158                        NO_SUCH_ALIAS)
   158                        NO_SUCH_ALIAS)
   159         self.__delete()
   159         self.__delete()
   160         del self._dests[:]
   160         del self._dests[:]
   161 
   161 
   162 
   162