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