26 """Creates a new *Relocated* instance. The ``address`` is the |
26 """Creates a new *Relocated* instance. The ``address`` is the |
27 old e-mail address of the user. |
27 old e-mail address of the user. |
28 |
28 |
29 Use `setDestination()` to set/update the new address, where the |
29 Use `setDestination()` to set/update the new address, where the |
30 user has moved to.""" |
30 user has moved to.""" |
31 if isinstance(address, EmailAddress): |
31 if not isinstance(address, EmailAddress): |
32 self._addr = address |
|
33 else: |
|
34 raise TypeError("Argument 'address' is not an EmailAddress") |
32 raise TypeError("Argument 'address' is not an EmailAddress") |
|
33 self._addr = address |
35 self._dbh = dbh |
34 self._dbh = dbh |
36 self._gid = get_gid(self._dbh, self._addr.domainname) |
35 self._gid = get_gid(self._dbh, self._addr.domainname) |
37 self._dest = None |
36 self._dest = None |
38 |
37 |
39 self.__load() |
38 self.__load() |
51 self._dest = EmailAddress(destination[0]) |
50 self._dest = EmailAddress(destination[0]) |
52 |
51 |
53 def setDestination(self, destination): |
52 def setDestination(self, destination): |
54 """Sets/updates the new address of the relocated user.""" |
53 """Sets/updates the new address of the relocated user.""" |
55 update = False |
54 update = False |
56 if isinstance(destination, EmailAddress): |
55 if not isinstance(destination, EmailAddress): |
57 if self._addr == destination: |
56 raise TypeError("Argument 'destination' is not an EmailAddress") |
58 raise VMMRE(_(u'Address and destination are identical.'), |
57 if self._addr == destination: |
59 RELOCATED_ADDR_DEST_IDENTICAL) |
58 raise VMMRE(_(u'Address and destination are identical.'), |
60 if self._dest: |
59 RELOCATED_ADDR_DEST_IDENTICAL) |
61 if self._dest == destination: |
60 if self._dest: |
62 raise VMMRE( |
61 if self._dest == destination: |
63 _(u'The relocated user “%s” already exists.') % |
62 raise VMMRE(_(u'The relocated user %r already exists.') % |
64 self._addr, RELOCATED_EXISTS) |
63 self._addr, RELOCATED_EXISTS) |
65 else: |
|
66 self._dest = destination |
|
67 update = True |
|
68 else: |
64 else: |
69 self._dest = destination |
65 self._dest = destination |
|
66 update = True |
70 else: |
67 else: |
71 raise TypeError("Argument 'destination' is not an EmailAddress") |
68 self._dest = destination |
72 |
69 |
73 dbc = self._dbh.cursor() |
70 dbc = self._dbh.cursor() |
74 if not update: |
71 if not update: |
75 dbc.execute('INSERT INTO relocated VALUES (%s, %s, %s)', |
72 dbc.execute('INSERT INTO relocated VALUES (%s, %s, %s)', |
76 self._gid, self._addr.localpart, str(self._dest)) |
73 self._gid, self._addr.localpart, str(self._dest)) |
81 self._dbh.commit() |
78 self._dbh.commit() |
82 dbc.close() |
79 dbc.close() |
83 |
80 |
84 def getInfo(self): |
81 def getInfo(self): |
85 """Returns the address to which mails should be sent.""" |
82 """Returns the address to which mails should be sent.""" |
86 if self._dest: |
83 if not self._dest: |
87 return self._dest |
84 raise VMMRE(_(u"The relocated user %r doesn't exist.") % |
88 else: |
|
89 raise VMMRE(_(u"The relocated user “%s” doesn't exist.") % |
|
90 self._addr, NO_SUCH_RELOCATED) |
85 self._addr, NO_SUCH_RELOCATED) |
|
86 return self._dest |
91 |
87 |
92 def delete(self): |
88 def delete(self): |
93 """Deletes the relocated entry from the database.""" |
89 """Deletes the relocated entry from the database.""" |
94 if not self._dest: |
90 if not self._dest: |
95 raise VMMRE(_(u"The relocated user “%s” doesn't exist.") % |
91 raise VMMRE(_(u"The relocated user %r doesn't exist.") % |
96 self._addr, NO_SUCH_RELOCATED) |
92 self._addr, NO_SUCH_RELOCATED) |
97 dbc = self._dbh.cursor() |
93 dbc = self._dbh.cursor() |
98 dbc.execute("DELETE FROM relocated WHERE gid = %s AND address = %s", |
94 dbc.execute("DELETE FROM relocated WHERE gid = %s AND address = %s", |
99 self._gid, self._addr.localpart) |
95 self._gid, self._addr.localpart) |
100 if dbc.rowcount > 0: |
96 if dbc.rowcount > 0: |