-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
319 lines (257 loc) · 9.46 KB
/
app.py
File metadata and controls
319 lines (257 loc) · 9.46 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'''Store Inventory App
This script allows the user to:
-Initializes a Sqlite database
-Creates a database model called Product
-Connect the database and create tables
-Reads in the existing CSV data
-Cleans up the data before adding each product from the csv
-Adds the data from CSV into the database
-Creates a Menu to make selections
-Displays a product by its ID - Menu Option V
-Manually Adds a new product to the database - Menu Option A
- Backups the database (Export new CSV) - Menu Option B
-Include only production-ready files in the repo
This tool accepts comma separated value files(.csv)
as manual entry for adding product
This script requires that `pandas` be installed within the Python
environment you are running this script in .
contains the following
functions:
* menu: creates a menu for the script
*view_product: shows product
*add_product
*create_backup
*clean_date
*clean_quantity
*clean_price
*add_csv
*app
*main - the main function of the script
'''
# Standard library imports
import csv
import datetime
import os
import sys
import time
# Related third-party imports
from models import Base, session, Product, engine
def menu():
'''
Menu
creates a menu for the application
args:None
Returns:str
'''
while True:
print(
'''
Store Inventory
\n* View a single product\'s inventory(v)
\r*Add a new product to the database (a)
\r*Make a backup of the entire inventory(b)
''')
choice = input('What would you like to do?: ')
if choice in ['v', 'a', 'b']:
return choice
else:
input(
'''
\rPlease enter one of the options above
\rletters a, b or v only
\rPress enter to try again:''')
print()
def view_product():
try:
product_id = int(input('Please enter the id of the product: '))
print()
print()
desired_product = session.query(Product).\
filter(Product.product_id == product_id)
if desired_product is not None:
for product in desired_product:
print(f'''
Product Name: {product.product_name}
Product Quantity:{product.product_quantity} units
Product Price:${product.product_price/100:.2f}
Date last updated:{product.date_updated}
'''
)
if product_id > len(session.query(Product).all()):
raise Exception('This product id is not valid, please try again ')
except ValueError:
print(f'Please enter a valid value for the id\
-a number from 1-{len(session.query(Product).all())}')
view_product()
except Exception as e:
print()
print(e)
print()
view_product()
else:
time.sleep(1.5)
def add_product():
product_name = input('Please enter a product name')
try:
product_quantity = int(input('''Please enter the quantityof the product'''))
except ValueError:
print('Please enter only whole numbers')
product_quantity = int(
input('Please enter the quantity of the product'))
try:
product_price = input('Please enter the price for the product')
transformed_price = clean_price(product_price)
if type(transformed_price) != int:
raise Exception('Please enter a valid value for the price ')
except Exception as e:
print(e)
product_price = input('Please enter the price for the product')
transformed_price = clean_price(product_price)
product_in_db = session.query(Product).\
filter(Product.product_name == product_name).\
one_or_none()
# if the product doesn't exist in the database
if product_in_db is None:
product_added = Product(product_name=product_name,
product_quantity=product_quantity,
product_price=transformed_price)
session.add(product_added)
session.commit()
elif product_in_db is not None:
print('Product already in the system, updating with new details')
product_in_db.product_price = clean_price(product_price)
product_in_db.product_quantity = clean_quantity(product_quantity)
product_in_db.date_updated = datetime.datetime.now()
session.commit()
def create_backup():
"creates 2 backup files"
with open('backup1.csv', 'w') as backup_csv1:
field_names1 = [
'product_id',
'product_name',
'product_quantity',
'product_price',
'date_updated'
]
backup_writer = csv.DictWriter(backup_csv1, fieldnames=field_names1)
backup_writer.writeheader()
data = session.query(Product).all()
for datum in data:
backup_writer.writerow(
{
'product_id': datum.product_id,
'product_name': datum.product_name,
'product_quantity': datum.product_quantity,
'product_price': datum.product_price,
'date_updated': datum.date_updated
}
)
with open('backup2.csv', 'w') as backup_csv2:
field_names2 = [
'Product Id',
'Product Name',
'Product Quantity',
'Product Price',
'Date Updated',
]
backup_writer = csv.DictWriter(backup_csv2, fieldnames=field_names2)
backup_writer.writeheader()
data = session.query(Product).all()
for datum in data:
backup_writer.writerow(
{
'Product Id': datum.product_id,
'Product Name': datum.product_name,
'Product Quantity': datum.product_quantity,
'Product Price': datum.product_price,
'Date Updated': datum.date_updated
}
)
print('backup1.csv and backup2.csv were successfully created')
print()
print()
def clean_date(date_str):
'''Turns the string into a date object'''
split_date = date_str.split('/')
month = int(split_date[0])
day = int(split_date[1])
year = int(split_date[2])
return datetime.date(year, month, day)
def clean_quantity(qty_str):
return int(qty_str)
def clean_price(price_str):
if '$' in price_str:
split_price = price_str.split('$')
price_float = float(split_price[1])
else:
price_float = float(price_str)
return int(price_float * 100)
def add_csv():
with open('inventory.csv') as inventory_csv:
data = csv.DictReader(inventory_csv)
for row in data:
product_in_db = session.query(Product).\
filter(Product.product_name == row['product_name']).\
one_or_none()
if product_in_db is None:
product_name = row['product_name']
product_price = clean_price(row['product_price'])
product_quantity = clean_quantity(row['product_quantity'])
date_updated = clean_date(row['date_updated'])
new_product = Product(product_name=product_name,
product_quantity=product_quantity,
product_price=product_price,
date_updated=date_updated
)
session.add(new_product)
session.commit()
if product_in_db is not None:
if product_in_db.product_name == row['product_name']:
date_in_db = product_in_db.date_updated
csv_date = clean_date(row['date_updated'])
if date_in_db > csv_date:
print(' updating the database')
elif date_in_db < csv_date:
print('loading new data from source for product:',
product_in_db.product_name)
product_in_db.product_price = clean_price(row['product_price'])
product_in_db.product_quantity = clean_quantity(row['product_quantity'])
product_in_db.date_updated = clean_date(row['date_updated'])
session.commit()
print('the following changes have been made:')
print(product_in_db.product_price)
print('database is up-to-date')
def app():
try:
add_csv()
except (FileNotFoundError):
print()
print()
print('No source csv found, please review')
print()
print()
app_running = True
while app_running:
choice = menu()
if choice == 'v':
# view a single product
view_product()
elif choice == 'a':
# add a product
add_product()
elif choice == 'b':
# create backup
create_backup()
pass
else:
app_running = False
input(
'''
\rPlease enter one of the options above
\rletters a, b or v only
\rPress enter to try again: ''')
print('Thank you for using our system, good bye ')
sys.exit()
if __name__ == '__main__':
Base.metadata.create_all(engine)
app()