8 Virtual Mail Manager's Alias class to manage e-mail aliases. |
8 Virtual Mail Manager's Alias class to manage e-mail aliases. |
9 """ |
9 """ |
10 |
10 |
11 from VirtualMailManager.Domain import get_gid |
11 from VirtualMailManager.Domain import get_gid |
12 from VirtualMailManager.EmailAddress import EmailAddress |
12 from VirtualMailManager.EmailAddress import EmailAddress |
13 from VirtualMailManager.Exceptions import VMMAliasException as VMMAE |
13 from VirtualMailManager.errors import AliasError as AErr |
14 from VirtualMailManager.constants.ERROR import ALIAS_ADDR_DEST_IDENTICAL, \ |
14 from VirtualMailManager.constants.ERROR import ALIAS_ADDR_DEST_IDENTICAL, \ |
15 ALIAS_EXCEEDS_EXPANSION_LIMIT, ALIAS_EXISTS, NO_SUCH_ALIAS |
15 ALIAS_EXCEEDS_EXPANSION_LIMIT, ALIAS_EXISTS, NO_SUCH_ALIAS |
16 |
16 |
17 |
17 |
18 _ = lambda msg: msg |
18 _ = lambda msg: msg |
61 u"""Can't add new destination to alias %(address)r. |
61 u"""Can't add new destination to alias %(address)r. |
62 This alias already exceeds it's expansion limit (%(count)i/%(limit)i). |
62 This alias already exceeds it's expansion limit (%(count)i/%(limit)i). |
63 So its unusable, all messages addressed to this alias will be bounced. |
63 So its unusable, all messages addressed to this alias will be bounced. |
64 Hint: Delete some destination addresses.""") |
64 Hint: Delete some destination addresses.""") |
65 if failed: |
65 if failed: |
66 raise VMMAE(errmsg % {'address': str(self._addr), 'count': dcount, |
66 raise AErr(errmsg % {'address': str(self._addr), 'count': dcount, |
67 'limit': limit}, |
67 'limit': limit}, |
68 ALIAS_EXCEEDS_EXPANSION_LIMIT) |
68 ALIAS_EXCEEDS_EXPANSION_LIMIT) |
69 |
69 |
70 def __delete(self, destination=None): |
70 def __delete(self, destination=None): |
71 """Deletes a destination from the alias, if ``destination`` is |
71 """Deletes a destination from the alias, if ``destination`` is |
72 not ``None``. If ``destination`` is None, the alias with all |
72 not ``None``. If ``destination`` is None, the alias with all |
73 it's destination addresses will be deleted. |
73 it's destination addresses will be deleted. |
91 |
91 |
92 def add_destination(self, destination, expansion_limit): |
92 def add_destination(self, destination, expansion_limit): |
93 """Adds the ``destination`` `EmailAddress` to the alias.""" |
93 """Adds the ``destination`` `EmailAddress` to the alias.""" |
94 assert isinstance(destination, EmailAddress) |
94 assert isinstance(destination, EmailAddress) |
95 if self._addr == destination: |
95 if self._addr == destination: |
96 raise VMMAE(_(u"Address and destination are identical."), |
96 raise AErr(_(u"Address and destination are identical."), |
97 ALIAS_ADDR_DEST_IDENTICAL) |
97 ALIAS_ADDR_DEST_IDENTICAL) |
98 if destination in self._dests: |
98 if destination in self._dests: |
99 raise VMMAE(_( |
99 raise AErr(_( |
100 u'The alias %(a)r has already the destination %(d)r.') % |
100 u'The alias %(a)r has already the destination %(d)r.') % |
101 {'a': str(self._addr), 'd': str(destination)}, |
101 {'a': str(self._addr), 'd': str(destination)}, |
102 ALIAS_EXISTS) |
102 ALIAS_EXISTS) |
103 self.__check_expansion(expansion_limit) |
103 self.__check_expansion(expansion_limit) |
104 dbc = self._dbh.cursor() |
104 dbc = self._dbh.cursor() |
105 dbc.execute('INSERT INTO alias (gid, address, destination) \ |
105 dbc.execute('INSERT INTO alias (gid, address, destination) \ |
106 VALUES (%s, %s, %s)', |
106 VALUES (%s, %s, %s)', |
107 self._gid, self._addr.localpart, str(destination)) |
107 self._gid, self._addr.localpart, str(destination)) |
111 |
111 |
112 def del_destination(self, destination): |
112 def del_destination(self, destination): |
113 """Deletes the specified ``destination`` address from the alias.""" |
113 """Deletes the specified ``destination`` address from the alias.""" |
114 assert isinstance(destination, EmailAddress) |
114 assert isinstance(destination, EmailAddress) |
115 if not self._dests: |
115 if not self._dests: |
116 raise VMMAE(_(u"The alias %r doesn't exist.") % str(self._addr), |
116 raise AErr(_(u"The alias %r doesn't exist.") % str(self._addr), |
117 NO_SUCH_ALIAS) |
117 NO_SUCH_ALIAS) |
118 if not destination in self._dests: |
118 if not destination in self._dests: |
119 raise VMMAE(_(u"The address %(d)r isn't a destination of \ |
119 raise AErr(_(u"The address %(d)r isn't a destination of \ |
120 the alias %(a)r.") % |
120 the alias %(a)r.") % |
121 {'a': str(self._addr), 'd': str(destination)}, |
121 {'a': str(self._addr), 'd': str(destination)}, |
122 NO_SUCH_ALIAS) |
122 NO_SUCH_ALIAS) |
123 self.__delete(destination) |
123 self.__delete(destination) |
124 self._dests.remove(destination) |
124 self._dests.remove(destination) |
125 |
125 |
126 def get_destinations(self): |
126 def get_destinations(self): |
127 """Returns an iterator for all destinations of the alias.""" |
127 """Returns an iterator for all destinations of the alias.""" |
128 if not self._dests: |
128 if not self._dests: |
129 raise VMMAE(_(u"The alias %r doesn't exist.") % str(self._addr), |
129 raise AErr(_(u"The alias %r doesn't exist.") % str(self._addr), |
130 NO_SUCH_ALIAS) |
130 NO_SUCH_ALIAS) |
131 return iter(self._dests) |
131 return iter(self._dests) |
132 |
132 |
133 def delete(self): |
133 def delete(self): |
134 """Deletes the alias with all it's destinations.""" |
134 """Deletes the alias with all it's destinations.""" |
135 if not self._dests: |
135 if not self._dests: |
136 raise VMMAE(_(u"The alias %r doesn't exist.") % str(self._addr), |
136 raise AErr(_(u"The alias %r doesn't exist.") % str(self._addr), |
137 NO_SUCH_ALIAS) |
137 NO_SUCH_ALIAS) |
138 self.__delete() |
138 self.__delete() |
139 del self._dests[:] |
139 del self._dests[:] |
140 |
140 |
141 |
141 |
142 del _ |
142 del _ |