mirror of
https://github.com/kennethreitz/responder.git
synced 2026-06-05 23:00:17 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0678daa880 | |||
| 6761e3bdd8 | |||
| ead213a506 | |||
| 75b5782eee | |||
| a80df809e4 | |||
| 7f3177f662 | |||
| 906cd2fbbf | |||
| 9d0129da56 | |||
| 86361523e2 |
+16
-2
@@ -5,6 +5,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
## [v2.0.4] - 2019-11-19
|
||||
### Fixed
|
||||
- Fix static app resolving
|
||||
|
||||
## [v2.0.3] - 2019-09-20
|
||||
### Fixed
|
||||
- Fix template conflicts
|
||||
|
||||
## [v2.0.2] - 2019-09-20
|
||||
### Fixed
|
||||
- Fix template conflicts
|
||||
|
||||
## [v2.0.1] - 2019-09-20
|
||||
### Fixed
|
||||
@@ -216,8 +227,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
### Added
|
||||
- Conception!
|
||||
|
||||
[Unreleased]: https://github.com/taoufik07/responder/compare/v2.0.1..HEAD
|
||||
[v2.0.0]: https://github.com/taoufik07/responder/compare/v2.0.0..v2.0.1
|
||||
[Unreleased]: https://github.com/taoufik07/responder/compare/v2.0.4..HEAD
|
||||
[v2.0.4]: https://github.com/taoufik07/responder/compare/v2.0.3..v2.0.4
|
||||
[v2.0.3]: https://github.com/taoufik07/responder/compare/v2.0.2..v2.0.3
|
||||
[v2.0.2]: https://github.com/taoufik07/responder/compare/v2.0.1..v2.0.2
|
||||
[v2.0.1]: https://github.com/taoufik07/responder/compare/v2.0.0..v2.0.1
|
||||
[v2.0.0]: https://github.com/taoufik07/responder/compare/v1.3.2..v2.0.0
|
||||
[v1.3.2]: https://github.com/taoufik07/responder/compare/v1.3.1..v1.3.2
|
||||
[v1.3.1]: https://github.com/taoufik07/responder/compare/v1.3.0..v1.3.1
|
||||
|
||||
Generated
+7
@@ -116,6 +116,13 @@
|
||||
],
|
||||
"version": "==2.8"
|
||||
},
|
||||
"itsdangerous": {
|
||||
"hashes": [
|
||||
"sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19",
|
||||
"sha256:b12271b2047cb23eeb98c8b5622e2e5c5e9abd9784a153e9d8ef9cb4dd09d749"
|
||||
],
|
||||
"version": "==1.1.0"
|
||||
},
|
||||
"jinja2": {
|
||||
"hashes": [
|
||||
"sha256:74320bb91f31270f9551d46522e33af46a80c3d619f4a4bf42b3164d30b5911f",
|
||||
|
||||
@@ -85,9 +85,10 @@ Usage::
|
||||
def hello(req, resp, name):
|
||||
resp.html = templates.render("hello.html", name=name)
|
||||
|
||||
|
||||
|
||||
Also a ``render_async`` is available::
|
||||
|
||||
templates = Templates(enable_async=True)
|
||||
resp.html = await templates.render_async("hello.html", who=who)
|
||||
|
||||
You can also use the existing ``api.template(filename, *args, **kwargs)`` to render templates::
|
||||
|
||||
@@ -1 +1 @@
|
||||
__version__ = "2.0.1"
|
||||
__version__ = "2.0.4"
|
||||
|
||||
+3
-14
@@ -76,15 +76,6 @@ class API:
|
||||
self.static_dir = static_dir
|
||||
self.static_route = static_route
|
||||
|
||||
self.built_in_templates_dir = Path(
|
||||
os.path.abspath(os.path.dirname(__file__) + "/templates")
|
||||
)
|
||||
|
||||
if templates_dir is not None:
|
||||
templates_dir = Path(os.path.abspath(templates_dir))
|
||||
|
||||
self.templates_dir = templates_dir or self.built_in_templates_dir
|
||||
|
||||
self.hsts_enabled = enable_hsts
|
||||
self.cors = cors
|
||||
self.cors_params = cors_params
|
||||
@@ -98,10 +89,8 @@ class API:
|
||||
allowed_hosts = ["*"]
|
||||
self.allowed_hosts = allowed_hosts
|
||||
|
||||
# Make the static/templates directory if they don't exist.
|
||||
for _dir in (self.static_dir, self.templates_dir):
|
||||
if _dir is not None:
|
||||
os.makedirs(_dir, exist_ok=True)
|
||||
if self.static_dir is not None:
|
||||
os.makedirs(self.static_dir, exist_ok=True)
|
||||
|
||||
if self.static_dir is not None:
|
||||
self.mount(self.static_route, self.static_app)
|
||||
@@ -229,7 +218,7 @@ class API:
|
||||
index = (self.static_dir / "index.html").resolve()
|
||||
if os.path.exists(index):
|
||||
with open(index, "r") as f:
|
||||
resp.html = "Hello world !"
|
||||
resp.html = f.read()
|
||||
else:
|
||||
resp.status_code = status_codes.HTTP_404
|
||||
resp.text = "Not found."
|
||||
|
||||
+9
-8
@@ -302,6 +302,15 @@ class Router:
|
||||
path = scope["path"]
|
||||
root_path = scope.get("root_path", "")
|
||||
|
||||
# Check "primary" mounted routes first (before submounted apps)
|
||||
route = self._resolve_route(scope)
|
||||
|
||||
scope["before_requests"] = self.before_requests
|
||||
|
||||
if route is not None:
|
||||
await route(scope, receive, send)
|
||||
return
|
||||
|
||||
# Call into a submounted app, if one exists.
|
||||
for path_prefix, app in self.apps.items():
|
||||
if path.startswith(path_prefix):
|
||||
@@ -315,12 +324,4 @@ class Router:
|
||||
await app(scope, receive, send)
|
||||
return
|
||||
|
||||
route = self._resolve_route(scope)
|
||||
|
||||
scope["before_requests"] = self.before_requests
|
||||
|
||||
if route is not None:
|
||||
await route(scope, receive, send)
|
||||
return
|
||||
|
||||
await self.default_response(scope, receive, send)
|
||||
|
||||
@@ -4,10 +4,14 @@ import jinja2
|
||||
|
||||
|
||||
class Templates:
|
||||
def __init__(self, directory="templates", autoescape=True, context=None):
|
||||
def __init__(
|
||||
self, directory="templates", autoescape=True, context=None, enable_async=False
|
||||
):
|
||||
self.directory = directory
|
||||
self._env = jinja2.Environment(
|
||||
loader=jinja2.FileSystemLoader([str(self.directory)]), autoescape=autoescape
|
||||
loader=jinja2.FileSystemLoader([str(self.directory)]),
|
||||
autoescape=autoescape,
|
||||
enable_async=enable_async,
|
||||
)
|
||||
self.default_context = {} if context is None else {**context}
|
||||
self._env.globals.update(self.default_context)
|
||||
|
||||
@@ -40,6 +40,7 @@ required = [
|
||||
"docopt",
|
||||
"requests-toolbelt",
|
||||
"apistar",
|
||||
"itsdangerous",
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -56,3 +56,12 @@ def schema():
|
||||
return f"Hello {name}"
|
||||
|
||||
return graphene.Schema(query=Query)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def template_path(tmpdir):
|
||||
# create a Jinja template file on the filesystem
|
||||
template_name = "test.html"
|
||||
template_file = tmpdir.mkdir("static").join(template_name)
|
||||
template_file.write("{{ var }}")
|
||||
return template_file
|
||||
|
||||
+26
-8
@@ -8,6 +8,7 @@ import requests
|
||||
import string
|
||||
import io
|
||||
from responder.routes import Router, Route, WebSocketRoute
|
||||
from responder.templates import Templates
|
||||
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.responses import PlainTextResponse
|
||||
@@ -590,22 +591,39 @@ def test_template_string_rendering(api):
|
||||
assert r.text == "hello"
|
||||
|
||||
|
||||
def test_template_rendering(tmpdir):
|
||||
# create a Jinja template file on the filesystem
|
||||
template_name = "test.html"
|
||||
template_file = tmpdir.mkdir("static").join(template_name)
|
||||
template_file.write("{{ var }}")
|
||||
|
||||
api = responder.API(templates_dir=template_file.dirpath())
|
||||
def test_template_rendering(template_path):
|
||||
api = responder.API(templates_dir=template_path.dirpath())
|
||||
|
||||
@api.route("/")
|
||||
def view(req, resp):
|
||||
resp.content = api.template(template_name, var="hello")
|
||||
resp.content = api.template(template_path.basename, var="hello")
|
||||
|
||||
r = api.requests.get(api.url_for(view))
|
||||
assert r.text == "hello"
|
||||
|
||||
|
||||
def test_template(api, template_path):
|
||||
templates = Templates(directory=template_path.dirpath())
|
||||
|
||||
@api.route("/{var}/")
|
||||
def view(req, resp, var):
|
||||
resp.html = templates.render(template_path.basename, var=var)
|
||||
|
||||
r = api.requests.get("/test/")
|
||||
assert r.text == "test"
|
||||
|
||||
|
||||
def test_template_async(api, template_path):
|
||||
templates = Templates(directory=template_path.dirpath(), enable_async=True)
|
||||
|
||||
@api.route("/{var}/async")
|
||||
async def view_async(req, resp, var):
|
||||
resp.html = await templates.render_async(template_path.basename, var=var)
|
||||
|
||||
r = api.requests.get("/test/async")
|
||||
assert r.text == "test"
|
||||
|
||||
|
||||
def test_file_uploads(api):
|
||||
@api.route("/")
|
||||
async def upload(req, resp):
|
||||
|
||||
Reference in New Issue
Block a user