* 'VirtualMailManager/VirtualMailManager.py'
authorPascal Volk <neverseen@users.sourceforge.net>
Fri, 15 Aug 2008 21:00:26 +0000
changeset 44 c9ab6900ede9
parent 43 92a6132940f5
child 45 9e66138aad0b
* 'VirtualMailManager/VirtualMailManager.py' - VirtualMailManager.domain_info() returns also alias domains if parameter »detailed« is given. * 'VirtualMailManager/Domain.py' - Fixed order in Domain.delete() - Added alias domains to Domain.getInfo() - Fixed queries in Domain.getAccounts() and Domain.getAliases() - Implemented Domain.getAliaseNames() - Rewrote search(), so that alias domains are shown below, indented, the »main« domain. * 'vmm' - Added 'aliasdomains' to _getOrder()/domaininfo - Renamed _printUsers() to _printList() - Implemented _formatDom() - Rewrote _printDomList()
VirtualMailManager/Domain.py
VirtualMailManager/VirtualMailManager.py
vmm
--- a/VirtualMailManager/Domain.py	Thu Aug 14 18:05:21 2008 +0000
+++ b/VirtualMailManager/Domain.py	Fri Aug 15 21:00:26 2008 +0000
@@ -154,8 +154,8 @@
             dbc.execute('DELETE FROM alias WHERE gid=%s', self._id)
             dbc.execute('DELETE FROM users WHERE gid=%s', self._id)
             dbc.execute('DELETE FROM relocated WHERE gid=%s', self._id)
+            dbc.execute('DELETE FROM domain_name WHERE gid=%s', self._id)
             dbc.execute('DELETE FROM domain_data WHERE gid=%s', self._id)
-            dbc.execute('DELETE FROM domain_name WHERE gid=%s', self._id)
             self._dbh.commit()
             dbc.close()
         else:
@@ -204,9 +204,8 @@
 
     def getInfo(self):
         """Returns a dictionary with information about the domain."""
-        # XXX add alias domain count
         sql = """\
-SELECT gid, domainname, transport, domaindir, accounts, aliases
+SELECT gid, domainname, transport, domaindir, aliasdomains, accounts, aliases
   FROM vmm_domain_info
  WHERE gid = %i""" % self._id
         dbc = self._dbh.cursor()
@@ -217,50 +216,76 @@
             raise VMMDomainException((_("Domain doesn't exist yet."),
                 ERR.NO_SUCH_DOMAIN))
         else:
-            keys = ['gid', 'domainname', 'transport', 'domaindir', 'accounts',
-                    'aliases']
+            keys = ['gid', 'domainname', 'transport', 'domaindir',
+                    'aliasdomains', 'accounts', 'aliases']
             return dict(zip(keys, info))
 
     def getAccounts(self):
         """Returns a list with all accounts from the domain."""
         dbc = self._dbh.cursor()
-        dbc.execute("SELECT userid AS users FROM dovecot_user WHERE gid = %s\
- ORDER BY users",
-                self._id)
+        dbc.execute("SELECT local_part from users where gid = %s ORDER BY\
+ local_part", self._id)
         users = dbc.fetchall()
         dbc.close()
         accounts = []
         if len(users) > 0:
             for account in users:
-                accounts.append(account[0])
+                accounts.append('%s@%s' % (account[0], self._name))
         return accounts
 
     def getAliases(self):
         """Returns a list with all aliases from the domain."""
         dbc = self._dbh.cursor()
-        dbc.execute("SELECT DISTINCT address FROM postfix_alias WHERE gid=%s\
- ORDER BY address",
+        dbc.execute("SELECT address from alias where gid = %s ORDER BY address",
                 self._id)
         addresses = dbc.fetchall()
         dbc.close()
         aliases = []
         if len(addresses) > 0:
             for alias in addresses:
-                aliases.append(alias[0])
+                aliases.append('%s@%s' % (alias[0], self._name))
         return aliases
 
+    def getAliaseNames(self):
+        """Returns a list with all alias names from the domain."""
+        dbc = self._dbh.cursor()
+        dbc.execute("SELECT domainname FROM domain_name WHERE gid = %s\
+ AND NOT is_primary", self._id)
+        anames = dbc.fetchall()
+        dbc.close()
+        aliasdomains = []
+        if len(anames) > 0:
+            for aname in anames:
+                aliasdomains.append(aname[0])
+        return aliasdomains
+
 def search(dbh, pattern=None, like=False):
-    sql = 'SELECT domainname FROM domain_name'
+    sql = 'SELECT gid, domainname, is_primary FROM domain_name'
     if pattern is None:
         pass
     elif like:
         sql += " WHERE domainname LIKE '%s'" % pattern
     else:
         sql += " WHERE domainname = '%s'" % pattern
-    sql += ' ORDER BY domainname'
-    # XXX + is_primary // add prefix like [P] || [A]
+    sql += ' ORDER BY is_primary DESC, domainname'
     dbc = dbh.cursor()
     dbc.execute(sql)
-    domains = dbc.fetchall()
+    doms = dbc.fetchall()
     dbc.close()
-    return domains
+
+    domdict = {}
+    order = [dom[0] for dom in doms if dom[2]]
+    if len(order) == 0:
+        for dom in doms:
+            if dom[0] not in order:
+                order.append(dom[0])
+    for gid, dom, is_primary in doms:
+        if is_primary:
+            domdict[gid] = [dom]
+        else:
+            try:
+                domdict[gid].append(dom)
+            except KeyError:
+                domdict[gid] = [None, dom]
+    del doms
+    return order, domdict
--- a/VirtualMailManager/VirtualMailManager.py	Thu Aug 14 18:05:21 2008 +0000
+++ b/VirtualMailManager/VirtualMailManager.py	Fri Aug 15 21:00:26 2008 +0000
@@ -500,7 +500,8 @@
         if detailed is None:
             return dominfo
         elif detailed == 'detailed':
-            return dominfo, dom.getAccounts(), dom.getAliases()
+            return (dominfo, dom.getAliaseNames(), dom.getAccounts(),
+                    dom.getAliases())
         else:
             raise VMMDomainException(("%s: '%s'" % (_('Invalid argument'),
                 detailed),  ERR.INVALID_OPTION))
--- a/vmm	Thu Aug 14 18:05:21 2008 +0000
+++ b/vmm	Fri Aug 15 21:00:26 2008 +0000
@@ -88,7 +88,8 @@
     order = ()
     if sys.argv[1] in ['di', 'domaininfo']:
         order = (('domainname', 0), ('gid', 1), ('transport', 0),
-                ('domaindir', 0), ('accounts', 0), ('aliases', 0))
+                ('domaindir', 0), ('aliasdomains', 0), ('accounts', 0),
+                ('aliases', 0))
     elif sys.argv[1] in ['ui', 'userinfo']:
         if argc == 4 or vmm.cfgGetBoolean('maildir', 'diskusage'):
             order = (('address', 0), ('name', 0), ('uid', 1), ('gid', 1),
@@ -112,12 +113,12 @@
             print '\t%s: %s' % (k.title().ljust(15, '.'), info[k])
     print
 
-def _printUsers(users, title):
+def _printList(alist, title):
     msg = '%s %s' % (_('Available'), title)
     print '%s\n%s' % (msg, '-'*len(msg))
-    if len(users) > 0:
-        for user in users:
-            print '\t%s' % user
+    if len(alist) > 0:
+        for val in alist:
+            print '\t%s' % val
     else:
         print _('\tNone')
     print
@@ -133,7 +134,16 @@
         print _('\tNone')
     print
 
-def _printDomList(domains):
+def _formatDom(domain, main=True):
+    if domain.startswith('xn--'):
+        domain = '%s (%s)' % (domain, vmm.ace2idna(domain))
+    if main:
+        return '\t[+] %s' %  domain
+    else:
+        return '\t[-]     %s' % domain
+
+
+def _printDomList(dids, domains):
     if argc < 3:
         msg = _('Available domains')
     else:
@@ -142,12 +152,12 @@
     if not len(domains):
         print _('\tNone')
     else:
-        for domain in domains:
-            d = domain[0]
-            if not d.startswith('xn--'):
-                print '\t%s' % d
-            else:
-                print "\t%s (%s)" % (d, vmm.ace2idna(d))
+        for id in dids:
+            if domains[id][0] is not None:
+                print _formatDom(domains[id][0])
+            if len(domains[id]) > 1:
+                for alias in domains[id][1:]:
+                    print _formatDom(alias, main=False)
     print
 
 def domain_add():
@@ -170,12 +180,13 @@
     if argc < 3:
         usage(EXIT.MISSING_ARGS, _('Missing domain name.'))
     elif argc < 4:
-        _printInfo(vmm.domain_info(sys.argv[2].lower()), 'Domain')
+        _printInfo(vmm.domain_info(sys.argv[2].lower()), _('Domain'))
     else:
         infos = vmm.domain_info(sys.argv[2].lower(), sys.argv[3])
-        _printInfo(infos[0], 'Domain')
-        _printUsers(infos[1], _('accounts'))
-        _printUsers(infos[2], _('aliases'))
+        _printInfo(infos[0], _('Domain'))
+        _printList(infos[1], _('alias domains'))
+        _printList(infos[2], _('accounts'))
+        _printList(infos[3], _('aliases'))
 
 def domain_transport():
     if argc < 3:
@@ -279,9 +290,10 @@
 
 def domain_list():
     if argc < 3:
-        _printDomList(vmm.domain_list())
+        order, doms = vmm.domain_list()
     else:
-        _printDomList(vmm.domain_list(sys.argv[2].lower()))
+        order, doms = vmm.domain_list(sys.argv[2].lower())
+    _printDomList(order, doms)
 
 def showWarnings():
     if vmm.hasWarnings():