7 |
7 |
8 Virtual Mail Manager's EmailAddress class to handle e-mail addresses. |
8 Virtual Mail Manager's EmailAddress class to handle e-mail addresses. |
9 """ |
9 """ |
10 import re |
10 import re |
11 |
11 |
12 from VirtualMailManager.domain import check_domainname |
12 from VirtualMailManager.domain import check_domainname, get_gid |
13 from VirtualMailManager.constants import \ |
13 from VirtualMailManager.constants import \ |
14 DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG |
14 DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG |
15 from VirtualMailManager.errors import EmailAddressError as EAErr |
15 from VirtualMailManager.errors import EmailAddressError as EAErr |
16 |
16 |
17 |
17 |
81 DOMAIN_NO_NAME) |
81 DOMAIN_NO_NAME) |
82 self._localpart = check_localpart(parts[0]) |
82 self._localpart = check_localpart(parts[0]) |
83 self._domainname = check_domainname(parts[1]) |
83 self._domainname = check_domainname(parts[1]) |
84 |
84 |
85 |
85 |
|
86 class DestinationEmailAddress(EmailAddress): |
|
87 """Provides additionally the domains group ID - when the domain is known |
|
88 in the database.""" |
|
89 __slots__ = ('_gid') |
|
90 |
|
91 def __init__(self, address, dbh): |
|
92 """Creates a new DestinationEmailAddress instance |
|
93 |
|
94 Arguments: |
|
95 |
|
96 `address`: string/unicode |
|
97 a e-mail address like user@example.com |
|
98 `dbh`: pyPgSQL.PgSQL.Connection/pyPgSQL.PgSQL.connection |
|
99 a database connection for the database access |
|
100 """ |
|
101 super(DestinationEmailAddress, self).__init__(address) |
|
102 self._gid = 0 |
|
103 self._find_domain(dbh) |
|
104 |
|
105 def _find_domain(self, dbh): |
|
106 """Checks if the domain is known""" |
|
107 self._gid = get_gid(dbh, self._domainname) |
|
108 if self._gid: |
|
109 self._localpart = self._localpart.lower() |
|
110 |
|
111 @property |
|
112 def gid(self): |
|
113 """The domains group ID. 0 if the domain is not known.""" |
|
114 return self._gid |
|
115 |
|
116 |
86 def check_localpart(localpart): |
117 def check_localpart(localpart): |
87 """Returns the validated local-part `localpart`. |
118 """Returns the validated local-part `localpart`. |
88 |
119 |
89 Throws a `EmailAddressError` if the local-part is too long or contains |
120 Throws a `EmailAddressError` if the local-part is too long or contains |
90 invalid characters. |
121 invalid characters. |