VirtualMailManager/Domain.py
changeset 43 92a6132940f5
parent 35 22cc616aef61
child 44 c9ab6900ede9
equal deleted inserted replaced
42:9d10877e1c10 43:92a6132940f5
    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         dbc.execute("SELECT gid,tid,domaindir FROM domains WHERE domainname=%s",
    57         # XXX check for primary
    58                 self._name)
    58         dbc.execute("SELECT gid, tid, domaindir FROM domain_data WHERE gid =\
       
    59  (SELECT gid FROM domain_name WHERE domainname = %s)", self._name)
    59         result = dbc.fetchone()
    60         result = dbc.fetchone()
    60         dbc.close()
    61         dbc.close()
    61         if result is not None:
    62         if result is not None:
    62             self._id, self._domaindir = result[0], result[2]
    63             self._id, self._domaindir = result[0], result[2]
    63             self._transport = Transport(self._dbh, tid=result[1])
    64             self._transport = Transport(self._dbh, tid=result[1])
    66             return False
    67             return False
    67 
    68 
    68     def _setID(self):
    69     def _setID(self):
    69         """Sets the ID of the domain."""
    70         """Sets the ID of the domain."""
    70         dbc = self._dbh.cursor()
    71         dbc = self._dbh.cursor()
    71         dbc.execute("SELECT nextval('domains_gid')")
    72         dbc.execute("SELECT nextval('domain_gid')")
    72         self._id = dbc.fetchone()[0]
    73         self._id = dbc.fetchone()[0]
    73         dbc.close()
    74         dbc.close()
    74 
    75 
    75     def _prepare(self):
    76     def _prepare(self):
    76         self._setID()
    77         self._setID()
   128     def save(self):
   129     def save(self):
   129         """Stores the new domain in the database."""
   130         """Stores the new domain in the database."""
   130         if self._id < 1:
   131         if self._id < 1:
   131             self._prepare()
   132             self._prepare()
   132             dbc = self._dbh.cursor()
   133             dbc = self._dbh.cursor()
   133             dbc.execute("INSERT INTO domains (gid, domainname, tid, domaindir)\
   134             dbc.execute("INSERT INTO domain_data (gid, tid, domaindir)\
   134  VALUES (%s, %s, %s, %s)", self._id, self._name, self._transport.getID(),
   135  VALUES (%s, %s, %s)", self._id, self._transport.getID(), self._domaindir)
   135                 self._domaindir)
   136             dbc.execute("INSERT INTO domain_name (domainname, gid, is_primary)\
       
   137  VALUES (%s, %s, %s)", self._name, self._id, True)
   136             self._dbh.commit()
   138             self._dbh.commit()
   137             dbc.close()
   139             dbc.close()
   138         else:
   140         else:
   139             raise VMMDomainException((_('Domain already exists.'),
   141             raise VMMDomainException((_('Domain already exists.'),
   140                 ERR.DOMAIN_EXISTS))
   142                 ERR.DOMAIN_EXISTS))
   150             self._chkDelete(delUser, delAlias)
   152             self._chkDelete(delUser, delAlias)
   151             dbc = self._dbh.cursor()
   153             dbc = self._dbh.cursor()
   152             dbc.execute('DELETE FROM alias WHERE gid=%s', self._id)
   154             dbc.execute('DELETE FROM alias WHERE gid=%s', self._id)
   153             dbc.execute('DELETE FROM users WHERE gid=%s', self._id)
   155             dbc.execute('DELETE FROM users WHERE gid=%s', self._id)
   154             dbc.execute('DELETE FROM relocated WHERE gid=%s', self._id)
   156             dbc.execute('DELETE FROM relocated WHERE gid=%s', self._id)
   155             dbc.execute('DELETE FROM domains WHERE gid=%s', self._id)
   157             dbc.execute('DELETE FROM domain_data WHERE gid=%s', self._id)
       
   158             dbc.execute('DELETE FROM domain_name WHERE gid=%s', self._id)
   156             self._dbh.commit()
   159             self._dbh.commit()
   157             dbc.close()
   160             dbc.close()
   158         else:
   161         else:
   159             raise VMMDomainException((_("Domain doesn't exist yet."),
   162             raise VMMDomainException((_("Domain doesn't exist yet."),
   160                 ERR.NO_SUCH_DOMAIN))
   163                 ERR.NO_SUCH_DOMAIN))
   167         force -- True/False force new transport for all accounts (bool)
   170         force -- True/False force new transport for all accounts (bool)
   168         """
   171         """
   169         if self._id > 0:
   172         if self._id > 0:
   170             trsp = Transport(self._dbh, transport=transport)
   173             trsp = Transport(self._dbh, transport=transport)
   171             dbc = self._dbh.cursor()
   174             dbc = self._dbh.cursor()
   172             dbc.execute("UPDATE domains SET tid=%s WHERE gid=%s", trsp.getID(),
   175             dbc.execute("UPDATE domain_data SET tid = %s WHERE gid = %s",
   173                     self._id)
   176                     trsp.getID(), self._id)
   174             if dbc.rowcount > 0:
   177             if dbc.rowcount > 0:
   175                 self._dbh.commit()
   178                 self._dbh.commit()
   176             if force:
   179             if force:
   177                 dbc.execute("UPDATE users SET tid=%s WHERE gid=%s",
   180                 dbc.execute("UPDATE users SET tid = %s WHERE gid = %s",
   178                         trsp.getID(), self._id)
   181                         trsp.getID(), self._id)
   179                 if dbc.rowcount > 0:
   182                 if dbc.rowcount > 0:
   180                     self._dbh.commit()
   183                     self._dbh.commit()
   181             dbc.close()
   184             dbc.close()
   182         else:
   185         else:
   199         """Returns the ID from the domain's transport."""
   202         """Returns the ID from the domain's transport."""
   200         return self._transport.getID()
   203         return self._transport.getID()
   201 
   204 
   202     def getInfo(self):
   205     def getInfo(self):
   203         """Returns a dictionary with information about the domain."""
   206         """Returns a dictionary with information about the domain."""
       
   207         # XXX add alias domain count
   204         sql = """\
   208         sql = """\
   205 SELECT gid, domainname, transport, domaindir, accounts, aliases
   209 SELECT gid, domainname, transport, domaindir, accounts, aliases
   206   FROM vmm_domain_info
   210   FROM vmm_domain_info
   207  WHERE gid = %i""" % self._id
   211  WHERE gid = %i""" % self._id
   208         dbc = self._dbh.cursor()
   212         dbc = self._dbh.cursor()
   244             for alias in addresses:
   248             for alias in addresses:
   245                 aliases.append(alias[0])
   249                 aliases.append(alias[0])
   246         return aliases
   250         return aliases
   247 
   251 
   248 def search(dbh, pattern=None, like=False):
   252 def search(dbh, pattern=None, like=False):
   249     sql = 'SELECT domainname FROM domains'
   253     sql = 'SELECT domainname FROM domain_name'
   250     if pattern is None:
   254     if pattern is None:
   251         pass
   255         pass
   252     elif like:
   256     elif like:
   253         sql += " WHERE domainname LIKE '%s'" % pattern
   257         sql += " WHERE domainname LIKE '%s'" % pattern
   254     else:
   258     else:
   255         sql += " WHERE domainname = '%s'" % pattern
   259         sql += " WHERE domainname = '%s'" % pattern
   256     sql += ' ORDER BY domainname'
   260     sql += ' ORDER BY domainname'
       
   261     # XXX + is_primary // add prefix like [P] || [A]
   257     dbc = dbh.cursor()
   262     dbc = dbh.cursor()
   258     dbc.execute(sql)
   263     dbc.execute(sql)
   259     domains = dbc.fetchall()
   264     domains = dbc.fetchall()
   260     dbc.close()
   265     dbc.close()
   261     return domains
   266     return domains