forked from public/pyceo
Lookup name and program based on uwdir id
This commit is contained in:
parent
31c05d869f
commit
b7b67399a8
|
@ -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
|
|
@ -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(),
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue