Made it possible to view uncategorized books
[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 uncategorizedMenu():
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.refreshBooksUncategorized()
129     b.eventLoop()
130     b.clear()
131
132 def checkedout_menu():
133     w=curses.newwin(3,5)
134     b = browser.bookBrowser(w,hb)
135     (r,c) = w.getmaxyx()
136     (my,mx)=stdscr.getmaxyx()
137     w.mvwin((my-r)//2 -2, (mx-c)//2)
138     b.refreshBooksCheckedout()
139     b.columnDefs = [("id",0,3),
140                     ("uwid",0,8),
141                     ("date",0,10),
142                     ("title",100,None)]
143     b.calcColWidths()
144     b.eventLoop()
145     b.clear()
146
147 def onshelf_menu():
148     w=curses.newwin(3,5)
149     b = browser.bookBrowser(w,hb)
150     (r,c) = w.getmaxyx()
151     (my,mx)=stdscr.getmaxyx()
152     w.mvwin((my-r)//2 -2, (mx-c)//2)
153     b.refreshBooksOnshelf()
154     b.eventLoop()
155     b.clear()
156
157 def co_menu():
158     w=curses.newwin(1,1)
159     (my,mx)=stdscr.getmaxyx()
160     co.checkout_procedure(w,hb,my//2,mx//2,mx)
161
162 def return_menu():
163     w=curses.newwin(1,1)
164     (my,mx)=stdscr.getmaxyx()
165     co.return_procedure(w,hb,my//2,mx//2,mx)
166
167 def catMenu():
168     (my,mx)=stdscr.getmaxyx()
169     w=curses.newwin(3,5)
170     cat = browser.categoryBrowser(w,hb)
171     (r,c) = w.getmaxyx()
172     w.mvwin((my-r)//2 -2, (mx-c)//2)
173     cat.refreshCategories()
174     cat.sortByColumn('category')
175     cat.eventLoop()
176     cat.clear()
177
178
179 if __name__ == "__main__":
180     db.initializeDatabase()
181     m = [("Browse Library", browseMenu),
182          ("Add Book", addForm),
183          ("Categories", catMenu),
184          ("View Trash", trashMenu),
185          ("View Uncategorized Books", uncategorizedMenu),
186          ("",exit),
187          ("Check Out a Book", co_menu),
188          ("Return a Book", return_menu),
189          ("",exit),
190          ("View Checked Out Books", checkedout_menu),
191          ("View On Shelf Books", onshelf_menu),
192          ("",exit),
193          ("Exit", exit)]
194     curses.wrapper(menutest, m)
195
196