# HG changeset patch # User Pascal Volk # Date 1265792151 0 # Node ID 02d467e4fbab45d5a80021273189d73234384abc # Parent d2712e8c724e246663b4fcd0e529af504b7b0734 VMM/Domain: added function get_gid() to the Domain module. We don't need to load all the domain related information from the database, when we need only the GID of a domain. For example in the Alias or Relocated classes. diff -r d2712e8c724e -r 02d467e4fbab VirtualMailManager/Alias.py --- a/VirtualMailManager/Alias.py Wed Feb 10 07:38:19 2010 +0000 +++ b/VirtualMailManager/Alias.py Wed Feb 10 08:55:51 2010 +0000 @@ -8,11 +8,11 @@ Virtual Mail Manager's Alias class to manage e-mail aliases. """ -from VirtualMailManager.Domain import Domain +from VirtualMailManager.Domain import get_gid from VirtualMailManager.EmailAddress import EmailAddress from VirtualMailManager.Exceptions import VMMAliasException as VMMAE from VirtualMailManager.constants.ERROR import ALIAS_ADDR_DEST_IDENTICAL, \ - ALIAS_EXCEEDS_EXPANSION_LIMIT, ALIAS_EXISTS, NO_SUCH_ALIAS, NO_SUCH_DOMAIN + ALIAS_EXCEEDS_EXPANSION_LIMIT, ALIAS_EXISTS, NO_SUCH_ALIAS _ = lambda msg: msg @@ -28,20 +28,11 @@ else: raise TypeError("Argument 'address' is not an EmailAddress") self._dbh = dbh - self._gid = 0 + self._gid = get_gid(self._dbh, self._addr.domainname) self._dests = [] - self.__set_gid() self.__load_dests() - def __set_gid(self): - """Sets the alias' _gid based on its _addr.domainname.""" - dom = Domain(self._dbh, self._addr.domainname) - self._gid = dom.getID() - if self._gid == 0: - raise VMMAE(_(u"The domain “%s” doesn't exist.") % - self._addr.domainname, NO_SUCH_DOMAIN) - def __load_dests(self): """Loads all known destination addresses into the _dests list.""" dbc = self._dbh.cursor() diff -r d2712e8c724e -r 02d467e4fbab VirtualMailManager/Domain.py --- a/VirtualMailManager/Domain.py Wed Feb 10 07:38:19 2010 +0000 +++ b/VirtualMailManager/Domain.py Wed Feb 10 08:55:51 2010 +0000 @@ -278,6 +278,7 @@ aliasdomains = [aname[0] for aname in anames] return aliasdomains + def search(dbh, pattern=None, like=False): if pattern is not None and like is False: pattern = chk_domainname(pattern) @@ -311,3 +312,18 @@ del doms return order, domdict +def get_gid(dbh, domainname): + """Returns the *GID* of the domain *domainname*. + + Raises an `VMMDomainException` if the domain does not exist. + """ + domainname = chk_domainname(domainname) + dbc = dbh.cursor() + dbc.execute('SELECT gid FROM domain_name WHERE domainname=%s', domainname) + gid = dbc.fetchone() + dbc.close() + if gid: + return gid[0] + else: + raise VMMDE(_(u"The domain “%s” doesn't exist.") % domainname, + NO_SUCH_DOMAIN) diff -r d2712e8c724e -r 02d467e4fbab VirtualMailManager/Relocated.py --- a/VirtualMailManager/Relocated.py Wed Feb 10 07:38:19 2010 +0000 +++ b/VirtualMailManager/Relocated.py Wed Feb 10 08:55:51 2010 +0000 @@ -8,10 +8,10 @@ Virtual Mail Manager's Relocated class to handle relocated users. """ -from VirtualMailManager.Domain import Domain +from VirtualMailManager.Domain import get_gid from VirtualMailManager.EmailAddress import EmailAddress from VirtualMailManager.Exceptions import VMMRelocatedException as VMMRE -from VirtualMailManager.constants.ERROR import NO_SUCH_DOMAIN, \ +from VirtualMailManager.constants.ERROR import \ NO_SUCH_RELOCATED, RELOCATED_ADDR_DEST_IDENTICAL, RELOCATED_EXISTS @@ -33,20 +33,11 @@ else: raise TypeError("Argument 'address' is not an EmailAddress") self._dbh = dbh - self._gid = 0 + self._gid = get_gid(self._dbh, self._addr.domainname) self._dest = None - self.__set_gid() self.__load() - def __set_gid(self): - """Sets the `_gid` attribute, based on the `_addr.domainname`.""" - dom = Domain(self._dbh, self._addr.domainname) - self._gid = dom.getID() - if self._gid == 0: - raise VMMRE(_(u"The domain “%s” doesn't exist.") % - self._addr.domainname, NO_SUCH_DOMAIN) - def __load(self): """Loads the destination address from the database into the `_dest` attribute."""