Improve group search
authorDavid Bartley <dtbartle@csclub.uwaterloo.ca>
Wed, 14 Nov 2007 01:50:14 +0000 (20:50 -0500)
committerDavid Bartley <dtbartle@csclub.uwaterloo.ca>
Wed, 14 Nov 2007 01:50:14 +0000 (20:50 -0500)
* Add 'Search by group'
* Use member_list to display results

pylib/csc/adm/members.py
pylib/csc/apps/urwid/main.py
pylib/csc/apps/urwid/search.py

index 83152ca..3c6e74a 100644 (file)
@@ -193,6 +193,31 @@ def list_name(name):
     return ldap_connection.member_search_name(name)
 
 
+def list_group(group):
+    """
+    Build a list of members in a group.
+
+    Parameters:
+        group - the group to match members against
+
+    Returns: a list of member dictionaries
+
+    Example: list_name('syscom'): -> {
+                 'mspang': { 'cn': 'Michael Spang', ... },
+                 ...
+             ]
+    """
+
+    members = group_members(group)
+    if members:
+        ret = {}
+        for member in members:
+            ret[member] = get(member)
+        return ret
+    else:
+        return {}
+
+
 def delete(userid):
     """
     Erase all records of a member.
@@ -306,10 +331,16 @@ def group_members(group):
     """
 
     group = ldap_connection.group_lookup(group)
-    if not 'uniqueMember' in group:
-        return []
+    if group:
+        if 'uniqueMember' in group:
+            r = re.compile('^uid=([^,]*)')
+            return map(lambda x: r.match(x).group(1), group['uniqueMember'])
+        elif 'memberUid' in group:
+            return group['memberUid']
+        else:
+            return []
     else:
-        return group['uniqueMember']
+        return []
 
 
 ### Tests ###
index 5a01cf3..ee9bf7c 100644 (file)
@@ -121,6 +121,7 @@ def search_members(data):
     menu = [
         ("Members by term", search_term, None),
         ("Members by name", search_name, None),
+        ("Members by group", search_group, None),
         ("Back", raise_back, None),
     ]
 
@@ -133,6 +134,9 @@ def search_name(data):
 def search_term(data):
     push_wizard("By Term", [ search.TermPage ])
 
+def search_group(data):
+    push_wizard("By Group", [ search.GroupPage ])
+
 def group_members(data):
     menu = [
         ("Add %s member" % data["name"].lower(), add_group_member, data),
@@ -151,18 +155,9 @@ def remove_group_member(data):
     pass
 
 def list_group_members(data):
-
     if not members.connected(): members.connect()
-    group_members = members.group_members(data["group"])
-    r = re.compile('^uid=([^,]*)')
-    menu = []
-    for group in group_members:
-        menu.append( (r.match(group).group(1), None, None) )
-    menu.append( ("--------", raise_back, None) )
-    menu.append( ("Back", raise_back, None) )
-
-    listbox = urwid.ListBox( menu_items( menu ) )
-    push_window(listbox, "Members")
+    mlist = members.list_group( data["group"] ).values()
+    search.member_list( mlist )
 
 def run():
     push_window( main_menu(), program_name() )
index c6eb8f6..d531a82 100644 (file)
@@ -41,13 +41,33 @@ class NamePage(WizardPanel):
         if not members.connected(): members.connect()
         self.state['name'] = self.name.get_edit_text()
         if not self.state['name']:
-            self.focus_widget( self.term )
+            self.focus_widget( self.name )
             set_status( "Invalid name" )
             return True
         mlist = members.list_name( self.state['name'] ).values()
         pop_window()
         member_list( mlist )
 
+class GroupPage(WizardPanel):
+    def init_widgets(self):
+        self.group = SingleEdit("Group: ")
+
+        self.widgets = [
+            urwid.Text( "Members by Group" ),
+            urwid.Divider(),
+            self.group,
+        ]
+    def check(self):
+        if not members.connected(): members.connect()
+        self.state['group'] = self.group.get_edit_text()
+        if not self.state['group']:
+            self.focus_widget( self.group )
+            set_status( "Invalid group" )
+            return True
+        mlist = members.list_group( self.state['group'] ).values()
+        pop_window()
+        member_list( mlist )
+
 def member_list(mlist):
     mlist = list(mlist)
     mlist.sort( lambda x, y: cmp(x['uid'], y['uid']) )