Add create club account menu item
[public/pyceo-broken.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 ClubIntroPage(WizardPanel):
20     def init_widgets(self):
21         self.widgets = [
22             urwid.Text( "Club Services" ),
23             urwid.Divider(),
24             urwid.Text( "We provide other UW clubs accounts for email and "
25                         "web hosting, free of charge. Like members, clubs "
26                         "get web hosting at %s. We can also arrange for "
27                         "uwaterloo.ca subdomains; please instruct the club "
28                         "representative to contact the systems committee "
29                         "for more information."
30                         "\n\nNote: This is not complete. Authorizing members "
31                         "to access the club account still requires manual "
32                         "intervention."
33                         % "http://csclub.uwaterloo.ca/~clubid/"
34             )
35         ]
36     def focusable(self):
37         return False
38
39 class InfoPage(WizardPanel):
40     def init_widgets(self):
41         self.userid = WordEdit("UWdir ID: ")
42         self.name = SingleEdit("Full name: ")
43         self.program = SingleEdit("Program of Study: ")
44         self.widgets = [
45             urwid.Text( "Member Information - Please Check ID" ),
46             urwid.Divider(),
47             self.userid,
48             self.name,
49             self.program,
50         ]
51     def check(self):
52         self.state['userid'] = self.userid.get_edit_text()
53         self.state['name'] = self.name.get_edit_text()
54         self.state['program'] = self.program.get_edit_text()
55
56         if len( self.state['userid'] ) < 4:
57             self.focus_widget( self.userid )
58             set_status("Username is too short")
59             return True
60         elif len( self.state['name'] ) < 4:
61             self.focus_widget( self.name )
62             set_status("Name is too short")
63             return True
64         clear_status()
65
66 class ClubInfoPage(WizardPanel):
67     def init_widgets(self):
68         self.userid = WordEdit("Username: ")
69         self.name = SingleEdit("Club Name: ")
70         self.widgets = [
71             urwid.Text( "Club Information" ),
72             urwid.Divider(),
73             self.userid,
74             self.name,
75         ]
76     def check(self):
77         self.state['userid'] = self.userid.get_edit_text()
78         self.state['name'] = self.name.get_edit_text()
79
80         if len( self.state['userid'] ) < 3:
81             self.focus_widget( self.userid )
82             set_status("Username is too short")
83             return True
84         elif len( self.state['name'] ) < 4:
85             self.focus_widget( self.name )
86             set_status("Name is too short")
87             return True
88         clear_status()
89
90 class SignPage(WizardPanel):
91     def init_widgets(self):
92         self.widgets = [
93             urwid.Text( "Machine Usage Policy" ),
94             urwid.Divider(),
95             urwid.Text( "Ensure the new member has signed the "
96                         "Machine Usage Policy. Accounts of users who have not "
97                         "signed will be suspended if discovered." ),
98         ]
99     def focusable(self):
100         return False
101
102 class PassPage(WizardPanel):
103     def init_widgets(self):
104         self.password = PassEdit("Password: ")
105         self.pwcheck = PassEdit("Re-enter: ")
106         self.widgets = [
107             urwid.Text( "Member Password" ),
108             urwid.Divider(),
109             self.password,
110             self.pwcheck,
111         ]
112     def focus_widget(self, widget):
113         self.box.set_focus( self.widgets.index( widget ) )
114     def clear_password(self):
115         self.focus_widget( self.password )
116         self.password.set_edit_text("")
117         self.pwcheck.set_edit_text("")
118     def check(self):
119         self.state['password'] = self.password.get_edit_text()
120         pwcheck = self.pwcheck.get_edit_text()
121
122         if self.state['password'] != pwcheck:
123             self.clear_password()
124             set_status("Passwords do not match")
125             return True
126         elif len(self.state['password']) < 5:
127             self.clear_password()
128             set_status("Password is too short")
129             return True
130         clear_status()
131
132 class EndPage(WizardPanel):
133     def __init__(self, state, utype='member'):
134         self.type = utype
135         WizardPanel.__init__(self, state)
136     def init_widgets(self):
137         self.headtext = urwid.Text("")
138         self.midtext = urwid.Text("")
139         self.widgets = [
140             self.headtext,
141             urwid.Divider(),
142             self.midtext,
143         ]
144     def focusable(self):
145         return False
146     def check(self):
147         pop_window()
148     def activate(self):
149         problem = None
150         if self.type == 'member':
151             try:
152                 if not members.connected(): members.connect()
153                 members.new( self.state['userid'], self.state['name'], self.state['program'] )
154             except members.InvalidRealName:
155                 problem = "Invalid real name"
156             except InvalidArgument, e:
157                 if e.argname == 'uid' and e.explanation == 'duplicate uid':
158                     problem = 'Duplicate userid'
159                 else:
160                     raise
161         if not problem:
162             try:
163                 if not accounts.connected(): accounts.connect()
164                 if self.type == 'member':
165                     accounts.create_member( self.state['userid'], self.state['password'], self.state['name'] )
166                 elif self.type == 'club':
167                     accounts.create_club( self.state['userid'], self.state['name'] )
168                 else:
169                     raise Exception("Internal Error")
170             except accounts.NameConflict, e:
171                 problem = str(e)
172             except accounts.NoAvailableIDs, e:
173                 problem = str(e)
174             except accounts.InvalidArgument, e:
175                 problem = str(e)
176             except accounts.LDAPException, e:
177                 problem = str(e)
178             except accounts.KrbException, e:
179                 problem = str(e)
180         if problem:
181             self.headtext.set_text("Failed to add user")
182             self.midtext.set_text("The error was: '%s'" % problem)
183         else:
184             self.headtext.set_text("User Added")
185             self.midtext.set_text("Congratulations, %s has been added "
186                 "successfully. Please run 'addhomedir %s'."
187                 % (self.state['userid'], self.state['userid']))