Checkout and check-in works!
[public/pyceo-broken.git] / ceo / urwid / library.py
1 import urwid
2 from ceo import members
3 from ceo.urwid import search
4 from ceo.urwid.widgets import *
5 from ceo.urwid.window import *
6 from sqlobject.sqlbuilder import *
7 from datetime import datetime
8
9 import ceo.library as lib
10
11
12
13 def library(data):
14     menu = make_menu([
15         ("Checkout Book", checkout_book, None),
16         ("Return Book", return_book, None),
17 #        ("Search Books", search_books, None),
18 #        ("Add Book", add_book, None),
19         ("Back", raise_back, None),
20     ])
21     push_window(menu, "Library")
22
23 def search_books(data):
24     menu = make_menu([
25         ("Overdue Books", overdue_books, None),
26     ])
27     push_window(menu, "Book Search")
28
29 def overdue_books(data):
30     None
31
32 def checkout_book(data):
33     push_wizard("Checkout", [CheckoutPage, BookSearchPage, ConfirmPage])
34
35 def return_book(data):
36     push_wizard("Checkout", [CheckinPage, ConfirmPage])
37
38 class BookSearchPage(WizardPanel):
39     def init_widgets(self):
40         self.search = None
41         self.state["book"] = None
42         self.isbn = SingleEdit("ISBN: ")
43         self.title = SingleEdit("Title: ")
44
45         self.widgets = [
46             urwid.Text("Book Search"),
47             urwid.Text("(Only one field required.)"),
48             urwid.Divider(),
49             self.isbn,
50             self.title
51         ]
52
53     def check(self):
54         if self.state["book"] is None:
55             push_window(SearchPage(self.isbn.get_edit_text(),
56                                    self.title.get_edit_text(),
57                                    None,
58                                    self.state))
59             return True
60         else:
61             return False
62         
63
64 class CheckoutPage(WizardPanel):
65     def init_widgets(self):
66         self.state["user"] = "ERROR"
67         self.state["task"] = "sign_out"
68         self.user = SingleEdit("Username: ")
69         
70         self.widgets = [
71             urwid.Text("Book Checkout"),
72             urwid.Divider(),
73             self.user,
74         ]
75
76     def check(self):
77         self.state['user'] = self.user.get_edit_text()
78
79 class ConfirmPage(WizardPanel):
80     def init_widgets(self):
81         self.user = urwid.Text("Username: ")
82         self.book = urwid.Text("Book: ")
83
84         title = ""
85         if self.state["task"] and self.state["task"]=="sign_in":
86             title = "Checkin"
87         else:
88             title = "Checkout"
89
90         self.widgets = [
91             urwid.Text("Confirm " + title),
92             urwid.Divider(),
93             self.user,
94             self.book
95         ]
96
97     def activate(self):
98         self.user.set_text("Username: " + self.state["user"])
99         if self.state["book"]:
100             self.book.set_text("Book: " + self.state["book"].title)
101
102     def check(self):
103         #TODO: Validate user at some point (preferrably user entry screen)
104         if self.state["task"] and self.state["task"]=="sign_in":
105             self.state["book"].sign_in(self.state["user"])
106         else:
107             self.state["book"].sign_out(self.state["user"])
108         pop_window()
109
110         
111 class SearchPage(urwid.WidgetWrap):
112     def __init__(self, isbn, title, user, state):
113         self.state = state
114         books = []
115         widgets = []
116         if not title is None and not title=="":
117             books = lib.Book.select(LIKE(lib.Book.q.title, "%" + title + "%"))
118         elif not isbn is None and not isbn=="":
119             books = lib.Book.select(lib.Book.q.isbn==isbn)
120         elif not user is None and not user=="":
121             st = lib.Signout.select(AND(lib.Signout.q.username==user, lib.Signout.q.indate==None))
122             for s in st:
123                 books.append(s.book)
124
125         for b in books:
126             widgets.append(urwid.AttrWrap(ButtonText(self.select, b, str(b)),
127                                           None, 'selected'))
128             widgets.append(urwid.Divider())
129
130         urwid.WidgetWrap.__init__(self, urwid.ListBox(widgets))
131
132     def select(self, book):
133         self.state["book"] = book
134         pop_window()
135
136 class CheckinPage(WizardPanel):
137     def init_widgets(self):
138         self.state["book"] = None
139         self.state["user"] = "ERROR"
140         self.state["task"] = "sign_in"
141         self.user = SingleEdit("Username: ")
142         
143         self.widgets = [
144             urwid.Text("Book Checkin"),
145             urwid.Divider(),
146             self.user,
147         ]
148
149     def check(self):
150         if self.state["book"] is None:
151             push_window(SearchPage(None,
152                                    None,
153                                    self.user.get_edit_text(),
154                                    self.state))
155             return True
156         else:
157             self.state["user"] = self.user.get_edit_text()
158             return False