5bc19b35e43cbcd874cd1c463214133deae892bb
[mspang/pyceo.git] / pylib / csc / apps / urwid / groups.py
1 import urwid, pwd, grp
2 from csc.apps.urwid.widgets import *
3 from csc.apps.urwid.window import *
4 import csc.apps.urwid.search as search
5
6 from csc.adm import members
7 from csc.common.excep import InvalidArgument
8
9 def menu_items(items):
10     return [ urwid.AttrWrap( ButtonText( cb, data, txt ), 'menu', 'selected') for (txt, cb, data) in items ]
11
12 def change_group_member(data):
13     push_wizard("%s %s Member" % (data["action"], data["name"]), [
14         (ChangeMember, data),
15         EndPage,
16     ])
17
18 def list_group_members(data):
19     mlist = members.list_group( data["group"] ).values()
20     search.member_list( mlist )
21
22 def group_members(data):
23     add_data = data.copy()
24     add_data['action'] = 'Add'
25     remove_data = data.copy()
26     remove_data['action'] = 'Remove'
27     menu = [
28         ("Add %s member" % data["name"].lower(),
29             change_group_member, add_data),
30         ("Remove %s member" % data["name"].lower(),
31             change_group_member, remove_data),
32         ("List %s members" % data["name"].lower(), list_group_members, data),
33         ("Back", raise_back, None),
34     ]
35
36     listbox = urwid.ListBox( menu_items( menu ) )
37     push_window(listbox, "Manage %s" % data["name"])
38
39 class IntroPage(WizardPanel):
40     def init_widgets(self):
41         self.widgets = [
42             urwid.Text( "Managing Club or Group" ),
43             urwid.Divider(),
44             urwid.Text( "Adding a member to a club will also grant them "
45                         "access to the club's files and allow them to "
46                         "become_club."
47                         "\n\n"
48                         "Do not manage office and syscom related groups using "
49                         "this interface. Instead use the \"Manage Office "
50                         "Staff\" and \"Manage Systems Committee\" entries "
51                         "from the main menu." )
52         ]
53     def focusable(self):
54         return False
55
56 class InfoPage(WizardPanel):
57     def init_widgets(self):
58         self.group = WordEdit("Club or Group: ")
59         self.widgets = [
60             urwid.Text( "Club or Group Information"),
61             urwid.Divider(),
62             self.group,
63         ]
64     def check(self):
65         group = self.group.get_edit_text()
66         # TODO - check that group is valid
67         group_name = group # TODO
68         data = {
69             "name" : group,
70             "group" : group_name,
71             "groups" : [group],
72         }
73         group_members(data)
74
75 class ChangeMember(WizardPanel):
76     def __init__(self, state, data):
77         state['data'] = data
78         WizardPanel.__init__(self, state)
79     def init_widgets(self):
80         self.userid = WordEdit("Username: ")
81
82         data = self.state['data']
83         self.widgets = [
84             urwid.Text( "%s %s Member" % (data['action'], data['name']) ),
85             urwid.Divider(),
86             self.userid,
87         ]
88     def check(self):
89         self.state['userid'] = self.userid.get_edit_text()
90         if self.state['userid']:
91             self.state['member'] = members.get(self.userid.get_edit_text())
92         if not self.state['member']:
93             set_status("Member not found")
94             self.focus_widget(self.userid)
95             return True
96         clear_status()
97
98 class EndPage(WizardPanel):
99     def init_widgets(self):
100         self.headtext = urwid.Text("")
101         self.midtext = urwid.Text("")
102         self.widgets = [
103             self.headtext,
104             urwid.Divider(),
105             self.midtext,
106         ]
107     def focusable(self):
108         return False
109     def check(self):
110         pop_window()
111     def activate(self):
112         data = self.state['data']
113         action = data['action'].lower()
114         failed = []
115         for group in data['groups']:
116             try:
117                 members.change_group_member(action, group, self.state['userid'])
118             except:
119                 failed.append(group)
120         if len(failed) == 0:
121             self.headtext.set_text("%s succeeded" % data['action'])
122             self.midtext.set_text("Congratulations, the group modification "
123                 "has succeeded.")
124         else:
125             self.headtext.set_text("%s partially succeeded" % data['action'])
126             self.midtext.set_text("Failed to %s member to %s for the "
127                 "following groups: %s. This may indicate an attempt to add a "
128                 "duplicate group member or to delete a non-present group "
129                 "member." % (data['action'].lower(), data['name'],
130                 ', '.join(failed)))