From d45c662e113b7184686e684806422320ccf4b6b4 Mon Sep 17 00:00:00 2001 From: Jakob Cornell Date: Sun, 12 Jan 2020 18:14:49 -0600 Subject: [PATCH] Add student rename --- main.py | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index 27e0f11..8ceae69 100644 --- a/main.py +++ b/main.py @@ -159,6 +159,18 @@ class MainModel(Model): self.select_auto() self.view.update_controls() + def rename_student(self, curr_name, new_name): + if new_name != curr_name: + if new_name in self.data: + raise ValueError("Name already in use") + else: + data = self.data[curr_name] + self.data[new_name] = data + del self.data[curr_name] + self.view.set_student_list(self.view_names()) + curr = new_name if self.state['student'] == curr_name else self.state['student'] + self.select_student(curr) + def select_auto(self): names = self.view_names() self.select_student(names[0] if names else None) @@ -181,28 +193,6 @@ class MainModel(Model): self.view.set_student(name) -class AsyncHttpServer: - def __init__(self, port, data): - class Handler(http.server.BaseHTTPRequestHandler): - def __init__(self): pass - def __call__(self, *args): super().__init__(*args) - - def do_GET(self): - self.send_response(200) - self.send_header('Content-Type', 'application/json; charset=UTF-8') - self.send_header('Access-Control-Allow-Origin', '*') - self.end_headers() - self.wfile.write(data) - - self.server = http.server.HTTPServer(('localhost', port), Handler()) - - def start(self): - Thread(target = lambda: self.server.serve_forever()).start() - - def stop(self): - self.server.shutdown() - self.server.server_close() - class MainView(View): @contextmanager def no_propagate(self, key): @@ -261,6 +251,16 @@ class MainView(View): messagebox.showerror("Error", "Student already exists") add_student_btn = ttk.Button(self.student_frame, text = "Add", command = on_student_add) + def on_student_rename(): + curr = self.controller.current_student() + name = simpledialog.askstring("Rename student", "New name for {}".format(curr)) + if name: + try: + self.controller.rename_student(curr, name) + except ValueError as e: + messagebox.showerror("Error", str(e)) + self.rename_student_btn = ttk.Button(self.student_frame, text = "Rename", command = on_student_rename) + def on_student_del(): curr_name = self.controller.current_student() result = messagebox.askokcancel("Confirm", "Delete student {}?".format(curr_name)) @@ -343,6 +343,7 @@ class MainView(View): self.student_sel.pack(**TK_PAD, side = tk.LEFT) add_student_btn.pack(**TK_PAD, side = tk.LEFT) + self.rename_student_btn.pack(**TK_PAD, side = tk.LEFT) self.del_student_btn.pack(**TK_PAD, side = tk.LEFT) self.student_frame.pack(**TK_PAD, fill = tk.X) self.edit_data_btn.pack(**TK_PAD, fill = tk.X) @@ -362,6 +363,7 @@ class MainView(View): state = 'enable' if self.controller.data else 'disable' students_only = [ self.student_sel, + self.rename_student_btn, self.del_student_btn, self.edit_data_btn, self.gen_plot_btn, -- 2.30.2