No more office/syscom entries. Check if group is valid
[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.SignPage,
38         newmember.PassPage,
39         newmember.EndPage,
40     ], (60, 15))
41
42 def new_club(*args, **kwargs):
43     push_wizard("New Club Account", [
44         newmember.ClubIntroPage,
45         newmember.ClubInfoPage,
46         (newmember.EndPage, "club"),
47     ], (60, 15))
48
49 def new_club_user(*args, **kwargs):
50     push_wizard("New Club Rep Account", [
51         newmember.ClubUserIntroPage,
52         newmember.InfoPage,
53         newmember.SignPage,
54         newmember.PassPage,
55         (newmember.EndPage, "clubuser"),
56     ], (60, 15))
57
58 def manage_group(*args, **kwargs):
59     push_wizard("Manage Club or Group Members", [
60         groups.IntroPage,
61         groups.InfoPage,
62     ], (60, 15))
63
64 def renew_member(*args, **kwargs):
65     push_wizard("Renew Membership", [
66         renew.IntroPage,
67         renew.UserPage,
68         renew.EmailPage,
69         renew.EmailDonePage,
70         renew.TermPage,
71         renew.PayPage,
72         renew.EndPage,
73     ], (60, 15))
74
75 def renew_club_user(*args, **kwargs):
76     push_wizard("Renew Club Rep Account", [
77         renew.ClubUserIntroPage,
78         renew.UserPage,
79         renew.EmailPage,
80         renew.EmailDonePage,
81         (renew.TermPage, "clubuser"),
82         (renew.EndPage, "clubuser"),
83     ], (60, 15))
84
85 def display_member(data):
86     push_wizard("Display Member", [
87         renew.UserPage,
88         info.InfoPage,
89     ], (60, 15))
90
91 def search_members(data):
92     menu = make_menu([
93         ("Members by term", search_term, None),
94         ("Members by name", search_name, None),
95         ("Members by group", search_group, None),
96         ("Back", raise_back, None),
97     ])
98     push_window(menu, "Search Members")
99
100 def search_name(data):
101     push_wizard("By Name", [ search.NamePage ])
102
103 def search_term(data):
104     push_wizard("By Term", [ search.TermPage ])
105
106 def search_group(data):
107     push_wizard("By Group", [ search.GroupPage ])
108
109 def manage_positions(data):
110     push_wizard("Manage Positions", [
111         positions.IntroPage,
112         positions.InfoPage,
113         positions.EndPage,
114     ], (50, 15))
115
116 def change_shell(data):
117     push_wizard("Change Shell", [
118         shell.IntroPage,
119         shell.YouPage,
120         shell.ShellPage,
121         shell.EndPage
122     ], (50, 20))
123
124 def create_mysql_db(data):
125     push_wizard("Create MySQL database", [
126         databases.IntroPage,
127         databases.UserPage,
128         databases.EndPage,
129     ], (60, 15))
130
131 def check_group(group):
132     try:
133         me = pwd.getpwuid(os.getuid()).pw_name
134         return me in grp.getgrnam(group).gr_mem
135     except KeyError:
136         pass
137
138 def top_menu():
139     office_only = [
140         ("New Member", new_member, None),
141         ("New Club Rep", new_club_user, None),
142         ("Renew Membership", renew_member, None),
143         ("Renew Club Rep", renew_club_user, None),
144         ("New Club", new_club, None),
145         ("Library", library.library, None),
146     ]
147     syscom_only = [
148         ("Manage Club or Group Members", manage_group, None),
149         ("Manage Positions", manage_positions, None),
150     ]
151     unrestricted = [
152         ("Display Member", display_member, None),
153         ("Search Members", search_members, None),
154         ("Change Shell", change_shell, None),
155         ("Create MySQL database", create_mysql_db, None),
156     ]
157     footer = [
158         ("Exit", raise_abort, None),
159     ]
160     menu = None
161
162     # reorder the menu for convenience
163     if not check_group('office') and not check_group('syscom'):
164         menu = labelled_menu([
165             ('Unrestricted', unrestricted),
166             ('Office Staff', office_only),
167             ('Systems Committee', syscom_only),
168             (None, footer)
169         ])
170     else:
171         menu = labelled_menu([
172             ('Office Staff', office_only),
173             ('Unrestricted', unrestricted),
174             ('Systems Committee', syscom_only),
175             (None, footer)
176         ])
177
178     return menu
179
180 def run():
181     push_window(top_menu(), program_name())
182     event_loop(ui)
183
184 def start():
185     ui.run_wrapper( run )
186
187 if __name__ == '__main__':
188     start()