diff -r af813ede1e19 -r 14c0a092d7d2 VirtualMailManager/Alias.py --- a/VirtualMailManager/Alias.py Sat Sep 06 03:07:28 2008 +0000 +++ b/VirtualMailManager/Alias.py Mon Sep 08 05:30:17 2008 +0000 @@ -1,4 +1,3 @@ -#!/usr/bin/env python # -*- coding: UTF-8 -*- # Copyright 2007-2008 VEB IT # See COPYING for distribution information. @@ -15,62 +14,55 @@ from Exceptions import VMMAliasException as VMMAE from Domain import Domain +from EmailAddress import EmailAddress import constants.ERROR as ERR import VirtualMailManager as VMM class Alias: - """Class to manage e-mail accounts.""" + """Class to manage e-mail aliases.""" def __init__(self, dbh, address, destination=None): + if isinstance(address, EmailAddress): + self._addr = address + else: + raise TypeError("Argument 'address' is not an EmailAddress") + if destination is None: + self._dest = None + elif isinstance(destination, EmailAddress): + self._dest = destination + else: + raise TypeError("Argument 'destination' is not an EmailAddress") if address == destination: raise VMMAE(_(u"Address and destination are identical."), ERR.ALIAS_ADDR_DEST_IDENTICAL) self._dbh = dbh - self._addr = VMM.VirtualMailManager.chkEmailAddress(address) - if destination is None: - self._dest = None - elif destination.count('@'): - self._dest = VMM.VirtualMailManager.chkEmailAddress(destination) - else: - self._dest = VMM.VirtualMailManager.chkLocalpart(destination) - self._localpart = None self._gid = 0 self._isNew = False self._setAddr() if not self._dest is None: self._exists() - if self._isAccount(): - raise VMMAE(_(u"There is already an account with address »%s«.") % + if VMM.VirtualMailManager.accountExists(self._dbh, self._addr): + raise VMMAE(_(u"There is already an account with address »%s«.") %\ self._addr, ERR.ACCOUNT_EXISTS) + if VMM.VirtualMailManager.relocatedExists(self._dbh, self._addr): + raise VMMAE( + _(u"There is already an relocated user with the address »%s«.") %\ + self._addr, ERR.RELOCATED_EXISTS) def _exists(self): dbc = self._dbh.cursor() dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\ - AND destination=%s", self._gid, self._localpart, self._dest) + AND destination=%s", self._gid, self._addr._localpart, str(self._dest)) gid = dbc.fetchone() dbc.close() if gid is None: self._isNew = True - else: - self._isNew = False - def _isAccount(self): - dbc = self._dbh.cursor() - dbc.execute("SELECT uid FROM users WHERE gid=%s AND local_part=%s", - self._gid, self._localpart) - uid = dbc.fetchone() - dbc.close() - if uid is not None: - return True - else: - return False - def _setAddr(self): - self._localpart, d = self._addr.split('@') - dom = Domain(self._dbh, d) + dom = Domain(self._dbh, self._addr._domainname) self._gid = dom.getID() if self._gid == 0: - raise VMMAE(_(u"The domain »%s« doesn't exist yet.") % d, - ERR.NO_SUCH_DOMAIN) + raise VMMAE(_(u"The domain »%s« doesn't exist yet.") %\ + self._addr._domainname, ERR.NO_SUCH_DOMAIN) def save(self): if self._dest is None: @@ -79,17 +71,18 @@ if self._isNew: dbc = self._dbh.cursor() dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\ - (%s, %s, %s)", self._gid, self._localpart, self._dest) + (%s, %s, %s)", self._gid, self._addr._localpart, str(self._dest)) self._dbh.commit() dbc.close() else: - raise VMMAE(_(u"The alias »%s« already exists.") % self._addr, - ERR.ALIAS_EXISTS) + raise VMMAE( + _(u"The alias »%(a)s« with destination »%(d)s« already exists.")\ + % {'a': self._addr, 'd': self._dest}, ERR.ALIAS_EXISTS) def getInfo(self): dbc = self._dbh.cursor() dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s', - self._gid, self._localpart) + self._gid, self._addr._localpart) destinations = dbc.fetchall() dbc.close() if len(destinations) > 0: @@ -105,15 +98,19 @@ dbc = self._dbh.cursor() if self._dest is None: dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s", - self._gid, self._localpart) + self._gid, self._addr._localpart) else: dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s AND \ - destination=%s", self._gid, self._localpart, self._dest) + destination=%s", self._gid, self._addr._localpart, str(self._dest)) rowcount = dbc.rowcount dbc.close() if rowcount > 0: self._dbh.commit() else: - raise VMMAE(_(u"The alias »%s« doesn't exists.") % self._addr, - ERR.NO_SUCH_ALIAS) + if self._dest is None: + msg = u"The alias »%s« doesn't exists." % self._addr + else: + msg = u"The alias »%(a)s« with destination »%(d)s« doesn't\ + exists." % {'a': self._addr, 'd': self._dest} + raise VMMAE(_(msg), ERR.NO_SUCH_ALIAS)