VirtualMailManager/DomainAlias.py
changeset 53 5b50eb306d37
parent 48 0d5f58f8b8f5
--- a/VirtualMailManager/DomainAlias.py	Thu Aug 21 01:23:31 2008 +0000
+++ b/VirtualMailManager/DomainAlias.py	Fri Aug 22 03:07:53 2008 +0000
@@ -15,20 +15,74 @@
 
 from Exceptions import VMMDomainAliasException as VDAE
 import constants.ERROR as ERR
+import VirtualMailManager as VMM
 
 class DomainAlias:
     """Class to manage e-mail alias domains."""
-    def __init__(self, dbh, domainname, targetDomain):
+    def __init__(self, dbh, domainname, targetDomain=None):
         self._dbh = dbh
+        self.__name = VMM.VirtualMailManager.chkDomainname(domainname)
+        self.__gid = 0
+        self._domain = targetDomain
+        self._exists()
 
     def _exists(self):
-        pass
+        dbc = self._dbh.cursor()
+        dbc.execute('SELECT gid, is_primary FROM domain_name WHERE domainname\
+ = %s', self.__name)
+        alias = dbc.fetchone()
+        dbc.close()
+        if alias is not None:
+            self.__gid, primary = alias
+            if primary:
+                raise VDAE(_(u"The domain »%s« is a primary domain.") %
+                        self.__name, ERR.DOMAIN_ALIAS_ISDOMAIN)
 
     def save(self):
-        pass
+        if self.__gid > 0:
+            raise VDAE(_(u'The domain alias »%s« already exists.') %self.__name,
+                ERR.DOMAIN_ALIAS_EXISTS)
+        if self._domain is None:
+            raise VDAE(_(u'No destination domain for alias domain denoted.'),
+                    ERR.DOMAIN_ALIAS_NO_DOMDEST)
+        if self._domain._id < 1:
+            raise VDAE (_(u"The target domain »%s« doesn't exist yet.") %
+                    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._id)
+        self._dbh.commit()
+        dbc.close()
+
 
     def info(self):
-        pass
+        if self.__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()
+            if domain is not None:
+                return _(u"The domain alias »%(alias)s« belongs to »%(dom)s«.")\
+                        % {'alias': self.__name, 'dom': domain[0]}
+            else:# an almost unlikely case, isn't it?
+                raise VDAE(
+                    _(u'There is no primary domain for the domain alias »%s«.')\
+                            % self.__name, ERR.NO_SUCH_DOMAIN)
+        else:
+            raise VDAE(
+                  _(u"The domain alias »%s« doesn't exist yet.") % self.__name,
+                  ERR.NO_SUCH_DOMAIN_ALIAS)
     
     def delete(self):
-        pass
+        if self.__gid > 0:
+            dbc = self._dbh.cursor()
+            dbc.execute('DELETE FROM domain_name WHERE domainname = %s \
+ AND NOT is_primary', self.__name)
+            if dbc.rowcount > 0:
+                self._dbh.commit()
+        else:
+            raise VDAE(
+                  _(u"The domain alias »%s« doesn't exist yet.") % self.__name,
+                  ERR.NO_SUCH_DOMAIN_ALIAS)
+