VirtualMailManager/Domain.py
branchv0.6.x
changeset 225 a51809f7940b
parent 222 d0c16e70a9fb
child 236 084331dd1e4c
equal deleted inserted replaced
224:7e9874a50d92 225:a51809f7940b
    13 from VirtualMailManager.errors import DomainError as DomErr
    13 from VirtualMailManager.errors import DomainError as DomErr
    14 from VirtualMailManager.Transport import Transport
    14 from VirtualMailManager.Transport import Transport
    15 
    15 
    16 
    16 
    17 MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
    17 MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
       
    18 _ = lambda x: x
    18 
    19 
    19 
    20 
    20 class Domain(object):
    21 class Domain(object):
    21     """Class to manage e-mail domains."""
    22     """Class to manage e-mail domains."""
    22     __slots__ = ('_basedir','_domaindir','_id','_name','_transport','_dbh')
    23     __slots__ = ('_basedir', '_domaindir', '_id', '_name', '_transport',
       
    24                  '_dbh')
       
    25 
    23     def __init__(self, dbh, domainname, basedir=None, transport=None):
    26     def __init__(self, dbh, domainname, basedir=None, transport=None):
    24         """Creates a new Domain instance.
    27         """Creates a new Domain instance.
    25 
    28 
    26         Keyword arguments:
    29         Keyword arguments:
    27         dbh -- a pyPgSQL.PgSQL.connection
    30         dbh -- a pyPgSQL.PgSQL.connection
   134         """Stores the new domain in the database."""
   137         """Stores the new domain in the database."""
   135         if self._id < 1:
   138         if self._id < 1:
   136             self._prepare()
   139             self._prepare()
   137             dbc = self._dbh.cursor()
   140             dbc = self._dbh.cursor()
   138             dbc.execute("INSERT INTO domain_data (gid, tid, domaindir)\
   141             dbc.execute("INSERT INTO domain_data (gid, tid, domaindir)\
   139  VALUES (%s, %s, %s)", self._id, self._transport.getID(), self._domaindir)
   142  VALUES (%s, %s, %s)", self._id, self._transport.id, self._domaindir)
   140             dbc.execute("INSERT INTO domain_name (domainname, gid, is_primary)\
   143             dbc.execute("INSERT INTO domain_name (domainname, gid, is_primary)\
   141  VALUES (%s, %s, %s)", self._name, self._id, True)
   144  VALUES (%s, %s, %s)", self._name, self._id, True)
   142             self._dbh.commit()
   145             self._dbh.commit()
   143             dbc.close()
   146             dbc.close()
   144         else:
   147         else:
   153         delAlias -- force deletion of available aliases (bool)
   156         delAlias -- force deletion of available aliases (bool)
   154         """
   157         """
   155         if self._id > 0:
   158         if self._id > 0:
   156             self._chkDelete(delUser, delAlias)
   159             self._chkDelete(delUser, delAlias)
   157             dbc = self._dbh.cursor()
   160             dbc = self._dbh.cursor()
   158             for t in ('alias','users','relocated','domain_name','domain_data'):
   161             for tbl in ('alias', 'users', 'relocated', 'domain_name',
   159                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
   162                         'domain_data'):
       
   163                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (tbl, self._id))
   160             self._dbh.commit()
   164             self._dbh.commit()
   161             dbc.close()
   165             dbc.close()
   162         else:
   166         else:
   163             raise DomErr(_(u"The domain “%s” doesn't exist.") % self._name,
   167             raise DomErr(_(u"The domain “%s” doesn't exist.") % self._name,
   164                          NO_SUCH_DOMAIN)
   168                          NO_SUCH_DOMAIN)
   169         Keyword arguments:
   173         Keyword arguments:
   170         transport -- the new transport (str)
   174         transport -- the new transport (str)
   171         force -- True/False force new transport for all accounts (bool)
   175         force -- True/False force new transport for all accounts (bool)
   172         """
   176         """
   173         if self._id > 0:
   177         if self._id > 0:
   174             if transport == self._transport.getTransport():
   178             if transport == self._transport.transport:
   175                 return
   179                 return
   176             trsp = Transport(self._dbh, transport=transport)
   180             trsp = Transport(self._dbh, transport=transport)
   177             dbc = self._dbh.cursor()
   181             dbc = self._dbh.cursor()
   178             dbc.execute("UPDATE domain_data SET tid = %s WHERE gid = %s",
   182             dbc.execute("UPDATE domain_data SET tid = %s WHERE gid = %s",
   179                     trsp.getID(), self._id)
   183                         trsp.id, self._id)
   180             if dbc.rowcount > 0:
   184             if dbc.rowcount > 0:
   181                 self._dbh.commit()
   185                 self._dbh.commit()
   182             if force:
   186             if force:
   183                 dbc.execute("UPDATE users SET tid = %s WHERE gid = %s",
   187                 dbc.execute("UPDATE users SET tid = %s WHERE gid = %s",
   184                         trsp.getID(), self._id)
   188                             trsp.id, self._id)
   185                 if dbc.rowcount > 0:
   189                 if dbc.rowcount > 0:
   186                     self._dbh.commit()
   190                     self._dbh.commit()
   187             dbc.close()
   191             dbc.close()
   188         else:
   192         else:
   189             raise DomErr(_(u"The domain “%s” doesn't exist.") % self._name,
   193             raise DomErr(_(u"The domain “%s” doesn't exist.") % self._name,
   197         """Returns the directory of the domain."""
   201         """Returns the directory of the domain."""
   198         return self._domaindir
   202         return self._domaindir
   199 
   203 
   200     def getTransport(self):
   204     def getTransport(self):
   201         """Returns domain's transport."""
   205         """Returns domain's transport."""
   202         return self._transport.getTransport()
   206         return self._transport.transport
   203 
   207 
   204     def getTransportID(self):
   208     def getTransportID(self):
   205         """Returns the ID from the domain's transport."""
   209         """Returns the ID from the domain's transport."""
   206         return self._transport.getID()
   210         return self._transport.id
   207 
   211 
   208     def getInfo(self):
   212     def getInfo(self):
   209         """Returns a dictionary with information about the domain."""
   213         """Returns a dictionary with information about the domain."""
   210         sql = """\
   214         sql = """\
   211 SELECT gid, domainname, transport, domaindir, aliasdomains, accounts,
   215 SELECT gid, domainname, transport, domaindir, aliasdomains, accounts,
   240 
   244 
   241     def getAliases(self):
   245     def getAliases(self):
   242         """Returns a list with all aliases from the domain."""
   246         """Returns a list with all aliases from the domain."""
   243         dbc = self._dbh.cursor()
   247         dbc = self._dbh.cursor()
   244         dbc.execute("SELECT DISTINCT address FROM alias WHERE gid = %s\
   248         dbc.execute("SELECT DISTINCT address FROM alias WHERE gid = %s\
   245  ORDER BY address",  self._id)
   249  ORDER BY address", self._id)
   246         addresses = dbc.fetchall()
   250         addresses = dbc.fetchall()
   247         dbc.close()
   251         dbc.close()
   248         aliases = []
   252         aliases = []
   249         if len(addresses) > 0:
   253         if len(addresses) > 0:
   250             addr = u'@'.join
   254             addr = u'@'.join
   310             except KeyError:
   314             except KeyError:
   311                 domdict[gid] = [None, dom]
   315                 domdict[gid] = [None, dom]
   312     del doms
   316     del doms
   313     return order, domdict
   317     return order, domdict
   314 
   318 
       
   319 
   315 def get_gid(dbh, domainname):
   320 def get_gid(dbh, domainname):
   316     """Returns the group id of the domain *domainname*.
   321     """Returns the group id of the domain *domainname*.
   317 
   322 
   318     If the domain couldn't be found in the database 0 will be returned.
   323     If the domain couldn't be found in the database 0 will be returned.
   319 
   324 
   324     gid = dbc.fetchone()
   329     gid = dbc.fetchone()
   325     dbc.close()
   330     dbc.close()
   326     if gid:
   331     if gid:
   327         return gid[0]
   332         return gid[0]
   328     return 0
   333     return 0
       
   334 
       
   335 
       
   336 del _