Moved JsonDataView into views/special,py

This commit is contained in:
Simon Willison 2018-06-07 08:22:29 -07:00
commit a246f476b4
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
2 changed files with 31 additions and 22 deletions

View file

@ -25,6 +25,7 @@ from .views.base import (
) )
from .views.database import DatabaseDownload, DatabaseView from .views.database import DatabaseDownload, DatabaseView
from .views.index import IndexView from .views.index import IndexView
from .views.special import JsonDataView
from .views.table import RowView, TableView from .views.table import RowView, TableView
from . import hookspecs from . import hookspecs
@ -100,28 +101,6 @@ DEFAULT_CONFIG = {
} }
class JsonDataView(RenderMixin):
def __init__(self, datasette, filename, data_callback):
self.ds = datasette
self.jinja_env = datasette.jinja_env
self.filename = filename
self.data_callback = data_callback
async def get(self, request, as_json):
data = self.data_callback()
if as_json:
headers = {}
if self.ds.cors:
headers["Access-Control-Allow-Origin"] = "*"
return response.HTTPResponse(
json.dumps(data), content_type="application/json", headers=headers
)
else:
return self.render(["show_json.html"], filename=self.filename, data=data)
async def favicon(request): async def favicon(request):
return response.text("") return response.text("")

View file

@ -0,0 +1,30 @@
import json
from sanic import response
from .base import RenderMixin
class JsonDataView(RenderMixin):
def __init__(self, datasette, filename, data_callback):
self.ds = datasette
self.jinja_env = datasette.jinja_env
self.filename = filename
self.data_callback = data_callback
async def get(self, request, as_json):
data = self.data_callback()
if as_json:
headers = {}
if self.ds.cors:
headers["Access-Control-Allow-Origin"] = "*"
return response.HTTPResponse(
json.dumps(data),
content_type="application/json",
headers=headers
)
else:
return self.render(
["show_json.html"],
filename=self.filename,
data=data
)