From c9610f77658baf6c690d0f00a326deb904d43d51 Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 26 Apr 2021 22:50:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D1=81=D0=BC=D0=B5=D1=82=D0=B8?= =?UTF-8?q?=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D0=BA=D0=BE=D0=B4?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/api.py | 33 +++++++++++++++++---------------- frontend/api_demo.py | 4 +++- frontend/user.py | 42 ++++++++++++++++++++---------------------- frontend/workspace.py | 9 ++++----- 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/frontend/api.py b/frontend/api.py index f3c5f0d..ad54387 100644 --- a/frontend/api.py +++ b/frontend/api.py @@ -101,13 +101,13 @@ class UserApi(object): headers=self._access_token_(), ) ) - + def lists_delete(self, id): """ Auth required - + Deletes a to-do list by id - + Parameters ---------- id: to-do list id to delete @@ -118,7 +118,7 @@ class UserApi(object): headers=self._access_token_(), ) ) - + def lists_update(self, title, id): """ Rename a new to-do list @@ -126,7 +126,7 @@ class UserApi(object): Parameters ---------- - title : str + title : str New name for a list. id : int @@ -138,7 +138,7 @@ class UserApi(object): headers=self._access_token_(), ) ) - + def todo_items_list(self, **argv): """ List all the exsiting to-do items. @@ -155,7 +155,7 @@ class UserApi(object): url=self.get_api(API_TODO_ITEMS_LIST), headers=self._access_token_(), params=argv ) ) - + def todo_items_create(self, parent, text="Note"): """ Create a new to-do item @@ -171,17 +171,17 @@ class UserApi(object): return UserApi._raise_or_return_( requests.post( url=self.get_api(API_TODO_ITEMS_CREATE), - json={"text": text, "parent":parent, "finished":False}, + json={"text": text, "parent": parent, "finished": False}, headers=self._access_token_(), ) ) - + def todo_items_delete(self, id): """ Auth required - + Deletes a to-do item by id - + Parameters ---------- id: to-do item id to delete @@ -192,7 +192,7 @@ class UserApi(object): headers=self._access_token_(), ) ) - + def todo_items_update(self, id, text, finished, parent): """ Rename a new to-do list @@ -202,7 +202,7 @@ class UserApi(object): ---------- id : int Note id - text : str + text : str New note for the item. finished : bool New state for the item @@ -212,11 +212,11 @@ class UserApi(object): return UserApi._raise_or_return_( requests.put( url=self.get_api(API_TODO_ITEMS_UPDATE.format(id)), - json={"text": text, "finished":finished, "parent":parent}, + json={"text": text, "finished": finished, "parent": parent}, headers=self._access_token_(), ) ) - + # def create(self, title="Untitled"): # """ # Create a new to-do list @@ -315,5 +315,6 @@ class UserApi(object): response.raise_for_status() try: return response.json() - except: + except Exception as e: + print(e) return response.content diff --git a/frontend/api_demo.py b/frontend/api_demo.py index ee1959b..9ac61b9 100644 --- a/frontend/api_demo.py +++ b/frontend/api_demo.py @@ -4,7 +4,9 @@ from user import User def print_lists(lists): for item in lists: - print(f"List: '{item.title}'", f"Id: {item.id}", "|", "|".join([str(x) for x in item.items_])) + print( + f"List: '{item.title}'", f"Id: {item.id}", "|", "|".join([str(x) for x in item.items_]) + ) DEFAULT_URL = "http://127.0.0.1:8000" diff --git a/frontend/user.py b/frontend/user.py index 51fab7d..bce6629 100644 --- a/frontend/user.py +++ b/frontend/user.py @@ -1,7 +1,5 @@ import os -from types import SimpleNamespace - from datetime import datetime import numpy as np @@ -14,12 +12,13 @@ UPDATE_ERROR = "Failed to update property: {0}" DATETIME_STR = "%Y-%m-%dT%H:%M:%S.%fZ" + def bad_arguments(x, d): return list((set(x) - set(d))) + class ToDoList(object): - def __init__(self, id, title, created_at=None, items=None, - parent=None, user=None): + def __init__(self, id, title, created_at=None, items=None, parent=None, user=None): self.id = id self.title = title self.items_ = [] if items is None else items @@ -44,7 +43,7 @@ class ToDoList(object): def index(self, value): return self.items_.index(value) - + def remove(self, index): """ Remove item at index from db @@ -52,7 +51,7 @@ class ToDoList(object): item = self.items_[index] self.items_.remove(item) item.dispose() - + def append(self, text): """ Add a new item to db @@ -72,26 +71,24 @@ class ToDoList(object): for key, value in argv.items(): setattr(self, key, value) self.sync() - + def dispose(self): print(f"To-do list id '{self.id}' is being disposed of...") if "DEBUG" in os.environ: return for item in self.items_: item.dispose() - self.user.lists_delete(self.id) - + self.user.lists_delete(self.id) + def sync(self): print(f"Item '{self}' is being synchronized...") if "DEBUG" in os.environ: return self.user.lists_update(title=self.title, id=self.id) - class ToDoItem(object): - def __init__(self, id, text, finished=False, created_at=None, - parent=None, user=None): + def __init__(self, id, text, finished=False, created_at=None, parent=None, user=None): self.id = id self.text = text self.finished = finished @@ -113,19 +110,21 @@ class ToDoItem(object): for key, value in argv.items(): setattr(self, key, value) self.sync() - + def dispose(self): print(f"To-do item id '{self.id}' is being disposed of...") if "DEBUG" in os.environ: return self.user.todo_items_delete(self.id) - + # ToDo def sync(self): print(f"Item '{self}' is being synchronized...") if "DEBUG" in os.environ: return - self.user.todo_items_update(id=self.id, text=self.text, finished=self.finished, parent=self.parent) + self.user.todo_items_update( + id=self.id, text=self.text, finished=self.finished, parent=self.parent + ) def make_debug_lists(): @@ -142,8 +141,8 @@ def make_debug_lists(): for i in range(10) ] + class User(UserApi): - def auth(self, user, passwd): """ Basic authentification @@ -151,10 +150,10 @@ class User(UserApi): if "DEBUG" in os.environ: return UserApi.auth(self, user, passwd) - + # Storing lists - mostly for debug purposes lists_ = make_debug_lists() - + def fetchUserLists(self): """ Fetch existing user lists from the server @@ -165,7 +164,7 @@ class User(UserApi): return self.lists_ user_lists = self.lists_list()["results"] user_items = self.todo_items_list()["results"] - toDoLists = {x["id"]:ToDoList(**x, user=self) for x in user_lists} + toDoLists = {x["id"]: ToDoList(**x, user=self) for x in user_lists} toDoItems = [ToDoItem(**x, user=self) for x in user_items] for toDoItem in toDoItems: # Catching stray items @@ -180,15 +179,14 @@ class User(UserApi): def removeUserList(self, id): """ - Remove existing user to-do list from the serverreturns: + Remove existing user to-do list from the serverreturns: """ to_remove = [item for item in self.lists_ if item.id == id][0] self.lists_.remove(to_remove) if not ("DEBUG" in os.environ): to_remove.dispose() return self.lists_ - - + def appendUserList(self, title): """ Create a new user list diff --git a/frontend/workspace.py b/frontend/workspace.py index c3eedcf..c82d2fd 100644 --- a/frontend/workspace.py +++ b/frontend/workspace.py @@ -152,7 +152,7 @@ class WorkSpaceFrame(tk.Frame): # todo lists self.toToList = ToDoListWidget(self) self.toToList.pack(side="left", fill="both", expand=1) - + def add_list(self, *args): text = self.add_list_text.get(1.0, "end").strip() if len(text) == 0: @@ -160,18 +160,17 @@ class WorkSpaceFrame(tk.Frame): return self.user.appendUserList(title=text) self.lists = self.user.fetchUserLists() - + # fill list box - self.listBox.delete(0,'end') + self.listBox.delete(0, "end") for item in self.lists: s = f"{str(item)}: {item.created_at.strftime('%Y-%m-%d %H:%M:%S')}" self.listBox.insert(tk.END, s) self.listBox.pack() len(self.lists) > 0 and self.listBox.selection_set(first=0) - def listBox_selected(self, *args): - + self.toToList.clear() selection = self.listBox.curselection()