VirtualMailManager/Domain.py
changeset 50 927b0705d31a
parent 48 0d5f58f8b8f5
child 53 5b50eb306d37
equal deleted inserted replaced
49:9bd033177377 50:927b0705d31a
    13 __revision__ = 'rev '+'$Rev$'.split()[1]
    13 __revision__ = 'rev '+'$Rev$'.split()[1]
    14 __date__ = '$Date$'.split()[1]
    14 __date__ = '$Date$'.split()[1]
    15 
    15 
    16 from random import choice
    16 from random import choice
    17 
    17 
    18 from Exceptions import VMMDomainException
    18 from Exceptions import VMMDomainException as VMMDE
    19 import constants.ERROR as ERR
    19 import constants.ERROR as ERR
       
    20 import VirtualMailManager as VMM
    20 from Transport import Transport
    21 from Transport import Transport
    21 
    22 
    22 MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
    23 MAILDIR_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'
    23 
    24 
    24 class Domain:
    25 class Domain:
    30         dbh -- a pyPgSQL.PgSQL.connection
    31         dbh -- a pyPgSQL.PgSQL.connection
    31         domainname -- name of the domain (str)
    32         domainname -- name of the domain (str)
    32         transport -- default vmm.cfg/misc/transport  (str)
    33         transport -- default vmm.cfg/misc/transport  (str)
    33         """
    34         """
    34         self._dbh = dbh
    35         self._dbh = dbh
    35         self._name = domainname
    36         self._name = VMM.VirtualMailManager.chkDomainname(domainname)
    36         self._basedir = basedir
    37         self._basedir = basedir
    37         if transport is not None:
    38         if transport is not None:
    38             self._transport = Transport(self._dbh, transport=transport)
    39             self._transport = Transport(self._dbh, transport=transport)
    39         else:
    40         else:
    40             self._transport = transport
    41             self._transport = transport
    60             return True
    61             return True
    61         else:
    62         else:
    62             return False
    63             return False
    63 
    64 
    64     def _aliasExists(self, aliasname):
    65     def _aliasExists(self, aliasname):
       
    66         aliasname = VMM.VirtualMailManager.chkDomainname(aliasname)
    65         dbc = self._dbh.cursor()
    67         dbc = self._dbh.cursor()
    66         dbc.execute("SELECT gid, is_primary FROM domain_name\
    68         dbc.execute("SELECT gid, is_primary FROM domain_name\
    67  WHERE domainname = %s", aliasname)
    69  WHERE domainname = %s", aliasname)
    68         result = dbc.fetchone()
    70         result = dbc.fetchone()
    69         dbc.close()
    71         dbc.close()
    70         if result is None:
    72         if result is None:
    71             return False
    73             return False
    72         elif result[1]:
    74         elif result[1]:
    73             raise VMMDomainException(_('Domain already exists.'),
    75             raise VMMDE(_(u'The domain »%s« already exists.') % self._name,
    74                 ERR.DOMAIN_EXISTS)
    76                 ERR.DOMAIN_EXISTS)
    75         else:
    77         else:
    76             raise VMMDomainException(_('Domain alias already exists.'),
    78             raise VMMDE(_(u'The domain alias »%s« already exists.') % aliasname,
    77                 ERR.DOMAIN_ALIAS_EXISTS)
    79                 ERR.DOMAIN_ALIAS_EXISTS)
    78 
    80 
    79     def _setID(self):
    81     def _setID(self):
    80         """Sets the ID of the domain."""
    82         """Sets the ID of the domain."""
    81         dbc = self._dbh.cursor()
    83         dbc = self._dbh.cursor()
   125         if not delAlias:
   127         if not delAlias:
   126             hasAlias = self._has('alias')
   128             hasAlias = self._has('alias')
   127         else:
   129         else:
   128             hasAlias = False
   130             hasAlias = False
   129         if hasUser and hasAlias:
   131         if hasUser and hasAlias:
   130             raise VMMDomainException(_('There are accounts and aliases.'),
   132             raise VMMDE(_(u'There are accounts and aliases.'),
   131                 ERR.ACCOUNT_AND_ALIAS_PRESENT)
   133                 ERR.ACCOUNT_AND_ALIAS_PRESENT)
   132         elif hasUser:
   134         elif hasUser:
   133             raise VMMDomainException(_('There are accounts.'),
   135             raise VMMDE(_(u'There are accounts.'),
   134                 ERR.ACCOUNT_PRESENT)
   136                 ERR.ACCOUNT_PRESENT)
   135         elif hasAlias:
   137         elif hasAlias:
   136             raise VMMDomainException(_('There are aliases.'),
   138             raise VMMDE(_(u'There are aliases.'),
   137                 ERR.ALIAS_PRESENT)
   139                 ERR.ALIAS_PRESENT)
   138 
   140 
   139     def save(self):
   141     def save(self):
   140         """Stores the new domain in the database."""
   142         """Stores the new domain in the database."""
   141         if self._id < 1:
   143         if self._id < 1:
   146             dbc.execute("INSERT INTO domain_name (domainname, gid, is_primary)\
   148             dbc.execute("INSERT INTO domain_name (domainname, gid, is_primary)\
   147  VALUES (%s, %s, %s)", self._name, self._id, True)
   149  VALUES (%s, %s, %s)", self._name, self._id, True)
   148             self._dbh.commit()
   150             self._dbh.commit()
   149             dbc.close()
   151             dbc.close()
   150         else:
   152         else:
   151             raise VMMDomainException(_('Domain already exists.'),
   153             raise VMMDE(_(u'The domain »%s« already exists.') % self._name,
   152                 ERR.DOMAIN_EXISTS)
   154                 ERR.DOMAIN_EXISTS)
   153 
   155 
   154     def delete(self, delUser=False, delAlias=False):
   156     def delete(self, delUser=False, delAlias=False):
   155         """Deletes the domain.
   157         """Deletes the domain.
   156 
   158 
   164             for t in ('alias','users','relocated','domain_name','domain_data'):
   166             for t in ('alias','users','relocated','domain_name','domain_data'):
   165                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
   167                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
   166             self._dbh.commit()
   168             self._dbh.commit()
   167             dbc.close()
   169             dbc.close()
   168         else:
   170         else:
   169             raise VMMDomainException(_("Domain doesn't exist yet."),
   171             raise VMMDE(_(u"The domain »%s« doesn't exist yet.") % self._name,
   170                 ERR.NO_SUCH_DOMAIN)
   172                 ERR.NO_SUCH_DOMAIN)
   171 
   173 
   172     def updateTransport(self, transport, force = False):
   174     def updateTransport(self, transport, force=False):
   173         """Sets a new transport for the domain.
   175         """Sets a new transport for the domain.
   174 
   176 
   175         Keyword arguments:
   177         Keyword arguments:
   176         transport -- the new transport (str)
   178         transport -- the new transport (str)
   177         force -- True/False force new transport for all accounts (bool)
   179         force -- True/False force new transport for all accounts (bool)
   188                         trsp.getID(), self._id)
   190                         trsp.getID(), self._id)
   189                 if dbc.rowcount > 0:
   191                 if dbc.rowcount > 0:
   190                     self._dbh.commit()
   192                     self._dbh.commit()
   191             dbc.close()
   193             dbc.close()
   192         else:
   194         else:
   193             raise VMMDomainException(_("Domain doesn't exist yet."),
   195             raise VMMDE(_(u"The domain »%s« doesn't exist yet.") % self._name,
   194                 ERR.NO_SUCH_DOMAIN)
   196                 ERR.NO_SUCH_DOMAIN)
   195 
   197 
   196     def saveAlias(self, aliasname):
   198     def saveAlias(self, aliasname):
   197         """Stores the alias name for the domain in the database.
   199         """Stores the alias name for the domain in the database.
   198 
   200 
   199         Keyword arguments:
   201         Keyword arguments:
   200         aliasname -- the alias name of the domain (str)
   202         aliasname -- the alias name of the domain (str)
   201         """
   203         """
       
   204         aliasname = VMM.VirtualMailManager.chkDomainname(aliasname)
   202         if self._id > 0 and not self._aliasExists(aliasname):
   205         if self._id > 0 and not self._aliasExists(aliasname):
   203             dbc = self._dbh.cursor()
   206             dbc = self._dbh.cursor()
   204             dbc.execute('INSERT INTO domain_name VALUES (%s, %s, %s)',
   207             dbc.execute('INSERT INTO domain_name VALUES (%s, %s, %s)',
   205                     aliasname, self._id, False)
   208                     aliasname, self._id, False)
   206             if dbc.rowcount == 1:
   209             if dbc.rowcount == 1:
   207                 self._dbh.commit()
   210                 self._dbh.commit()
   208             dbc.close()
   211             dbc.close()
   209         else:
   212         else:
   210             raise VMMDomainException(_("Domain doesn't exist yet."),
   213             raise VMMDE(_(u"The domain »%s« doesn't exist yet.") % self._name,
   211                 ERR.NO_SUCH_DOMAIN)
   214                 ERR.NO_SUCH_DOMAIN)
   212 
   215 
   213     def getID(self):
   216     def getID(self):
   214         """Returns the ID of the domain."""
   217         """Returns the ID of the domain."""
   215         return self._id
   218         return self._id
   235         dbc = self._dbh.cursor()
   238         dbc = self._dbh.cursor()
   236         dbc.execute(sql)
   239         dbc.execute(sql)
   237         info = dbc.fetchone()
   240         info = dbc.fetchone()
   238         dbc.close()
   241         dbc.close()
   239         if info is None:
   242         if info is None:
   240             raise VMMDomainException(_("Domain doesn't exist yet."),
   243             raise VMMDE(_(u"The domain »%s« doesn't exist yet.") % self._name,
   241                 ERR.NO_SUCH_DOMAIN)
   244                     ERR.NO_SUCH_DOMAIN)
   242         else:
   245         else:
   243             keys = ['gid', 'domainname', 'transport', 'domaindir',
   246             keys = ['gid', 'domainname', 'transport', 'domaindir',
   244                     'aliasdomains', 'accounts', 'aliases']
   247                     'aliasdomains', 'accounts', 'aliases']
   245             return dict(zip(keys, info))
   248             return dict(zip(keys, info))
   246 
   249 
   282             for aname in anames:
   285             for aname in anames:
   283                 aliasdomains.append(aname[0])
   286                 aliasdomains.append(aname[0])
   284         return aliasdomains
   287         return aliasdomains
   285 
   288 
   286 def search(dbh, pattern=None, like=False):
   289 def search(dbh, pattern=None, like=False):
       
   290     if pattern is not None and like is False: 
       
   291         pattern = VMM.VirtualMailManager.chkDomainname(pattern)
   287     sql = 'SELECT gid, domainname, is_primary FROM domain_name'
   292     sql = 'SELECT gid, domainname, is_primary FROM domain_name'
   288     if pattern is None:
   293     if pattern is None:
   289         pass
   294         pass
   290     elif like:
   295     elif like:
   291         sql += " WHERE domainname LIKE '%s'" % pattern
   296         sql += " WHERE domainname LIKE '%s'" % pattern
   313                 domdict[gid] = [None, dom]
   318                 domdict[gid] = [None, dom]
   314     del doms
   319     del doms
   315     return order, domdict
   320     return order, domdict
   316 
   321 
   317 def deleteAlias(dbh, aliasname):
   322 def deleteAlias(dbh, aliasname):
       
   323     aliasname = VMM.VirtualMailManager.chkDomainname(aliasname)
   318     dbc = dbh.cursor()
   324     dbc = dbh.cursor()
   319     dbc.execute('DELETE FROM domain_name WHERE domainname = %s', aliasname)
   325     dbc.execute('DELETE FROM domain_name WHERE domainname = %s', aliasname)
   320     if dbc.rowcount > 0:
   326     if dbc.rowcount > 0:
   321         dbh.commit()
   327         dbh.commit()
   322     dbc.close()
   328     dbc.close()