diff --git a/src/tt/cli.py b/src/tt/cli.py index 0c30117..36895ee 100644 --- a/src/tt/cli.py +++ b/src/tt/cli.py @@ -3,7 +3,8 @@ import httpx import lxml.html import sqlite3 from typing_extensions import Annotated -from .db import initialize_db +from .db import initialize_db, db +from .sync import full_sync from .tui.things import run as things_tui # from .tui.overview import run as overview_tui @@ -68,8 +69,6 @@ def backup(backup_path: str): """ Perform a SQLite backup using the .backup dot command """ - from tt.db import db - conn = db.connection() backup_conn = None @@ -81,5 +80,14 @@ def backup(backup_path: str): backup_conn.close() +@app.command() +def sync(): + """ + Sync with tt server. + """ + initialize_db() + full_sync() + + if __name__ == "__main__": app() diff --git a/src/tt/controller/things.py b/src/tt/controller/things.py index bcf0499..18526f5 100644 --- a/src/tt/controller/things.py +++ b/src/tt/controller/things.py @@ -80,6 +80,8 @@ def get_things( # TODO: which fields are searchable should by dynamic query = query.where(fn.Lower(Thing.data["text"]).contains(search_text.lower())) + if filters is None: + filters = {} for param, val in filters.items(): if val is not None: # no _in query for JSON fields, so use OR diff --git a/src/tt/sync.py b/src/tt/sync.py new file mode 100644 index 0000000..61493c8 --- /dev/null +++ b/src/tt/sync.py @@ -0,0 +1,21 @@ +import httpx +from .controller.things import get_things + + +def sync_thing(thing): + resp = httpx.post( + "http://localhost:9999/api/thing/?api_key=j@jpt.sh", + json={ + "id": thing.id, + "data": thing.data, + "type": thing.type, + } + ) + print(resp.text) + resp.raise_for_status() + + +def full_sync(): + # all things for now + for thing in get_things(None, None): + sync_thing(thing)