-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
188 lines (166 loc) · 6.2 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
from tkinter.messagebox import NO
from sqlalchemy import Integer, true
from models import (Base, Product,
session, engine)
import datetime
import csv
import time
def menu():
while True:
print('''
\nPRODUCT INVENTORY
\r
\rChose a option to continue:
\r
\rv- View the details of a product
\ra- Add a new product
\rb- Make a backup of the content of the database
\re- Exit
''')
choice = input('\rWhat would you like to do? ').lower()
if choice in ['v', 'a', 'b', 'e']:
return choice
else:
input('''
\rPlease enter one of the options in the menu above.
\rPress Enter to continue. ''')
def clean_price(price_str):
split_price = price_str.split('$')
price_float = float(split_price[1])
return int(price_float * 100)
def clean_date(date_str):
split_date = date_str.split('/')
month = int(split_date[0])
day = int(split_date[1])
year = int(split_date[2])
return datetime.datetime(year, month, day)
def clean_id(id_string, option):
try:
product_id = int(id_string)
except ValueError:
return
else:
if product_id in option:
return product_id
else:
return
def view_product():
id_options = []
for prod in session.query(Product):
id_options.append(prod.id)
id_error = True
while id_error:
id_chosen = input(f'''
\nPlease, enter the ID number between
\r{id_options[0]} and {id_options[len(id_options)-1]}
\rfor the product you are looking
\rfor: ''')
id_chosen = clean_id(id_chosen, id_options)
if type(id_chosen) == int:
id_error = False
else:
print('''\n****** ID ERROR *******
\rThat ID does not exist.
\rPlease, try again.
\r***********************''')
chosen_product = session.query(Product).filter(Product.id == id_chosen).first()
print(f'''\nProduct ID: {chosen_product.id}
\rName: {chosen_product.product_name}
\rStock: {chosen_product.product_quantity} Units
\rPrice: $ {chosen_product.product_price/100}
\rLast Updated: {chosen_product.date_updated.strftime("%B %d, %Y")}''')
def add_product():
name = input("Enter product's name: ")
try:
quantity = int(input("Enter the quantity: "))
except ValueError:
input('''\n****** Quantity Error ******
\rPlease enter a valid number.
\rPress Enter to continue.
\r**************************''')
return
price_error = True
while price_error:
price = input('Enter price (ex. $12.44): ')
try:
price = clean_price(price)
price_error = False
except IndexError:
input('''\n***** Price Error *****
\rDon't forget the $ sign.
\rPress Enter to continue.
\r************************''')
except ValueError:
input('''\n***** Price Error *****
\rPlease, enter a number
\rwith dot instead of coma.
\rPress Enter to continue.
\r************************''')
previous_entry = session.query(Product).filter(Product.product_name == name).one_or_none()
if previous_entry == None:
new_product = Product(product_name=name, product_quantity= quantity, product_price = price, date_updated = datetime.datetime.today())
session.add(new_product)
print('Product Added!')
else:
previous_entry.product_quantity = quantity
previous_entry.product_price = price
previous_entry.date_updated = datetime.date.today()
print('Product Updated!')
session.commit()
time.sleep(2)
def back_up():
header = ['product_name','product_price','product_quantity','date_updated']
with open('backup.csv', 'w') as cvsfile:
writer = csv.writer(cvsfile)
writer.writerow(header)
for prod in session.query(Product):
prod_list = []
price = '$'+ format(prod.product_price/100, '.2f')
split_date = str(prod.date_updated).split('-')
month = split_date[1]
day = split_date[2].split(' ')[0]
year = split_date[0]
date = f'{month}/{day}/{year}'
prod_list = [prod.product_name, price, prod.product_quantity, date]
writer.writerow(prod_list)
print('Back Up done')
time.sleep(2)
def add_csv():
with open('inventory.csv') as csvfile:
next(csvfile)
data = csv.reader(csvfile)
names = []
for row in data:
product_in_db = session.query(Product).filter(Product.product_name==row[0]).one_or_none()
if product_in_db==None:
name = row[0]
names.append(name)
price = clean_price(row[1])
quantity = int(row[2])
date = clean_date(row[3])
new_product = Product(product_name=name, product_price=price,product_quantity=quantity, date_updated=date)
session.add(new_product)
else:
if product_in_db.date_updated < clean_date(row[3]):
product_in_db.product_quantity = int(row[2])
product_in_db.product_price = clean_price(row[1])
product_in_db.date_updated = clean_date(row[3])
print('Product Updated!')
session.commit()
def app():
app_running = True
while app_running:
choice = menu()
if choice == 'v':
view_product()
elif choice == 'a':
add_product()
elif choice == 'b':
back_up()
else:
print('Goodbye!')
app_running = False
if __name__ == '__main__':
Base.metadata.create_all(engine)
add_csv()
app()