-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
185 lines (154 loc) · 5.38 KB
/
Copy pathapp.py
File metadata and controls
185 lines (154 loc) · 5.38 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
from collections import OrderedDict
import datetime
import sys
import os
import csv
from peewee import *
db = SqliteDatabase('inventory.db')
class Product(Model):
# content
product_id = AutoField()
product_name = CharField(max_length=255, unique=True)
product_quantity = IntegerField()
product_price = IntegerField()
date_updated = DateTimeField(default=datetime.datetime.now)
class Meta:
database = db
def read_csv():
with open('inventory.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',')
rows = list(reader)
for row in rows:
row['product_quantity'] = int(row['product_quantity'])
row['product_price'] = int(
row['product_price'].replace('$', '').replace('.', ''))
row['date_updated'] = datetime.datetime.strptime(
row['date_updated'], '%m/%d/%Y')
for row in rows:
try:
Product.create(
product_name=row['product_name'],
product_quantity=row['product_quantity'],
product_price=row['product_price'],
date_updated=row['date_updated']
).save()
except IntegrityError:
temp = Product.get(product_name=row['product_name'])
temp.product_name = row['product_name']
temp.product_quantity = row['product_quantity']
temp.product_price = row['product_price']
temp.date_updated = row['date_updated']
temp.save()
def menu_loop():
user_input = None
user_inputs = ['q', 'v', 'a', 'b']
while user_input != 'q':
print("Justin's store inventory\n\n")
print('Please choose one of the 3 options or put q to exit')
for key, value in menu.items():
print("{}) {}".format(key, value.__doc__))
user_input = input('\nChoose an option: ').lower().strip()
if user_input not in user_inputs:
clear()
print('That is not a correct option')
elif user_input in menu:
menu[user_input]()
def view_entry():
"""View Entry """
clear()
while True:
user_id = None
try:
user_id = int(input('Please enter the product ID \n'))
except ValueError:
clear()
print('That is not a valid option. Please try agian.\n')
entries = Product.select().where(Product.product_id == user_id)
if entries:
clear()
print('Product ID: {}\n'.format(user_id))
for entry in entries:
print('Product name: {}\n'.format(entry.product_name))
print('Product price: ${:.2f}\n'.format(
float(entry.product_price) / 100))
print('Product quantity: {}\n'.format(entry.product_quantity))
print('Date updated: {}\n'.format(entry.date_updated))
else:
print('Sorry, that product ID does not exist')
try_agian = input(
'Would you like to search for another item y/n? \n').lower()
if try_agian == 'n':
break
def add_entry():
"""Add an Entry"""
new_name = input('Please tell us the name of the new item.\n')
while True:
new_quantity = input('How many items are there.\n')
try:
new_quantity = int(new_quantity)
break
except ValueError:
print('Sorry please enter a number')
while True:
new_price = input('What is the price of the item.\n')
try:
new_price = float(new_price)
new_price = int(new_price * 100)
break
except ValueError:
print('Sorry please enter a number')
try:
Product.create(
product_name=new_name,
product_quantity=new_quantity,
product_price=new_price,
date_updated=datetime.datetime.now()
).save()
except IntegrityError:
temp = Product.get(product_name=new_name)
temp.product_quantity = new_quantity
temp.product_price = new_price
temp.date_updated = datetime.datetime.now()
temp.save()
def backup_data():
"""Backup Data"""
clear()
file_backup = 'inventory_backup.csv'
field_names = [
'product_name',
'product_price',
'product_quantity',
'date_updated',
]
with open(file_backup, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=field_names)
writer.writeheader()
products = Product.select()
for product in products:
writer.writerow({
'product_name': product.product_name,
'product_quantity': product.product_quantity,
'product_price': product.product_price,
'date_updated': product.date_updated
})
if os.path.isfile(file_backup):
clear()
print('Your shop has been backed up')
else:
clear()
print('It seems something went wrong... Try again.')
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
def initialize():
db.connect()
db.create_tables([Product], safe=True)
read_csv()
menu_loop()
menu = OrderedDict([
('v', view_entry),
('a', add_entry),
('b', backup_data),
])
if __name__ == '__main__':
clear()
initialize()