No more office/syscom entries. Check if group is valid
[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         ]
42     def focusable(self):
43         return False
44
45 class InfoPage(WizardPanel):
46     def init_widgets(self):
47         self.group = LdapWordEdit(csclub_uri, csclub_base, 'uid',
48             "Club or Group: ")
49         self.widgets = [
50             urwid.Text( "Club or Group Information"),
51             urwid.Divider(),
52             self.group,
53         ]
54     def check(self):
55         group = self.group.get_edit_text()
56
57         # check if group is valid
58         if not group or not members.get_group(group):
59             set_status("Group not found")
60             self.focus_widget(self.group)
61             return True
62
63         data = {
64             "name" : group,
65             "group" : group,
66             "groups" : [],
67         }
68
69         # Office Staff and Syscom get added to more groups
70         if group == "syscom":
71             data["name"] = "Systems Committee"
72             data["groups"] = [ "office", "staff", "adm", "src" ]
73         elif group == "office":
74             data["name"] = "Office Staff"
75             data["groups"] = [ "cdrom", "audio", "video", "www" ]
76
77         group_members(data)
78
79 class ChangeMember(WizardPanel):
80     def __init__(self, state, data):
81         state['data'] = data
82         WizardPanel.__init__(self, state)
83     def init_widgets(self):
84         self.userid = LdapWordEdit(csclub_uri, csclub_base, 'uid',
85             "Username: ")
86
87         data = self.state['data']
88         self.widgets = [
89             urwid.Text( "%s %s Member" % (data['action'], data['name']) ),
90             urwid.Divider(),
91             self.userid,
92         ]
93     def check(self):
94         self.state['userid'] = self.userid.get_edit_text()
95         if self.state['userid']:
96             self.state['member'] = members.get(self.userid.get_edit_text())
97         if not self.state['member']:
98             set_status("Member not found")
99             self.focus_widget(self.userid)
100             return True
101         clear_status()
102
103 class EndPage(WizardPanel):
104     def init_widgets(self):
105         self.headtext = urwid.Text("")
106         self.midtext = urwid.Text("")
107         self.widgets = [
108             self.headtext,
109             urwid.Divider(),
110             self.midtext,
111         ]
112     def focusable(self):
113         return False
114     def check(self):
115         pop_window()
116     def activate(self):
117         data = self.state['data']
118         action = data['action'].lower()
119         failed = []
120         for group in data['groups'] + [data['group']]:
121             try:
122                 members.change_group_member(action, group, self.state['userid'])
123             except ldap.LDAPError:
124                 failed.append(group)
125         if len(failed) == 0:
126             self.headtext.set_text("%s succeeded" % data['action'])
127             self.midtext.set_text("Congratulations, the group modification "
128                 "has succeeded.")
129         else:
130             self.headtext.set_text("%s Results" % data['action'])
131             self.midtext.set_text("Failed to %s member to %s for the "
132                 "following groups: %s. This may indicate an attempt to add a "
133                 "duplicate group member or to delete a member that was not in "
134                 "the group." % (data['action'].lower(), data['name'],
135                 ', '.join(failed)))