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