mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Moved JsonDataView into views/special,py
This commit is contained in:
parent
864328e074
commit
a246f476b4
2 changed files with 31 additions and 22 deletions
|
|
@ -25,6 +25,7 @@ from .views.base import (
|
|||
)
|
||||
from .views.database import DatabaseDownload, DatabaseView
|
||||
from .views.index import IndexView
|
||||
from .views.special import JsonDataView
|
||||
from .views.table import RowView, TableView
|
||||
|
||||
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):
|
||||
return response.text("")
|
||||
|
||||
|
|
|
|||
30
datasette/views/special.py
Normal file
30
datasette/views/special.py
Normal 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
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue