Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 5, 2019
1 parent 424e146 commit 8da2db4
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 15 deletions.
3 changes: 3 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
ConfigOption("force_https_urls", False, """
Force URLs in API output to always use https:// protocol
""".strip()),
ConfigOption("url_prefix", "", """
URLs within Datasette itself should start with this (defaults to auto)
""".strip()),
)
DEFAULT_CONFIG = {
option.name: option.default
Expand Down
2 changes: 2 additions & 0 deletions datasette/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def convert(self, config, param, ctx):
)
return
return name, int(value)
elif isinstance(default, str):
return name, value
else:
# Should never happen:
self.fail('Invalid option')
Expand Down
12 changes: 6 additions & 6 deletions datasette/templates/database.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% block body_class %}db db-{{ database|to_css_class }}{% endblock %}

{% block content %}
<div class="hd"><a href="/">home</a></div>
<div class="hd"><a href="{{ prefix }}">home</a></div>

<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash[:6] }}">{{ metadata.title or database }}</h1>

Expand All @@ -27,22 +27,22 @@ <h3>Custom SQL query</h3>
{% for table in tables %}
{% if show_hidden or not table.hidden %}
<div class="db-table">
<h2><a href="/{{ database }}-{{ database_hash }}/{{ table.name|quote_plus }}">{{ table.name }}</a>{% if table.hidden %}<em> (hidden)</em>{% endif %}</h2>
<h2><a href="{{ prefix }}{{ database }}-{{ database_hash }}/{{ table.name|quote_plus }}">{{ table.name }}</a>{% if table.hidden %}<em> (hidden)</em>{% endif %}</h2>
<p><em>{% for column in table.columns[:9] %}{{ column }}{% if not loop.last %}, {% endif %}{% endfor %}{% if table.columns|length > 9 %}...{% endif %}</em></p>
<p>{{ "{:,}".format(table.count) }} row{% if table.count == 1 %}{% else %}s{% endif %}</p>
</div>
{% endif %}
{% endfor %}

{% if hidden_count and not show_hidden %}
<p>... and <a href="/{{ database }}-{{ database_hash }}?_show_hidden=1">{{ "{:,}".format(hidden_count) }} hidden table{% if hidden_count == 1 %}{% else %}s{% endif %}</a></p>
<p>... and <a href="{{ prefix }}{{ database }}-{{ database_hash }}?_show_hidden=1">{{ "{:,}".format(hidden_count) }} hidden table{% if hidden_count == 1 %}{% else %}s{% endif %}</a></p>
{% endif %}

{% if views %}
<h2>Views</h2>
<ul>
{% for view in views %}
<li><a href="/{{ database }}-{{ database_hash }}/{{ view|urlencode }}">{{ view }}</a></li>
<li><a href="{{ prefix }}{{ database }}-{{ database_hash }}/{{ view|urlencode }}">{{ view }}</a></li>
{% endfor %}
</ul>
{% endif %}
Expand All @@ -51,13 +51,13 @@ <h2>Views</h2>
<h2>Queries</h2>
<ul>
{% for query in queries %}
<li><a href="/{{ database }}-{{ database_hash }}/{{ query.name|urlencode }}" title="{{ query.description or query.sql }}">{{ query.title or query.name }}</a></li>
<li><a href="{{ prefix }}{{ database }}-{{ database_hash }}/{{ query.name|urlencode }}" title="{{ query.description or query.sql }}">{{ query.title or query.name }}</a></li>
{% endfor %}
</ul>
{% endif %}

{% if config.allow_download %}
<p>Download SQLite DB: <a href="/{{ database }}-{{ database_hash }}.db">{{ database }}.db</a></p>
<p>Download SQLite DB: <a href="{{ prefix }}{{ database }}-{{ database_hash }}.db">{{ database }}.db</a></p>
{% endif %}

{% include "_codemirror_foot.html" %}
Expand Down
4 changes: 2 additions & 2 deletions datasette/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h1>{{ metadata.title or "Datasette" }}</h1>
{% block description_source_license %}{% include "_description_source_license.html" %}{% endblock %}

{% for database in databases %}
<h2 style="padding-left: 10px; border-left: 10px solid #{{ database.hash[:6] }}"><a href="{{ database.path }}">{{ database.name }}</a></h2>
<h2 style="padding-left: 10px; border-left: 10px solid #{{ database.hash[:6] }}"><a href="{{ prefix }}{{ database.path }}">{{ database.name }}</a></h2>
<p>
{{ "{:,}".format(database.table_rows_sum) }} rows in {{ database.tables_count }} table{% if database.tables_count != 1 %}s{% endif %}{% if database.tables_count and database.hidden_tables_count %}, {% endif %}
{% if database.hidden_tables_count %}
Expand All @@ -21,7 +21,7 @@ <h2 style="padding-left: 10px; border-left: 10px solid #{{ database.hash[:6] }}"
{{ "{:,}".format(database.views_count) }} view{% if database.views_count != 1 %}s{% endif %}
{% endif %}
</p>
<p>{% for table in database.tables_truncated %}<a href="{{ database.path }}/{{ table.name|quote_plus }}" title="{{ table.count }} rows">{{ table.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}{% if database.tables_more %}, <a href="{{ database.path }}">...</a>{% endif %}</p>
<p>{% for table in database.tables_truncated %}<a href="{{ prefix }}{{ database.path }}/{{ table.name|quote_plus }}" title="{{ table.count }} rows">{{ table.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}{% if database.tables_more %}, <a href="{{ prefix }}{{ database.path }}">...</a>{% endif %}</p>
{% endfor %}

{% endblock %}
4 changes: 2 additions & 2 deletions datasette/templates/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
{% block body_class %}query db-{{ database|to_css_class }}{% endblock %}

{% block content %}
<div class="hd"><a href="/">home</a> / <a href="/{{ database }}-{{ database_hash }}">{{ database }}</a></div>
<div class="hd"><a href="{{ prefix }}">home</a> / <a href="{{ prefix }}{{ database }}-{{ database_hash }}">{{ database }}</a></div>

<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash[:6] }}">{{ metadata.title or database }}</h1>

{% block description_source_license %}{% include "_description_source_license.html" %}{% endblock %}

<form class="sql" action="/{{ database }}-{{ database_hash }}{% if canned_query %}/{{ canned_query }}{% endif %}" method="get">
<form class="sql" action="{{ prefix }}{{ database }}-{{ database_hash }}{% if canned_query %}/{{ canned_query }}{% endif %}" method="get">
<h3>Custom SQL query{% if display_rows %} returning {% if truncated %}more than {% endif %}{{ "{:,}".format(display_rows|length) }} row{% if display_rows|length == 1 %}{% else %}s{% endif %}{% endif %}</h3>
{% if editable and config.allow_sql %}
<p><textarea name="sql">{% if query and query.sql %}{{ query.sql }}{% else %}select * from {{ tables[0].name|escape_sqlite }}{% endif %}</textarea></p>
Expand Down
4 changes: 2 additions & 2 deletions datasette/templates/row.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% block body_class %}row db-{{ database|to_css_class }} table-{{ table|to_css_class }}{% endblock %}

{% block content %}
<div class="hd"><a href="/">home</a> / <a href="/{{ database }}-{{ database_hash }}">{{ database }}</a> / <a href="/{{ database }}-{{ database_hash }}/{{ table|quote_plus }}">{{ table }}</a></div>
<div class="hd"><a href="{{ prefix }}">home</a> / <a href="{{ prefix }}{{ database }}-{{ database_hash }}">{{ database }}</a> / <a href="{{ prefix }}{{ database }}-{{ database_hash }}/{{ table|quote_plus }}">{{ table }}</a></div>

<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash[:6] }}">{{ table }}: {{ ', '.join(primary_key_values) }}</a></h1>

Expand All @@ -31,7 +31,7 @@ <h2>Links from other tables</h2>
<ul>
{% for other in foreign_key_tables %}
<li>
<a href="/{{ database }}-{{ database_hash }}/{{ other.other_table|quote_plus }}?{{ other.other_column }}={{ ', '.join(primary_key_values) }}">
<a href="{{ prefix }}{{ database }}-{{ database_hash }}/{{ other.other_table|quote_plus }}?{{ other.other_column }}={{ ', '.join(primary_key_values) }}">
{{ "{:,}".format(other.count) }} row{% if other.count == 1 %}{% else %}s{% endif %}</a>
from {{ other.other_column }} in {{ other.other_table }}
</li>
Expand Down
4 changes: 2 additions & 2 deletions datasette/templates/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{% block body_class %}table db-{{ database|to_css_class }} table-{{ table|to_css_class }}{% endblock %}

{% block content %}
<div class="hd"><a href="/">home</a> / <a href="/{{ database }}-{{ database_hash }}">{{ database }}</a></div>
<div class="hd"><a href="{{ prefix }}">home</a> / <a href="{{ prefix }}{{ database }}-{{ database_hash }}">{{ database }}</a></div>

<h1 style="padding-left: 10px; border-left: 10px solid #{{ database_hash[:6] }}">{{ metadata.title or table }}{% if is_view %} (view){% endif %}</h1>

Expand All @@ -29,7 +29,7 @@ <h3>{% if filtered_table_rows_count or filtered_table_rows_count == 0 %}{{ "{:,}
</h3>
{% endif %}

<form class="filters" action="/{{ database }}-{{ database_hash }}/{{ table|quote_plus }}" method="get">
<form class="filters" action="{{ prefix }}{{ database }}-{{ database_hash }}/{{ table|quote_plus }}" method="get">
{% if supports_search %}
<div class="search-row"><label for="_search">Search:</label><input id="_search" type="search" name="_search" value="{{ search }}"></div>
{% endif %}
Expand Down
3 changes: 2 additions & 1 deletion datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def render(self, templates, **context):
"extra_js_urls": self._asset_urls(
"extra_js_urls", template, context
),
"prefix": self.ds.config("url_prefix") or "/",
}
}
)
Expand Down Expand Up @@ -190,7 +191,7 @@ def resolve_db_name(self, db_name, **kwargs):
return name, expected, None

def absolute_url(self, request, path):
url = urllib.parse.urljoin(request.url, path)
url = urllib.parse.urljoin(self.ds.config("url_prefix") or request.url, path)
if url.startswith("http://") and self.ds.config("force_https_urls"):
url = "https://" + url[len("http://"):]
return url
Expand Down

0 comments on commit 8da2db4

Please sign in to comment.