VirtualMailManager/Domain.py
changeset 45 9e66138aad0b
parent 44 c9ab6900ede9
child 47 191d5a5adc4a
equal deleted inserted replaced
44:c9ab6900ede9 45:9e66138aad0b
    52 
    52 
    53         If the domain exists _id will be set and returns True, otherwise False
    53         If the domain exists _id will be set and returns True, otherwise False
    54         will be returned.
    54         will be returned.
    55         """
    55         """
    56         dbc = self._dbh.cursor()
    56         dbc = self._dbh.cursor()
    57         # XXX check for primary
       
    58         dbc.execute("SELECT gid, tid, domaindir FROM domain_data WHERE gid =\
    57         dbc.execute("SELECT gid, tid, domaindir FROM domain_data WHERE gid =\
    59  (SELECT gid FROM domain_name WHERE domainname = %s)", self._name)
    58  (SELECT gid FROM domain_name WHERE domainname = %s AND is_primary)",
       
    59             self._name)
    60         result = dbc.fetchone()
    60         result = dbc.fetchone()
    61         dbc.close()
    61         dbc.close()
    62         if result is not None:
    62         if result is not None:
    63             self._id, self._domaindir = result[0], result[2]
    63             self._id, self._domaindir = result[0], result[2]
    64             self._transport = Transport(self._dbh, tid=result[1])
    64             self._transport = Transport(self._dbh, tid=result[1])
    65             return True
    65             return True
    66         else:
    66         else:
    67             return False
    67             return False
       
    68 
       
    69     def _aliasExists(self, aliasname):
       
    70         dbc = self._dbh.cursor()
       
    71         dbc.execute("SELECT gid, is_primary FROM domain_name\
       
    72  WHERE domainname = %s", aliasname)
       
    73         result = dbc.fetchone()
       
    74         dbc.close()
       
    75         if result is None:
       
    76             return False
       
    77         elif result[1]:
       
    78             raise VMMDomainException((_('Domain already exists.'),
       
    79                 ERR.DOMAIN_EXISTS))
       
    80         else:
       
    81             raise VMMDomainException((_('Domain alias already exists.'),
       
    82                 ERR.DOMAIN_ALIAS_EXISTS))
    68 
    83 
    69     def _setID(self):
    84     def _setID(self):
    70         """Sets the ID of the domain."""
    85         """Sets the ID of the domain."""
    71         dbc = self._dbh.cursor()
    86         dbc = self._dbh.cursor()
    72         dbc.execute("SELECT nextval('domain_gid')")
    87         dbc.execute("SELECT nextval('domain_gid')")
   149         delAlias -- force deletion of available aliases (bool)
   164         delAlias -- force deletion of available aliases (bool)
   150         """
   165         """
   151         if self._id > 0:
   166         if self._id > 0:
   152             self._chkDelete(delUser, delAlias)
   167             self._chkDelete(delUser, delAlias)
   153             dbc = self._dbh.cursor()
   168             dbc = self._dbh.cursor()
   154             dbc.execute('DELETE FROM alias WHERE gid=%s', self._id)
   169             for t in ('alias','users','relocated','domain_name','domain_data'):
   155             dbc.execute('DELETE FROM users WHERE gid=%s', self._id)
   170                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
   156             dbc.execute('DELETE FROM relocated WHERE gid=%s', self._id)
       
   157             dbc.execute('DELETE FROM domain_name WHERE gid=%s', self._id)
       
   158             dbc.execute('DELETE FROM domain_data WHERE gid=%s', self._id)
       
   159             self._dbh.commit()
   171             self._dbh.commit()
   160             dbc.close()
   172             dbc.close()
   161         else:
   173         else:
   162             raise VMMDomainException((_("Domain doesn't exist yet."),
   174             raise VMMDomainException((_("Domain doesn't exist yet."),
   163                 ERR.NO_SUCH_DOMAIN))
   175                 ERR.NO_SUCH_DOMAIN))
   184             dbc.close()
   196             dbc.close()
   185         else:
   197         else:
   186             raise VMMDomainException((_("Domain doesn't exist yet."),
   198             raise VMMDomainException((_("Domain doesn't exist yet."),
   187                 ERR.NO_SUCH_DOMAIN))
   199                 ERR.NO_SUCH_DOMAIN))
   188 
   200 
       
   201     def saveAlias(self, aliasname):
       
   202         """Stores the alias name for the domain in the database.
       
   203 
       
   204         Keyword arguments:
       
   205         aliasname -- the alias name of the domain (str)
       
   206         """
       
   207         if self._id > 0 and not self._aliasExists(aliasname):
       
   208             dbc = self._dbh.cursor()
       
   209             dbc.execute('INSERT INTO domain_name VALUES (%s, %s, %s)',
       
   210                     aliasname, self._id, False)
       
   211             if dbc.rowcount == 1:
       
   212                 self._dbh.commit()
       
   213             dbc.close()
       
   214         else:
       
   215             raise VMMDomainException((_("Domain doesn't exist yet."),
       
   216                 ERR.NO_SUCH_DOMAIN))
       
   217 
   189     def getID(self):
   218     def getID(self):
   190         """Returns the ID of the domain."""
   219         """Returns the ID of the domain."""
   191         return self._id
   220         return self._id
   192 
   221 
   193     def getDir(self):
   222     def getDir(self):
   287                 domdict[gid].append(dom)
   316                 domdict[gid].append(dom)
   288             except KeyError:
   317             except KeyError:
   289                 domdict[gid] = [None, dom]
   318                 domdict[gid] = [None, dom]
   290     del doms
   319     del doms
   291     return order, domdict
   320     return order, domdict
       
   321 
       
   322 def deleteAlias(dbh, aliasname):
       
   323     dbc = dbh.cursor()
       
   324     dbc.execute('DELETE FROM domain_name WHERE domainname = %s', aliasname)
       
   325     if dbc.rowcount > 0:
       
   326         dbh.commit()
       
   327     dbc.close()
       
   328