VirtualMailManager/Domain.py
changeset 44 c9ab6900ede9
parent 43 92a6132940f5
child 45 9e66138aad0b
equal deleted inserted replaced
43:92a6132940f5 44:c9ab6900ede9
   152             self._chkDelete(delUser, delAlias)
   152             self._chkDelete(delUser, delAlias)
   153             dbc = self._dbh.cursor()
   153             dbc = self._dbh.cursor()
   154             dbc.execute('DELETE FROM alias WHERE gid=%s', self._id)
   154             dbc.execute('DELETE FROM alias WHERE gid=%s', self._id)
   155             dbc.execute('DELETE FROM users WHERE gid=%s', self._id)
   155             dbc.execute('DELETE FROM users WHERE gid=%s', self._id)
   156             dbc.execute('DELETE FROM relocated WHERE gid=%s', self._id)
   156             dbc.execute('DELETE FROM relocated WHERE gid=%s', self._id)
       
   157             dbc.execute('DELETE FROM domain_name WHERE gid=%s', self._id)
   157             dbc.execute('DELETE FROM domain_data WHERE gid=%s', self._id)
   158             dbc.execute('DELETE FROM domain_data WHERE gid=%s', self._id)
   158             dbc.execute('DELETE FROM domain_name WHERE gid=%s', self._id)
       
   159             self._dbh.commit()
   159             self._dbh.commit()
   160             dbc.close()
   160             dbc.close()
   161         else:
   161         else:
   162             raise VMMDomainException((_("Domain doesn't exist yet."),
   162             raise VMMDomainException((_("Domain doesn't exist yet."),
   163                 ERR.NO_SUCH_DOMAIN))
   163                 ERR.NO_SUCH_DOMAIN))
   202         """Returns the ID from the domain's transport."""
   202         """Returns the ID from the domain's transport."""
   203         return self._transport.getID()
   203         return self._transport.getID()
   204 
   204 
   205     def getInfo(self):
   205     def getInfo(self):
   206         """Returns a dictionary with information about the domain."""
   206         """Returns a dictionary with information about the domain."""
   207         # XXX add alias domain count
       
   208         sql = """\
   207         sql = """\
   209 SELECT gid, domainname, transport, domaindir, accounts, aliases
   208 SELECT gid, domainname, transport, domaindir, aliasdomains, accounts, aliases
   210   FROM vmm_domain_info
   209   FROM vmm_domain_info
   211  WHERE gid = %i""" % self._id
   210  WHERE gid = %i""" % self._id
   212         dbc = self._dbh.cursor()
   211         dbc = self._dbh.cursor()
   213         dbc.execute(sql)
   212         dbc.execute(sql)
   214         info = dbc.fetchone()
   213         info = dbc.fetchone()
   215         dbc.close()
   214         dbc.close()
   216         if info is None:
   215         if info is None:
   217             raise VMMDomainException((_("Domain doesn't exist yet."),
   216             raise VMMDomainException((_("Domain doesn't exist yet."),
   218                 ERR.NO_SUCH_DOMAIN))
   217                 ERR.NO_SUCH_DOMAIN))
   219         else:
   218         else:
   220             keys = ['gid', 'domainname', 'transport', 'domaindir', 'accounts',
   219             keys = ['gid', 'domainname', 'transport', 'domaindir',
   221                     'aliases']
   220                     'aliasdomains', 'accounts', 'aliases']
   222             return dict(zip(keys, info))
   221             return dict(zip(keys, info))
   223 
   222 
   224     def getAccounts(self):
   223     def getAccounts(self):
   225         """Returns a list with all accounts from the domain."""
   224         """Returns a list with all accounts from the domain."""
   226         dbc = self._dbh.cursor()
   225         dbc = self._dbh.cursor()
   227         dbc.execute("SELECT userid AS users FROM dovecot_user WHERE gid = %s\
   226         dbc.execute("SELECT local_part from users where gid = %s ORDER BY\
   228  ORDER BY users",
   227  local_part", self._id)
   229                 self._id)
       
   230         users = dbc.fetchall()
   228         users = dbc.fetchall()
   231         dbc.close()
   229         dbc.close()
   232         accounts = []
   230         accounts = []
   233         if len(users) > 0:
   231         if len(users) > 0:
   234             for account in users:
   232             for account in users:
   235                 accounts.append(account[0])
   233                 accounts.append('%s@%s' % (account[0], self._name))
   236         return accounts
   234         return accounts
   237 
   235 
   238     def getAliases(self):
   236     def getAliases(self):
   239         """Returns a list with all aliases from the domain."""
   237         """Returns a list with all aliases from the domain."""
   240         dbc = self._dbh.cursor()
   238         dbc = self._dbh.cursor()
   241         dbc.execute("SELECT DISTINCT address FROM postfix_alias WHERE gid=%s\
   239         dbc.execute("SELECT address from alias where gid = %s ORDER BY address",
   242  ORDER BY address",
       
   243                 self._id)
   240                 self._id)
   244         addresses = dbc.fetchall()
   241         addresses = dbc.fetchall()
   245         dbc.close()
   242         dbc.close()
   246         aliases = []
   243         aliases = []
   247         if len(addresses) > 0:
   244         if len(addresses) > 0:
   248             for alias in addresses:
   245             for alias in addresses:
   249                 aliases.append(alias[0])
   246                 aliases.append('%s@%s' % (alias[0], self._name))
   250         return aliases
   247         return aliases
   251 
   248 
       
   249     def getAliaseNames(self):
       
   250         """Returns a list with all alias names from the domain."""
       
   251         dbc = self._dbh.cursor()
       
   252         dbc.execute("SELECT domainname FROM domain_name WHERE gid = %s\
       
   253  AND NOT is_primary", self._id)
       
   254         anames = dbc.fetchall()
       
   255         dbc.close()
       
   256         aliasdomains = []
       
   257         if len(anames) > 0:
       
   258             for aname in anames:
       
   259                 aliasdomains.append(aname[0])
       
   260         return aliasdomains
       
   261 
   252 def search(dbh, pattern=None, like=False):
   262 def search(dbh, pattern=None, like=False):
   253     sql = 'SELECT domainname FROM domain_name'
   263     sql = 'SELECT gid, domainname, is_primary FROM domain_name'
   254     if pattern is None:
   264     if pattern is None:
   255         pass
   265         pass
   256     elif like:
   266     elif like:
   257         sql += " WHERE domainname LIKE '%s'" % pattern
   267         sql += " WHERE domainname LIKE '%s'" % pattern
   258     else:
   268     else:
   259         sql += " WHERE domainname = '%s'" % pattern
   269         sql += " WHERE domainname = '%s'" % pattern
   260     sql += ' ORDER BY domainname'
   270     sql += ' ORDER BY is_primary DESC, domainname'
   261     # XXX + is_primary // add prefix like [P] || [A]
       
   262     dbc = dbh.cursor()
   271     dbc = dbh.cursor()
   263     dbc.execute(sql)
   272     dbc.execute(sql)
   264     domains = dbc.fetchall()
   273     doms = dbc.fetchall()
   265     dbc.close()
   274     dbc.close()
   266     return domains
   275 
       
   276     domdict = {}
       
   277     order = [dom[0] for dom in doms if dom[2]]
       
   278     if len(order) == 0:
       
   279         for dom in doms:
       
   280             if dom[0] not in order:
       
   281                 order.append(dom[0])
       
   282     for gid, dom, is_primary in doms:
       
   283         if is_primary:
       
   284             domdict[gid] = [dom]
       
   285         else:
       
   286             try:
       
   287                 domdict[gid].append(dom)
       
   288             except KeyError:
       
   289                 domdict[gid] = [None, dom]
       
   290     del doms
       
   291     return order, domdict