diff options
| author | Tom Harley | 2021-08-01 14:27:21 +0000 |
|---|---|---|
| committer | Tom Harley | 2021-08-02 14:41:06 +0000 |
| commit | bafe42275e65b98951dd3335342e38d7bf1b1db5 (patch) | |
| tree | 69af3fd0d1ff77baea81a3396a45c7287afdecc0 | |
| parent | Support dev environment via nix-shell (diff) | |
| download | swap-untested.tar.gz swap-untested.zip | |
Utilise jinja inheritance to streamline in/outbox
untestedUsing 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.
| -rw-r--r-- | setup.py | 4 | ||||
| -rw-r--r-- | sw/db.py | 11 | ||||
| -rw-r--r-- | sw/templates/ap/collection_page.html | 19 | ||||
| -rw-r--r-- | sw/templates/ap/collection_page.json | 23 | ||||
| -rw-r--r-- | sw/templates/ap/ordered_collection_page.html | 1 | ||||
| -rw-r--r-- | sw/templates/ap/ordered_collection_page.json | 5 | ||||
| -rw-r--r-- | sw/templates/base.json | 6 | ||||
| -rw-r--r-- | sw/templates/get_user_inbox_page.html | 16 | ||||
| -rw-r--r-- | sw/templates/get_user_inbox_page.json | 21 | ||||
| -rw-r--r-- | sw/templates/get_user_outbox_page.html | 10 | ||||
| -rw-r--r-- | sw/templates/get_user_outbox_page.json | 15 | ||||
| -rw-r--r-- | sw/templates/user_info.html | 3 | ||||
| -rw-r--r-- | sw/users.py | 15 | ||||
| -rw-r--r-- | tests/conftest.py | 2 | ||||
| -rw-r--r-- | tests/test_auth.py | 13 | ||||
| -rw-r--r-- | tests/test_users.py | 5 |
16 files changed, 110 insertions, 59 deletions
@@ -6,5 +6,7 @@ setup( packages=find_packages(), include_package_data=True, zip_safe=False, - install_requires=["flask",], + install_requires=[ + "flask", + ], ) @@ -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 %} +<ol> + {% for item in items %} + {% block item_info scoped %} + <li value="{{ item.id }}"> + <a href="{{ url_for('items.get_item_info', item_id=item.id) }}"> + {{ item.summary or item.name or "unknown item" }} + </a> + </li> + {% endblock %} + {% endfor %} +</ol> +{% endblock %} + +{% endblock %} diff --git a/sw/templates/ap/collection_page.json b/sw/templates/ap/collection_page.json new file mode 100644 index 0000000..e3bfe3b --- /dev/null +++ b/sw/templates/ap/collection_page.json @@ -0,0 +1,23 @@ +{% extends "base.json" %} + +{% block content %} + + {% block type %} + "type": "CollectionPage", + {% endblock %} + + {% block nav %} + {% endblock %} + + {% block items %} + "items": [ + {% for item in items %} + {% block item_info scoped %} + "{{ url_for('items.get_item_info', item_id=item['id']) }}" + {% endblock %} + {{- "," if not loop.last }} + {% endfor %} + ], + {% endblock %} + +{% endblock %} diff --git a/sw/templates/ap/ordered_collection_page.html b/sw/templates/ap/ordered_collection_page.html new file mode 100644 index 0000000..57ed54b --- /dev/null +++ b/sw/templates/ap/ordered_collection_page.html @@ -0,0 +1 @@ +{% extends "ap/collection_page.html" %} diff --git a/sw/templates/ap/ordered_collection_page.json b/sw/templates/ap/ordered_collection_page.json new file mode 100644 index 0000000..1a97b4a --- /dev/null +++ b/sw/templates/ap/ordered_collection_page.json @@ -0,0 +1,5 @@ +{% extends "ap/collection_page.json" %} + +{% block type %} +"type": "OrderedCollectionPage", +{% endblock %} diff --git a/sw/templates/base.json b/sw/templates/base.json index e0a0876..58da7f8 100644 --- a/sw/templates/base.json +++ b/sw/templates/base.json @@ -1,4 +1,8 @@ { -{% block content %}{% endblock %} +{% block content %} +{% endblock %} + +{% block context %} "@context": "https://www.w3.org/ns/activitystreams" +{% endblock %} } diff --git a/sw/templates/get_user_inbox_page.html b/sw/templates/get_user_inbox_page.html index a331049..a839015 100644 --- a/sw/templates/get_user_inbox_page.html +++ b/sw/templates/get_user_inbox_page.html @@ -1,13 +1,13 @@ -{% extends "base.html" %} +{% extends "ap/ordered_collection_page.html" %} {% block header %} -<h1>{% block title %}{{ user["username"] }}'s Inbox {% endblock %}</h1> +<h1>{% block title %}{{ user["username"] }}'s Inbox{% endblock %}</h1> {% endblock %} -{% block content %} -<ul> -{% for item in items %} -<li value="{{ items['id'] }}">{{ item }}</li> -{% endfor %} -</ul> +{% block item_info scoped %} + +<li> <a href="{{ item }}"> + {{ item }} +</a> </li> + {% endblock %} diff --git a/sw/templates/get_user_inbox_page.json b/sw/templates/get_user_inbox_page.json index 96bf9e8..886aeda 100644 --- a/sw/templates/get_user_inbox_page.json +++ b/sw/templates/get_user_inbox_page.json @@ -1,20 +1,19 @@ -{% extends "base.json" %} - -{% block content %} -"type": "OrderedCollectionPage", +{% extends "ap/ordered_collection_page.json" %} +{% block nav %} "id": "{{ url_for('.get_user_inbox_page', username=user['username'], max_id=max_id) }}", +"current": "{{ url_for('.get_user_inbox_page', username=user['username']) }}", +"partOf": "{{ url_for('.get_user_inbox', username=user['username']) }}", {% if items %} "next": "{{ url_for('.get_user_inbox_page', username=user['username'], max_id=(min_id-1)) }}", "prev": "{{ url_for('.get_user_inbox_page', username=user['username'], min_id=(max_id+1)) }}", {% endif %} -"current": "{{ url_for('.get_user_inbox_page', username=user['username']) }}", -"partOf": "{{ url_for('.get_user_inbox', username=user['username']) }}", -"items": [ - {% for item in items %} - "{{ item }}"{{ ',' if not loop.last }} - {% endfor %} -], +{% endblock %} + +{% block item_info scoped %} + +"{{ item }}" + {% endblock %} diff --git a/sw/templates/get_user_outbox_page.html b/sw/templates/get_user_outbox_page.html index 2907604..dfa60c2 100644 --- a/sw/templates/get_user_outbox_page.html +++ b/sw/templates/get_user_outbox_page.html @@ -1,13 +1,5 @@ -{% extends "base.html" %} +{% extends "ap/ordered_collection_page.html" %} {% block header %} <h1>{% block title %}{{ user["username"] }}'s Outbox {% endblock %}</h1> {% endblock %} - -{% block content %} -<ul> -{% for item in items %} -<li value="{{ item['id'] }}">{{ item }}</li> -{% endfor %} -</ul> -{% endblock %} diff --git a/sw/templates/get_user_outbox_page.json b/sw/templates/get_user_outbox_page.json index 2f3936f..8cdb93b 100644 --- a/sw/templates/get_user_outbox_page.json +++ b/sw/templates/get_user_outbox_page.json @@ -1,20 +1,13 @@ -{% extends "base.json" %} - -{% block content %} -"type": "OrderedCollecdtionPage", +{% extends "ap/ordered_collection_page.json" %} +{% block nav %} "id": "{{ url_for('.get_user_outbox_page', username=user['username'], max_id=max_id) }}", +"current": "{{ url_for('.get_user_outbox_page', username=user['username']) }}", +"partOf": "{{ url_for('.get_user_outbox', username=user['username']) }}", {% if items %} "next": "{{ url_for('.get_user_outbox_page', username=user['username'], max_id=(min_id-1)) }}", "prev": "{{ url_for('.get_user_outbox_page', username=user['username'], min_id=(max_id+1)) }}", {% endif %} -"current": "{{ url_for('.get_user_outbox_page', username=user['username']) }}", -"partOf": "{{ url_for('.get_user_outbox', username=user['username']) }}", -"items": [ - {% for item in items %} - "{{ item }}"{{ "," if not loop.last }} - {% endfor %} -], {% endblock %} diff --git a/sw/templates/user_info.html b/sw/templates/user_info.html index 96c3d2f..557957a 100644 --- a/sw/templates/user_info.html +++ b/sw/templates/user_info.html @@ -9,5 +9,6 @@ <p>user ID: {{ user["id"] }}</p> <p>no. of posts: {{ user["outbox_size"] }}</p> <br> -<p>inbox: <a href="{{ url_for('users.get_user_inbox', username=user["username"]) }}">{{ url_for('users.get_user_inbox', username=user["username"]) }}</a></p> +<p><a href="{{ url_for('users.get_user_inbox', username=user["username"]) }}">Inbox</a></p> +<p><a href="{{ url_for('users.get_user_outbox', username=user["username"]) }}">Outbox</a></p> {% 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 |
