Add experimental urwid-based GUI
[public/pyceo-broken.git] / pylib / csc / apps / urwid / search.py
1 import urwid
2
3 from csc.apps.urwid.widgets import *
4 from csc.apps.urwid.window import *
5
6 from csc.adm import accounts, members, terms
7 from csc.common.excep import InvalidArgument
8
9 class TermPage(WizardPanel):
10     def init_widgets(self):
11         self.term = SingleEdit("Term: ")
12
13         self.widgets = [
14             urwid.Text( "Terms Members" ),
15             urwid.Divider(),
16             self.term,
17         ]
18     def check(self):
19         if not members.connected(): members.connect()
20         try:
21             self.state['term'] = self.term.get_edit_text()
22             terms.parse( self.state['term'] )
23         except:
24             self.focus_widget( self.term )
25             set_status( "Invalid term" )
26             return True
27         mlist = members.list_term( self.state['term'] ).values()
28         pop_window()
29         member_list( mlist )
30
31 class NamePage(WizardPanel):
32     def init_widgets(self):
33         self.name = SingleEdit("Name: ")
34
35         self.widgets = [
36             urwid.Text( "Members by Name" ),
37             urwid.Divider(),
38             self.name,
39         ]
40     def check(self):
41         if not members.connected(): members.connect()
42         self.state['name'] = self.name.get_edit_text()
43         if not self.state['name']:
44             self.focus_widget( self.term )
45             set_status( "Invalid name" )
46             return True
47         mlist = members.list_name( self.state['name'] ).values()
48         pop_window()
49         member_list( mlist )
50
51 def member_list(mlist):
52     mlist = list(mlist)
53     mlist.sort( lambda x, y: cmp(x['uid'], y['uid']) )
54     buf = ''
55     for member in mlist:
56         if 'uid' in member:
57             uid = member['uid'][0]
58         else:
59             uid = None
60         if 'program' in member:
61             program = member['program'][0]
62         else:
63             program = None
64         attrs = ( uid, member['cn'][0], program )
65         buf += "%10s %30s\n%41s\n\n" % attrs
66     set_status("Press escape to return to the menu")
67     push_window(urwid.ListBox([urwid.Text(buf)]))
68
69