SmartPicasso/client/app.py

307 lines
9.8 KiB
Python
Raw Normal View History

2020-12-14 07:02:08 +01:00
import tkinter as tk
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
import requests
2020-12-14 13:24:23 +01:00
FONT = ("Verdana", 12)
FONT_B = ("Verdana", 12, 'bold')
FONT_LARGE = ("Verdana", 20)
2020-12-14 07:02:08 +01:00
URL = "http://localhost:8000/api/authenticate"
URL_REGISTER = "http://localhost:8000/api/register"
URL_PROFILE = "http://localhost:8000/api/profile"
2020-12-21 11:33:45 +01:00
URL_PROJECTS = "http://localhost:8000/api/projects"
2020-12-14 07:02:08 +01:00
2020-12-21 14:23:52 +01:00
2020-12-14 07:02:08 +01:00
class SmartPicasso(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
self.title('SmartPicasso')
self.geometry('610x460')
2020-12-14 13:24:23 +01:00
container.pack(side="top", fill="both", expand=True)
2020-12-14 07:02:08 +01:00
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
2020-12-14 13:24:23 +01:00
for F in (LoginView, MainView, RegisterView):
2020-12-14 07:02:08 +01:00
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(LoginView)
2020-12-14 13:24:23 +01:00
def show_frame(self, view, token=None):
frame = self.frames[view]
2020-12-14 07:02:08 +01:00
frame.tkraise()
2020-12-14 13:24:23 +01:00
if token:
frame.token = token
2020-12-14 07:02:08 +01:00
class LoginView(tk.Frame):
def __init__(self, parent, controller):
2020-12-14 13:24:23 +01:00
tk.Frame.__init__(self, parent)
2020-12-14 07:02:08 +01:00
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE)
2020-12-14 13:24:23 +01:00
label.pack(pady=10, padx=10)
2020-12-14 07:02:08 +01:00
label1 = tk.Label(self, text='Login:', font=FONT)
label1.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
input1 = tk.Entry(self)
input1.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
label2 = tk.Label(self, text='Password:', font=FONT)
label2.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
input2 = tk.Entry(self, show="*")
input2.pack()
2020-12-14 13:24:23 +01:00
button = tk.Button(self, text="Login", font=FONT,
command=lambda: self.login(controller, input1.get(), input2.get()))
2020-12-14 07:02:08 +01:00
button.pack()
button2 = tk.Button(self, text="Register", font=FONT, command=lambda: controller.show_frame(RegisterView))
button2.pack()
2020-12-14 13:24:23 +01:00
def login(self, controller, login, password):
2020-12-14 07:02:08 +01:00
print(login)
2020-12-14 13:24:23 +01:00
print(password)
2020-12-14 07:02:08 +01:00
data = {
"email": str(login),
2020-12-14 13:24:23 +01:00
"password": str(password)
2020-12-14 07:02:08 +01:00
}
resp = requests.post(URL, json=data)
print(resp)
2020-12-14 13:24:23 +01:00
if resp.status_code == 200:
response = resp.json()
token = response['token']
controller.show_frame(MainView, token)
2020-12-14 07:02:08 +01:00
else:
print("bad pass")
2020-12-14 13:24:23 +01:00
bad_pass_label = tk.Label(self, text='Wrong login/password!', font=FONT)
bad_pass_label.pack()
return ()
2020-12-14 07:02:08 +01:00
class MainView(tk.Frame):
def __init__(self, parent, controller):
2020-12-14 13:24:23 +01:00
tk.Frame.__init__(self, parent)
self.frames = {}
self.token = ''
2020-12-21 11:33:45 +01:00
for F in (ProfileView, ProjectsView, ProjectsAddView):
2020-12-21 14:23:52 +01:00
frame = F(parent, controller, self)
2020-12-14 13:24:23 +01:00
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
2020-12-14 07:02:08 +01:00
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE)
2020-12-14 13:24:23 +01:00
label.pack(pady=10, padx=10)
2020-12-14 07:02:08 +01:00
label_u = tk.Label(self, text="Main menu", font=FONT)
2020-12-14 13:24:23 +01:00
label_u.pack(pady=10, padx=10)
2020-12-21 11:33:45 +01:00
2020-12-21 14:23:52 +01:00
button_projects = tk.Button(self, text="Projects", font=FONT,
command=lambda: self.show_frame(ProjectsView))
button_projects.pack()
2020-12-14 13:24:23 +01:00
button_profile = tk.Button(self, text="My profile", font=FONT,
command=lambda: self.show_frame(ProfileView))
2020-12-14 07:02:08 +01:00
button_profile.pack()
2020-12-14 13:24:23 +01:00
def show_frame(self, view):
frame = self.frames[view]
frame.tkraise()
if self.token:
print(self.token)
frame.token = self.token
frame.start()
2020-12-14 07:02:08 +01:00
class RegisterView(tk.Frame):
def __init__(self, parent, controller):
2020-12-14 13:24:23 +01:00
tk.Frame.__init__(self, parent)
2020-12-14 07:02:08 +01:00
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE)
2020-12-14 13:24:23 +01:00
label.pack(pady=10, padx=10)
2020-12-14 07:02:08 +01:00
label_u = tk.Label(self, text="Register", font=FONT)
2020-12-14 13:24:23 +01:00
label_u.pack(pady=10, padx=10)
2020-12-14 07:02:08 +01:00
label0 = tk.Label(self, text='Email:', font=FONT)
label0.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
input0 = tk.Entry(self)
input0.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
label1 = tk.Label(self, text='Login:', font=FONT)
label1.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
input1 = tk.Entry(self)
input1.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
label2 = tk.Label(self, text='Password:', font=FONT)
label2.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
input2 = tk.Entry(self, show="*")
input2.pack()
label3 = tk.Label(self, text='First name:', font=FONT)
label3.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
input3 = tk.Entry(self)
input3.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
label4 = tk.Label(self, text='Last name:', font=FONT)
label4.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
input4 = tk.Entry(self)
input4.pack()
2020-12-14 13:24:23 +01:00
button1 = tk.Button(self, text="Register", font=FONT,
command=lambda: self.register(controller, input0.get(), input1.get(), input2.get(),
input3.get(), input4.get()))
2020-12-14 07:02:08 +01:00
button1.pack()
button2 = tk.Button(self, text="Cancel", font=FONT, command=lambda: controller.show_frame(LoginView))
button2.pack()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
def register(self, controller, email, login, passw, name, lastname):
data = {
"email": str(email),
"password": str(passw),
"profile": {
"username": str(login),
"first_name": str(name),
"last_name": str(lastname)
}
2020-12-14 13:24:23 +01:00
}
2020-12-14 07:02:08 +01:00
print(data)
2020-12-14 13:24:23 +01:00
response = requests.post(URL_REGISTER, json=data)
print(response)
if response.status_code == 201:
response = response.json()
2020-12-14 07:02:08 +01:00
controller.show_frame(LoginView)
else:
print("sth wrong")
2020-12-14 13:24:23 +01:00
bad_pass_label = tk.Label(self, text='Something went wrong!', font=FONT)
bad_pass_label.pack()
return ()
2020-12-14 07:02:08 +01:00
class ProfileView(tk.Frame):
2020-12-21 14:23:52 +01:00
def __init__(self, parent, controller, main_view_controller):
2020-12-14 13:24:23 +01:00
tk.Frame.__init__(self, parent)
self.token = ''
2020-12-14 07:02:08 +01:00
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE)
2020-12-14 13:24:23 +01:00
label.pack(pady=10, padx=10)
2020-12-14 07:02:08 +01:00
label_l1 = tk.Label(self, text="Login:", font=FONT_B)
2020-12-14 13:24:23 +01:00
label_l1.pack(pady=10, padx=10)
self.label_username = tk.Label(self, text='', font=FONT)
self.label_username.pack(pady=10, padx=10)
2020-12-14 07:02:08 +01:00
label_n1 = tk.Label(self, text="Name:", font=FONT_B)
2020-12-14 13:24:23 +01:00
label_n1.pack(pady=10, padx=10)
self.label_first_name = tk.Label(self, text='', font=FONT)
self.label_first_name.pack(pady=10, padx=10)
2020-12-14 07:02:08 +01:00
label_ln1 = tk.Label(self, text="Last name", font=FONT_B)
2020-12-14 13:24:23 +01:00
label_ln1.pack(pady=10, padx=10)
self.label_last_name = tk.Label(self, text='', font=FONT)
self.label_last_name.pack(pady=10, padx=10)
2020-12-14 07:02:08 +01:00
2020-12-14 13:24:23 +01:00
button_profile = tk.Button(self, text="Back", font=FONT,
command=lambda: controller.show_frame(MainView, self.token))
2020-12-14 07:02:08 +01:00
button_profile.pack()
2020-12-14 13:24:23 +01:00
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']
2020-12-21 14:23:52 +01:00
2020-12-21 11:33:45 +01:00
class ProjectsView(tk.Frame):
2020-12-21 14:23:52 +01:00
def __init__(self, parent, controller, main_view_controller):
2020-12-21 11:33:45 +01:00
tk.Frame.__init__(self, parent)
self.token = ''
2020-12-21 14:23:52 +01:00
self.projects_buttons = []
2020-12-21 11:33:45 +01:00
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE)
label.pack(pady=10, padx=10)
button_add = tk.Button(self, text="Add project", font=FONT,
2020-12-21 14:23:52 +01:00
command=lambda: main_view_controller.show_frame(ProjectsAddView))
2020-12-21 11:33:45 +01:00
button_add.pack()
2020-12-21 14:23:52 +01:00
2020-12-21 11:33:45 +01:00
button_back = tk.Button(self, text="Back", font=FONT,
2020-12-21 14:23:52 +01:00
command=lambda: controller.show_frame(MainView, self.token))
2020-12-21 11:33:45 +01:00
button_back.pack()
label0 = tk.Label(self, text='Projects:', font=FONT)
label0.pack()
def start(self):
2020-12-21 14:23:52 +01:00
for button in self.projects_buttons:
button.destroy()
self.projects_buttons = []
2020-12-21 11:33:45 +01:00
headers = {'Authorization': 'Bearer ' + self.token}
resp = requests.get(URL_PROJECTS, headers=headers)
response = resp.json()
print(response)
for projects in response:
button_proj = tk.Button(self, text=projects['name'], font=FONT)
button_proj.pack()
2020-12-21 14:23:52 +01:00
self.projects_buttons.append(button_proj)
2020-12-21 11:33:45 +01:00
class ProjectsAddView(tk.Frame):
2020-12-21 14:23:52 +01:00
def __init__(self, parent, controller, main_view_controller):
2020-12-21 11:33:45 +01:00
tk.Frame.__init__(self, parent)
self.token = ''
2020-12-21 14:23:52 +01:00
2020-12-21 11:33:45 +01:00
label0 = tk.Label(self, text='Project name:', font=FONT)
label0.pack()
2020-12-21 14:23:52 +01:00
2020-12-21 11:33:45 +01:00
input0 = tk.Entry(self)
input0.pack()
2020-12-21 14:23:52 +01:00
2020-12-21 11:33:45 +01:00
button_add = tk.Button(self, text="Confirm and add", font=FONT,
2020-12-21 14:23:52 +01:00
command=lambda: self.add_project(main_view_controller, input0.get()))
2020-12-21 11:33:45 +01:00
button_add.pack()
button_back = tk.Button(self, text="Back", font=FONT,
2020-12-21 14:23:52 +01:00
command=lambda: main_view_controller.show_frame(ProjectsView))
2020-12-21 11:33:45 +01:00
button_back.pack()
def start(self):
print("ok")
2020-12-21 14:23:52 +01:00
def add_project(self, controller, project_name):
2020-12-21 11:33:45 +01:00
headers = {'Authorization': 'Bearer ' + self.token}
data = {
2020-12-21 14:23:52 +01:00
"name": str(project_name)
}
2020-12-21 11:33:45 +01:00
print(data)
response = requests.post(URL_PROJECTS, json=data, headers=headers)
print(response)
if response.status_code == 201:
response = response.json()
2020-12-21 14:23:52 +01:00
controller.show_frame(ProjectsView)
2020-12-21 11:33:45 +01:00
else:
print("sth wrong")
bad_pass_label = tk.Label(self, text='Something went wrong!', font=FONT)
bad_pass_label.pack()
return ()
2020-12-14 13:24:23 +01:00
2020-12-14 07:02:08 +01:00
app = SmartPicasso()
2020-12-14 13:24:23 +01:00
app.mainloop()