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