VMM/Handler: small code cleanups and an improved import statement.
# -*- coding: UTF-8 -*-# Copyright (c) 2008 - 2010, Pascal Volk# See COPYING for distribution information."""Virtual Mail Manager's AliasDomain class to manage alias domains."""importVirtualMailManager.constants.ERRORasERRfromVirtualMailManagerimportcheck_domainnamefromVirtualMailManager.errorsimportAliasDomainErrorasADEclassAliasDomain(object):"""Class to manage e-mail alias domains."""__slots__=('__gid','__name','_domain','_dbh')def__init__(self,dbh,domainname,targetDomain=None):self._dbh=dbhself.__name=check_domainname(domainname)self.__gid=0self._domain=targetDomainself._exists()def_exists(self):dbc=self._dbh.cursor()dbc.execute('SELECT gid, is_primary FROM domain_name WHERE domainname\ = %s',self.__name)alias=dbc.fetchone()dbc.close()ifaliasisnotNone:self.__gid,primary=aliasifprimary:raiseADE(_(u"The domain “%s” is a primary domain.")%self.__name,ERR.ALIASDOMAIN_ISDOMAIN)defsave(self):ifself.__gid>0:raiseADE(_(u'The alias domain “%s” already exists.')%self.__name,ERR.ALIASDOMAIN_EXISTS)ifself._domainisNone:raiseADE(_(u'No destination domain specified for alias domain.'),ERR.ALIASDOMAIN_NO_DOMDEST)ifself._domain.gid<1:raiseADE(_(u"The target domain “%s” doesn't exist.")%self._domain.name,ERR.NO_SUCH_DOMAIN)dbc=self._dbh.cursor()dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary)\ VALUES (%s, %s, FALSE)',self.__name,self._domain.gid)self._dbh.commit()dbc.close()definfo(self):ifself.__gid>0:dbc=self._dbh.cursor()dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s\ AND is_primary',self.__gid)domain=dbc.fetchone()dbc.close()ifdomainisnotNone:return{'alias':self.__name,'domain':domain[0]}else:# an almost unlikely case, isn't it?raiseADE(_(u'There is no primary domain for the alias domain “%s”.')\%self.__name,ERR.NO_SUCH_DOMAIN)else:raiseADE(_(u"The alias domain “%s” doesn't exist.")%self.__name,ERR.NO_SUCH_ALIASDOMAIN)defswitch(self):ifself._domainisNone:raiseADE(_(u'No destination domain specified for alias domain.'),ERR.ALIASDOMAIN_NO_DOMDEST)ifself._domain.gid<1:raiseADE(_(u"The target domain “%s” doesn't exist.")%self._domain.name,ERR.NO_SUCH_DOMAIN)ifself.__gid<1:raiseADE(_(u"The alias domain “%s” doesn't exist.")%self.__name,ERR.NO_SUCH_ALIASDOMAIN)ifself.__gid==self._domain.gid:raiseADE(_(u"The alias domain “%(alias)s” is already assigned to\ the domain “%(domain)s”.")%{'alias':self.__name,'domain':self._domain.name},ERR.ALIASDOMAIN_EXISTS)dbc=self._dbh.cursor()dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s\ AND domainname = %s AND NOT is_primary',self._domain.gid,self.__gid,self.__name)self._dbh.commit()dbc.close()defdelete(self):ifself.__gid>0:dbc=self._dbh.cursor()dbc.execute('DELETE FROM domain_name WHERE domainname = %s\ AND NOT is_primary',self.__name)ifdbc.rowcount>0:self._dbh.commit()else:raiseADE(_(u"The alias domain “%s” doesn't exist.")%self.__name,ERR.NO_SUCH_ALIASDOMAIN)