-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
188 lines (167 loc) · 5.47 KB
/
backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#! /usr/bin/python
import peewee as pw
from smartLibrary import *
import zbar
from sys import argv #only for barcod scaner or cam (will be removed probably)
def read_barcode():
'''
Read barcode from sorce and return barcode data
Still neds optimization. try:,
'''
try:
proc = zbar.Processor()
proc.parse_config('enable')
device = '/dev/video0'
if len(argv) > 1:
device = argv[1]
proc.init(device)
proc.visible = True
proc.process_one()
proc.visible = False
except:
print "reading barcode:" + bcolors.F + " FAIL " + bcolors.E
return False #returns False, that means error
for symbol in proc.results:
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data #just for debuging
return symbol.data
def generate_barcode():
'''
Generate barcode that doesn't exsist in the database
problem is if the book has the barcode that you generated before
'''
pass
def add_book():
'''
Add book adds book in the library with author that you select or
create new avtor; problably just for the terminal not gui
'''
print "add book to library:"
book_name = raw_input("Book Name: ")
print "authors:"
for aut in Author.select():
print aut.idauthor, aut.name, aut.lastname
arg = raw_input("press n or author id number: ")
if arg == "n":
book_author = add_author(True)
else:
book_author = arg
barcode = read_barcode()
if barcode != False:
nbook = Books.create(name= book_name, author= book_author, barcode=barcode)
else:
nbook = Books.create(name= book_name, author= book_author)
def change_book_name(book_name, new_book_name):
'''
change the name of the book, input book name, new book name
'''
for book in Books.select().where(Books.name==book_name):
book.name = new_book_name
book.save()
def change_bcc(bbc, new_bbc):
'''
Change barcode for the book
'''
for bc in Books.select().where(Books.barcode == bbc):
bc.barcode = new_bbc
bc.save()
def change_author (book_name, new_author): ## se ni koncano
'''
change author by book name (still not working; porbably have to change db
collum. Don't know if its better to have book name input or book id input?
'''
for book in Book.select().where(Book.name==book_name):
for author in Author.select().where(Author.idauthor==book.author):
book.author = new_author
book.seve()
def add_author(x=False):
'''
adds author to db (only for terminal script) if input is true returns id of
author
'''
print "add author to library"
author_name = raw_input("author name: ")
author_last = raw_input("author last name: ")
naut = Author.create(name= author_name, lastname= author_last)
if x == True:
return naut.idauthor
def print_book(bn):
'''
print book with book name
'''
for book in Books.select().where(Books.name==bn):
for author in Author.select().where(Author.idauthor == book.author):
if book.name != 'none':
return "fuck you" #, author.name, author.lastname
else: return "fuck you to"
def print_books():
'''
print all books in the library
'''
print "all books in library:"
for book in Books.select():
for author in Author.select().where(Author.idauthor == book.author):
print book.name, '- ', author.name, author.lastname
def print_barcode():
'''
Prints books that have that barcode
'''
barcode = read_barcode()
if barcode == False:
pass
else:
for book in Books.select().where(Books.barcode == barcode):
for author in Author.select().where(Author.idauthor == book.author):
print book.name, '-', author.name, author.lastname
def print_by_author():
'''
prints author, you select author and it prits all the books written by him.
change: input author id or name/lastname and print (return) all the books
'''
print "authors:"
for author in Author.select():
print author.idauthor, author.name, author.lastname
arg = raw_input("pick author: ")
for author in Author.select().where(Author.idauthor == arg):
print author.name, author.lastname, ':'
for book in Books.select().where(Books.author == arg):
print ' ', book.name
def usage():
'''
no need to put info here :)
'''
print "Usage: python backend.py [path to camera or barcode reader]"
print " b Addd book"
print " a Add author"
print " p Print books"
print " pa Print by author"
print " h Help"
print " s Stop program"
print " r Read fom barcode reader "
def main(x = False):
if x == False:
print "Backand scrpit for library"
check_conn()
usage()
stop = False
while (stop != True):
arg = raw_input("> ")
if arg == "b":
add_book()
print "book added"
elif arg == "a":
add_author()
print "Author added"
elif arg == 'p':
print_books()
elif arg == 'r':
print_barcode()
elif arg == 's':
stop = True
elif arg == 'h':
usage()
elif arg == 'pa':
print_by_author()
else:
usage()
if __name__ == '__main__':
main()