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