VirtualMailManager/Domain.py
changeset 9 e3d3dbeb5b84
parent 8 7e3ce56f49e6
child 18 c98e08791ee8
equal deleted inserted replaced
8:7e3ce56f49e6 9:e3d3dbeb5b84
    15 
    15 
    16 from random import choice
    16 from random import choice
    17 
    17 
    18 from Exceptions import VMMDomainException
    18 from Exceptions import VMMDomainException
    19 import constants.ERROR as ERR
    19 import constants.ERROR as ERR
       
    20 from Transport import Transport
    20 
    21 
    21 MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
    22 MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
    22 
    23 
    23 class Domain:
    24 class Domain:
    24     """Class to manage e-mail domains."""
    25     """Class to manage e-mail domains."""
    25     def __init__(self, dbh, domainname, basedir, transport=None):
    26     def __init__(self, dbh, domainname, basedir=None, transport=None):
    26         """Creates a new Domain instance.
    27         """Creates a new Domain instance.
    27         
    28         
    28         Keyword arguments:
    29         Keyword arguments:
    29         dbh -- a pyPgSQL.PgSQL.connection
    30         dbh -- a pyPgSQL.PgSQL.connection
    30         domainname -- name of the domain (str)
    31         domainname -- name of the domain (str)
    31         transport -- see transport(5), default 'dovecot:'  (str)
    32         transport -- default vmm.cfg/misc/transport  (str)
    32         """
    33         """
    33         self._dbh = dbh
    34         self._dbh = dbh
    34         self._name = domainname
    35         self._name = domainname
    35         self._basedir = basedir
    36         self._basedir = basedir
    36         if transport is None:
    37         if transport is not None:
    37             self._transport = 'dovecot:'
    38             self._transport = Transport(self._dbh, transport=transport)
    38         else:
    39         else:
    39             self._transport = transport
    40             self._transport = transport
    40         self._id = 0
    41         self._id = 0
    41         self._domaindir = None
    42         self._domaindir = None
    42         self._exists()
    43         self._exists()
    46 
    47 
    47         If the domain exists _id will be set and returns True, otherwise False
    48         If the domain exists _id will be set and returns True, otherwise False
    48         will be returned.
    49         will be returned.
    49         """
    50         """
    50         dbc = self._dbh.cursor()
    51         dbc = self._dbh.cursor()
    51         dbc.execute("SELECT gid, domaindir FROM domains WHERE domainname=%s",
    52         dbc.execute("SELECT gid,tid,domaindir FROM domains WHERE domainname=%s",
    52                 self._name)
    53                 self._name)
    53         result = dbc.fetchone()
    54         result = dbc.fetchone()
    54         dbc.close()
    55         dbc.close()
    55         if result is not None:
    56         if result is not None:
    56             self._id, self._domaindir = result[0], result[1]
    57             self._id, self._domaindir = result[0], result[2]
       
    58             self._transport = Transport(self._dbh, tid=result[1])
    57             return True
    59             return True
    58         else:
    60         else:
    59             return False
    61             return False
    60 
    62 
    61     def _setID(self):
    63     def _setID(self):
   120     def save(self):
   122     def save(self):
   121         """Stores the new domain in the database."""
   123         """Stores the new domain in the database."""
   122         if self._id < 1:
   124         if self._id < 1:
   123             self._prepare()
   125             self._prepare()
   124             dbc = self._dbh.cursor()
   126             dbc = self._dbh.cursor()
   125             dbc.execute("INSERT INTO domains (gid, domainname, transport,\
   127             dbc.execute("INSERT INTO domains (gid, domainname, tid, domaindir)\
   126  domaindir) VALUES (%s, %s, %s, %s)", self._id, self._name, self._transport,
   128  VALUES (%s, %s, %s, %s)", self._id, self._name, self._transport.getID(),
   127                 self._domaindir)
   129                 self._domaindir)
   128             self._dbh.commit()
   130             self._dbh.commit()
   129             dbc.close()
   131             dbc.close()
   130         else:
   132         else:
   131             raise VMMDomainException(('Domain already exists.',
   133             raise VMMDomainException(('Domain already exists.',
   156 
   158 
   157         Keyword arguments:
   159         Keyword arguments:
   158         transport -- the new transport (str)
   160         transport -- the new transport (str)
   159         """
   161         """
   160         if self._id > 0:
   162         if self._id > 0:
       
   163             trsp = Transport(self._dbh, transport=transport)
   161             dbc = self._dbh.cursor()
   164             dbc = self._dbh.cursor()
   162             dbc.execute("UPDATE domains SET transport=%s WHERE gid=%s",
   165             dbc.execute("UPDATE domains SET tid=%s WHERE gid=%s", trsp.getID(),
   163                     transport, self._id)
   166                     self._id)
   164             if dbc.rowcount > 0:
   167             if dbc.rowcount > 0:
   165                 self._dbh.commit()
   168                 self._dbh.commit()
   166             dbc.close()
   169             dbc.close()
   167         else:
   170         else:
   168             raise VMMDomainException(("Domain doesn't exist yet.",
   171             raise VMMDomainException(("Domain doesn't exist yet.",
   174 
   177 
   175     def getDir(self):
   178     def getDir(self):
   176         """Returns the directory of the domain."""
   179         """Returns the directory of the domain."""
   177         return self._domaindir
   180         return self._domaindir
   178 
   181 
       
   182     def getTransport(self):
       
   183         """Returns domain's transport."""
       
   184         return self._transport.getTransport()
       
   185 
       
   186     def getTransportID(self):
       
   187         """Returns the ID from the domain's transport."""
       
   188         return self._transport.getID()
       
   189 
   179     def getInfo(self):
   190     def getInfo(self):
   180         """Returns a dictionary with information about the domain."""
   191         """Returns a dictionary with information about the domain."""
   181         sql = """\
   192         sql = """\
   182 SELECT gid, domainname, transport, domaindir, count(uid) AS accounts, aliases
   193 SELECT gid, domainname, transport, domaindir, accounts, aliases
   183   FROM domains
   194   FROM vmm_domain_info
   184        LEFT JOIN users USING (gid)
   195  WHERE gid = %i""" % self._id
   185        LEFT JOIN vmm_alias_count USING (gid)
       
   186  WHERE gid = %i
       
   187 GROUP BY gid, domainname, transport, domaindir, aliases""" % self._id
       
   188         dbc = self._dbh.cursor()
   196         dbc = self._dbh.cursor()
   189         dbc.execute(sql)
   197         dbc.execute(sql)
   190         info = dbc.fetchone()
   198         info = dbc.fetchone()
   191         dbc.close()
   199         dbc.close()
   192         if info is None:
   200         if info is None: