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