-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2461 from samuelhwilliams/add-host-support
Add Flask `host_matching` support to admin instances
- Loading branch information
Showing
13 changed files
with
458 additions
and
5 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
This example shows how to configure Flask-Admin when you're using Flask's `host_matching` mode. Any Flask-Admin instance can be exposed on just a specific host, or on every host. | ||
|
||
To run this example: | ||
|
||
1. Clone the repository:: | ||
|
||
git clone https://github.com/flask-admin/flask-admin.git | ||
cd flask-admin | ||
|
||
2. Create and activate a virtual environment:: | ||
|
||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
|
||
3. Install requirements:: | ||
|
||
pip install -r 'examples/host-matching/requirements.txt' | ||
|
||
4. Run the application:: | ||
|
||
python examples/host-matching/app.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from flask import Flask, url_for | ||
|
||
import flask_admin as admin | ||
|
||
|
||
# Views | ||
class FirstView(admin.BaseView): | ||
@admin.expose('/') | ||
def index(self): | ||
return self.render('first.html') | ||
|
||
|
||
class SecondView(admin.BaseView): | ||
@admin.expose('/') | ||
def index(self): | ||
return self.render('second.html') | ||
|
||
|
||
class ThirdViewAllHosts(admin.BaseView): | ||
@admin.expose('/') | ||
def index(self): | ||
return self.render('third.html') | ||
|
||
|
||
# Create flask app | ||
app = Flask(__name__, template_folder='templates', host_matching=True, static_host='static.localhost:5000') | ||
|
||
|
||
# Flask views | ||
@app.route('/', host='<anyhost>') | ||
def index(anyhost): | ||
return ( | ||
f'<a href="{url_for("admin.index")}">Click me to get to Admin 1</a>' | ||
f'<br/>' | ||
f'<a href="{url_for("admin2.index")}">Click me to get to Admin 2</a>' | ||
f'<br/>' | ||
f'<a href="{url_for("admin3.index", admin_routes_host="anything.localhost:5000")}">Click me to get to Admin 3 under `anything.localhost:5000`</a>' | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
# Create first administrative interface at `first.localhost:5000/admin1` | ||
admin1 = admin.Admin(app, url='/admin1', host='first.localhost:5000') | ||
admin1.add_view(FirstView()) | ||
|
||
# Create second administrative interface at `second.localhost:5000/admin2` | ||
admin2 = admin.Admin(app, url='/admin2', endpoint='admin2', host='second.localhost:5000') | ||
admin2.add_view(SecondView()) | ||
|
||
# Create third administrative interface, available on any domain at `/admin3` | ||
admin3 = admin.Admin(app, url='/admin3', endpoint='admin3', host='*') | ||
admin3.add_view(ThirdViewAllHosts()) | ||
|
||
# Start app | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Flask | ||
Flask-Admin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% extends 'admin/master.html' %} | ||
{% block body %} | ||
First admin view. | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% extends 'admin/master.html' %} | ||
{% block body %} | ||
Second admin view. | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% extends 'admin/master.html' %} | ||
{% block body %} | ||
Third admin view. | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import typing as t | ||
|
||
from flask import request, Flask | ||
from flask.blueprints import Blueprint as FlaskBlueprint | ||
from flask.blueprints import BlueprintSetupState as FlaskBlueprintSetupState | ||
|
||
from flask_admin.consts import ADMIN_ROUTES_HOST_VARIABLE_NAME, \ | ||
ADMIN_ROUTES_HOST_VARIABLE | ||
|
||
|
||
class _BlueprintSetupStateWithHostSupport(FlaskBlueprintSetupState): | ||
"""Adds the ability to set a hostname on all routes when registering the blueprint.""" | ||
|
||
def __init__(self, blueprint, app, options, first_registration): | ||
super().__init__(blueprint, app, options, first_registration) | ||
self.host = self.options.get("host") | ||
|
||
def add_url_rule(self, rule, endpoint=None, view_func=None, **options): | ||
# Ensure that every route registered by this blueprint has the host parameter | ||
options.setdefault("host", self.host) | ||
super().add_url_rule(rule, endpoint, view_func, **options) | ||
|
||
|
||
class _BlueprintWithHostSupport(FlaskBlueprint): | ||
def make_setup_state(self, app, options, first_registration=False): | ||
return _BlueprintSetupStateWithHostSupport( | ||
self, app, options, first_registration | ||
) | ||
|
||
def attach_url_defaults_and_value_preprocessor(self, app: Flask, host: str): | ||
if host != ADMIN_ROUTES_HOST_VARIABLE: | ||
return | ||
|
||
# Automatically inject `admin_routes_host` into `url_for` calls on admin | ||
# endpoints. | ||
@self.url_defaults | ||
def inject_admin_routes_host_if_required( | ||
endpoint: str, values: t.Dict[str, t.Any] | ||
) -> None: | ||
if app.url_map.is_endpoint_expecting( | ||
endpoint, ADMIN_ROUTES_HOST_VARIABLE_NAME | ||
): | ||
values.setdefault(ADMIN_ROUTES_HOST_VARIABLE_NAME, request.host) | ||
|
||
# Automatically strip `admin_routes_host` from the endpoint values so | ||
# that the view methods don't receive that parameter, as it's not actually | ||
# required by any of them. | ||
@self.url_value_preprocessor | ||
def strip_admin_routes_host_from_static_endpoint( | ||
endpoint: t.Optional[str], values: t.Optional[t.Dict[str, t.Any]] | ||
) -> None: | ||
if ( | ||
endpoint | ||
and values | ||
and app.url_map.is_endpoint_expecting( | ||
endpoint, ADMIN_ROUTES_HOST_VARIABLE_NAME | ||
) | ||
): | ||
values.pop(ADMIN_ROUTES_HOST_VARIABLE_NAME, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.