mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
parent
11eb1e026f
commit
c1d386ef67
2 changed files with 61 additions and 59 deletions
|
|
@ -40,6 +40,7 @@ from .views.special import (
|
||||||
)
|
)
|
||||||
from .views.table import RowView, TableView
|
from .views.table import RowView, TableView
|
||||||
from .renderer import json_renderer
|
from .renderer import json_renderer
|
||||||
|
from .url_builder import Urls
|
||||||
from .database import Database, QueryInterrupted
|
from .database import Database, QueryInterrupted
|
||||||
|
|
||||||
from .utils import (
|
from .utils import (
|
||||||
|
|
@ -53,7 +54,6 @@ from .utils import (
|
||||||
format_bytes,
|
format_bytes,
|
||||||
module_from_path,
|
module_from_path,
|
||||||
parse_metadata,
|
parse_metadata,
|
||||||
path_with_format,
|
|
||||||
resolve_env_secrets,
|
resolve_env_secrets,
|
||||||
sqlite3,
|
sqlite3,
|
||||||
to_css_class,
|
to_css_class,
|
||||||
|
|
@ -1280,61 +1280,3 @@ class DatasetteClient:
|
||||||
async def request(self, method, path, **kwargs):
|
async def request(self, method, path, **kwargs):
|
||||||
async with httpx.AsyncClient(app=self.app) as client:
|
async with httpx.AsyncClient(app=self.app) as client:
|
||||||
return await client.request(method, self._fix(path), **kwargs)
|
return await client.request(method, self._fix(path), **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class Urls:
|
|
||||||
def __init__(self, ds):
|
|
||||||
self.ds = ds
|
|
||||||
|
|
||||||
def path(self, path, format=None):
|
|
||||||
if path.startswith("/"):
|
|
||||||
path = path[1:]
|
|
||||||
path = self.ds.config("base_url") + path
|
|
||||||
if format is not None:
|
|
||||||
path = path_with_format(path=path, format=format)
|
|
||||||
return path
|
|
||||||
|
|
||||||
def instance(self, format=None):
|
|
||||||
return self.path("", format=format)
|
|
||||||
|
|
||||||
def static(self, path):
|
|
||||||
return self.path("-/static/{}".format(path))
|
|
||||||
|
|
||||||
def static_plugins(self, plugin, path):
|
|
||||||
return self.path("-/static-plugins/{}/{}".format(plugin, path))
|
|
||||||
|
|
||||||
def logout(self):
|
|
||||||
return self.path("-/logout")
|
|
||||||
|
|
||||||
def database(self, database, format=None):
|
|
||||||
db = self.ds.databases[database]
|
|
||||||
if self.ds.config("hash_urls") and db.hash:
|
|
||||||
path = self.path(
|
|
||||||
"{}-{}".format(database, db.hash[:HASH_LENGTH]), format=format
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
path = self.path(database, format=format)
|
|
||||||
return path
|
|
||||||
|
|
||||||
def table(self, database, table, format=None):
|
|
||||||
path = "{}/{}".format(self.database(database), urllib.parse.quote_plus(table))
|
|
||||||
if format is not None:
|
|
||||||
path = path_with_format(path=path, format=format)
|
|
||||||
return path
|
|
||||||
|
|
||||||
def query(self, database, query, format=None):
|
|
||||||
path = "{}/{}".format(self.database(database), urllib.parse.quote_plus(query))
|
|
||||||
if format is not None:
|
|
||||||
path = path_with_format(path=path, format=format)
|
|
||||||
return path
|
|
||||||
|
|
||||||
def row(self, database, table, row_path, format=None):
|
|
||||||
path = "{}/{}".format(self.table(database, table), row_path)
|
|
||||||
if format is not None:
|
|
||||||
path = path_with_format(path=path, format=format)
|
|
||||||
return path
|
|
||||||
|
|
||||||
def row_blob(self, database, table, row_path, column):
|
|
||||||
return self.table(database, table) + "/{}.blob?_blob_column={}".format(
|
|
||||||
row_path, urllib.parse.quote_plus(column)
|
|
||||||
)
|
|
||||||
|
|
|
||||||
60
datasette/url_builder.py
Normal file
60
datasette/url_builder.py
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
from .utils import path_with_format, HASH_LENGTH
|
||||||
|
import urllib
|
||||||
|
|
||||||
|
|
||||||
|
class Urls:
|
||||||
|
def __init__(self, ds):
|
||||||
|
self.ds = ds
|
||||||
|
|
||||||
|
def path(self, path, format=None):
|
||||||
|
if path.startswith("/"):
|
||||||
|
path = path[1:]
|
||||||
|
path = self.ds.config("base_url") + path
|
||||||
|
if format is not None:
|
||||||
|
path = path_with_format(path=path, format=format)
|
||||||
|
return path
|
||||||
|
|
||||||
|
def instance(self, format=None):
|
||||||
|
return self.path("", format=format)
|
||||||
|
|
||||||
|
def static(self, path):
|
||||||
|
return self.path("-/static/{}".format(path))
|
||||||
|
|
||||||
|
def static_plugins(self, plugin, path):
|
||||||
|
return self.path("-/static-plugins/{}/{}".format(plugin, path))
|
||||||
|
|
||||||
|
def logout(self):
|
||||||
|
return self.path("-/logout")
|
||||||
|
|
||||||
|
def database(self, database, format=None):
|
||||||
|
db = self.ds.databases[database]
|
||||||
|
if self.ds.config("hash_urls") and db.hash:
|
||||||
|
path = self.path(
|
||||||
|
"{}-{}".format(database, db.hash[:HASH_LENGTH]), format=format
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
path = self.path(database, format=format)
|
||||||
|
return path
|
||||||
|
|
||||||
|
def table(self, database, table, format=None):
|
||||||
|
path = "{}/{}".format(self.database(database), urllib.parse.quote_plus(table))
|
||||||
|
if format is not None:
|
||||||
|
path = path_with_format(path=path, format=format)
|
||||||
|
return path
|
||||||
|
|
||||||
|
def query(self, database, query, format=None):
|
||||||
|
path = "{}/{}".format(self.database(database), urllib.parse.quote_plus(query))
|
||||||
|
if format is not None:
|
||||||
|
path = path_with_format(path=path, format=format)
|
||||||
|
return path
|
||||||
|
|
||||||
|
def row(self, database, table, row_path, format=None):
|
||||||
|
path = "{}/{}".format(self.table(database, table), row_path)
|
||||||
|
if format is not None:
|
||||||
|
path = path_with_format(path=path, format=format)
|
||||||
|
return path
|
||||||
|
|
||||||
|
def row_blob(self, database, table, row_path, column):
|
||||||
|
return self.table(database, table) + "/{}.blob?_blob_column={}".format(
|
||||||
|
row_path, urllib.parse.quote_plus(column)
|
||||||
|
)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue