Remove ternary operators
[mspang/pyceo.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 EmailPage(WizardPanel):
53     def init_widgets(self):
54         self.email = SingleEdit("Email: ")
55
56         self.widgets = [
57             urwid.Text( "Mail Forwarding" ),
58             urwid.Divider(),
59             urwid.Text("Please ensure the forwarding address for your CSC "
60                 "email is up to date. You may leave this blank if you do not "
61                 "want your CSC email forwarded, and intend to log in "
62                 "regularly to check it."),
63             urwid.Divider(),
64             urwid.Text("Warning: Changing this overwrites ~/.forward"),
65             urwid.Divider(),
66             self.email,
67         ]
68     def activate(self):
69         cfwd = members.current_email(self.state['userid'])
70         if cfwd:
71             self.state['old_forward'] = cfwd
72         else:
73             self.state['old_forward'] = ''
74         self.email.set_edit_text(self.state['old_forward'])
75     def check(self):
76         fwd = self.email.get_edit_text().strip().lower()
77         if fwd:
78             msg = members.check_email(fwd)
79             if msg:
80                 set_status(msg)
81                 return True
82             if fwd == '%s@csclub.uwaterloo.ca' % self.state['userid']:
83                 set_status('You cannot forward your address to itself. Leave it blank to disable forwarding.')
84                 return True
85         self.state['new_forward'] = fwd
86
87 class EmailDonePage(WizardPanel):
88     def init_widgets(self):
89         self.status = urwid.Text("")
90         self.widgets = [
91             urwid.Text("Mail Forwarding"),
92             urwid.Divider(),
93             self.status,
94         ]
95     def focusable(self):
96         return False
97     def activate(self):
98         if self.state['old_forward'] == self.state['new_forward']:
99             if self.state['old_forward']:
100                 self.status.set_text(
101                     'You have chosen to leave your forwarding address '
102                     'as %s. Make sure to check this email for updates '
103                     'from the CSC.' % self.state['old_forward'])
104             else:
105                 self.status.set_text(
106                     'You have chosen not to set a forwarding address. '
107                     'Please check your CSC email regularly (via IMAP, POP, or locally) '
108                     'for updates from the CSC.'
109                     '\n\n'
110                     'Note: If you do have a ~/.forward, we were not able to read it or '
111                     'it was not a single email address. Do not worry, we have left it '
112                     'as is.')
113         else:
114             try:
115                 msg = members.change_email(self.state['userid'], self.state['new_forward'])
116                 if msg:
117                     self.status.set_text("Errors occured updating your forwarding address:"
118                                          "\n\n%s" % msg)
119                 else:
120                     if self.state['new_forward']:
121                         self.status.set_text(
122                             'Your email forwarding address has been successfully set '
123                             'to %s. Test it out by emailing %s@csclub.uwaterloo.ca and '
124                             'making sure you receive it at your forwarding address.'
125                             % (self.state['new_forward'], self.state['userid']))
126                     else:
127                         self.status.set_text(
128                             'Your email forwarding address has been successfully cleared. '
129                             'Please check your CSC email regularly (via IMAP, POP, or locally) '
130                             'for updates from the CSC.')
131             except Exception, e:
132                 self.status.set_text(
133                     'An exception occured updating your email:\n\n%s' % e)
134
135 class TermPage(WizardPanel):
136     def __init__(self, state, utype='member'):
137         self.utype = utype
138         WizardPanel.__init__(self, state)
139     def init_widgets(self):
140         self.start = SingleEdit("Start: ")
141         self.count = SingleIntEdit("Count: ")
142
143         self.widgets = [
144             urwid.Text( "Terms to Register" ),
145             urwid.Divider(),
146             self.start,
147             self.count,
148         ]
149     def activate(self):
150         if not self.start.get_edit_text():
151             self.terms = self.state['member'].get('term', [])
152             self.nmterms = self.state['member'].get('nonMemberTerm', [])
153
154             if self.utype == 'member':
155                 self.start.set_edit_text( terms.next_unregistered( self.terms ) )
156             else:
157                 self.start.set_edit_text( terms.next_unregistered( self.terms + self.nmterms ) )
158
159             self.count.set_edit_text( "1" )
160     def check(self):
161         try:
162             self.state['terms'] = terms.interval( self.start.get_edit_text(), self.count.value() )
163         except Exception, e:
164             self.focus_widget( self.start )
165             set_status( "Invalid start term" )
166             return True
167         for term in self.state['terms']:
168             if self.utype == 'member':
169                 already = term in self.terms
170             else:
171                 already = term in self.terms or term in self.nmterms
172
173             if already:
174                 self.focus_widget( self.start )
175                 set_status( "Already registered for " + term )
176                 return True
177         if len(self.state['terms']) == 0:
178             self.focus_widget(self.count)
179             set_status( "Registering for zero terms?" )
180             return True
181
182 class PayPage(WizardPanel):
183     def init_widgets(self):
184         self.midtext = urwid.Text("")
185
186         self.widgets = [
187             urwid.Text("Membership Fee"),
188             urwid.Divider(),
189             self.midtext,
190         ]
191     def focusable(self):
192         return False
193     def activate(self):
194         regterms = self.state['terms']
195         plural = "term"
196         if len(self.state['terms']) > 1:
197             plural = "terms"
198         self.midtext.set_text("You are registering for %d %s, and owe the "
199                        "Computer Science Club $%d.00 in membership fees. "
200                        "Please deposit the money in the safe before "
201                        "continuing. " % ( len(regterms), plural, len(regterms * 2)))
202
203 class EndPage(WizardPanel):
204     def __init__(self, state, utype='member'):
205         self.utype = utype
206         WizardPanel.__init__(self, state)
207     def init_widgets(self):
208         self.headtext = urwid.Text("")
209         self.midtext = urwid.Text("")
210
211         self.widgets = [
212             self.headtext,
213             urwid.Divider(),
214             self.midtext,
215         ]
216     def focusable(self):
217         return False
218     def activate(self):
219         problem = None
220         try:
221             self.headtext.set_text("Registration Succeeded")
222             if self.utype == 'member':
223                 members.register( self.state['userid'], self.state['terms'] )
224                 self.midtext.set_text("The member has been registered for the following "
225                                  "terms: " + ", ".join(self.state['terms']) + ".")
226             else:
227                 members.register_nonmember( self.state['userid'], self.state['terms'] )
228                 self.midtext.set_text("The club user has been registered for the following "
229                                  "terms: " + ", ".join(self.state['terms']) + ".")
230         except ldap.LDAPError, e:
231             problem = ldapi.format_ldaperror(e)
232         except members.MemberException, e:
233             problem = str(e)
234         if problem:
235             self.headtext.set_text("Failed to Register")
236             self.midtext.set_text("You may refund any fees paid or retry. "
237                              "The error was:\n\n%s" % problem)
238
239     def check(self):
240         pop_window()