Lookup name and program based on uwdir id
authorDavid Bartley <dtbartle@csclub.uwaterloo.ca>
Sun, 11 Nov 2007 04:32:01 +0000 (23:32 -0500)
committerMichael Spang <mspang@csclub.uwaterloo.ca>
Sun, 11 Nov 2007 05:03:28 +0000 (00:03 -0500)
pylib/csc/apps/urwid/ldapfilter.py [new file with mode: 0644]
pylib/csc/apps/urwid/newmember.py
pylib/csc/apps/urwid/widgets.py

diff --git a/pylib/csc/apps/urwid/ldapfilter.py b/pylib/csc/apps/urwid/ldapfilter.py
new file mode 100644 (file)
index 0000000..46d425e
--- /dev/null
@@ -0,0 +1,39 @@
+import ldap
+
+class LdapFilter:
+    def __init__(self, widget):
+        self.widget = widget
+
+    def set_ldap_filter(self, ldap_uri, ldap_base, ldap_attr, ldap_map):
+        try:
+            self.ldap = ldap.initialize(ldap_uri)
+            self.ldap.simple_bind_s("", "")
+        except ldap.LDAPError:
+            return
+        self.base = ldap_base
+        self.attr = ldap_attr
+        self.map = ldap_map
+
+    def keypress(self, size, key):
+        if self.ldap != None:
+            if key == 'enter' or key == 'down' or key == 'up':
+                attr = self.escape(self.attr)
+                search = self.escape(self.widget.get_edit_text(self))
+                filter = '(%s=%s)' % (attr, search)
+                try:
+                    matches = self.ldap.search_s(self.base, ldap.SCOPE_SUBTREE, filter)
+                    if len(matches) > 0:
+                        (_, attrs) = matches[0]
+                        for (k, v) in self.map.items():
+                            if attrs.has_key(k) and len(attrs[k]) > 0:
+                                v.set_edit_text(attrs[k][0])
+                except ldap.LDAPError:
+                    pass
+        return self.widget.keypress(self, size, key)
+
+    def escape(self, value):
+        value = str(value)
+        value = value.replace('\\', '\\5c').replace('*', '\\2a')
+        value = value.replace('(', '\\28').replace(')', '\\29')
+        value = value.replace('\x00', '\\00')
+        return value
index 8e11e11..48f3210 100644 (file)
@@ -1,6 +1,7 @@
 import urwid
 from csc.apps.urwid.widgets import *
 from csc.apps.urwid.window import *
+from csc.apps.urwid.ldapfilter import LdapFilter
 
 from csc.adm import accounts, members
 from csc.common.excep import InvalidArgument
@@ -38,9 +39,13 @@ class ClubIntroPage(WizardPanel):
 
 class InfoPage(WizardPanel):
     def init_widgets(self):
-        self.userid = WordEdit("UWdir ID: ")
+        self.userid = LdapFilterWordEdit("UWdir ID: ")
         self.name = SingleEdit("Full name: ")
         self.program = SingleEdit("Program of Study: ")
+        self.userid.set_ldap_filter(
+            "ldap://uwldap.uwaterloo.ca/", "dc=uwaterloo,dc=ca",
+            "uid", {'cn':self.name, 'ou':self.program}
+        )
         self.widgets = [
             urwid.Text( "Member Information - Please Check ID" ),
             urwid.Divider(),
index 2216240..5fe8214 100644 (file)
@@ -1,4 +1,5 @@
 import urwid
+from csc.apps.urwid.ldapfilter import *
 
 class ButtonText(urwid.Text):
     def __init__(self, callback, *args, **kwargs):
@@ -30,6 +31,11 @@ class WordEdit(SingleEdit):
     def valid_char(self, ch):
         return urwid.Edit.valid_char(self, ch) and ch != ' '
 
+class LdapFilterWordEdit(LdapFilter, WordEdit):
+    def __init__(self, *args):
+        LdapFilter.__init__(self, WordEdit)
+        WordEdit.__init__(self, *args)
+
 class PassEdit(SingleEdit):
     def get_text(self):
         text = urwid.Edit.get_text(self)