Skip to content

Commit

Permalink
Added initial code brought over from datasette-paste
Browse files Browse the repository at this point in the history
Closes #1

Made very slight modifications - mainly replacing 'paste' with 'import'

Revision history of this code is here:
https://github.com/datasette/datasette-paste/commits/b16ded30471cdb2737026cf4c43a1c61438ebe19/
  • Loading branch information
simonw committed Apr 6, 2024
1 parent 06f4d2a commit 0b294dc
Show file tree
Hide file tree
Showing 6 changed files with 495 additions and 9 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ datasette install datasette-import
```
## Usage

Usage instructions go here.
This plugin adds a database action item called "Create table with imported data".

This action is available to users with the `create-table` permission.

It links to a page that allows users to upload files or paste in CSV, TSV or JSON data, and then use that to create and populate a new table in Datasette.

CSV and TSV data must have headers on the first row.

JSON data must be an array of objects with the same keys, or a container object object where one of the keys is an array of objects.

## Credits

The CSV and TSV parsing is performed using [Papa Parse](https://www.papaparse.com/), an MIT licensed JavaScript library that is bundled with this plugin.

## Development

Expand Down
61 changes: 60 additions & 1 deletion datasette_import/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
from datasette import hookimpl
from datasette import hookimpl, Response, Forbidden


async def import_create_table(datasette, request):
database = request.url_vars["database"]
if not await can_import(datasette, request.actor, database):
raise Forbidden("Permission denied to import data")
return Response.html(
await datasette.render_template(
"import_create_table.html",
{
"database": database,
"papaparse_url": datasette.urls.static_plugins(
"datasette-import", "papaparse-5-4-1.min.js"
),
},
request=request,
)
)


@hookimpl
def register_routes():
return [
(r"^/(?P<database>[^/]+)/-/import$", import_create_table),
]


@hookimpl
def database_actions(datasette, actor, database):
async def inner():
if not await can_import(datasette, actor, database):
return []
return [
{
"href": datasette.urls.database(database) + "/-/import",
"label": "Create table with imported data",
"description": "Import using JSON, CSV or TSV data (e.g. from Google Sheets)",
}
]

return inner


async def can_import(datasette, actor, database_name, to_table=None):
if actor is None:
return False
if not to_table:
# Need create-table for database
can_create_table = await datasette.permission_allowed(
actor, "create-table", resource=database_name
)
if not can_create_table:
return False
return True
else:
# Need insert-row for that table
return await datasette.permission_allowed(
actor, "insert-row", resource=(database_name, to_table)
)
Loading

0 comments on commit 0b294dc

Please sign in to comment.