VMM/Domain: added function get_gid() to the Domain module. v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Wed, 10 Feb 2010 08:55:51 +0000
branchv0.6.x
changeset 198 02d467e4fbab
parent 197 d2712e8c724e
child 199 0684790fff7c
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.
VirtualMailManager/Alias.py
VirtualMailManager/Domain.py
VirtualMailManager/Relocated.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()
--- 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)
--- 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."""