Merge branch 'master' of /users/git/public/pyceo
[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 ClubUserIntroPage(IntroPage):
18     def init_widgets(self):
19         self.widgets = [
20             urwid.Text( "Renewing Club User Account" ),
21             urwid.Divider(),
22             urwid.Text( "In order for clubs to maintain websites hosted by "
23                         "the Computer Science Club, they need access to our "
24                         "machines. We grant accounts to club users at no charge "
25                         "in order to provide this access. Registering a user "
26                         "in this way grants one term of free access to our "
27                         "machines, without any other membership privileges "
28                         "(they can't vote, hold office, etc). If such a user "
29                         "decides to join, use the Renew Membership option." )
30         ]
31
32 class UserPage(WizardPanel):
33     def init_widgets(self):
34         self.userid = LdapWordEdit(csclub_uri, csclub_base, 'uid',
35             "Username: ")
36
37         self.widgets = [
38             urwid.Text( "Member Information" ),
39             urwid.Divider(),
40             self.userid,
41         ]
42     def check(self):
43         self.state['userid'] = self.userid.get_edit_text()
44         self.state['member'] = None
45         if self.state['userid']:
46             self.state['member'] = members.get(self.userid.get_edit_text())
47         if not self.state['member']:
48             set_status("Member not found")
49             self.focus_widget(self.userid)
50             return True
51
52 class TermPage(WizardPanel):
53     def __init__(self, state, utype='member'):
54         self.utype = utype
55         WizardPanel.__init__(self, state)
56     def init_widgets(self):
57         self.start = SingleEdit("Start: ")
58         self.count = SingleIntEdit("Count: ")
59
60         self.widgets = [
61             urwid.Text( "Terms to Register" ),
62             urwid.Divider(),
63             self.start,
64             self.count,
65         ]
66     def activate(self):
67         if not self.start.get_edit_text():
68             self.terms = self.state['member'].get('term', [])
69             self.nmterms = self.state['member'].get('nonMemberTerm', [])
70
71             if self.utype == 'member':
72                 self.start.set_edit_text( terms.next_unregistered( self.terms ) )
73             else:
74                 self.start.set_edit_text( terms.next_unregistered( self.terms + self.nmterms ) )
75
76             self.count.set_edit_text( "1" )
77     def check(self):
78         try:
79             self.state['terms'] = terms.interval( self.start.get_edit_text(), self.count.value() )
80         except Exception, e:
81             self.focus_widget( self.start )
82             set_status( "Invalid start term" )
83             return True
84         for term in self.state['terms']:
85             if self.utype == 'member':
86                 already = term in self.terms
87             else:
88                 already = term in self.terms or term in self.nmterms
89
90             if already:
91                 self.focus_widget( self.start )
92                 set_status( "Already registered for " + term )
93                 return True
94         if len(self.state['terms']) == 0:
95             self.focus_widget(self.count)
96             set_status( "Registering for zero terms?" )
97             return True
98
99 class PayPage(WizardPanel):
100     def init_widgets(self):
101         self.midtext = urwid.Text("")
102
103         self.widgets = [
104             urwid.Text("Membership Fee"),
105             urwid.Divider(),
106             self.midtext,
107         ]
108     def focusable(self):
109         return False
110     def activate(self):
111         regterms = self.state['terms']
112         plural = "term"
113         if len(self.state['terms']) > 1:
114             plural = "terms"
115         self.midtext.set_text("You are registering for %d %s, and owe the "
116                        "Computer Science Club $%d.00 in membership fees. "
117                        "Please deposit the money in the safe before "
118                        "continuing. " % ( len(regterms), plural, len(regterms * 2)))
119
120 class EndPage(WizardPanel):
121     def __init__(self, state, utype='member'):
122         self.utype = utype
123         WizardPanel.__init__(self, state)
124     def init_widgets(self):
125         self.headtext = urwid.Text("")
126         self.midtext = urwid.Text("")
127
128         self.widgets = [
129             self.headtext,
130             urwid.Divider(),
131             self.midtext,
132         ]
133     def focusable(self):
134         return False
135     def activate(self):
136         problem = None
137         try:
138             self.headtext.set_text("Registration Succeeded")
139             if self.utype == 'member':
140                 members.register( self.state['userid'], self.state['terms'] )
141                 self.midtext.set_text("The member has been registered for the following "
142                                  "terms: " + ", ".join(self.state['terms']) + ".")
143             else:
144                 members.register_nonmember( self.state['userid'], self.state['terms'] )
145                 self.midtext.set_text("The club user has been registered for the following "
146                                  "terms: " + ", ".join(self.state['terms']) + ".")
147         except ldap.LDAPError, e:
148             problem = ldapi.format_ldaperror(e)
149         except members.MemberException, e:
150             problem = str(e)
151         if problem:
152             self.headtext.set_text("Failed to Register")
153             self.midtext.set_text("You may refund any fees paid or retry. "
154                              "The error was:\n\n%s" % problem)
155
156     def check(self):
157         pop_window()