Add tab completion for userid fields
[public/pyceo-broken.git] / ceo / urwid / renew.py
1 import urwid, ldap
2 from ceo import members, terms, ldapi
3 from ceo.urwid.widgets import *
4 from ceo.urwid.window import *
5
6 class IntroPage(WizardPanel):
7     def init_widgets(self):
8         self.widgets = [
9             urwid.Text( "Renewing Membership" ),
10             urwid.Divider(),
11             urwid.Text( "CSC membership is $2.00 per term. You may pre-register "
12                         "for future terms if desired." )
13         ]
14     def focusable(self):
15         return False
16
17 class UserPage(WizardPanel):
18     def init_widgets(self):
19         self.userid = LdapWordEdit(csclub_uri, csclub_base, 'uid',
20             "Username: ")
21
22         self.widgets = [
23             urwid.Text( "Member Information" ),
24             urwid.Divider(),
25             self.userid,
26         ]
27     def check(self):
28         self.state['userid'] = self.userid.get_edit_text()
29         self.state['member'] = None
30         if self.state['userid']:
31             self.state['member'] = members.get(self.userid.get_edit_text())
32         if not self.state['member']:
33             set_status("Member not found")
34             self.focus_widget(self.userid)
35             return True
36
37 class TermPage(WizardPanel):
38     def init_widgets(self):
39         self.start = SingleEdit("Start: ")
40         self.count = SingleIntEdit("Count: ")
41
42         self.widgets = [
43             urwid.Text( "Terms to Register" ),
44             urwid.Divider(),
45             self.start,
46             self.count,
47         ]
48     def activate(self):
49         if not self.start.get_edit_text():
50             old_terms = []
51             if 'term' in self.state['member']:
52                 old_terms = self.state['member']['term']
53             self.start.set_edit_text( terms.next_unregistered( old_terms ) )
54             self.count.set_edit_text( "1" )
55     def check(self):
56         try:
57             self.state['terms'] = terms.interval( self.start.get_edit_text(), self.count.value() )
58         except Exception, e:
59             self.focus_widget( self.start )
60             set_status( "Invalid start term" )
61             return True
62         for term in self.state['terms']:
63             if members.registered( self.state['userid'], term):
64                 self.focus_widget( self.start )
65                 set_status( "Already registered for " + term )
66                 return True
67         if len(self.state['terms']) == 0:
68             self.focus_widget(self.count)
69             set_status( "Registering for zero terms?" )
70             return True
71
72 class PayPage(WizardPanel):
73     def init_widgets(self):
74         self.midtext = urwid.Text("")
75
76         self.widgets = [
77             urwid.Text("Membership Fee"),
78             urwid.Divider(),
79             self.midtext,
80         ]
81     def focusable(self):
82         return False
83     def activate(self):
84         regterms = self.state['terms']
85         plural = "term"
86         if len(self.state['terms']) > 1:
87             plural = "terms"
88         self.midtext.set_text("You are registering for %d %s, and owe the "
89                        "Computer Science Club $%d.00 in membership fees. "
90                        "Please deposit the money in the safe before "
91                        "continuing. " % ( len(regterms), plural, len(regterms * 2)))
92
93 class EndPage(WizardPanel):
94     def init_widgets(self):
95         self.headtext = urwid.Text("")
96         self.midtext = urwid.Text("")
97
98         self.widgets = [
99             self.headtext,
100             urwid.Divider(),
101             self.midtext,
102         ]
103     def focusable(self):
104         return False
105     def activate(self):
106         problem = None
107         try:
108             members.register( self.state['userid'], self.state['terms'] )
109             self.headtext.set_text("Registration Succeeded")
110             self.midtext.set_text("The member has been registered for the following "
111                              "terms: " + ", ".join(self.state['terms']) + ".")
112         except ldap.LDAPError, e:
113             problem = ldapi.format_ldaperror(e)
114         except members.MemberException, e:
115             problem = str(e)
116         if problem:
117             self.headtext.set_text("Failed to Register")
118             self.midtext.set_text("You may refund any fees paid or retry. "
119                              "The error was:\n\n%s" % problem)
120
121     def check(self):
122         pop_window()