Skip to content

Commit

Permalink
Use can_render to only suggest .atom when supported by query
Browse files Browse the repository at this point in the history
Will release as 0.6. Depends on Datasette 0.43. Closes #10.

See also simonw/datasette#770
  • Loading branch information
simonw committed May 28, 2020
1 parent 095941c commit df98a6c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
6 changes: 5 additions & 1 deletion datasette_atom/__init__.py
Expand Up @@ -9,7 +9,7 @@

@hookimpl
def register_output_renderer():
return {"extension": "atom", "callback": render_atom}
return {"extension": "atom", "render": render_atom, "can_render": can_render_atom}


def render_atom(args, data, view_name):
Expand Down Expand Up @@ -66,6 +66,10 @@ def render_atom(args, data, view_name):
}


def can_render_atom(columns):
return {"atom_id", "atom_title", "atom_updated"}.issubset(columns)


def clean(html):
cleaned = bleach.clean(
html,
Expand Down
4 changes: 2 additions & 2 deletions setup.py
@@ -1,7 +1,7 @@
from setuptools import setup
import os

VERSION = "0.5"
VERSION = "0.6"


def get_long_description():
Expand All @@ -23,7 +23,7 @@ def get_long_description():
version=VERSION,
packages=["datasette_atom"],
entry_points={"datasette": ["atom = datasette_atom"]},
install_requires=["datasette", "feedgen", "bleach"],
install_requires=["datasette~=0.43", "feedgen", "bleach"],
extras_require={"test": ["pytest", "pytest-asyncio", "httpx"]},
tests_require=["datasette-atom[test]"],
)
28 changes: 28 additions & 0 deletions tests/test_atom.py
Expand Up @@ -180,3 +180,31 @@ async def test_atom_with_bad_html():
EXPECTED_ATOM_WITH_HTML.format(version=datasette.__version__)
== response.content.decode("utf-8").strip()
)


@pytest.mark.asyncio
async def test_atom_link_only_shown_for_correct_queries():
sql = """
select
'atom-id' as atom_id,
'title' as atom_title,
'2019-10-23T21:32:12-07:00' as atom_updated,
'https://www.niche-museums.com/' as atom_link,
'<h2>blah</h2><script>alert("bad")</script>' as atom_content_html;
"""
app = Datasette([], immutables=[], memory=True).app()
async with httpx.AsyncClient(app=app) as client:
response = await client.get(
"http://localhost/:memory:?" + urllib.parse.urlencode({"sql": sql})
)
assert 200 == response.status_code
assert "text/html; charset=utf-8" == response.headers["content-type"]
assert b'<a href="/:memory:.atom' in response.content
# But with a different query that link is not shown:
async with httpx.AsyncClient(app=app) as client:
response = await client.get(
"http://localhost/:memory:?"
+ urllib.parse.urlencode({"sql": "select sqlite_version()"})
)
assert b'<a href="/:memory:.json' in response.content
assert b'<a href="/:memory:.atom' not in response.content

0 comments on commit df98a6c

Please sign in to comment.