SMART-43 implemented user profile view

This commit is contained in:
s460930 2020-12-14 13:24:23 +01:00
parent 5080ab5c8d
commit fb65e04600
5 changed files with 109 additions and 87 deletions

View File

@ -1,36 +1,31 @@
import tkinter as tk import tkinter as tk
import requests import requests
FONT= ("Verdana", 12) FONT = ("Verdana", 12)
FONT_B= ("Verdana", 12, 'bold') FONT_B = ("Verdana", 12, 'bold')
FONT_LARGE= ("Verdana", 20) FONT_LARGE = ("Verdana", 20)
URL = "http://localhost:8000/api/authenticate" URL = "http://localhost:8000/api/authenticate"
URL_REGISTER = "http://localhost:8000/api/register" URL_REGISTER = "http://localhost:8000/api/register"
URL_PROFILE = "http://localhost:8000/api/profile" URL_PROFILE = "http://localhost:8000/api/profile"
TOKEN = ""
USERNAME = ""
FIRST_NAME = ""
LAST_NAME = ""
class SmartPicasso(tk.Tk): class SmartPicasso(tk.Tk):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs) tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self) container = tk.Frame(self)
self.title('SmartPicasso') self.title('SmartPicasso')
self.geometry('610x460') self.geometry('610x460')
container.pack(side="top", fill="both", expand = True) container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1) container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1)
self.frames = {} self.frames = {}
for F in (LoginView, MainView, RegisterView, ProfileView): for F in (LoginView, MainView, RegisterView):
frame = F(container, self) frame = F(container, self)
self.frames[F] = frame self.frames[F] = frame
@ -39,18 +34,20 @@ class SmartPicasso(tk.Tk):
self.show_frame(LoginView) self.show_frame(LoginView)
def show_frame(self, cont): def show_frame(self, view, token=None):
frame = self.frames[view]
frame = self.frames[cont]
frame.tkraise() frame.tkraise()
if token:
frame.token = token
class LoginView(tk.Frame): class LoginView(tk.Frame):
def __init__(self, parent, controller): def __init__(self, parent, controller):
tk.Frame.__init__(self,parent) tk.Frame.__init__(self, parent)
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE) label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE)
label.pack(pady=10,padx=10) label.pack(pady=10, padx=10)
label1 = tk.Label(self, text='Login:', font=FONT) label1 = tk.Label(self, text='Login:', font=FONT)
label1.pack() label1.pack()
@ -64,62 +61,73 @@ class LoginView(tk.Frame):
input2 = tk.Entry(self, show="*") input2 = tk.Entry(self, show="*")
input2.pack() input2.pack()
button = tk.Button(self, text="Login", font=FONT, command=lambda: self.login(controller, input1.get(), input2.get())) button = tk.Button(self, text="Login", font=FONT,
command=lambda: self.login(controller, input1.get(), input2.get()))
button.pack() button.pack()
button2 = tk.Button(self, text="Register", font=FONT, command=lambda: controller.show_frame(RegisterView)) button2 = tk.Button(self, text="Register", font=FONT, command=lambda: controller.show_frame(RegisterView))
button2.pack() button2.pack()
def login(self, controller, login, passw,): def login(self, controller, login, password):
print(login) print(login)
print(passw) print(password)
data = { data = {
"email": str(login), "email": str(login),
"password": str(passw) "password": str(password)
} }
resp = requests.post(URL, json=data) resp = requests.post(URL, json=data)
print(resp) print(resp)
if (resp.status_code==200): if resp.status_code == 200:
response=resp.json() response = resp.json()
TOKEN = response['token'] token = response['token']
hed = {'Authorization': 'Bearer ' + TOKEN} controller.show_frame(MainView, token)
resp = requests.get(URL_PROFILE, headers=hed)
response=resp.json()
USERNAME = response['profile']['username']
FIRST_NAME = response['profile']['first_name']
LAST_NAME = response['profile']['last_name']
controller.show_frame(MainView)
else: else:
print("bad pass") print("bad pass")
badPassLabel = tk.Label(self, text='Wrong login/password!', font=FONT) bad_pass_label = tk.Label(self, text='Wrong login/password!', font=FONT)
badPassLabel.pack() bad_pass_label.pack()
return() return ()
class MainView(tk.Frame): class MainView(tk.Frame):
def __init__(self, parent, controller): def __init__(self, parent, controller):
tk.Frame.__init__(self,parent) tk.Frame.__init__(self, parent)
self.profile = ProfileView() self.frames = {}
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE) self.token = ''
label.pack(pady=10,padx=10) for F in (ProfileView,):
label_u = tk.Label(self, text="Main menu", font=FONT) frame = F(parent, controller)
label_u.pack(pady=10,padx=10)
button_profile = tk.Button(self, text="My profile", font=FONT, command=lambda: controller.show_frame(ProfileView)) self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE)
label.pack(pady=10, padx=10)
label_u = tk.Label(self, text="Main menu", font=FONT)
label_u.pack(pady=10, padx=10)
button_profile = tk.Button(self, text="My profile", font=FONT,
command=lambda: self.show_frame(ProfileView))
button_profile.pack() button_profile.pack()
def show_frame(self, view):
frame = self.frames[view]
frame.tkraise()
if self.token:
print(self.token)
frame.token = self.token
frame.start()
class RegisterView(tk.Frame): class RegisterView(tk.Frame):
def __init__(self, parent, controller): def __init__(self, parent, controller):
tk.Frame.__init__(self,parent) tk.Frame.__init__(self, parent)
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE) label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE)
label.pack(pady=10,padx=10) label.pack(pady=10, padx=10)
label_u = tk.Label(self, text="Register", font=FONT) label_u = tk.Label(self, text="Register", font=FONT)
label_u.pack(pady=10,padx=10) label_u.pack(pady=10, padx=10)
label0 = tk.Label(self, text='Email:', font=FONT) label0 = tk.Label(self, text='Email:', font=FONT)
label0.pack() label0.pack()
@ -151,11 +159,14 @@ class RegisterView(tk.Frame):
input4 = tk.Entry(self) input4 = tk.Entry(self)
input4.pack() input4.pack()
button1 = tk.Button(self, text="Register", font=FONT, command=lambda: self.register(controller, input0.get(), input1.get(), input2.get(), input3.get(), input4.get())) button1 = tk.Button(self, text="Register", font=FONT,
command=lambda: self.register(controller, input0.get(), input1.get(), input2.get(),
input3.get(), input4.get()))
button1.pack() button1.pack()
button2 = tk.Button(self, text="Cancel", font=FONT, command=lambda: controller.show_frame(LoginView)) button2 = tk.Button(self, text="Cancel", font=FONT, command=lambda: controller.show_frame(LoginView))
button2.pack() button2.pack()
def register(self, controller, email, login, passw, name, lastname): def register(self, controller, email, login, passw, name, lastname):
data = { data = {
"email": str(email), "email": str(email),
@ -164,45 +175,54 @@ class RegisterView(tk.Frame):
"username": str(login), "username": str(login),
"first_name": str(name), "first_name": str(name),
"last_name": str(lastname) "last_name": str(lastname)
}
} }
}
print(data) print(data)
resp = requests.post(URL_REGISTER, json=data) response = requests.post(URL_REGISTER, json=data)
print(resp) print(response)
if (resp.status_code==201): if response.status_code == 201:
response=resp.json() response = response.json()
controller.show_frame(LoginView) controller.show_frame(LoginView)
else: else:
print("sth wrong") print("sth wrong")
badPassLabel = tk.Label(self, text='Something went wrong!', font=FONT) bad_pass_label = tk.Label(self, text='Something went wrong!', font=FONT)
badPassLabel.pack() bad_pass_label.pack()
return() return ()
class ProfileView(tk.Frame): class ProfileView(tk.Frame):
def __init__(self, parent, controller): def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Frame.__init__(self,parent) self.token = ''
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE) label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE)
label.pack(pady=10,padx=10) label.pack(pady=10, padx=10)
label_l1 = tk.Label(self, text="Login:", font=FONT_B) label_l1 = tk.Label(self, text="Login:", font=FONT_B)
label_l1.pack(pady=10,padx=10) label_l1.pack(pady=10, padx=10)
label_l2 = tk.Label(self, text=USERNAME, font=FONT) self.label_username = tk.Label(self, text='', font=FONT)
label_l2.pack(pady=10,padx=10) self.label_username.pack(pady=10, padx=10)
label_n1 = tk.Label(self, text="Name:", font=FONT_B) label_n1 = tk.Label(self, text="Name:", font=FONT_B)
label_n1.pack(pady=10,padx=10) label_n1.pack(pady=10, padx=10)
label_n2 = tk.Label(self, text=FIRST_NAME, font=FONT) self.label_first_name = tk.Label(self, text='', font=FONT)
label_n2.pack(pady=10,padx=10) self.label_first_name.pack(pady=10, padx=10)
label_ln1 = tk.Label(self, text="Last name", font=FONT_B) label_ln1 = tk.Label(self, text="Last name", font=FONT_B)
label_ln1.pack(pady=10,padx=10) label_ln1.pack(pady=10, padx=10)
label_ln2 = tk.Label(self, text=LAST_NAME, font=FONT) self.label_last_name = tk.Label(self, text='', font=FONT)
label_ln2.pack(pady=10,padx=10) self.label_last_name.pack(pady=10, padx=10)
button_profile = tk.Button(self, text="Back", font=FONT,
button_profile = tk.Button(self, text="Back", font=FONT, command=lambda: controller.show_frame(MainView)) command=lambda: controller.show_frame(MainView, self.token))
button_profile.pack() button_profile.pack()
def start(self):
headers = {'Authorization': 'Bearer ' + self.token}
resp = requests.get(URL_PROFILE, headers=headers)
response = resp.json()
print(response)
self.label_username['text'] = response['profile']['username']
self.label_first_name['text'] = response['profile']['first_name']
self.label_last_name['text'] = response['profile']['last_name']
app = SmartPicasso() app = SmartPicasso()
app.mainloop() app.mainloop()

2
client/requirements.txt Normal file
View File

@ -0,0 +1,2 @@
tkinter
requests

Binary file not shown.