VirtualMailManager/domain.py
branchv0.6.x
changeset 352 22d115376e4d
parent 336 d24c2ea39710
child 363 502d59f4bb34
equal deleted inserted replaced
351:4bba5fb90b78 352:22d115376e4d
    62         domain.
    62         domain.
    63         """
    63         """
    64         dbc = self._dbh.cursor()
    64         dbc = self._dbh.cursor()
    65         dbc.execute('SELECT dd.gid, tid, domaindir, is_primary FROM '
    65         dbc.execute('SELECT dd.gid, tid, domaindir, is_primary FROM '
    66                     'domain_data dd, domain_name dn WHERE domainname = %s AND '
    66                     'domain_data dd, domain_name dn WHERE domainname = %s AND '
    67                     'dn.gid = dd.gid', self._name)
    67                     'dn.gid = dd.gid', (self._name,))
    68         result = dbc.fetchone()
    68         result = dbc.fetchone()
    69         dbc.close()
    69         dbc.close()
    70         if result:
    70         if result:
    71             if not result[3]:
    71             if not result[3]:
    72                 raise DomErr(_(u"The domain '%s' is an alias domain.") %
    72                 raise DomErr(_(u"The domain '%s' is an alias domain.") %
   158         if not self._new:
   158         if not self._new:
   159             raise DomErr(_(u"The domain '%s' already exists.") % self._name,
   159             raise DomErr(_(u"The domain '%s' already exists.") % self._name,
   160                          DOMAIN_EXISTS)
   160                          DOMAIN_EXISTS)
   161         assert self._directory is not None and self._transport is not None
   161         assert self._directory is not None and self._transport is not None
   162         dbc = self._dbh.cursor()
   162         dbc = self._dbh.cursor()
   163         dbc.execute("INSERT INTO domain_data VALUES (%s, %s, %s)", self._gid,
   163         dbc.execute("INSERT INTO domain_data VALUES (%s, %s, %s)", (self._gid,
   164                     self._transport.tid, self._directory)
   164                     self._transport.tid, self._directory))
   165         dbc.execute("INSERT INTO domain_name VALUES (%s, %s, %s)", self._name,
   165         dbc.execute("INSERT INTO domain_name VALUES (%s, %s, %s)", (self._name,
   166                     self._gid, True)
   166                     self._gid, True))
   167         self._dbh.commit()
   167         self._dbh.commit()
   168         dbc.close()
   168         dbc.close()
   169         self._new = False
   169         self._new = False
   170 
   170 
   171     def delete(self, force=False):
   171     def delete(self, force=False):
   212         assert isinstance(transport, Transport)
   212         assert isinstance(transport, Transport)
   213         if transport == self._transport:
   213         if transport == self._transport:
   214             return
   214             return
   215         dbc = self._dbh.cursor()
   215         dbc = self._dbh.cursor()
   216         dbc.execute("UPDATE domain_data SET tid = %s WHERE gid = %s",
   216         dbc.execute("UPDATE domain_data SET tid = %s WHERE gid = %s",
   217                     transport.tid, self._gid)
   217                     (transport.tid, self._gid))
   218         if dbc.rowcount > 0:
   218         if dbc.rowcount > 0:
   219             self._dbh.commit()
   219             self._dbh.commit()
   220         if force:
   220         if force:
   221             dbc.execute("UPDATE users SET tid = %s WHERE gid = %s",
   221             dbc.execute("UPDATE users SET tid = %s WHERE gid = %s",
   222                         transport.tid, self._gid)
   222                         (transport.tid, self._gid))
   223             if dbc.rowcount > 0:
   223             if dbc.rowcount > 0:
   224                 self._dbh.commit()
   224                 self._dbh.commit()
   225         dbc.close()
   225         dbc.close()
   226         self._transport = transport
   226         self._transport = transport
   227 
   227 
   229         """Returns a dictionary with information about the domain."""
   229         """Returns a dictionary with information about the domain."""
   230         self._chk_state()
   230         self._chk_state()
   231         dbc = self._dbh.cursor()
   231         dbc = self._dbh.cursor()
   232         dbc.execute('SELECT gid, domainname, transport, domaindir, '
   232         dbc.execute('SELECT gid, domainname, transport, domaindir, '
   233                     'aliasdomains, accounts, aliases, relocated FROM '
   233                     'aliasdomains, accounts, aliases, relocated FROM '
   234                     'vmm_domain_info WHERE gid = %s', self._gid)
   234                     'vmm_domain_info WHERE gid = %s', (self._gid,))
   235         info = dbc.fetchone()
   235         info = dbc.fetchone()
   236         dbc.close()
   236         dbc.close()
   237         keys = ('gid', 'domainname', 'transport', 'domaindir', 'aliasdomains',
   237         keys = ('gid', 'domainname', 'transport', 'domaindir', 'aliasdomains',
   238                 'accounts', 'aliases', 'relocated')
   238                 'accounts', 'aliases', 'relocated')
   239         return dict(zip(keys, info))
   239         return dict(zip(keys, info))
   241     def get_accounts(self):
   241     def get_accounts(self):
   242         """Returns a list with all accounts of the domain."""
   242         """Returns a list with all accounts of the domain."""
   243         self._chk_state()
   243         self._chk_state()
   244         dbc = self._dbh.cursor()
   244         dbc = self._dbh.cursor()
   245         dbc.execute('SELECT local_part from users where gid = %s ORDER BY '
   245         dbc.execute('SELECT local_part from users where gid = %s ORDER BY '
   246                     'local_part', self._gid)
   246                     'local_part', (self._gid,))
   247         users = dbc.fetchall()
   247         users = dbc.fetchall()
   248         dbc.close()
   248         dbc.close()
   249         accounts = []
   249         accounts = []
   250         if users:
   250         if users:
   251             addr = u'@'.join
   251             addr = u'@'.join
   256     def get_aliases(self):
   256     def get_aliases(self):
   257         """Returns a list with all aliases e-mail addresses of the domain."""
   257         """Returns a list with all aliases e-mail addresses of the domain."""
   258         self._chk_state()
   258         self._chk_state()
   259         dbc = self._dbh.cursor()
   259         dbc = self._dbh.cursor()
   260         dbc.execute('SELECT DISTINCT address FROM alias WHERE gid = %s ORDER '
   260         dbc.execute('SELECT DISTINCT address FROM alias WHERE gid = %s ORDER '
   261                     'BY address', self._gid)
   261                     'BY address', (self._gid,))
   262         addresses = dbc.fetchall()
   262         addresses = dbc.fetchall()
   263         dbc.close()
   263         dbc.close()
   264         aliases = []
   264         aliases = []
   265         if addresses:
   265         if addresses:
   266             addr = u'@'.join
   266             addr = u'@'.join
   271     def get_relocated(self):
   271     def get_relocated(self):
   272         """Returns a list with all addresses of relocated users."""
   272         """Returns a list with all addresses of relocated users."""
   273         self._chk_state()
   273         self._chk_state()
   274         dbc = self._dbh.cursor()
   274         dbc = self._dbh.cursor()
   275         dbc.execute('SELECT address FROM relocated WHERE gid = %s ORDER BY '
   275         dbc.execute('SELECT address FROM relocated WHERE gid = %s ORDER BY '
   276                     'address', self._gid)
   276                     'address', (self._gid,))
   277         addresses = dbc.fetchall()
   277         addresses = dbc.fetchall()
   278         dbc.close()
   278         dbc.close()
   279         relocated = []
   279         relocated = []
   280         if addresses:
   280         if addresses:
   281             addr = u'@'.join
   281             addr = u'@'.join
   286     def get_aliase_names(self):
   286     def get_aliase_names(self):
   287         """Returns a list with all alias domain names of the domain."""
   287         """Returns a list with all alias domain names of the domain."""
   288         self._chk_state()
   288         self._chk_state()
   289         dbc = self._dbh.cursor()
   289         dbc = self._dbh.cursor()
   290         dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s AND '
   290         dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s AND '
   291                     'NOT is_primary ORDER BY domainname', self._gid)
   291                     'NOT is_primary ORDER BY domainname', (self._gid,))
   292         anames = dbc.fetchall()
   292         anames = dbc.fetchall()
   293         dbc.close()
   293         dbc.close()
   294         aliasdomains = []
   294         aliasdomains = []
   295         if anames:
   295         if anames:
   296             aliasdomains = [aname[0] for aname in anames]
   296             aliasdomains = [aname[0] for aname in anames]
   319 
   319 
   320     If the domain couldn't be found in the database 0 will be returned.
   320     If the domain couldn't be found in the database 0 will be returned.
   321     """
   321     """
   322     domainname = check_domainname(domainname)
   322     domainname = check_domainname(domainname)
   323     dbc = dbh.cursor()
   323     dbc = dbh.cursor()
   324     dbc.execute('SELECT gid FROM domain_name WHERE domainname=%s', domainname)
   324     dbc.execute('SELECT gid FROM domain_name WHERE domainname = %s',
       
   325                 (domainname,))
   325     gid = dbc.fetchone()
   326     gid = dbc.fetchone()
   326     dbc.close()
   327     dbc.close()
   327     if gid:
   328     if gid:
   328         return gid[0]
   329         return gid[0]
   329     return 0
   330     return 0