simonw / datasette-cluster-map

Datasette plugin that shows a map for any data with latitude/longitude columns
Apache License 2.0
88 stars 16 forks source link

Plugin can break other plugins #19

Closed simonw closed 4 years ago

simonw commented 4 years ago

This plugin broke my installation of datasette-edit-schema. I got this error: 'dict' object has no attribute 'lower'

The traceback lead me here:

> /Users/simon/.local/share/virtualenvs/datasette-for-datasettecloud-tDBqAXoo/lib/python3.8/site-packages/datasette_cluster_map/__init__.py(83)<listcomp>()
-> columns = [column.lower() for column in columns]
(Pdb) list
 78     
 79     
 80     def has_columns(database, table, columns, datasette):
 81         if not columns:
 82             return False
 83  ->     columns = [column.lower() for column in columns]
 84         config = (
 85             datasette.plugin_config("datasette-cluster-map", database=database, table=table)
 86             or {}
 87         )
 88         latitude_column = config.get("latitude_column") or "latitude"
simonw commented 4 years ago

After further investigation I got here:

(Pdb) list
717                 request=request,
718                 datasette=self,
719             ):
720                 extra_script = await await_me_maybe(extra_script)
721                 body_scripts.append(Markup(extra_script))
722     
723             extra_template_vars = {}
724             # pylint: disable=no-member
725             for extra_vars in pm.hook.extra_template_vars(
726                 template=template.name,
727                 database=context.get("database"),
(Pdb) locals()
{'templates': ['edit_schema_table.html'], 'context': {'database': <Database: fixtures (mutable, size=258048)>, 'table': 'attraction_characteristic', 'columns': [{'name': 'pk', 'type': 'INTEGER'}, {'name': 'name', 'type': 'TEXT'}], 'schema': 'CREATE TABLE attraction_characteristic (\n    pk integer primary key,\n    name text\n)', 'types': ['TEXT', 'REAL', 'INTEGER', 'BLOB']}, 'view_name': None, 'template': <Template 'edit_schema_table.html'>, 'body_scripts': [], 'request': <datasette.utils.asgi.Request object at 0x10d370a90>, 'self': <datasette.app.Datasette object at 0x10b31f190>}

The problem is this code: https://github.com/simonw/datasette-cluster-map/blob/b8004700910c11c4416a5e94b47acc57cf2417b1/datasette_cluster_map/__init__.py#L50-L90

It's running a check based on the columns, but it turns out those are simply pulled out of the context - so they won't make sense if the plugin hook is running for a page other than a default query or template page.

view_name': None, is the clue we can hook into here.