Merge branch 'master' of /users/git/public/pyceo
[public/pyceo-broken.git] / ceo / urwid / groups.py
1 import urwid
2 from ceo import members
3 from ceo.urwid import search
4 from ceo.urwid.widgets import *
5 from ceo.urwid.window import *
6
7 def menu_items(items):
8     return [ urwid.AttrWrap( ButtonText( cb, data, txt ), 'menu', 'selected') for (txt, cb, data) in items ]
9
10 def change_group_member(data):
11     push_wizard("%s %s Member" % (data["action"], data["name"]), [
12         (ChangeMember, data),
13         EndPage,
14     ])
15
16 def list_group_members(data):
17     mlist = members.list_group( data["group"] ).values()
18     search.member_list( mlist )
19
20 def group_members(data):
21     add_data = data.copy()
22     add_data['action'] = 'Add'
23     remove_data = data.copy()
24     remove_data['action'] = 'Remove'
25     menu = [
26         ("Add %s member" % data["name"].lower(),
27             change_group_member, add_data),
28         ("Remove %s member" % data["name"].lower(),
29             change_group_member, remove_data),
30         ("List %s members" % data["name"].lower(), list_group_members, data),
31         ("Back", raise_back, None),
32     ]
33
34     listbox = urwid.ListBox( menu_items( menu ) )
35     push_window(listbox, "Manage %s" % data["name"])
36
37 class IntroPage(WizardPanel):
38     def init_widgets(self):
39         self.widgets = [
40             urwid.Text( "Managing Club or Group" ),
41             urwid.Divider(),
42             urwid.Text( "Adding a member to a club will also grant them "
43                         "access to the club's files and allow them to "
44                         "become_club."
45                         "\n\n"
46                         "Do not manage office and syscom related groups using "
47                         "this interface. Instead use the \"Manage Office "
48                         "Staff\" and \"Manage Systems Committee\" entries "
49                         "from the main menu." )
50         ]
51     def focusable(self):
52         return False
53
54 class InfoPage(WizardPanel):
55     def init_widgets(self):
56         self.group = LdapWordEdit(csclub_uri, csclub_base, 'uid',
57             "Club or Group: ")
58         self.widgets = [
59             urwid.Text( "Club or Group Information"),
60             urwid.Divider(),
61             self.group,
62         ]
63     def check(self):
64         group = self.group.get_edit_text()
65         # TODO - check that group is valid
66         group_name = group # TODO
67         data = {
68             "name" : group,
69             "group" : group_name,
70             "groups" : [group],
71         }
72         group_members(data)
73
74 class ChangeMember(WizardPanel):
75     def __init__(self, state, data):
76         state['data'] = data
77         WizardPanel.__init__(self, state)
78     def init_widgets(self):
79         self.userid = LdapWordEdit(csclub_uri, csclub_base, 'uid',
80             "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'] + [data['group']]:
116             try:
117                 members.change_group_member(action, group, self.state['userid'])
118             except ldap.LDAPError:
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 Results" % 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 member that was not in "
129                 "the group." % (data['action'].lower(), data['name'],
130                 ', '.join(failed)))