1 # -*- coding: UTF-8 -*- |
|
2 # Copyright (c) 2008 - 2010, Pascal Volk |
|
3 # See COPYING for distribution information. |
|
4 |
|
5 """ |
|
6 VirtualMailManager.Relocated |
|
7 |
|
8 Virtual Mail Manager's Relocated class to handle relocated users. |
|
9 """ |
|
10 |
|
11 from VirtualMailManager.Domain import get_gid |
|
12 from VirtualMailManager.EmailAddress import EmailAddress |
|
13 from VirtualMailManager.errors import RelocatedError as RErr |
|
14 from VirtualMailManager.constants import NO_SUCH_DOMAIN, \ |
|
15 NO_SUCH_RELOCATED, RELOCATED_ADDR_DEST_IDENTICAL, RELOCATED_EXISTS |
|
16 |
|
17 |
|
18 _ = lambda msg: msg |
|
19 |
|
20 |
|
21 class Relocated(object): |
|
22 """Class to handle e-mail addresses of relocated users.""" |
|
23 __slots__ = ('_addr', '_dest', '_gid', '_dbh') |
|
24 |
|
25 def __init__(self, dbh, address): |
|
26 """Creates a new *Relocated* instance. The ``address`` is the |
|
27 old e-mail address of the user. |
|
28 |
|
29 Use `setDestination()` to set/update the new address, where the |
|
30 user has moved to. |
|
31 |
|
32 """ |
|
33 assert isinstance(address, EmailAddress) |
|
34 self._addr = address |
|
35 self._dbh = dbh |
|
36 self._gid = get_gid(self._dbh, self._addr.domainname) |
|
37 if not self._gid: |
|
38 raise RErr(_(u"The domain %r doesn't exist.") % |
|
39 self._addr.domainname, NO_SUCH_DOMAIN) |
|
40 self._dest = None |
|
41 |
|
42 self.__load() |
|
43 |
|
44 def __nonzero__(self): |
|
45 """Returns `True` if the Relocated is known, `False` if it's new.""" |
|
46 return self._dest is not None |
|
47 |
|
48 def __load(self): |
|
49 """Loads the destination address from the database into the |
|
50 `_dest` attribute. |
|
51 |
|
52 """ |
|
53 dbc = self._dbh.cursor() |
|
54 dbc.execute('SELECT destination FROM relocated WHERE gid = %s AND ' |
|
55 'address = %s', self._gid, self._addr.localpart) |
|
56 destination = dbc.fetchone() |
|
57 dbc.close() |
|
58 if destination: |
|
59 self._dest = EmailAddress(destination[0]) |
|
60 |
|
61 @property |
|
62 def address(self): |
|
63 """The Relocated's EmailAddress instance.""" |
|
64 return self._addr |
|
65 |
|
66 def set_destination(self, destination): |
|
67 """Sets/updates the new address of the relocated user.""" |
|
68 update = False |
|
69 assert isinstance(destination, EmailAddress) |
|
70 if self._addr == destination: |
|
71 raise RErr(_(u'Address and destination are identical.'), |
|
72 RELOCATED_ADDR_DEST_IDENTICAL) |
|
73 if self._dest: |
|
74 if self._dest == destination: |
|
75 raise RErr(_(u"The relocated user '%s' already exists.") % |
|
76 self._addr, RELOCATED_EXISTS) |
|
77 else: |
|
78 self._dest = destination |
|
79 update = True |
|
80 else: |
|
81 self._dest = destination |
|
82 |
|
83 dbc = self._dbh.cursor() |
|
84 if not update: |
|
85 dbc.execute('INSERT INTO relocated VALUES (%s, %s, %s)', |
|
86 self._gid, self._addr.localpart, str(self._dest)) |
|
87 else: |
|
88 dbc.execute('UPDATE relocated SET destination = %s WHERE gid = %s ' |
|
89 'AND address = %s', str(self._dest), self._gid, |
|
90 self._addr.localpart) |
|
91 self._dbh.commit() |
|
92 dbc.close() |
|
93 |
|
94 def get_info(self): |
|
95 """Returns the address to which mails should be sent.""" |
|
96 if not self._dest: |
|
97 raise RErr(_(u"The relocated user '%s' doesn't exist.") % |
|
98 self._addr, NO_SUCH_RELOCATED) |
|
99 return self._dest |
|
100 |
|
101 def delete(self): |
|
102 """Deletes the relocated entry from the database.""" |
|
103 if not self._dest: |
|
104 raise RErr(_(u"The relocated user '%s' doesn't exist.") % |
|
105 self._addr, NO_SUCH_RELOCATED) |
|
106 dbc = self._dbh.cursor() |
|
107 dbc.execute('DELETE FROM relocated WHERE gid = %s AND address = %s', |
|
108 self._gid, self._addr.localpart) |
|
109 if dbc.rowcount > 0: |
|
110 self._dbh.commit() |
|
111 dbc.close() |
|
112 self._dest = None |
|
113 |
|
114 |
|
115 del _ |
|