Fixing imports.
[public/pyceo-broken.git] / ceo / urwid / positions.py
1 import urwid
2 from ceo import members
3 from ceo.urwid.widgets import *
4 from ceo.urwid.window import *
5
6 position_data = [
7     ('president',       'President'),
8     ('vice-president',  'Vice-president'),
9     ('treasurer',       'Treasurer'),
10     ('secretary',       'Secretary'),
11     ('sysadmin',        'System Administrator'),
12     ('cro',             'Chief Returning Officer'),
13     ('librarian',       'Librarian'),
14     ('imapd',           'Imapd'),
15     ('webmaster',       'Web Master'),
16 ]
17
18 class IntroPage(WizardPanel):
19     def init_widgets(self):
20         self.widgets = [
21             urwid.Text( "Managing Positions" ),
22             urwid.Divider(),
23             urwid.Text( "Enter a username for each position. If a position is "
24                         "held by multiple people, enter a comma-separated "
25                         "list of usernames. If a position is held by nobody "
26                         "leave the username blank." ),
27         ]
28     def focusable(self):
29         return False
30
31 class InfoPage(WizardPanel):
32     def init_widgets(self):
33         self.widgets = [
34             urwid.Text( "Positions" ),
35             urwid.Divider(),
36         ]
37         positions = members.list_positions()
38         self.position_widgets = {}
39         for (position, text) in position_data:
40             widget = LdapWordEdit(csclub_uri, csclub_base, 'uid',
41                 "%s: " % text)
42             if position in positions:
43                 widget.set_edit_text(','.join(positions[position].keys()))
44             else:
45                 widget.set_edit_text('')
46             self.position_widgets[position] = widget
47             self.widgets.append(widget)
48
49     def parse(self, entry):
50         if len(entry) == 0:
51             return []
52         return entry.split(',')
53
54     def check(self):
55         self.state['positions'] = {}
56         for (position, widget) in self.position_widgets.iteritems():
57             self.state['positions'][position] = \
58                 self.parse(widget.get_edit_text())
59             for p in self.state['positions'][position]:
60                 if members.get(p) == None:
61                     self.focus_widget(widget)
62                     set_status( "Invalid username: '%s'" % p )
63                     return True
64         clear_status()
65
66 class EndPage(WizardPanel):
67     def init_widgets(self):
68         old = members.list_positions()
69         self.headtext = urwid.Text("")
70         self.midtext = urwid.Text("")
71
72         self.widgets = [
73             self.headtext,
74             urwid.Divider(),
75             self.midtext,
76         ]
77     def focusable(self):
78         return False
79     def activate(self):
80         failed = []
81         for (position, info) in self.state['positions'].iteritems():
82             try:
83                 members.set_position(position, info)
84             except ldap.LDAPError:
85                 failed.append(position)
86         if len(failed) == 0:
87             self.headtext.set_text("Positions Updated")
88             self.midtext.set_text("Congratulations, positions have been "
89                 "updated. You should rebuild the website in order to update "
90                 "the Positions page.")
91         else:
92             self.headtext.set_text("Positions Results")
93             self.midtext.set_text("Failed to update the following positions: "
94                 "%s." % join(failed))
95     def check(self):
96         pop_window()