Косметические изменения в коде

This commit is contained in:
Ivan
2021-04-26 22:50:40 +03:00
parent b2b447e392
commit c9610f7765
4 changed files with 44 additions and 44 deletions

View File

@@ -101,13 +101,13 @@ class UserApi(object):
headers=self._access_token_(), headers=self._access_token_(),
) )
) )
def lists_delete(self, id): def lists_delete(self, id):
""" """
Auth required Auth required
Deletes a to-do list by id Deletes a to-do list by id
Parameters Parameters
---------- ----------
id: to-do list id to delete id: to-do list id to delete
@@ -118,7 +118,7 @@ class UserApi(object):
headers=self._access_token_(), headers=self._access_token_(),
) )
) )
def lists_update(self, title, id): def lists_update(self, title, id):
""" """
Rename a new to-do list Rename a new to-do list
@@ -126,7 +126,7 @@ class UserApi(object):
Parameters Parameters
---------- ----------
title : str title : str
New name for a list. New name for a list.
id : int id : int
@@ -138,7 +138,7 @@ class UserApi(object):
headers=self._access_token_(), headers=self._access_token_(),
) )
) )
def todo_items_list(self, **argv): def todo_items_list(self, **argv):
""" """
List all the exsiting to-do items. 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 url=self.get_api(API_TODO_ITEMS_LIST), headers=self._access_token_(), params=argv
) )
) )
def todo_items_create(self, parent, text="Note"): def todo_items_create(self, parent, text="Note"):
""" """
Create a new to-do item Create a new to-do item
@@ -171,17 +171,17 @@ class UserApi(object):
return UserApi._raise_or_return_( return UserApi._raise_or_return_(
requests.post( requests.post(
url=self.get_api(API_TODO_ITEMS_CREATE), 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_(), headers=self._access_token_(),
) )
) )
def todo_items_delete(self, id): def todo_items_delete(self, id):
""" """
Auth required Auth required
Deletes a to-do item by id Deletes a to-do item by id
Parameters Parameters
---------- ----------
id: to-do item id to delete id: to-do item id to delete
@@ -192,7 +192,7 @@ class UserApi(object):
headers=self._access_token_(), headers=self._access_token_(),
) )
) )
def todo_items_update(self, id, text, finished, parent): def todo_items_update(self, id, text, finished, parent):
""" """
Rename a new to-do list Rename a new to-do list
@@ -202,7 +202,7 @@ class UserApi(object):
---------- ----------
id : int id : int
Note id Note id
text : str text : str
New note for the item. New note for the item.
finished : bool finished : bool
New state for the item New state for the item
@@ -212,11 +212,11 @@ class UserApi(object):
return UserApi._raise_or_return_( return UserApi._raise_or_return_(
requests.put( requests.put(
url=self.get_api(API_TODO_ITEMS_UPDATE.format(id)), 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_(), headers=self._access_token_(),
) )
) )
# def create(self, title="Untitled"): # def create(self, title="Untitled"):
# """ # """
# Create a new to-do list # Create a new to-do list
@@ -315,5 +315,6 @@ class UserApi(object):
response.raise_for_status() response.raise_for_status()
try: try:
return response.json() return response.json()
except: except Exception as e:
print(e)
return response.content return response.content

View File

@@ -4,7 +4,9 @@ from user import User
def print_lists(lists): def print_lists(lists):
for item in 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" DEFAULT_URL = "http://127.0.0.1:8000"

View File

@@ -1,7 +1,5 @@
import os import os
from types import SimpleNamespace
from datetime import datetime from datetime import datetime
import numpy as np import numpy as np
@@ -14,12 +12,13 @@ UPDATE_ERROR = "Failed to update property: {0}"
DATETIME_STR = "%Y-%m-%dT%H:%M:%S.%fZ" DATETIME_STR = "%Y-%m-%dT%H:%M:%S.%fZ"
def bad_arguments(x, d): def bad_arguments(x, d):
return list((set(x) - set(d))) return list((set(x) - set(d)))
class ToDoList(object): class ToDoList(object):
def __init__(self, id, title, created_at=None, items=None, def __init__(self, id, title, created_at=None, items=None, parent=None, user=None):
parent=None, user=None):
self.id = id self.id = id
self.title = title self.title = title
self.items_ = [] if items is None else items self.items_ = [] if items is None else items
@@ -44,7 +43,7 @@ class ToDoList(object):
def index(self, value): def index(self, value):
return self.items_.index(value) return self.items_.index(value)
def remove(self, index): def remove(self, index):
""" """
Remove item at index from db Remove item at index from db
@@ -52,7 +51,7 @@ class ToDoList(object):
item = self.items_[index] item = self.items_[index]
self.items_.remove(item) self.items_.remove(item)
item.dispose() item.dispose()
def append(self, text): def append(self, text):
""" """
Add a new item to db Add a new item to db
@@ -72,26 +71,24 @@ class ToDoList(object):
for key, value in argv.items(): for key, value in argv.items():
setattr(self, key, value) setattr(self, key, value)
self.sync() self.sync()
def dispose(self): def dispose(self):
print(f"To-do list id '{self.id}' is being disposed of...") print(f"To-do list id '{self.id}' is being disposed of...")
if "DEBUG" in os.environ: if "DEBUG" in os.environ:
return return
for item in self.items_: for item in self.items_:
item.dispose() item.dispose()
self.user.lists_delete(self.id) self.user.lists_delete(self.id)
def sync(self): def sync(self):
print(f"Item '{self}' is being synchronized...") print(f"Item '{self}' is being synchronized...")
if "DEBUG" in os.environ: if "DEBUG" in os.environ:
return return
self.user.lists_update(title=self.title, id=self.id) self.user.lists_update(title=self.title, id=self.id)
class ToDoItem(object): class ToDoItem(object):
def __init__(self, id, text, finished=False, created_at=None, def __init__(self, id, text, finished=False, created_at=None, parent=None, user=None):
parent=None, user=None):
self.id = id self.id = id
self.text = text self.text = text
self.finished = finished self.finished = finished
@@ -113,19 +110,21 @@ class ToDoItem(object):
for key, value in argv.items(): for key, value in argv.items():
setattr(self, key, value) setattr(self, key, value)
self.sync() self.sync()
def dispose(self): def dispose(self):
print(f"To-do item id '{self.id}' is being disposed of...") print(f"To-do item id '{self.id}' is being disposed of...")
if "DEBUG" in os.environ: if "DEBUG" in os.environ:
return return
self.user.todo_items_delete(self.id) self.user.todo_items_delete(self.id)
# ToDo # ToDo
def sync(self): def sync(self):
print(f"Item '{self}' is being synchronized...") print(f"Item '{self}' is being synchronized...")
if "DEBUG" in os.environ: if "DEBUG" in os.environ:
return 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(): def make_debug_lists():
@@ -142,8 +141,8 @@ def make_debug_lists():
for i in range(10) for i in range(10)
] ]
class User(UserApi): class User(UserApi):
def auth(self, user, passwd): def auth(self, user, passwd):
""" """
Basic authentification Basic authentification
@@ -151,10 +150,10 @@ class User(UserApi):
if "DEBUG" in os.environ: if "DEBUG" in os.environ:
return return
UserApi.auth(self, user, passwd) UserApi.auth(self, user, passwd)
# Storing lists - mostly for debug purposes # Storing lists - mostly for debug purposes
lists_ = make_debug_lists() lists_ = make_debug_lists()
def fetchUserLists(self): def fetchUserLists(self):
""" """
Fetch existing user lists from the server Fetch existing user lists from the server
@@ -165,7 +164,7 @@ class User(UserApi):
return self.lists_ return self.lists_
user_lists = self.lists_list()["results"] user_lists = self.lists_list()["results"]
user_items = self.todo_items_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] toDoItems = [ToDoItem(**x, user=self) for x in user_items]
for toDoItem in toDoItems: for toDoItem in toDoItems:
# Catching stray items # Catching stray items
@@ -180,15 +179,14 @@ class User(UserApi):
def removeUserList(self, id): 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] to_remove = [item for item in self.lists_ if item.id == id][0]
self.lists_.remove(to_remove) self.lists_.remove(to_remove)
if not ("DEBUG" in os.environ): if not ("DEBUG" in os.environ):
to_remove.dispose() to_remove.dispose()
return self.lists_ return self.lists_
def appendUserList(self, title): def appendUserList(self, title):
""" """
Create a new user list Create a new user list

View File

@@ -152,7 +152,7 @@ class WorkSpaceFrame(tk.Frame):
# todo lists # todo lists
self.toToList = ToDoListWidget(self) self.toToList = ToDoListWidget(self)
self.toToList.pack(side="left", fill="both", expand=1) self.toToList.pack(side="left", fill="both", expand=1)
def add_list(self, *args): def add_list(self, *args):
text = self.add_list_text.get(1.0, "end").strip() text = self.add_list_text.get(1.0, "end").strip()
if len(text) == 0: if len(text) == 0:
@@ -160,18 +160,17 @@ class WorkSpaceFrame(tk.Frame):
return return
self.user.appendUserList(title=text) self.user.appendUserList(title=text)
self.lists = self.user.fetchUserLists() self.lists = self.user.fetchUserLists()
# fill list box # fill list box
self.listBox.delete(0,'end') self.listBox.delete(0, "end")
for item in self.lists: for item in self.lists:
s = f"{str(item)}: {item.created_at.strftime('%Y-%m-%d %H:%M:%S')}" s = f"{str(item)}: {item.created_at.strftime('%Y-%m-%d %H:%M:%S')}"
self.listBox.insert(tk.END, s) self.listBox.insert(tk.END, s)
self.listBox.pack() self.listBox.pack()
len(self.lists) > 0 and self.listBox.selection_set(first=0) len(self.lists) > 0 and self.listBox.selection_set(first=0)
def listBox_selected(self, *args): def listBox_selected(self, *args):
self.toToList.clear() self.toToList.clear()
selection = self.listBox.curselection() selection = self.listBox.curselection()