SMART-49 implemented add project view

This commit is contained in:
s460930 2020-12-21 14:23:52 +01:00
parent ad6a8a4e3c
commit 0aad49aaec

View File

@ -10,6 +10,7 @@ URL_REGISTER = "http://localhost:8000/api/register"
URL_PROFILE = "http://localhost:8000/api/profile" URL_PROFILE = "http://localhost:8000/api/profile"
URL_PROJECTS = "http://localhost:8000/api/projects" URL_PROJECTS = "http://localhost:8000/api/projects"
class SmartPicasso(tk.Tk): class SmartPicasso(tk.Tk):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -96,7 +97,7 @@ class MainView(tk.Frame):
self.frames = {} self.frames = {}
self.token = '' self.token = ''
for F in (ProfileView, ProjectsView, ProjectsAddView): for F in (ProfileView, ProjectsView, ProjectsAddView):
frame = F(parent, controller) frame = F(parent, controller, self)
self.frames[F] = frame self.frames[F] = frame
@ -110,16 +111,10 @@ class MainView(tk.Frame):
command=lambda: self.show_frame(ProjectsView)) command=lambda: self.show_frame(ProjectsView))
button_projects.pack() button_projects.pack()
button_projects2 = tk.Button(self, text="Projects Add", font=FONT,
command=lambda: self.show_frame(ProjectsAddView))
button_projects2.pack()
button_profile = tk.Button(self, text="My profile", font=FONT, button_profile = tk.Button(self, text="My profile", font=FONT,
command=lambda: self.show_frame(ProfileView)) command=lambda: self.show_frame(ProfileView))
button_profile.pack() button_profile.pack()
def show_frame(self, view): def show_frame(self, view):
frame = self.frames[view] frame = self.frames[view]
frame.tkraise() frame.tkraise()
@ -202,7 +197,7 @@ class RegisterView(tk.Frame):
class ProfileView(tk.Frame): class ProfileView(tk.Frame):
def __init__(self, parent, controller): def __init__(self, parent, controller, main_view_controller):
tk.Frame.__init__(self, parent) tk.Frame.__init__(self, parent)
self.token = '' self.token = ''
label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE) label = tk.Label(self, text="SmartPicasso", font=FONT_LARGE)
@ -233,15 +228,17 @@ class ProfileView(tk.Frame):
self.label_first_name['text'] = response['profile']['first_name'] self.label_first_name['text'] = response['profile']['first_name']
self.label_last_name['text'] = response['profile']['last_name'] self.label_last_name['text'] = response['profile']['last_name']
class ProjectsView(tk.Frame): class ProjectsView(tk.Frame):
def __init__(self, parent, controller): def __init__(self, parent, controller, main_view_controller):
tk.Frame.__init__(self, parent) tk.Frame.__init__(self, parent)
self.token = '' self.token = ''
self.projects_buttons = []
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)
button_add = tk.Button(self, text="Add project", font=FONT, button_add = tk.Button(self, text="Add project", font=FONT,
command=lambda: controller.show_frame(ProjectsAddView, self.token)) command=lambda: main_view_controller.show_frame(ProjectsAddView))
button_add.pack() button_add.pack()
button_back = tk.Button(self, text="Back", font=FONT, button_back = tk.Button(self, text="Back", font=FONT,
@ -251,6 +248,9 @@ class ProjectsView(tk.Frame):
label0.pack() label0.pack()
def start(self): def start(self):
for button in self.projects_buttons:
button.destroy()
self.projects_buttons = []
headers = {'Authorization': 'Bearer ' + self.token} headers = {'Authorization': 'Bearer ' + self.token}
resp = requests.get(URL_PROJECTS, headers=headers) resp = requests.get(URL_PROJECTS, headers=headers)
response = resp.json() response = resp.json()
@ -258,10 +258,12 @@ class ProjectsView(tk.Frame):
for projects in response: for projects in response:
button_proj = tk.Button(self, text=projects['name'], font=FONT) button_proj = tk.Button(self, text=projects['name'], font=FONT)
button_proj.pack() button_proj.pack()
self.projects_buttons.append(button_proj)
class ProjectsAddView(tk.Frame): class ProjectsAddView(tk.Frame):
def __init__(self, parent, controller): def __init__(self, parent, controller, main_view_controller):
tk.Frame.__init__(self, parent) tk.Frame.__init__(self, parent)
self.token = '' self.token = ''
@ -272,27 +274,27 @@ class ProjectsAddView(tk.Frame):
input0.pack() input0.pack()
button_add = tk.Button(self, text="Confirm and add", font=FONT, button_add = tk.Button(self, text="Confirm and add", font=FONT,
command=lambda: self.addproject(controller, input0.get())) command=lambda: self.add_project(main_view_controller, input0.get()))
button_add.pack() button_add.pack()
button_back = tk.Button(self, text="Back", font=FONT, button_back = tk.Button(self, text="Back", font=FONT,
command=lambda: controller.show_frame(ProjectsView)) command=lambda: main_view_controller.show_frame(ProjectsView))
button_back.pack() button_back.pack()
def start(self): def start(self):
print("ok") print("ok")
def addproject(self, controller, projectname): def add_project(self, controller, project_name):
headers = {'Authorization': 'Bearer ' + self.token} headers = {'Authorization': 'Bearer ' + self.token}
data = { data = {
"name": str(projectname) "name": str(project_name)
} }
print(data) print(data)
response = requests.post(URL_PROJECTS, json=data, headers=headers) response = requests.post(URL_PROJECTS, json=data, headers=headers)
print(response) print(response)
if response.status_code == 201: if response.status_code == 201:
response = response.json() response = response.json()
controller.show_frame(MainView) controller.show_frame(ProjectsView)
else: else:
print("sth wrong") print("sth wrong")
bad_pass_label = tk.Label(self, text='Something went wrong!', font=FONT) bad_pass_label = tk.Label(self, text='Something went wrong!', font=FONT)