Skip to content

Commit 4f880f7

Browse files
authored
make it easier to override the default templates (#636)
1 parent 1eb3085 commit 4f880f7

File tree

11 files changed

+39
-6
lines changed

11 files changed

+39
-6
lines changed

starlette_admin/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.15.0rc4"
1+
__version__ = "0.15.0rc7"
22

33
from ._types import ExportType as ExportType
44
from ._types import RequestAction as RequestAction

starlette_admin/base.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
from json import JSONDecodeError
33
from typing import Any, Awaitable, Callable, Dict, List, Optional, Sequence, Type, Union
44

5-
from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PackageLoader
5+
from jinja2 import (
6+
ChoiceLoader,
7+
Environment,
8+
FileSystemLoader,
9+
PackageLoader,
10+
PrefixLoader,
11+
)
612
from starlette.applications import Starlette
713
from starlette.datastructures import FormData
814
from starlette.exceptions import HTTPException
@@ -195,6 +201,13 @@ def _setup_templates(self) -> None:
195201
[
196202
FileSystemLoader(self.templates_dir),
197203
PackageLoader("starlette_admin", "templates"),
204+
PrefixLoader(
205+
{
206+
"@starlette-admin": PackageLoader(
207+
"starlette_admin", "templates"
208+
),
209+
}
210+
),
198211
]
199212
),
200213
extensions=["jinja2.ext.i18n"],
File renamed without changes.

tests/templates/basic/base.html

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% extends "@starlette-admin/base.html" %}
2+
{% block head_meta %}
3+
<meta name="custom-meta" content="Custom metadata" />
4+
{% endblock %}
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{% extends "layout.html" %}
2+
{% block content %}This is custom view to display some data.{% endblock %}

tests/templates/views/report.html

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{% extends "layout.html" %}
2+
{% block content %}This is custom view to display some data.{% endblock %}

tests/test_admin_basic.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_custom_index_view(self):
3131
index_view=CustomView(
3232
"Home", path="/home", template_path="custom_index.html"
3333
),
34-
templates_dir="tests/templates",
34+
templates_dir="tests/templates/basic",
3535
)
3636
admin.mount_to(app)
3737
assert len(admin._views) == 1
@@ -64,3 +64,15 @@ def test_admin_customisation(self):
6464
assert response.status_code == 200
6565
assert response.text.count("<title>DashBoard</title>") == 1
6666
assert response.text.count("https://test.com/logo.png") == 1
67+
68+
def test_template_override(self):
69+
app = Starlette()
70+
admin = BaseAdmin(templates_dir="tests/templates/basic")
71+
admin.mount_to(app)
72+
client = TestClient(app)
73+
response = client.get("/admin")
74+
assert response.status_code == 200
75+
assert (
76+
response.text.count('<meta name="custom-meta" content="Custom metadata" />')
77+
== 1
78+
)

tests/test_auth.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def setup_method(self, method):
204204
@pytest.fixture
205205
def client(self, report_view):
206206
admin = BaseAdmin(
207-
auth_provider=MyAuthProvider(), templates_dir="tests/templates"
207+
auth_provider=MyAuthProvider(), templates_dir="tests/templates/auth"
208208
)
209209
app = Starlette()
210210
admin.add_view(report_view)

tests/test_multiple_admin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_multiple_admin(report_view):
5454
"Admin1",
5555
base_url="/admin1",
5656
route_name="admin1",
57-
templates_dir="tests/templates",
57+
templates_dir="tests/templates/multiple-admin",
5858
)
5959
admin1.add_view(report_view)
6060
admin1.add_view(PostView)

tests/test_views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def setup_method(self, method):
116116
PostView.seq = len(PostView.db.keys()) + 1
117117

118118
def test_add_custom_view(self, report_view):
119-
admin = BaseAdmin(templates_dir="tests/templates")
119+
admin = BaseAdmin(templates_dir="tests/templates/views")
120120
app = Starlette()
121121
admin.add_view(report_view)
122122
admin.mount_to(app)

0 commit comments

Comments
 (0)