checkout success/failure for integrity error, and database move
[public/library.git] / librarian
1 #!/usr/bin/env python3
2
3 import curses
4 import library.database as db
5 import library.interface.browser as browser
6 import library.interface.form as form
7 import library.interface.help_bar as helpBar
8 import library.interface.checkout as co
9
10 from  library import book_data
11
12
13
14 stdscr=0
15 hb=0
16
17 menu_commands = [(' q','quit')]
18
19 def menutest(s, l):
20     global stdscr
21     global hb
22     stdscr=s
23     curses.curs_set(0)
24     (rows,cols)=stdscr.getmaxyx()
25     # set the default for the browser windows
26     browser.browserWindow._default_height = rows-10
27     browser.browserWindow._default_width = cols-10
28     bar = curses.newwin(1,cols-2,rows-1,1)
29     hb = helpBar.helpBar(bar)
30     hb.command=menu_commands
31     hb.refresh()
32     w = curses.newwin(15,40,(rows-10)//2, (cols-40)//2)
33
34     menu(w, l)
35     
36     curses.curs_set(1)
37
38 # item is a list of (string, callable) tuples
39 def menu(w, items):
40     w.keypad(1)
41     highlight=0
42     redrawMenu(w,items,highlight)
43
44     w.refresh()
45     ch=w.getch()
46     while (ch!=113 and ch!=27): # leave on q or ESC
47         if ch==curses.KEY_UP or ch==107 or ch==16:
48             if highlight!=0:
49                 w.chgat(highlight,0, 0)
50                 highlight -= 1
51                 while(items[highlight][0]==""):
52                     highlight -=1
53                 w.chgat(highlight,0, curses.A_REVERSE)
54         if ch==curses.KEY_DOWN or ch==106 or ch==14:
55             if highlight!=len(items)-1:
56                 w.chgat(highlight,0, 0)
57                 highlight += 1
58                 while(items[highlight][0]==""):
59                     highlight +=1
60                 w.chgat(highlight,0, curses.A_REVERSE)
61         if ch==curses.KEY_PPAGE:
62             w.chgat(highlight,0, 0)
63             highlight = 0
64             w.chgat(highlight,0, curses.A_REVERSE)
65         if ch==curses.KEY_NPAGE:
66             w.chgat(highlight,0, 0)
67             highlight = len(items)-1
68             w.chgat(highlight,0, curses.A_REVERSE)
69         if ch==114 or ch==10:
70             (s,f)=items[highlight]
71             f()
72             redrawMenu(w,items,highlight)
73         w.refresh()
74         ch = w.getch()
75
76 def redrawMenu(w,items,highlight):
77     i=0
78     for (mitem,fun) in items:
79         w.addstr(i,0, mitem)
80         i +=1
81     w.chgat(highlight, 0, curses.A_REVERSE)
82     w.refresh()
83     hb.commands=menu_commands
84     hb.refresh()
85
86
87 def addForm():
88     w=curses.newwin(1,1)
89     (my,mx)=stdscr.getmaxyx()
90     bf = form.BookForm(w,hb,width=mx-20)
91     (r,c)=w.getmaxyx()
92     w.mvwin((my-r)//2,(mx-c)//2)
93     bf.lookup_isbn=book_data.openLibrary_isbn
94     bf.lookup_lccn=book_data.openLibrary_lccn
95     bf.caption='Add a Book'
96     bf.blabel = 'Add'
97     book = bf.event_loop()
98     bf.clear()
99     if len(book)!=0:
100         db.addBook(book)
101
102 def browseMenu():
103     w=curses.newwin(3,5)
104     b = browser.bookBrowser(w,hb)
105     (r,c) = w.getmaxyx()
106     (my,mx)=stdscr.getmaxyx()
107     w.mvwin((my-r)//2 -2, (mx-c)//2)
108     b.refreshBooks()
109     b.eventLoop()
110     b.clear()
111
112 def trashMenu():
113     w=curses.newwin(3,5)
114     b = browser.trashBrowser(w,hb)
115     (r,c) = w.getmaxyx()
116     (my,mx)=stdscr.getmaxyx()
117     w.mvwin((my-r)//2 -2, (mx-c)//2)
118     b.refreshBooks()
119     b.eventLoop()
120     b.clear()
121
122 def checkedout_menu():
123     w=curses.newwin(3,5)
124     b = browser.bookBrowser(w,hb)
125     (r,c) = w.getmaxyx()
126     (my,mx)=stdscr.getmaxyx()
127     w.mvwin((my-r)//2 -2, (mx-c)//2)
128     b.load_data(db.get_checkedout_books())
129     b.columnDefs = [("id",0,3),
130                     ("uwid",0,8),
131                     ("date",0,10),
132                     ("title",100,None)]
133     b.calcColWidths()
134     b.eventLoop()
135     b.clear()
136
137 def onshelf_menu():
138     w=curses.newwin(3,5)
139     b = browser.bookBrowser(w,hb)
140     (r,c) = w.getmaxyx()
141     (my,mx)=stdscr.getmaxyx()
142     w.mvwin((my-r)//2 -2, (mx-c)//2)
143     b.load_data(db.get_onshelf_books())
144     b.eventLoop()
145     b.clear()
146
147 def co_menu():
148     w=curses.newwin(1,1)
149     (my,mx)=stdscr.getmaxyx()
150     co.checkout_procedure(w,hb,my//2,mx//2,mx) 
151
152 def return_menu():
153     w=curses.newwin(1,1)
154     (my,mx)=stdscr.getmaxyx()
155     co.return_procedure(w,hb,my//2,mx//2,mx) 
156
157 def catMenu():
158     (my,mx)=stdscr.getmaxyx()
159     w=curses.newwin(3,5)
160     cat = browser.categoryBrowser(w,hb, 10,40)
161     (r,c) = w.getmaxyx()
162     w.mvwin((my-r)//2 -2, (mx-c)//2)
163     cat.refreshCategories()
164     cat.sortByColumn('category')
165     cat.eventLoop()
166     cat.clear()
167
168
169 if __name__ == "__main__":
170     db.initializeDatabase()
171     m = [("Browse Library", browseMenu),
172          ("Add Book", addForm),
173          ("Categories", catMenu),
174          ("View Trash", trashMenu),
175          ("",exit),
176          ("Check Out a Book", co_menu),
177          ("Return a Book", return_menu),
178          ("",exit),
179          ("View Checked Out Books", checkedout_menu),
180          ("View On Shelf Books", onshelf_menu),
181          ("",exit),
182          ("Exit", exit)]
183     curses.wrapper(menutest, m)
184
185