Skip to content

Commit 7cc9d37

Browse files
committed
Added NEW Version of Modules - Exercise
1 parent 5597d48 commit 7cc9d37

30 files changed

+292
-0
lines changed
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
from json import loads, dump
2+
from tkinter import Button, Entry
3+
from buying_page import display_products
4+
from canvas import root, frame
5+
from helpers import clean_screen, get_password_hash
6+
7+
8+
def get_users_data():
9+
info_data = []
10+
11+
with open("db/users_information.txt", "r") as users_file:
12+
for line in users_file:
13+
info_data.append(loads(line))
14+
15+
return info_data
16+
17+
18+
def render_entry():
19+
register_btn = Button(
20+
root,
21+
text="Register",
22+
bg="green", # background color
23+
fg="white", # font color
24+
borderwidth=0,
25+
width=20,
26+
height=3,
27+
command=register,
28+
)
29+
30+
login_btn = Button(
31+
root,
32+
text="Login",
33+
bg="blue",
34+
fg="white",
35+
borderwidth=0,
36+
width=20,
37+
height=3,
38+
command=login,
39+
)
40+
41+
frame.create_window(350, 260, window=register_btn)
42+
frame.create_window(350, 320, window=login_btn)
43+
44+
45+
def login():
46+
clean_screen()
47+
48+
frame.create_text(100, 50, text="Username:")
49+
frame.create_text(100, 100, text="Password:")
50+
51+
frame.create_window(200, 50, window=username_box)
52+
frame.create_window(200, 100, window=password_box)
53+
54+
frame.create_window(250, 150, window=logging_btn)
55+
56+
57+
def logging():
58+
if check_logging():
59+
display_products()
60+
else:
61+
frame.create_text(160, 200, text="Invalid username or password!", fill="red")
62+
63+
64+
def check_logging():
65+
info_data = get_users_data()
66+
67+
user_username = username_box.get()
68+
user_password = get_password_hash(password_box.get())
69+
70+
for i in range(len(info_data)):
71+
username = info_data[i]["username"]
72+
password = info_data[i]["password"]
73+
74+
if username == user_username and password == user_password:
75+
return True
76+
77+
return False
78+
79+
80+
def register():
81+
clean_screen()
82+
83+
frame.create_text(100, 50, text="First name:")
84+
frame.create_text(100, 100, text="Last name:")
85+
frame.create_text(100, 150, text="Username:")
86+
frame.create_text(100, 200, text="Password:")
87+
88+
frame.create_window(200, 50, window=first_name_box)
89+
frame.create_window(200, 100, window=last_name_box)
90+
frame.create_window(200, 150, window=username_box)
91+
frame.create_window(200, 200, window=password_box)
92+
93+
registration_btn = Button(
94+
root,
95+
text="Register",
96+
bg="green",
97+
fg="white",
98+
command=registration,
99+
)
100+
101+
frame.create_window(300, 250, window=registration_btn)
102+
103+
104+
def registration():
105+
info_dict = {
106+
"first_name": first_name_box.get(),
107+
"last_name": last_name_box.get(),
108+
"username": username_box.get(),
109+
"password": get_password_hash(password_box.get()),
110+
}
111+
112+
if check_registration(info_dict):
113+
with open("db/users_information.txt", "a") as users_file:
114+
dump(info_dict, users_file)
115+
users_file.write('\n')
116+
display_products()
117+
118+
119+
def check_registration(info):
120+
for el in info.values():
121+
if not el.strip():
122+
frame.create_text(
123+
300,
124+
300,
125+
text="We are missing some information!",
126+
fill="red",
127+
tag="error"
128+
)
129+
130+
return False
131+
132+
frame.delete("error")
133+
134+
info_data = get_users_data()
135+
136+
for record in info_data:
137+
if record["username"] == info["username"]:
138+
frame.create_text(
139+
300,
140+
300,
141+
text="User with this username already exists!",
142+
fill="red",
143+
tag="error",
144+
)
145+
146+
return False
147+
148+
frame.delete("error")
149+
150+
return True
151+
152+
153+
def check_if_login_is_fulfilled(event):
154+
info = {
155+
"username": username_box.get(),
156+
"password": password_box.get(),
157+
}
158+
159+
for el in info.values():
160+
if not el.strip():
161+
logging_btn["state"] = "disabled"
162+
break
163+
else:
164+
logging_btn["state"] = "normal"
165+
166+
167+
first_name_box = Entry(root, bd=0)
168+
last_name_box = Entry(root, bd=0)
169+
username_box = Entry(root, bd=0)
170+
password_box = Entry(root, bd=0, show="*") # let the user choose
171+
172+
logging_btn = Button(
173+
root,
174+
text="Login",
175+
bg="blue",
176+
fg="white",
177+
command=logging,
178+
)
179+
180+
logging_btn["state"] = "disabled" # options: active, normal, disabled
181+
182+
root.bind('<KeyRelease>', check_if_login_is_fulfilled)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from json import load, dump
2+
from tkinter import Button
3+
from PIL import Image, ImageTk
4+
from canvas import frame, root
5+
from helpers import clean_screen
6+
7+
8+
def display_products():
9+
clean_screen()
10+
display_stock()
11+
12+
13+
def display_stock():
14+
global info
15+
16+
with open("db/products_data.json", "r") as stock:
17+
info = load(stock)
18+
19+
x, y = 150, 50
20+
21+
for item_name, item_info in info.items():
22+
item_img = ImageTk.PhotoImage(Image.open(item_info["image"]))
23+
images.append(item_img)
24+
25+
frame.create_text(x, y, text=item_name, font=("Comic Sans MS", 15))
26+
frame.create_image(x, y + 100, image=item_img)
27+
28+
if item_info["quantity"] > 0:
29+
color = "green"
30+
text = f"In stock: {item_info['quantity']}"
31+
32+
item_btn = Button(
33+
root,
34+
text="Buy",
35+
font=("Comic Sans MS", 12),
36+
bg="green",
37+
fg="white",
38+
width=5,
39+
command=lambda x=item_name: buy_product(x),
40+
)
41+
42+
frame.create_window(x, y + 230, window=item_btn)
43+
else:
44+
color = "red"
45+
text = "Out of stock"
46+
47+
frame.create_text(x, y + 180, text=text, font=("Comic Sans MS", 12), fill=color)
48+
49+
x += 200
50+
51+
if x > 550:
52+
x = 150
53+
y += 230
54+
55+
56+
def buy_product(product):
57+
info[product]["quantity"] -= 1
58+
59+
with open("db/products_data.json", "w") as stock:
60+
dump(info, stock)
61+
62+
display_products()
63+
64+
65+
images = []
66+
info = {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from tkinter import Tk, Canvas
2+
3+
4+
def create_root():
5+
root = Tk()
6+
7+
root.title("GUI Shop")
8+
root.resizable(False, False)
9+
root.geometry("700x600")
10+
11+
return root
12+
13+
14+
def create_frame():
15+
frame = Canvas(root, width=700, height=700)
16+
frame.grid(row=0, column=0)
17+
18+
return frame
19+
20+
21+
root = create_root()
22+
frame = create_frame()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"puma blue t-shirt": {"quantity": 2, "image": "images/blue_t_shirt.png"}, "nike blue t-shirt": {"quantity": 13, "image": "images/blue_t_shirt.png"}, "jeans": {"quantity": 5, "image": "images/black_jeans.png"}, "shoes": {"quantity": 14, "image": "images/shoes.png"}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"first_name": "Dimitar", "last_name": "Dimitrov", "username": "MitkoVtori", "password": "a1fe8f79a121256842e7aaef2ab1e339a553a74fe05834ca081259cf66ac5fb5"}
2+
{"first_name": "Gogo", "last_name": "gogo", "username": "xx", "password": "2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from hashlib import sha256
2+
from canvas import frame
3+
4+
5+
def clean_screen():
6+
frame.delete("all")
7+
8+
9+
def get_password_hash(password):
10+
hash_object = sha256(password.encode())
11+
12+
return str(hash_object.hexdigest())
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from authentication import render_entry
2+
from canvas import root
3+
4+
5+
if __name__ == '__main__':
6+
render_entry()
7+
root.mainloop()

0 commit comments

Comments
 (0)