author | Pascal Volk <neverseen@users.sourceforge.net> |
Mon, 08 Sep 2008 23:47:34 +0000 | |
changeset 79 | 0ae7597eed11 |
parent 76 | 14c0a092d7d2 |
child 82 | 6c85915f3815 |
permissions | -rw-r--r-- |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
2 |
# Copyright 2008 VEB IT |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
3 |
# See COPYING for distribution information. |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
4 |
# $Id$ |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
5 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
6 |
"""Virtual Mail Manager's Relocated class to manage relocated users.""" |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
7 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
8 |
from constants.VERSION import VERSION |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
9 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
10 |
__author__ = 'Pascal Volk <p.volk@veb-it.de>' |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
11 |
__version__ = VERSION |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
12 |
__revision__ = 'rev '+'$Rev$'.split()[1] |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
13 |
__date__ = '$Date$'.split()[1] |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
14 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
15 |
from Exceptions import VMMRelocatedException as VMMRE |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
16 |
from Domain import Domain |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
17 |
from EmailAddress import EmailAddress |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
18 |
import constants.ERROR as ERR |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
19 |
import VirtualMailManager as VMM |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
20 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
21 |
class Relocated: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
22 |
"""Class to manage e-mail addresses of relocated users.""" |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
23 |
def __init__(self, dbh, address, destination=None): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
24 |
if isinstance(address, EmailAddress): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
self._addr = address |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
26 |
else: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
27 |
raise TypeError("Argument 'address' is not an EmailAddress") |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
28 |
if destination is None: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
29 |
self._dest = None |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
30 |
elif isinstance(destination, EmailAddress): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
31 |
self._dest = destination |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
32 |
else: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
33 |
raise TypeError("Argument 'destination' is not an EmailAddress") |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
34 |
if address == destination: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
35 |
raise VMMRE(_(u"Address and destination are identical."), |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
36 |
ERR.RELOCATED_ADDR_DEST_IDENTICAL) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
37 |
self._dbh = dbh |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
38 |
self._gid = 0 |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
39 |
self._isNew = False |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
40 |
self._setAddr() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
41 |
self._exists() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
42 |
if VMM.VirtualMailManager.accountExists(self._dbh, self._addr): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
43 |
raise VMMRE(_(u"There is already an account with address »%s«.") %\ |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
44 |
self._addr, ERR.ACCOUNT_EXISTS) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
45 |
if VMM.VirtualMailManager.aliasExists(self._dbh, self._addr): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
46 |
raise VMMRE( |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
47 |
_(u"There is already an alias with the address »%s«.") %\ |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
48 |
self._addr, ERR.ALIAS_EXISTS) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
49 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
50 |
def _exists(self): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
51 |
dbc = self._dbh.cursor() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
52 |
dbc.execute("SELECT gid FROM relocated WHERE gid = %s AND address = %s", |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
53 |
self._gid, self._addr._localpart) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
54 |
gid = dbc.fetchone() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
55 |
dbc.close() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
56 |
if gid is None: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
57 |
self._isNew = True |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
58 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
59 |
def _setAddr(self): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
60 |
dom = Domain(self._dbh, self._addr._domainname) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
61 |
self._gid = dom.getID() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
62 |
if self._gid == 0: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
63 |
raise VMMRE(_(u"The domain »%s« doesn't exist yet.") %\ |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
64 |
self._addr._domainname, ERR.NO_SUCH_DOMAIN) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
65 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
66 |
def save(self): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
67 |
if self._dest is None: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
68 |
raise VMMRE(_(u"No destination address for relocated user denoted."), |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
69 |
ERR.RELOCATED_MISSING_DEST) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
70 |
if self._isNew: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
71 |
dbc = self._dbh.cursor() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
72 |
dbc.execute("INSERT INTO relocated VALUES (%s, %s, %s)", |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
73 |
self._gid, self._addr._localpart, str(self._dest)) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
74 |
self._dbh.commit() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
75 |
dbc.close() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
76 |
else: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
77 |
raise VMMRE( |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
78 |
_(u"The relocated user »%s« already exists.") % self._addr, |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
79 |
ERR.RELOCATED_EXISTS) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
80 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
81 |
def getInfo(self): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
82 |
dbc = self._dbh.cursor() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
83 |
dbc.execute('SELECT destination FROM relocated WHERE gid=%s\ |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
84 |
AND address=%s', |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
85 |
self._gid, self._addr._localpart) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
86 |
destination = dbc.fetchone() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
87 |
dbc.close() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
88 |
if destination is not None: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
89 |
return destination[0] |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
90 |
else: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
91 |
raise VMMRE( |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
92 |
_(u"The relocated user »%s« doesn't exists.") % self._addr, |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
93 |
ERR.NO_SUCH_RELOCATED) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
94 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
95 |
def delete(self): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
96 |
dbc = self._dbh.cursor() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
97 |
dbc.execute("DELETE FROM relocated WHERE gid = %s AND address = %s", |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
98 |
self._gid, self._addr._localpart) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
99 |
rowcount = dbc.rowcount |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
100 |
dbc.close() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
101 |
if rowcount > 0: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
102 |
self._dbh.commit() |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
103 |
else: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
104 |
raise VMMRE( |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
105 |
_(u"The relocated user »%s« doesn't exists.") % self._addr, |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
106 |
ERR.NO_SUCH_RELOCATED) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
107 |