Remind users that club accounts are free.
[public/pyceo-broken.git] / ceo / urwid / main.py
1 import os, grp, pwd, sys, random, urwid.curses_display
2 from ceo.urwid.widgets import *
3 from ceo.urwid.window import *
4 from ceo.urwid import newmember, renew, info, search, positions, groups, \
5     shell, library, databases
6
7 def program_name():
8     cwords = [ "CSC" ] * 20 + [ "Club" ] * 10 + [ "Campus" ] * 5 + \
9         [ "Communist", "Canadian", "Celestial", "Cryptographic", "Calum's",
10           "Canonical", "Capitalist", "Catastrophic", "Ceremonial", "Chaotic", "Civic",
11           "City", "County", "Caffeinated" ]
12     ewords = [ "Embellished", "Ergonomic", "Electric", "Eccentric", "European", "Economic",
13         "Evil", "Egotistical", "Elliptic", "Emasculating", "Embalming",
14         "Embryonic", "Emigrant", "Emissary's", "Emoting", "Employment", "Emulated",
15         "Enabling", "Enamoring", "Encapsulated", "Enchanted", "Encoded", "Encrypted",
16         "Encumbered", "Endemic", "Enhanced", "Enigmatic", "Enlightened", "Enormous",
17         "Enrollment", "Enshrouded", "Ephemeral", "Epidemic", "Episodic", "Epsilon",
18         "Equitable", "Equestrian", "Equilateral", "Erroneous", "Erratic",
19         "Espresso", "Essential", "Estate", "Esteemed", "Eternal", "Ethical", "Eucalyptus",
20         "Euphemistic", "Evangelist", "Evasive", "Everyday", "Evidence", "Eviction", "Evildoer's",
21         "Evolution", "Exacerbation", "Exalted", "Examiner's", "Excise", "Exciting", "Exclusion",
22         "Exec", "Executioner's", "Exile", "Existential", "Expedient", "Expert", "Expletive",
23         "Exploiter's", "Explosive", "Exponential", "Exposing", "Extortion", "Extraction",
24         "Extraneous", "Extravaganza", "Extreme", "Extraterrestrial", "Extremist", "Eerie" ]
25     owords = [ "Office" ] * 50 + [ "Outhouse", "Outpost" ]
26
27     cword = random.choice(cwords)
28     eword = random.choice(ewords)
29     oword = random.choice(owords)
30
31     return "%s %s %s" % (cword, eword, oword)
32
33 def new_member(*args, **kwargs):
34     push_wizard("New Member", [
35         newmember.IntroPage,
36         newmember.InfoPage,
37         newmember.NumberOfTermsPage,
38         newmember.SignPage,
39         newmember.PassPage,
40         newmember.EndPage,
41     ], (60, 15))
42
43 def new_club(*args, **kwargs):
44     push_wizard("New Club Account", [
45         newmember.ClubIntroPage,
46         newmember.ClubInfoPage,
47         (newmember.EndPage, "club"),
48     ], (60, 15))
49
50 def new_club_user(*args, **kwargs):
51     push_wizard("New Club Rep Account", [
52         newmember.ClubUserIntroPage,
53         newmember.ClubNoPayPage,
54         newmember.InfoPage,
55         newmember.NumberOfTermsPage,
56         newmember.SignPage,
57         newmember.PassPage,
58         (newmember.EndPage, "clubuser"),
59     ], (60, 15))
60
61 def manage_group(*args, **kwargs):
62     push_wizard("Manage Club or Group Members", [
63         groups.IntroPage,
64         groups.InfoPage,
65     ], (60, 15))
66
67 def renew_member(*args, **kwargs):
68     push_wizard("Renew Membership", [
69         renew.IntroPage,
70         renew.UserPage,
71         renew.EmailPage,
72         renew.EmailDonePage,
73         renew.TermPage,
74         renew.PayPage,
75         renew.EndPage,
76     ], (60, 15))
77
78 def renew_club_user(*args, **kwargs):
79     push_wizard("Renew Club Rep Account", [
80         renew.ClubUserIntroPage,
81         newmember.ClubNoPayPage,
82         renew.UserPage,
83         renew.EmailPage,
84         renew.EmailDonePage,
85         (renew.TermPage, "clubuser"),
86         (renew.EndPage, "clubuser"),
87     ], (60, 15))
88
89 def display_member(data):
90     push_wizard("Display Member", [
91         renew.UserPage,
92         info.InfoPage,
93     ], (60, 15))
94
95 def search_members(data):
96     menu = make_menu([
97         ("Members by term", search_term, None),
98         ("Members by name", search_name, None),
99         ("Members by group", search_group, None),
100         ("Back", raise_back, None),
101     ])
102     push_window(menu, "Search Members")
103
104 def search_name(data):
105     push_wizard("By Name", [ search.NamePage ])
106
107 def search_term(data):
108     push_wizard("By Term", [ search.TermPage ])
109
110 def search_group(data):
111     push_wizard("By Group", [ search.GroupPage ])
112
113 def manage_positions(data):
114     push_wizard("Manage Positions", [
115         positions.IntroPage,
116         positions.InfoPage,
117         positions.EndPage,
118     ], (50, 15))
119
120 def change_shell(data):
121     push_wizard("Change Shell", [
122         shell.IntroPage,
123         shell.YouPage,
124         shell.ShellPage,
125         shell.EndPage
126     ], (50, 20))
127
128 def create_mysql_db(data):
129     push_wizard("Create MySQL database", [
130         databases.IntroPage,
131         databases.UserPage,
132         databases.EndPage,
133     ], (60, 15))
134
135 def check_group(group):
136     try:
137         me = pwd.getpwuid(os.getuid()).pw_name
138         return me in grp.getgrnam(group).gr_mem
139     except KeyError:
140         pass
141
142 def top_menu():
143     office_only = [
144         ("New Member", new_member, None),
145         ("New Club Rep", new_club_user, None),
146         ("Renew Membership", renew_member, None),
147         ("Renew Club Rep", renew_club_user, None),
148         ("New Club", new_club, None),
149         ("Library", library.library, None),
150     ]
151     syscom_only = [
152         ("Manage Club or Group Members", manage_group, None),
153         ("Manage Positions", manage_positions, None),
154     ]
155     unrestricted = [
156         ("Display Member", display_member, None),
157         ("Search Members", search_members, None),
158         ("Change Shell", change_shell, None),
159         ("Create MySQL database", create_mysql_db, None),
160     ]
161     footer = [
162         ("Exit", raise_abort, None),
163     ]
164     menu = None
165
166     # reorder the menu for convenience
167     if not check_group('office') and not check_group('syscom'):
168         menu = labelled_menu([
169             ('Unrestricted', unrestricted),
170             ('Office Staff', office_only),
171             ('Systems Committee', syscom_only),
172             (None, footer)
173         ])
174     else:
175         menu = labelled_menu([
176             ('Office Staff', office_only),
177             ('Unrestricted', unrestricted),
178             ('Systems Committee', syscom_only),
179             (None, footer)
180         ])
181
182     return menu
183
184 def run():
185     push_window(top_menu(), program_name())
186     event_loop(ui)
187
188 def start():
189     ui.run_wrapper( run )
190
191 if __name__ == '__main__':
192     start()