From bafe42275e65b98951dd3335342e38d7bf1b1db5 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sun, 1 Aug 2021 14:27:21 +0000 Subject: Utilise jinja inheritance to streamline in/outbox Using jinja's templating extensions, we can recreate the activitypub inheritance hierarchy without using clunky python OO. Extending from multiple template files in sequence might cause unnecesary IO to slow response times; if this ever becomes an issue, it should be possible to pre-process some of the templating since a lot of it will be based on constant expressions (such as @context and type). I think some formatting fixes leaked into this commit; oops! I'm leaving this note here in the hopes that if I ever want to clean this up, I can locate this commit easier. --- setup.py | 4 +++- sw/db.py | 11 +++++++++-- sw/templates/ap/collection_page.html | 19 +++++++++++++++++++ sw/templates/ap/collection_page.json | 23 +++++++++++++++++++++++ sw/templates/ap/ordered_collection_page.html | 1 + sw/templates/ap/ordered_collection_page.json | 5 +++++ sw/templates/base.json | 6 +++++- sw/templates/get_user_inbox_page.html | 16 ++++++++-------- sw/templates/get_user_inbox_page.json | 21 ++++++++++----------- sw/templates/get_user_outbox_page.html | 10 +--------- sw/templates/get_user_outbox_page.json | 15 ++++----------- sw/templates/user_info.html | 3 ++- sw/users.py | 15 ++++----------- tests/conftest.py | 2 +- tests/test_auth.py | 13 +++++++++++-- tests/test_users.py | 5 ++++- 16 files changed, 110 insertions(+), 59 deletions(-) create mode 100644 sw/templates/ap/collection_page.html create mode 100644 sw/templates/ap/collection_page.json create mode 100644 sw/templates/ap/ordered_collection_page.html create mode 100644 sw/templates/ap/ordered_collection_page.json diff --git a/setup.py b/setup.py index 2715667..e744f66 100644 --- a/setup.py +++ b/setup.py @@ -6,5 +6,7 @@ setup( packages=find_packages(), include_package_data=True, zip_safe=False, - install_requires=["flask",], + install_requires=[ + "flask", + ], ) diff --git a/sw/db.py b/sw/db.py index 3739a62..344726b 100644 --- a/sw/db.py +++ b/sw/db.py @@ -1,20 +1,27 @@ import sqlite3 +import logging + +from collections import defaultdict import click -from flask import current_app, g + +from flask import current_app, g, url_for from flask.cli import with_appcontext from werkzeug.security import generate_password_hash from sw.time import now + def get_db(): if "db" not in g: g.db = sqlite3.connect( - current_app.config["DATABASE"], detect_types=sqlite3.PARSE_DECLTYPES, + current_app.config["DATABASE"], + detect_types=sqlite3.PARSE_DECLTYPES, ) g.db.row_factory = sqlite3.Row + g.db.set_trace_callback(logging.debug) return g.db diff --git a/sw/templates/ap/collection_page.html b/sw/templates/ap/collection_page.html new file mode 100644 index 0000000..dc209d0 --- /dev/null +++ b/sw/templates/ap/collection_page.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% block content %} + +{% block items %} +
user ID: {{ user["id"] }}
no. of posts: {{ user["outbox_size"] }}
inbox: {{ url_for('users.get_user_inbox', username=user["username"]) }}
+ + {% endblock %} diff --git a/sw/users.py b/sw/users.py index 2df6039..9853748 100644 --- a/sw/users.py +++ b/sw/users.py @@ -81,7 +81,7 @@ def get_user_inbox_page(username): return prepared_output( "get_user_inbox_page", user=g.user, - items=[url_for("items.get_item_info", item_id=x) for x in ids], + items=[x["content_id"] for x in res], max_id=max(ids, default=None), min_id=min(ids, default=None), ) @@ -131,7 +131,8 @@ def post_user_inbox_item(username): ) db.execute( - "update users set inbox_size = inbox_size + 1 where id = ? ;", (g.user["id"],), + "update users set inbox_size = inbox_size + 1 where id = ? ;", + (g.user["id"],), ) db.commit() @@ -166,20 +167,12 @@ def get_user_outbox_page(username): }, ).fetchall() - def ap(r): - def f(k): - return r[k] is not None - - res = {k: r[k] for k in filter(f, r.keys())} - - return res - ids = [x["id"] for x in res] return prepared_output( "get_user_outbox_page", user=g.user, - items=[url_for("items.get_item_info", item_id=x) for x in ids], + items=res, max_id=max(ids, default=None), min_id=min(ids, default=None), ) diff --git a/tests/conftest.py b/tests/conftest.py index 1e41314..459e3cb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -46,7 +46,7 @@ class AuthActions(object): def login(self, username="test", password="test"): return self._client.post( "/auth/login", - data=json.dumps({"username": username, "password": password,}), + data=json.dumps({"username": username, "password": password}), headers={"content-type": "application/json"}, ) diff --git a/tests/test_auth.py b/tests/test_auth.py index 96dd959..db55df7 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -11,7 +11,12 @@ from werkzeug.datastructures import Headers def test_register(client, app): response = client.post( "/auth/register", - data=json.dumps({"username": "a", "password": "a",}), + data=json.dumps( + { + "username": "a", + "password": "a", + } + ), content_type="application/json", ) @@ -19,7 +24,11 @@ def test_register(client, app): with app.app_context(): db_result = ( - get_db().execute("select * from users where username='a'",).fetchone() + get_db() + .execute( + "select * from users where username='a'", + ) + .fetchone() ) assert db_result is not None diff --git a/tests/test_users.py b/tests/test_users.py index c3c2373..057d4e3 100644 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -100,7 +100,10 @@ def test_get_user_inbox_page_latest(app, client, auth): @pytest.mark.parametrize( ("payload", "expected_status_code", "expected_inbox_size_increase"), - (('{"id": "dummy_id"}', 200, 1), ("", 400, 0),), + ( + ('{"id": "dummy_id"}', 200, 1), + ("", 400, 0), + ), ) def test_post_user_inbox_item( app, client, auth, payload, expected_status_code, expected_inbox_size_increase -- cgit v1.2.1