From 68703447d8fc57c7b77b66a6acc05bf22fb7d71b Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sat, 31 Jul 2021 14:06:45 +0000 Subject: Display inbox/outbox contents correctly As per previous commit, the contents of inboxes and outboxes were not being properly displayed, which was difficult to test because of there being very few items in the test DB. This commit includes moving functions such as prepared_output into a new file sw/util.py; including them in sw/users.py made little sense and caused a circular dependency with sw/auth.py. A stub function for item info retreival has also been created, in new file sw/items.py. --- sw/util.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sw/util.py (limited to 'sw/util.py') diff --git a/sw/util.py b/sw/util.py new file mode 100644 index 0000000..de80466 --- /dev/null +++ b/sw/util.py @@ -0,0 +1,43 @@ +from flask import request, abort, Response, render_template + +from jinja2.exceptions import TemplateNotFound + + +def decide_response_filetype(): + res = request.accept_mimetypes.best_match( + [ + "text/html", + "application/ld+json", + "application/json", + ] + ) + + if not request.accept_mimetypes: + return (".json", "application/ld+json") + + elif res == "text/html": + return (".html", "text/html") + + elif res == "application/json": + return (".json", "application/json") + + elif res == "application/ld+json": + return (".json", "application/ld+json") + + else: # res is None + abort(406) + + +def prepared_output(filename, status=200, **kwargs): + ext, mime = decide_response_filetype() + + try: + body = render_template(f"{filename}{ext}", **kwargs) + + except TemplateNotFound: + # TODO + # Should probably try the other file extensions available + # rather than error out if the first one fails + abort(406) + + return Response(body, content_type=mime, status=status) -- cgit v1.2.1