Add experimental urwid-based GUI
[mspang/pyceo.git] / pylib / csc / apps / urwid / newmember.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 class IntroPage(WizardPanel):
9     def init_widgets(self):
10         self.widgets = [
11             urwid.Text( "Joining the Computer Science Club" ),
12             urwid.Divider(),
13             urwid.Text( "CSC membership is $2.00 for one term. Please ensure "
14                         "the fee is deposited into the safe before continuing." ),
15         ]
16     def focusable(self):
17         return False
18
19 class InfoPage(WizardPanel):
20     def init_widgets(self):
21         self.userid = WordEdit("UWdir ID: ")
22         self.name = SingleEdit("Full name: ")
23         self.program = SingleEdit("Program of Study: ")
24         self.widgets = [
25             urwid.Text( "Member Information - Please Check ID" ),
26             urwid.Divider(),
27             self.userid,
28             self.name,
29             self.program,
30         ]
31     def check(self):
32         self.state['userid'] = self.userid.get_edit_text()
33         self.state['name'] = self.name.get_edit_text()
34         self.state['program'] = self.program.get_edit_text()
35
36         if len( self.state['userid'] ) < 4:
37             self.focus_widget( self.userid )
38             set_status("Username is too short")
39             return True
40         elif len( self.state['name'] ) < 4:
41             self.focus_widget( self.name )
42             set_status("Name is too short")
43             return True
44         clear_status()
45
46 class SignPage(WizardPanel):
47     def init_widgets(self):
48         self.widgets = [
49             urwid.Text( "Machine Usage Policy" ),
50             urwid.Divider(),
51             urwid.Text( "Ensure the new member has signed the "
52                         "Machine Usage Policy. Accounts of users who have not "
53                         "signed will be suspended if discovered." ),
54         ]
55     def focusable(self):
56         return False
57
58 class PassPage(WizardPanel):
59     def init_widgets(self):
60         self.password = PassEdit("Password: ")
61         self.pwcheck = PassEdit("Re-enter: ")
62         self.widgets = [
63             urwid.Text( "Member Password" ),
64             urwid.Divider(),
65             self.password,
66             self.pwcheck,
67         ]
68     def focus_widget(self, widget):
69         self.box.set_focus( self.widgets.index( widget ) )
70     def clear_password(self):
71         self.focus_widget( self.password )
72         self.password.set_edit_text("")
73         self.pwcheck.set_edit_text("")
74     def check(self):
75         self.state['password'] = self.password.get_edit_text()
76         pwcheck = self.pwcheck.get_edit_text()
77
78         if self.state['password'] != pwcheck:
79             self.clear_password()
80             set_status("Passwords do not match")
81             return True
82         elif len(self.state['password']) < 5:
83             self.clear_password()
84             set_status("Password is too short")
85             return True
86         clear_status()
87
88 class EndPage(WizardPanel):
89     def init_widgets(self):
90         self.headtext = urwid.Text("")
91         self.midtext = urwid.Text("")
92         self.widgets = [
93             self.headtext,
94             urwid.Divider(),
95             self.midtext,
96         ]
97     def focusable(self):
98         return False
99     def check(self):
100         pop_window()
101     def activate(self):
102         try:
103             if not members.connected(): members.connect()
104             members.new( self.state['userid'], self.state['name'], self.state['program'] )
105             problem = None
106         except members.InvalidRealName:
107             problem = "Invalid real name"
108         except InvalidArgument, e:
109             if e.argname == 'uid' and e.explanation == 'duplicate uid':
110                 problem = 'Duplicate userid'
111             else:
112                 raise
113         if not problem:
114             try:
115                 if not accounts.connected(): accounts.connect()
116                 accounts.create_member( self.state['userid'], self.state['password'], self.state['name'] )
117             except accounts.NameConflict, e:
118                 problem = str(e)
119             except accounts.NoAvailableIDs, e:
120                 problem = str(e)
121             except accounts.InvalidArgument, e:
122                 problem = str(e)
123             except accounts.LDAPException, e:
124                 problem = str(e)
125             except accounts.KrbException, e:
126                 problem = str(e)
127         if problem:
128             self.headtext.set_text("Failed to add member")
129             self.midtext.set_text("The error was: '%s'" % problem)
130         else:
131             self.headtext.set_text("Member Added")
132             self.midtext.set_text("Congratulations, %s has been added "
133                 "successfully. Please run 'addhomedir %s'."
134                 % (self.state['userid'], self.state['userid']))