Добавлено api для работы со списками и задачами
This commit is contained in:
192
frontend/api.py
192
frontend/api.py
@@ -1,17 +1,27 @@
|
|||||||
|
from types import SimpleNamespace
|
||||||
import urllib
|
import urllib
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
DEFAULT_URL = "http://127.0.0.1:8000"
|
DEFAULT_URL = "http://127.0.0.1:8000"
|
||||||
|
|
||||||
|
API_TODO_ITEMS_LIST = "api/todo_items/"
|
||||||
|
API_TODO_ITEMS_CREATE = "api/todo_items/"
|
||||||
|
API_TODO_ITEMS_READ = "api/todo_items/{0}/"
|
||||||
|
API_TODO_ITEMS_UPDATE = "api/todo_items/{0}/"
|
||||||
|
API_TODO_ITEMS_PARTIAL_UPDATE = "api/todo_items/{0}/"
|
||||||
|
API_TODO_ITEMS_DELETE = "api/todo_items/{0}/"
|
||||||
|
|
||||||
API_LISTS_LIST = "api/lists/"
|
API_LISTS_LIST = "api/lists/"
|
||||||
API_LISTS_CREATE = "api/lists/"
|
API_LISTS_CREATE = "api/lists/"
|
||||||
API_LISTS_READ = "lists/{0}/"
|
API_LISTS_READ = "lists/{0}/"
|
||||||
API_LISTS_UPDATE = "lists/{0}/"
|
API_LISTS_UPDATE = "lists/{0}/"
|
||||||
API_LISTS_PARTIAL_UPDATE = "lists/{0}/"
|
API_LISTS_PARTIAL_UPDATE = "lists/{0}/"
|
||||||
API_LISTS_DELETE = "lists/{0}/"
|
API_LISTS_DELETE = "lists/{0}/"
|
||||||
|
|
||||||
API_TOKEN = "api/token/"
|
API_TOKEN = "api/token/"
|
||||||
|
|
||||||
|
|
||||||
class User(object):
|
class UserApi(object):
|
||||||
def __init__(self, url=DEFAULT_URL, token=None):
|
def __init__(self, url=DEFAULT_URL, token=None):
|
||||||
"""
|
"""
|
||||||
Constructor
|
Constructor
|
||||||
@@ -50,14 +60,13 @@ class User(object):
|
|||||||
Generated auth token.
|
Generated auth token.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
url = self.get_api(API_TOKEN)
|
token = UserApi._raise_or_return_(
|
||||||
data = {"username": user, "password": passwd}
|
requests.post(url=self.get_api(API_TOKEN), json={"username": user, "password": passwd})
|
||||||
response = requests.post(url=url, json=data)
|
)
|
||||||
response.raise_for_status()
|
self.token = SimpleNamespace(**token)
|
||||||
self.token = response.json()
|
|
||||||
return self.token
|
return self.token
|
||||||
|
|
||||||
def list(self):
|
def lists_list(self, **argv):
|
||||||
"""
|
"""
|
||||||
List all the exsiting to-do lists.
|
List all the exsiting to-do lists.
|
||||||
Auth required
|
Auth required
|
||||||
@@ -68,13 +77,16 @@ class User(object):
|
|||||||
to-do lists.
|
to-do lists.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
response = requests.get(url=self.get_api(API_LISTS_LIST), headers=self._access_token_())
|
return UserApi._raise_or_return_(
|
||||||
response.raise_for_status()
|
requests.get(
|
||||||
return response.json()
|
url=self.get_api(API_LISTS_LIST), headers=self._access_token_(), params=argv
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def create(self, title="Untitled"):
|
def lists_create(self, title="Untitled"):
|
||||||
"""
|
"""
|
||||||
Create a new to-do list
|
Create a new to-do list
|
||||||
|
Auth required
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
@@ -82,83 +94,125 @@ class User(object):
|
|||||||
New list name. The default is "Untitled".
|
New list name. The default is "Untitled".
|
||||||
|
|
||||||
"""
|
"""
|
||||||
response = requests.post(
|
return UserApi._raise_or_return_(
|
||||||
url=self.get_api(API_LISTS_CREATE), json={"title": title}, headers=self._access_token_()
|
requests.post(
|
||||||
|
url=self.get_api(API_LISTS_CREATE),
|
||||||
|
json={"title": title},
|
||||||
|
headers=self._access_token_(),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
|
||||||
return response.json()
|
|
||||||
|
|
||||||
def read(self, id):
|
def todo_items_list(self, **argv):
|
||||||
"""
|
"""
|
||||||
Read a to-do list contents
|
List all the exsiting to-do items.
|
||||||
|
Auth required
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
id : int
|
|
||||||
List id.
|
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
list
|
list
|
||||||
Requested contents
|
to-do items.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
response = requests.post(
|
return UserApi._raise_or_return_(
|
||||||
url=self.get_api(API_LISTS_READ).format(id), headers=self._access_token_()
|
requests.get(
|
||||||
|
url=self.get_api(API_TODO_ITEMS_LIST), headers=self._access_token_(), params=argv
|
||||||
|
)
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
|
||||||
return response.json()
|
|
||||||
|
|
||||||
def update(self, id, title="Untitled"):
|
# def create(self, title="Untitled"):
|
||||||
"""
|
# """
|
||||||
Add a to-do item to the list
|
# Create a new to-do list
|
||||||
|
# Auth required
|
||||||
|
|
||||||
Parameters
|
# Parameters
|
||||||
----------
|
# ----------
|
||||||
id : int
|
# title : str, optional
|
||||||
List id.
|
# New list name. The default is "Untitled".
|
||||||
title : str, optional
|
|
||||||
To-do item title. The default is "Untitled".
|
|
||||||
|
|
||||||
"""
|
# """
|
||||||
response = requests.put(
|
# response = requests.post(
|
||||||
json={"title": title},
|
# url=self.get_api(API_LISTS_CREATE), json={"title": title}, headers=self._access_token_()
|
||||||
url=self.get_api(API_LISTS_UPDATE).format(id),
|
# url=self.get_api(API_TODO_ITEMS_CREATE), json={"title": title}, headers=self._access_token_()
|
||||||
headers=self._access_token_(),
|
# )
|
||||||
)
|
# response.raise_for_status()
|
||||||
response.raise_for_status()
|
# return response.json()
|
||||||
return response.json()
|
|
||||||
|
|
||||||
def partial_update(self, id, title="Untitled"):
|
# def read(self, id):
|
||||||
"""
|
# """
|
||||||
Update list item - untrusted
|
# Read a to-do list contents
|
||||||
|
|
||||||
"""
|
# Parameters
|
||||||
response = requests.patch(
|
# ----------
|
||||||
json={"title": title},
|
# id : int
|
||||||
url=self.get_api(API_LISTS_PARTIAL_UPDATE).format(id),
|
# List id.
|
||||||
headers=self._access_token_(),
|
|
||||||
)
|
|
||||||
response.raise_for_status()
|
|
||||||
return response.json()
|
|
||||||
|
|
||||||
def delete(self, id):
|
# Returns
|
||||||
"""
|
# -------
|
||||||
Delete list
|
# list
|
||||||
|
# Requested contents
|
||||||
|
|
||||||
Parameters
|
# """
|
||||||
----------
|
# response = requests.post(
|
||||||
id : int
|
# url=self.get_api(API_LISTS_READ).format(id), headers=self._access_token_()
|
||||||
List id to delete.
|
# )
|
||||||
|
# response.raise_for_status()
|
||||||
|
# return response.json()
|
||||||
|
|
||||||
"""
|
# def update(self, id, title="Untitled"):
|
||||||
response = requests.delete(
|
# """
|
||||||
url=self.get_api(API_LISTS_DELETE).format(id), headers=self._access_token_()
|
# Add a to-do item to the list
|
||||||
)
|
|
||||||
response.raise_for_status()
|
# Parameters
|
||||||
return response.json()
|
# ----------
|
||||||
|
# id : int
|
||||||
|
# List id.
|
||||||
|
# title : str, optional
|
||||||
|
# To-do item title. The default is "Untitled".
|
||||||
|
|
||||||
|
# """
|
||||||
|
# response = requests.put(
|
||||||
|
# json={"title": title},
|
||||||
|
# url=self.get_api(API_LISTS_UPDATE).format(id),
|
||||||
|
# headers=self._access_token_(),
|
||||||
|
# )
|
||||||
|
# response.raise_for_status()
|
||||||
|
# return response.json()
|
||||||
|
|
||||||
|
# def partial_update(self, id, title="Untitled"):
|
||||||
|
# """
|
||||||
|
# Update list item - untrusted
|
||||||
|
|
||||||
|
# """
|
||||||
|
# response = requests.patch(
|
||||||
|
# json={"title": title},
|
||||||
|
# url=self.get_api(API_LISTS_PARTIAL_UPDATE).format(id),
|
||||||
|
# headers=self._access_token_(),
|
||||||
|
# )
|
||||||
|
# response.raise_for_status()
|
||||||
|
# return response.json()
|
||||||
|
|
||||||
|
# def delete(self, id):
|
||||||
|
# """
|
||||||
|
# Delete list
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
# ----------
|
||||||
|
# id : int
|
||||||
|
# List id to delete.
|
||||||
|
|
||||||
|
# """
|
||||||
|
# response = requests.delete(
|
||||||
|
# url=self.get_api(API_LISTS_DELETE).format(id), headers=self._access_token_()
|
||||||
|
# )
|
||||||
|
# response.raise_for_status()
|
||||||
|
# return response.json()
|
||||||
|
|
||||||
def _access_token_(self):
|
def _access_token_(self):
|
||||||
if self.token is None:
|
if self.token is None:
|
||||||
raise RuntimeError("Authosization required for requested operation!")
|
raise RuntimeError("Authosization required for requested operation!")
|
||||||
return {"Authorization": f'Bearer {self.token["access"]}'}
|
return {"Authorization": f"Bearer {self.token.access}"}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _raise_or_return_(response):
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
|||||||
@@ -1,19 +1,48 @@
|
|||||||
from api import User
|
from user import User
|
||||||
|
|
||||||
|
|
||||||
def ignore_exceptions(*args, **argv):
|
def print_lists(lists):
|
||||||
try:
|
for item in lists:
|
||||||
args[0](*(args[1:]), **argv)
|
print(f"List: '{item}'", f"Id: {item.id}", "|", "|".join([str(x) for x in item.items]))
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
|
|
||||||
user = User()
|
DEFAULT_URL = "http://127.0.0.1:8000"
|
||||||
print("testing api methods...")
|
|
||||||
print("auth..."), ignore_exceptions(user.auth, "root", "root")
|
user = User(url=DEFAULT_URL)
|
||||||
print("list..."), ignore_exceptions(user.list)
|
user.auth("root", "root")
|
||||||
print("create..."), ignore_exceptions(user.create)
|
|
||||||
print("read..."), ignore_exceptions(user.read, id=0)
|
# Fetch existing lists:
|
||||||
print("update..."), ignore_exceptions(user.update, id=0, title="Title")
|
lists = user.fetchUserLists()
|
||||||
print("partial_update..."), ignore_exceptions(user.partial_update, id=0, title="Title")
|
print("Fecthing...")
|
||||||
print("delete..."), ignore_exceptions(user.update, id=0)
|
print_lists(lists)
|
||||||
|
|
||||||
|
# Remove user list by id:
|
||||||
|
user.removeUserList(5)
|
||||||
|
lists = user.fetchUserLists()
|
||||||
|
print(f"Removing {5}...")
|
||||||
|
print_lists(lists)
|
||||||
|
|
||||||
|
# Append a new list to user:
|
||||||
|
print("Appending list...")
|
||||||
|
scroll = user.appendUserList(title="a new list!")
|
||||||
|
print_lists(lists)
|
||||||
|
|
||||||
|
# Modify list 0:
|
||||||
|
print("Modifyng list...")
|
||||||
|
lists[0].modify(title="A new title")
|
||||||
|
print_lists(lists)
|
||||||
|
|
||||||
|
# Append item to list:
|
||||||
|
print("Appending item to last list...")
|
||||||
|
item = lists[-1].append(text="this is an item")
|
||||||
|
print_lists(lists)
|
||||||
|
|
||||||
|
# Modifying item
|
||||||
|
print("Modifyng appended item...")
|
||||||
|
item.modify(finished=True, text="this is an updated item")
|
||||||
|
print_lists(lists)
|
||||||
|
|
||||||
|
# Removing item at 0
|
||||||
|
print("Removing item 0 from list 0...")
|
||||||
|
lists[0].remove(0)
|
||||||
|
print_lists(lists)
|
||||||
|
|||||||
95
frontend/user.py
Normal file
95
frontend/user.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from api import UserApi
|
||||||
|
|
||||||
|
|
||||||
|
class ToDoList(object):
|
||||||
|
def __init__(self, id, title, created_at=None, items=[], parent=None):
|
||||||
|
self.id = id
|
||||||
|
self.title = title
|
||||||
|
self.items = items
|
||||||
|
self.created_at = created_at
|
||||||
|
|
||||||
|
def __getitem__(self, index):
|
||||||
|
return self.items[index]
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.items)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"[{self.id}] {self.title}"
|
||||||
|
|
||||||
|
# ToDo
|
||||||
|
def remove(self, index):
|
||||||
|
self.items.remove(self.items[0])
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# ToDo
|
||||||
|
def append(self, text):
|
||||||
|
item = ToDoItem(id=None, text=text, created_at=datetime.now())
|
||||||
|
self.items.append(item)
|
||||||
|
item.sync()
|
||||||
|
self.sync()
|
||||||
|
return item
|
||||||
|
|
||||||
|
def modify(self, **argv):
|
||||||
|
for key, value in argv.items():
|
||||||
|
setattr(self, key, value)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# ToDo
|
||||||
|
def sync(self):
|
||||||
|
# ToDo send request or store in form
|
||||||
|
print(f"Item '{self}' is being synchronized...")
|
||||||
|
|
||||||
|
|
||||||
|
class ToDoItem(object):
|
||||||
|
def __init__(self, id, text, finished=False, created_at=None, parent=None):
|
||||||
|
self.id = id
|
||||||
|
self.text = text
|
||||||
|
self.finished = finished
|
||||||
|
self.created_at = created_at
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"[{self.id}] {self.text}"
|
||||||
|
|
||||||
|
def modify(self, **argv):
|
||||||
|
for key, value in argv.items():
|
||||||
|
setattr(self, key, value)
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
# ToDo
|
||||||
|
def sync(self):
|
||||||
|
# ToDo send request or store in form
|
||||||
|
print(f"Item '{self}' is being synchronized...")
|
||||||
|
|
||||||
|
|
||||||
|
class User(UserApi):
|
||||||
|
|
||||||
|
# ToDo
|
||||||
|
items = [
|
||||||
|
ToDoList(
|
||||||
|
id=i,
|
||||||
|
title=f"List {i}",
|
||||||
|
created_at=datetime.now(),
|
||||||
|
items=[
|
||||||
|
ToDoItem(id=i * 10 + j, text=f"Item {i*10+j}", created_at=datetime.now())
|
||||||
|
for j in range(10)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
for i in range(10)
|
||||||
|
]
|
||||||
|
|
||||||
|
# ToDo
|
||||||
|
def fetchUserLists(self):
|
||||||
|
return self.items
|
||||||
|
|
||||||
|
# ToDo
|
||||||
|
def removeUserList(self, id):
|
||||||
|
self.items = [item for item in self.items if item.id != id]
|
||||||
|
|
||||||
|
# ToDo
|
||||||
|
def appendUserList(self, title):
|
||||||
|
item = ToDoList(id=None, title=title, created_at=datetime.now())
|
||||||
|
self.items.append(item)
|
||||||
|
item.sync()
|
||||||
|
return item
|
||||||
Reference in New Issue
Block a user