Add mysql database stuff
[mspang/pyceo.git] / ceo / urwid / main.py
index d604073..ba877f4 100644 (file)
@@ -1,7 +1,8 @@
-import sys, random, urwid.curses_display
+import os, grp, pwd, sys, random, urwid.curses_display
 from ceo.urwid.widgets import *
 from ceo.urwid.window import *
-from ceo.urwid import newmember, renew, info, search, positions, groups, shell
+from ceo.urwid import newmember, renew, info, search, positions, groups, \
+    shell, library, databases
 
 ui = urwid.curses_display.Screen()
 
@@ -51,26 +52,6 @@ syscom_data = {
     "groups" : [ "office", "staff", "adm", "src" ],
 }
 
-def main_menu():
-    menu = [
-        ("New Member", new_member, None),
-        ("New Club Rep", new_club_user, None),
-        ("Renew Membership", renew_member, None),
-        ("Renew Club Rep", renew_club_user, None),
-        ("New Club", new_club, None),
-        ("Display Member", display_member, None),
-        ("Change Shell", change_shell, None),
-        ("Search", search_members, None),
-        ("Manage Club or Group Members", manage_group, None),
-        ("Manage Positions", manage_positions, None),
-        ("Manage Office Staff", groups.group_members, office_data),
-        ("Manage Systems Committee", groups.group_members, syscom_data),
-        ("Exit", raise_abort, None),
-    ]
-
-    listbox = urwid.ListBox( menu_items( menu ) )
-    return listbox
-
 def new_member(*args, **kwargs):
     push_wizard("New Member", [
         newmember.IntroPage,
@@ -78,7 +59,7 @@ def new_member(*args, **kwargs):
         newmember.SignPage,
         newmember.PassPage,
         newmember.EndPage,
-    ])
+    ], (60, 15))
 
 def new_club(*args, **kwargs):
     push_wizard("New Club Account", [
@@ -106,15 +87,19 @@ def renew_member(*args, **kwargs):
     push_wizard("Renew Membership", [
         renew.IntroPage,
         renew.UserPage,
+        renew.EmailPage,
+        renew.EmailDonePage,
         renew.TermPage,
         renew.PayPage,
         renew.EndPage,
-    ])
+    ], (60, 15))
 
 def renew_club_user(*args, **kwargs):
     push_wizard("Renew Club Rep Account", [
         renew.ClubUserIntroPage,
         renew.UserPage,
+        renew.EmailPage,
+        renew.EmailDonePage,
         (renew.TermPage, "clubuser"),
         (renew.EndPage, "clubuser"),
     ], (60, 15))
@@ -126,15 +111,13 @@ def display_member(data):
     ], (60, 15))
 
 def search_members(data):
-    menu = [
+    menu = make_menu([
         ("Members by term", search_term, None),
         ("Members by name", search_name, None),
         ("Members by group", search_group, None),
         ("Back", raise_back, None),
-    ]
-
-    listbox = urwid.ListBox( menu_items( menu ) )
-    push_window(listbox, "Search")
+    ])
+    push_window(menu, "Search")
 
 def search_name(data):
     push_wizard("By Name", [ search.NamePage ])
@@ -160,9 +143,67 @@ def change_shell(data):
         shell.EndPage
     ], (50, 20))
 
+def create_mysql_db(data):
+    push_wizard("Create MySQL database", [
+        databases.IntroPage,
+        databases.UserPage,
+        databases.EndPage,
+    ], (60, 15))
+
+def check_group(group):
+    try:
+        me = pwd.getpwuid(os.getuid()).pw_name
+        return me in grp.getgrnam(group).gr_mem
+    except KeyError:
+        pass
+
+def top_menu():
+    office_only = [
+        ("New Member", new_member, None),
+        ("New Club Rep", new_club_user, None),
+        ("Renew Membership", renew_member, None),
+        ("Renew Club Rep", renew_club_user, None),
+        ("New Club", new_club, None),
+        ("Library", library.library, None),
+    ]
+    syscom_only = [
+        ("Manage Club or Group Members", manage_group, None),
+        ("Manage Positions", manage_positions, None),
+        ("Manage Office Staff", groups.group_members, office_data),
+        ("Manage Systems Committee", groups.group_members, syscom_data),
+    ]
+    unrestricted = [
+        ("Display Member", display_member, None),
+        ("Search", search_members, None),
+        ("Change Shell", change_shell, None),
+        ("Create MySQL database", create_mysql_db, None),
+    ]
+    footer = [
+        ("Exit", raise_abort, None),
+    ]
+    menu = None
+
+    # reorder the menu for convenience
+    if not check_group('office') and not check_group('syscom'):
+        menu = labelled_menu([
+            ('Unrestricted', unrestricted),
+            ('Office Staff', office_only),
+            ('Systems Committee', syscom_only),
+            (None, footer)
+        ])
+    else:
+        menu = labelled_menu([
+            ('Office Staff', office_only),
+            ('Unrestricted', unrestricted),
+            ('Systems Committee', syscom_only),
+            (None, footer)
+        ])
+
+    return menu
+
 def run():
-    push_window( main_menu(), program_name() )
-    event_loop( ui )
+    push_window(top_menu(), program_name())
+    event_loop(ui)
 
 def start():
     ui.run_wrapper( run )