Improve group search
[public/pyceo-broken.git] / pylib / csc / apps / urwid / main.py
1 import random, time, re
2 import urwid, urwid.curses_display
3
4 from csc.apps.urwid.widgets import *
5 from csc.apps.urwid.window import *
6 import csc.apps.urwid.newmember as newmember
7 import csc.apps.urwid.renew as renew
8 import csc.apps.urwid.info as info
9 import csc.apps.urwid.search as search
10
11 from csc.adm import accounts, members, terms
12 from csc.common.excep import InvalidArgument
13
14 ui = urwid.curses_display.Screen()
15
16 ui.register_palette([
17     # name, foreground, background, mono
18     ('banner', 'light gray', 'default', None),
19     ('menu', 'light gray', 'default', 'bold'),
20     ('selected', 'black', 'light gray', 'bold'),
21 ])
22
23
24 def program_name():
25     cwords = [ "CSC" ] * 20 + [ "Club" ] * 10 + [ "Campus" ] * 5 + \
26         [ "Communist", "Canadian", "Celestial", "Cryptographic", "Calum's",
27           "Canonical", "Capitalist", "Catastrophic", "Ceremonial", "Chaotic", "Civic",
28           "City", "County", "Caffeinated" ]
29     ewords = [ "Embellished", "Ergonomic", "Electric", "Eccentric", "European", "Economic",
30         "Evil", "Egotistical", "Elliptic", "Emasculating", "Embalming",
31         "Embryonic", "Emigrant", "Emissary's", "Emoting", "Employment", "Emulated",
32         "Enabling", "Enamoring", "Encapsulated", "Enchanted", "Encoded", "Encrypted",
33         "Encumbered", "Endemic", "Enhanced", "Enigmatic", "Enlightened", "Enormous",
34         "Enrollment", "Enshrouded", "Ephermal", "Epidemic", "Episodic", "Epsilon",
35         "Equitable", "Equestrian", "Equilateral", "Erroneous", "Erratic",
36         "Espresso", "Essential", "Estate", "Esteemed", "Eternal", "Ethical", "Eucalyptus",
37         "Euphemistic", "Envangelist", "Evasive", "Everyday", "Evidence", "Eviction", "Evildoer's",
38         "Evolution", "Exacerbation", "Exalted", "Examiner's", "Excise", "Exciting", "Exclusion",
39         "Exec", "Executioner's", "Exile", "Existential", "Expedient", "Expert", "Expletive",
40         "Exploiter's", "Explosive", "Exponential", "Exposing", "Extortion", "Extraction",
41         "Extraneous", "Extravaganza", "Extreme", "Extraterrestrial", "Extremist", "Eerie" ]
42     owords = [ "Office" ] * 50 + [ "Outhouse", "Outpost" ]
43
44     cword = random.choice(cwords)
45     eword = random.choice(ewords)
46     oword = random.choice(owords)
47
48     return "%s %s %s" % (cword, eword, oword)
49
50 office_data = {
51     "name" : "Office Staff",
52     "group" : "office"
53 }
54
55 syscom_data = {
56     "name" : "Systems Committee",
57     "group" : "syscom"
58 }
59
60 def menu_items(items):
61     return [ urwid.AttrWrap( ButtonText( cb, data, txt ), 'menu', 'selected') for (txt, cb, data) in items ]
62
63 def main_menu():
64     menu = [
65         ("New Member", new_member, None),
66         ("Renew Membership", renew_member, None),
67         ("Create Club Account", new_club, None),
68         ("Display Member", display_member, None),
69         ("Search", search_members, None),
70         ("Manage Office Staff", group_members, office_data),
71         ("Manage Systems Committee", group_members, syscom_data),
72         ("Exit", raise_abort, None),
73     ]
74
75     listbox = urwid.ListBox( menu_items( menu ) )
76     return listbox
77
78 def push_wizard(name, pages, dimensions=(50, 10)):
79     state = {}
80     wiz = Wizard()
81     for page in pages:
82         if type(page) != tuple:
83             page = (page, )
84         wiz.add_panel( page[0](state, *page[1:]) )
85     push_window( urwid.Filler( urwid.Padding(
86         urwid.LineBox(wiz), 'center', dimensions[0]),
87         'middle', dimensions[1] ), name )
88
89 def new_member(*args, **kwargs):
90     push_wizard("New Member", [
91         newmember.IntroPage,
92         newmember.InfoPage,
93         newmember.SignPage,
94         newmember.PassPage,
95         newmember.EndPage,
96     ])
97
98 def new_club(*args, **kwargs):
99     push_wizard("New Club Account", [
100         newmember.ClubIntroPage,
101         newmember.ClubInfoPage,
102         (newmember.EndPage, "club"),
103     ], (60,15))
104
105 def renew_member(*args, **kwargs):
106     push_wizard("Renew Membership", [
107         renew.IntroPage,
108         renew.UserPage,
109         renew.TermPage,
110         renew.PayPage,
111         renew.EndPage,
112     ])
113
114 def display_member(data):
115     push_wizard("Display Member", [
116         renew.UserPage,
117         info.InfoPage,
118     ], (60, 15))
119
120 def search_members(data):
121     menu = [
122         ("Members by term", search_term, None),
123         ("Members by name", search_name, None),
124         ("Members by group", search_group, None),
125         ("Back", raise_back, None),
126     ]
127
128     listbox = urwid.ListBox( menu_items( menu ) )
129     push_window(listbox, "Search")
130
131 def search_name(data):
132     push_wizard("By Name", [ search.NamePage ])
133
134 def search_term(data):
135     push_wizard("By Term", [ search.TermPage ])
136
137 def search_group(data):
138     push_wizard("By Group", [ search.GroupPage ])
139
140 def group_members(data):
141     menu = [
142         ("Add %s member" % data["name"].lower(), add_group_member, data),
143         ("Remove %s member" % data["name"].lower(), remove_group_member, data),
144         ("List %s members" % data["name"].lower(), list_group_members, data),
145         ("Back", raise_back, None),
146     ]
147
148     listbox = urwid.ListBox( menu_items( menu ) )
149     push_window(listbox, "Manage %s" % data["name"])
150
151 def add_group_member(data):
152     pass
153
154 def remove_group_member(data):
155     pass
156
157 def list_group_members(data):
158     if not members.connected(): members.connect()
159     mlist = members.list_group( data["group"] ).values()
160     search.member_list( mlist )
161
162 def run():
163     push_window( main_menu(), program_name() )
164     event_loop( ui )
165
166 def start():
167     ui.run_wrapper( run )
168
169 if __name__ == '__main__':
170     start()