VirtualMailManager/Domain.py
branchv0.6.x
changeset 185 6e1ef32fbd82
parent 162 0ac9ef587769
child 198 02d467e4fbab
equal deleted inserted replaced
184:d0425225ce52 185:6e1ef32fbd82
     4 
     4 
     5 """Virtual Mail Manager's Domain class to manage e-mail domains."""
     5 """Virtual Mail Manager's Domain class to manage e-mail domains."""
     6 
     6 
     7 from random import choice
     7 from random import choice
     8 
     8 
     9 from __main__ import ERR
     9 from VirtualMailManager import chk_domainname
    10 from Exceptions import VMMDomainException as VMMDE
    10 from VirtualMailManager.constants.ERROR import \
    11 import VirtualMailManager as VMM
    11      ACCOUNT_AND_ALIAS_PRESENT, ACCOUNT_PRESENT, ALIAS_PRESENT, \
    12 from Transport import Transport
    12      DOMAIN_ALIAS_EXISTS, DOMAIN_EXISTS, NO_SUCH_DOMAIN
       
    13 from VirtualMailManager.Exceptions import VMMDomainException as VMMDE
       
    14 from VirtualMailManager.Transport import Transport
       
    15 
    13 
    16 
    14 MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
    17 MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
       
    18 
    15 
    19 
    16 class Domain(object):
    20 class Domain(object):
    17     """Class to manage e-mail domains."""
    21     """Class to manage e-mail domains."""
    18     __slots__ = ('_basedir','_domaindir','_id','_name','_transport','_dbh')
    22     __slots__ = ('_basedir','_domaindir','_id','_name','_transport','_dbh')
    19     def __init__(self, dbh, domainname, basedir=None, transport=None):
    23     def __init__(self, dbh, domainname, basedir=None, transport=None):
    23         dbh -- a pyPgSQL.PgSQL.connection
    27         dbh -- a pyPgSQL.PgSQL.connection
    24         domainname -- name of the domain (str)
    28         domainname -- name of the domain (str)
    25         transport -- default vmm.cfg/misc/transport  (str)
    29         transport -- default vmm.cfg/misc/transport  (str)
    26         """
    30         """
    27         self._dbh = dbh
    31         self._dbh = dbh
    28         self._name = VMM.VirtualMailManager.chkDomainname(domainname)
    32         self._name = chk_domainname(domainname)
    29         self._basedir = basedir
    33         self._basedir = basedir
    30         if transport is not None:
    34         if transport is not None:
    31             self._transport = Transport(self._dbh, transport=transport)
    35             self._transport = Transport(self._dbh, transport=transport)
    32         else:
    36         else:
    33             self._transport = transport
    37             self._transport = transport
    34         self._id = 0
    38         self._id = 0
    35         self._domaindir = None
    39         self._domaindir = None
    36         if not self._exists() and self._isAlias():
    40         if not self._exists() and self._isAlias():
    37             raise VMMDE(_(u"The domain “%s” is an alias domain.") %self._name,
    41             raise VMMDE(_(u"The domain “%s” is an alias domain.") % self._name,
    38                     ERR.DOMAIN_ALIAS_EXISTS)
    42                         DOMAIN_ALIAS_EXISTS)
    39 
    43 
    40     def _exists(self):
    44     def _exists(self):
    41         """Checks if the domain already exists.
    45         """Checks if the domain already exists.
    42 
    46 
    43         If the domain exists _id will be set and returns True, otherwise False
    47         If the domain exists _id will be set and returns True, otherwise False
   118             hasAlias = self._has('alias')
   122             hasAlias = self._has('alias')
   119         else:
   123         else:
   120             hasAlias = False
   124             hasAlias = False
   121         if hasUser and hasAlias:
   125         if hasUser and hasAlias:
   122             raise VMMDE(_(u'There are accounts and aliases.'),
   126             raise VMMDE(_(u'There are accounts and aliases.'),
   123                 ERR.ACCOUNT_AND_ALIAS_PRESENT)
   127                 ACCOUNT_AND_ALIAS_PRESENT)
   124         elif hasUser:
   128         elif hasUser:
   125             raise VMMDE(_(u'There are accounts.'),
   129             raise VMMDE(_(u'There are accounts.'), ACCOUNT_PRESENT)
   126                 ERR.ACCOUNT_PRESENT)
       
   127         elif hasAlias:
   130         elif hasAlias:
   128             raise VMMDE(_(u'There are aliases.'),
   131             raise VMMDE(_(u'There are aliases.'), ALIAS_PRESENT)
   129                 ERR.ALIAS_PRESENT)
       
   130 
   132 
   131     def save(self):
   133     def save(self):
   132         """Stores the new domain in the database."""
   134         """Stores the new domain in the database."""
   133         if self._id < 1:
   135         if self._id < 1:
   134             self._prepare()
   136             self._prepare()
   139  VALUES (%s, %s, %s)", self._name, self._id, True)
   141  VALUES (%s, %s, %s)", self._name, self._id, True)
   140             self._dbh.commit()
   142             self._dbh.commit()
   141             dbc.close()
   143             dbc.close()
   142         else:
   144         else:
   143             raise VMMDE(_(u'The domain “%s” already exists.') % self._name,
   145             raise VMMDE(_(u'The domain “%s” already exists.') % self._name,
   144                 ERR.DOMAIN_EXISTS)
   146                         DOMAIN_EXISTS)
   145 
   147 
   146     def delete(self, delUser=False, delAlias=False):
   148     def delete(self, delUser=False, delAlias=False):
   147         """Deletes the domain.
   149         """Deletes the domain.
   148 
   150 
   149         Keyword arguments:
   151         Keyword arguments:
   157                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
   159                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
   158             self._dbh.commit()
   160             self._dbh.commit()
   159             dbc.close()
   161             dbc.close()
   160         else:
   162         else:
   161             raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
   163             raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
   162                 ERR.NO_SUCH_DOMAIN)
   164                         NO_SUCH_DOMAIN)
   163 
   165 
   164     def updateTransport(self, transport, force=False):
   166     def updateTransport(self, transport, force=False):
   165         """Sets a new transport for the domain.
   167         """Sets a new transport for the domain.
   166 
   168 
   167         Keyword arguments:
   169         Keyword arguments:
   183                 if dbc.rowcount > 0:
   185                 if dbc.rowcount > 0:
   184                     self._dbh.commit()
   186                     self._dbh.commit()
   185             dbc.close()
   187             dbc.close()
   186         else:
   188         else:
   187             raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
   189             raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
   188                 ERR.NO_SUCH_DOMAIN)
   190                         NO_SUCH_DOMAIN)
   189 
   191 
   190     def getID(self):
   192     def getID(self):
   191         """Returns the ID of the domain."""
   193         """Returns the ID of the domain."""
   192         return self._id
   194         return self._id
   193 
   195 
   214         dbc.execute(sql)
   216         dbc.execute(sql)
   215         info = dbc.fetchone()
   217         info = dbc.fetchone()
   216         dbc.close()
   218         dbc.close()
   217         if info is None:
   219         if info is None:
   218             raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
   220             raise VMMDE(_(u"The domain “%s” doesn't exist.") % self._name,
   219                     ERR.NO_SUCH_DOMAIN)
   221                         NO_SUCH_DOMAIN)
   220         else:
   222         else:
   221             keys = ['gid', 'domainname', 'transport', 'domaindir',
   223             keys = ['gid', 'domainname', 'transport', 'domaindir',
   222                     'aliasdomains', 'accounts', 'aliases', 'relocated']
   224                     'aliasdomains', 'accounts', 'aliases', 'relocated']
   223             return dict(zip(keys, info))
   225             return dict(zip(keys, info))
   224 
   226 
   276             aliasdomains = [aname[0] for aname in anames]
   278             aliasdomains = [aname[0] for aname in anames]
   277         return aliasdomains
   279         return aliasdomains
   278 
   280 
   279 def search(dbh, pattern=None, like=False):
   281 def search(dbh, pattern=None, like=False):
   280     if pattern is not None and like is False:
   282     if pattern is not None and like is False:
   281         pattern = VMM.VirtualMailManager.chkDomainname(pattern)
   283         pattern = chk_domainname(pattern)
   282     sql = 'SELECT gid, domainname, is_primary FROM domain_name'
   284     sql = 'SELECT gid, domainname, is_primary FROM domain_name'
   283     if pattern is None:
   285     if pattern is None:
   284         pass
   286         pass
   285     elif like:
   287     elif like:
   286         sql += " WHERE domainname LIKE '%s'" % pattern
   288         sql += " WHERE domainname LIKE '%s'" % pattern