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