-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
226 lines (211 loc) · 7.37 KB
/
app.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# import models
# main menu - add, search, analysis, exit, view
#
# add books to db
# edit books
# search books
# data cleaning
# loop that runs program
#
from ast import Try
from models import (Base, session, Book, engine)
import datetime
import csv
import time
def menu():
while True:
print('''
\nPROGRAMMING BOOKS
\r1)Add Book
\r2)View All Books
\r3)Search For Book
\r4)Book Analysis
\r5)Exit'''
)
choice = input('What would you like to do? ')
if choice in ['1', '2', '3', '4', '5']:
return choice
else:
input ('''
\rPlease choose an option from those listed above.
\rA number 1-5.
\rPress enter to try again.
''')
def submenu():
while True:
print('''
\nPROGRAMMING BOOKS
\r1)Edit
\r2)Delete
\r3)Return to Main Menu'''
)
choice = input('What would you like to do? ')
if choice in ['1', '2', '3', '4', '5']:
return choice
else:
input('''
\rPlease choose an option from those listed above.
\rA number 1-5.
\rPress enter to try again.
''')
def edit_check(column_name, current_value):
print(f'\n****** EDIT {column_name} ******')
if column_name == 'Price':
print(f'\rCurrent Value: {current_value / 100}')
elif column_name == 'Published':
cr_date = datetime.datetime.strptime(current_value, '%Y-%m-%d')
print(f'\rCurrent Value: {cr_date.strftime("%B %d, %Y")}')
else:
print(f'\rCurrent Value: {current_value}')
if column_name == 'Published' or column_name == 'Price':
while True:
changes = input('What would you like to change the value to? ')
if column_name == 'Published':
changes = clean_date(changes)
if type(changes) == datetime.date:
return changes
elif column_name == 'Price':
changes = clean_price(changes)
if type(changes) == int:
return changes
else:
return input('What would you like to change the value to? ')
def add_csv():
with open('suggested_books.csv') as csvfile:
data = csv.reader(csvfile)
for row in data:
book_in_db = session.query(Book).filter(Book.title==row[0]).one_or_none()
if book_in_db == None:
title = row[0]
author = row[1]
date = clean_date(row[2])
price = clean_price(row[3])
new_book = Book(title=title, author=author, date_published=date, price=price)
session.add(new_book)
session.commit()
def clean_date(date_str):
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
split_date = date_str.split(' ')
try:
month = int(months.index(split_date[0]) + 1)
day = int(split_date[1].split(',')[0])
year = int(split_date[2])
return_date = datetime.date(year, month, day)
except ValueError:
input('''
\n****** DATE ERROR *******
\rThe date should include a valid Month Day, Year from the past.
\rEx: January 01, 2013
\rPress Enter to try again.
\r**************************''')
else:
return return_date
def clean_price(price_str):
try:
price_float = float(price_str)
except ValueError:
input('''
\n****** Price ERROR *******
\rThe price should be a number without a currency symbol
\rEx: 10.99
\rPress Enter to try again.
\r**************************''')
else:
return int(price_float * 100)
def clean_id(id_str, options):
try:
book_id = int(id_str)
except ValueError:
input('''
\n****** ID ERROR *******
\rThe id should be a number
\rPress Enter to try again.
\r**************************''')
else:
if book_id in options:
return book_id
else:
input(f'''
\n****** ID ERROR *******
\rOption: {options}
\rPress Enter to try again.
\r**************************''')
return
def app():
app_running = True
while app_running:
choice = menu()
if choice == '1':
# Add Book
title = input('Title: ')
author = input('Author: ')
date_error = True
while date_error:
date = input('Published Date (Ex: October 25, 2017): ')
date = clean_date(date)
if type(date) == datetime.date:
date_error = False
price_error = True
while price_error:
price = input('Price (Ex: 25.65): ')
price = clean_price(price)
if type(price) == int:
price_error = False
new_book = Book(title=title, author=author, date_published=date, price=price)
session.add(new_book)
session.commit()
print('The Book Was Added')
time.sleep(1.5)
elif choice == '2':
# View Books
for book in session.query(Book):
print(f'{book.id} | {book.title} | {book.author} | {book.date_published} | {book.price}')
input('\nPress enter to return to the menu!')
pass
elif choice == '3':
# Search For Book
id_options = []
for book in session.query(Book):
id_options.append(book.id)
id_error = True
while id_error:
id_choice = input(f'''
\nId Options: {id_options}
\rBook id: ''')
id_choice = clean_id(id_choice, id_options)
if type(id_choice) == int:
id_error = False
the_book = session.query(Book).filter(Book.id==id_choice).first()
print(f'''
\n{the_book.title} by {the_book.author}
\rPublished: {the_book.date_published}
\rPrice: {the_book.price / 100}''')
sub_choice = submenu()
if sub_choice == '1':
# edit
the_book.title = edit_check('Title', the_book.title)
the_book.author = edit_check('Author', the_book.author)
the_book.date_published = edit_check('Published', the_book.date_published)
the_book.price = edit_check('Price', the_book.price)
session.commit()
print('Book Updated!')
time.sleep(1.5)
pass
elif sub_choice == '2':
# delete
session.delete(the_book)
session.commit()
print('Book Deleted!')
time.sleep(1.5)
elif choice == '4':
# Book Analysis
pass
else:
print('GOODBYE~!')
app_running = False
if __name__ == '__main__':
Base.metadata.create_all(engine)
add_csv()
app()
# for book in session.query(Book):
# print(book)