-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.py
More file actions
270 lines (180 loc) · 7.26 KB
/
methods.py
File metadata and controls
270 lines (180 loc) · 7.26 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
from prettytable import PrettyTable
import pymysql
import os
from dotenv import load_dotenv
import csv
import time
def connect_to_db():
# Load environment variables from .env file
load_dotenv()
host = os.environ.get("mysql_host")
user = os.environ.get("mysql_user")
password = os.environ.get("mysql_pass")
database = os.environ.get("mysql_db")
# Establish a database connection
connection = pymysql.connect(
host=host,
user=user,
password=password,
database=database
)
return connection
###########################################
def select_query(connection_object, query):
cursor = connection_object.cursor()
cursor.execute(query)
rows = cursor.fetchall()
return rows
###################################################
def write_db_to_csvfile(table_name):
connection_object = connect_to_db()
cursor = connection_object.cursor()
query = "SELECT * FROM "+ table_name
cursor.execute(query)
header = [columns[0] for columns in cursor.description]
rows = cursor.fetchall()
try:
with open (table_name + '.csv', 'w', newline= '') as file_data:
writer = csv.DictWriter(file_data, fieldnames= header )
writer.writeheader()
for row in rows:
writer.writerow(dict(zip(header, row)))
print('CSV file is now available.')
except Exception as err:
print('err')
##################################################
def commit_query(connection_object, query):
cursor = connection_object.cursor()
cursor.execute(query)
connection_object.commit()
################################################
def stay_at_menu_or_go_main(choice_name):
running = 1
if (choice_name =='Products'):
user_choose_where_to_go_input = input('''
Enter [Y/y] Return to Products Menu Options.
[N/n] Return to Main Menu Options. ''')
if user_choose_where_to_go_input in ['y', 'Y']:
os.system('cls')
return running
elif user_choose_where_to_go_input in ['n', 'N']:
os.system('cls')
running = 0
return running
elif (choice_name =='Couriers'):
user_choose_where_to_go_input = input('''
Enter [Y/y] Return to Courier Menu Options.
[N/n] Return to Main Menu Options. ''')
if user_choose_where_to_go_input in ['y', 'Y']:
os.system('cls')
return running
elif user_choose_where_to_go_input in ['n', 'N']:
os.system('cls')
running = 0
return running
elif (choice_name =='Orders'):
user_choose_where_to_go_input = input('''
Enter [Y/y] Return to Orders Menu Options.
[N/n] Return to Main Menu Options. ''')
if user_choose_where_to_go_input in ['y', 'Y']:
os.system('cls')
return running
elif user_choose_where_to_go_input in ['n', 'N']:
os.system('cls')
running = 0
return running
#####################################
def print_list(list_object):
if (len(list_object) != 0):
mytable = PrettyTable([key for key in list_object[0].keys()])
for item in list_object:
mytable.add_row([value for value in item.values()])
print(mytable)
##################################################
def db_to_list (rows, table_name):
if table_name == 'Products':
products_list = []
for row in rows:
products_list.append({'id' :row[0], 'name': row[1], 'price': float(row[2])})
return products_list
elif table_name == 'Couriers':
couriers_list = []
for row in rows:
couriers_list.append({'id' :row[0], 'name': row[1], 'phone': row[2]})
return couriers_list
elif table_name == 'Product_Inventory':
product_inventory_list = []
for row in rows:
product_inventory_list.append({'id' :row[0], 'product_id': row[1], 'Stock_Quantity': float(row[2]), 'Unit_Price': float(row[3]),
'Inventory_Value': float(row[4]) })
return product_inventory_list
elif table_name == 'Orders':
orders_list = []
for row in rows:
orders_list.append({'order_id' :row[0], 'customer_name': row[1], 'customer_address': row[2], 'customer_phone': row[3],
'courier_id': int(row[4]), 'status':int(row[5])})
return orders_list
elif table_name == 'Order_Status':
order_status_list = []
for row in rows:
order_status_list.append({'id' :row[0], 'status': row[1] })
return order_status_list
#####################################################
def show_product_inventory():
connection_object = connect_to_db()
query = '''SELECT Products.id, Products.name, Stock_Quantity, Unit_Price, Inventory_Value
from Product_Inventory
inner join Products
on Products.id = Product_Inventory.product_id '''
db_rows = select_query(connection_object, query)
# inventory_list = []
# for row in db_rows:
# inventory_list.append({'Product id' :row[0], 'Product name': row[1],
# 'Product Stock_Quantity': float(row[2]), 'Product unit price': float(row[3]),
# 'Product Inventory Value': float(row[4]) })
return db_to_list(db_rows, 'Product_Inventory')
######################################################
def convert_csv_db(file_name):
connection = connect_to_db()
cursor = connection.cursor()
try:
with open (file_name +'.csv', 'r', newline='' ) as file_content:
data = csv.reader(file_content)
# to retrieve the fieldsname
header = next(data)
if file_name == 'Orders':
for row in data:
cursor.execute(
"INSERT INTO Orders (customer_name, customer_address, customer_phone, courier_id, status, items) VALUES ( %s, %s, %s,%s,%s, %s)", row)
connection.commit()
elif file_name == 'Products':
for row in data:
cursor.execute(
"INSERT INTO Products (name, price) VALUES ( %s, %s)", row)
connection.commit()
elif file_name == 'Couriers':
for row in data:
cursor.execute(
"INSERT INTO Couriers (name, phone) VALUES ( %s, %s)", row)
connection.commit()
elif file_name == 'Customers':
for row in data:
cursor.execute(
"INSERT INTO Customers (name, address, phone) VALUES ( %s, %s,%s)", row)
connection.commit()
cursor.close()
connection.close()
os.system('cls')
print('CSV has been transfered to Database.')
time.sleep(3)
except FileNotFoundError as err:
print('Can\'t open the file')
##########################################################
def commit_query(connection_object, query):
cursor = connection_object.cursor()
cursor.execute(query)
connection_object.commit()
###########################################################
def close_db(connection):
(connection.cursor()).close()
connection.close()