-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.py
144 lines (112 loc) · 3.96 KB
/
source.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
import pymysql
from pymysql import cursors, DatabaseError
import sys
from helpers import *
from getpass import getpass
class State:
"""
A class that holds commonly used data across the whole program such as the database
cursor and information about the user as well as any messages that could be printed
by different areas of the program
"""
def __init__(self, cur: cursors.DictCursor, user_id: int, username: str):
self.cur = cur
self.user_id = user_id
self.username = username
self.message = ""
def update_message(self, m: str):
self.message = m
def clear_message(self):
self.message = ""
def print_message_reset(self):
"""
Prints and resets the previously entered message
"""
if self.message:
print(self.message + "\n")
self.clear_message()
def main():
print("Please log in to the local MySQL server:")
username = input("Username: ")
password = getpass("Password: ")
try:
cnx = pymysql.connect(
host="localhost",
user=username,
password=password,
db="recipemaster",
charset="utf8mb4",
)
except pymysql.err.OperationalError as e:
print(e)
print("Could not connect... shutting down")
sys.exit()
cur = cnx.cursor(cursor=cursors.DictCursor)
print_menu("Log in or create a new user for RecipeMaster", ["Log in", "New user"])
choice = get_num_input(1, 2, "Go to")
match choice:
case 1:
# Logging into an already existing account
while True:
username = input("Username: ")
password = getpass("Password: ")
cur.execute("SELECT get_user_id(%s, %s)", [username, password])
user_id = cur.fetchone().get(f"get_user_id('{username}', '{password}')")
if user_id != -1:
new_user = False
break
print("Invalid credentials, try again\n")
case 2:
# Creating a new user
while True:
username = input("Username: ")
password = getpass("Password: ")
try:
user_id = call_proc(cur, "create_user", [username, password])
user_id = user_id[0].get("user_id")
except DatabaseError as e:
if e.args[0] == DUPLICATE_CODE:
print("User already exists with that username, try again")
else:
print("Error creating new user, try again")
finally:
new_user = True
break
clear_screen()
state = State(cur, user_id, username)
if new_user:
state.update_message(f"Created new user with ID {user_id}")
# Entering point for the rest of the application, once this function returns, the program ends
main_menu(state)
print("Shutting down...")
cnx.close()
def main_menu(state: State):
clear_screen()
state.print_message_reset()
print_menu(
f"Welcome to RecipeMaster, {state.username}! To get started, choose one of the options below",
["Recipes", "Ingredients", "Lists", "Reviews", "Exit"],
)
choice = get_num_input(1, 5, "Go to")
match choice:
case 1:
# Opens the recipe menu
from recipe import recipe_module
recipe_module(state)
case 2:
# Opens the ingredient menu
from ingredient import ingredient_module
ingredient_module(state)
case 3:
# Opens the list menu
from list import list_module
list_module(state)
case 4:
# Opens the review menu
from review import review_module
review_module(state)
case 5:
# Exits the program
return
if __name__ == "__main__":
main()