-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Classbasedview #684
Merged
Merged
Classbasedview #684
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc68983
Add example for class based view
asvetlov 00d5208
Fix spaces
asvetlov 9196a54
Finish CBV example
asvetlov 6a27781
Fix Not Allowed processing
asvetlov a66a92e
Revert to GET from * in route
asvetlov b6e31ed
Implement web.View
asvetlov 9768c46
Improve coverage
asvetlov d6e6c5c
Add tests for class based views
asvetlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,67 @@ | ||
#!/usr/bin/env python3 | ||
"""Example for aiohttp.web class based views | ||
""" | ||
|
||
|
||
import asyncio | ||
import functools | ||
import json | ||
from aiohttp.web import json_response, Application, Response, View | ||
|
||
|
||
class MyView(View): | ||
|
||
async def get(self): | ||
return json_response({ | ||
'method': 'get', | ||
'args': dict(self.request.GET), | ||
'headers': dict(self.request.headers), | ||
}, dumps=functools.partial(json.dumps, indent=4)) | ||
|
||
async def post(self): | ||
data = await self.request.post() | ||
return json_response({ | ||
'method': 'post', | ||
'args': dict(self.request.GET), | ||
'data': dict(data), | ||
'headers': dict(self.request.headers), | ||
}, dumps=functools.partial(json.dumps, indent=4)) | ||
|
||
|
||
async def index(request): | ||
txt = """ | ||
<html> | ||
<head> | ||
<title>Class based view example</title> | ||
</head> | ||
<body> | ||
<h1>Class based view example</h1> | ||
<ul> | ||
<li><a href="/">/</a> This page | ||
<li><a href="/get">/get</a> Returns GET data. | ||
<li><a href="/post">/post</a> Returns POST data. | ||
</ul> | ||
</body> | ||
</html> | ||
""" | ||
return Response(text=txt, content_type='text/html') | ||
|
||
|
||
async def init(loop): | ||
app = Application(loop=loop) | ||
app.router.add_route('GET', '/', index) | ||
app.router.add_route('GET', '/get', MyView) | ||
app.router.add_route('POST', '/post', MyView) | ||
|
||
handler = app.make_handler() | ||
srv = await loop.create_server(handler, '127.0.0.1', 8080) | ||
print("Server started at http://127.0.0.1:8080") | ||
return srv, handler | ||
|
||
|
||
loop = asyncio.get_event_loop() | ||
srv, handler = loop.run_until_complete(init(loop)) | ||
try: | ||
loop.run_forever() | ||
except KeyboardInterrupt: | ||
loop.run_until_complete(handler.finish_connections()) |
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,57 @@ | ||
import asyncio | ||
import pytest | ||
|
||
from aiohttp import web | ||
from aiohttp.web_urldispatcher import View | ||
from unittest import mock | ||
|
||
|
||
def test_ctor(): | ||
request = mock.Mock() | ||
view = View(request) | ||
assert view.request is request | ||
|
||
|
||
@pytest.mark.run_loop | ||
def test_render_ok(): | ||
resp = web.Response(text='OK') | ||
|
||
class MyView(View): | ||
@asyncio.coroutine | ||
def get(self): | ||
return resp | ||
|
||
request = mock.Mock() | ||
request.method = 'GET' | ||
resp2 = yield from MyView(request) | ||
assert resp is resp2 | ||
|
||
|
||
@pytest.mark.run_loop | ||
def test_render_unknown_method(): | ||
|
||
class MyView(View): | ||
@asyncio.coroutine | ||
def get(self): | ||
return web.Response(text='OK') | ||
|
||
request = mock.Mock() | ||
request.method = 'UNKNOWN' | ||
with pytest.raises(web.HTTPMethodNotAllowed) as ctx: | ||
yield from MyView(request) | ||
assert ctx.value.status == 405 | ||
|
||
|
||
@pytest.mark.run_loop | ||
def test_render_unsupported_method(): | ||
|
||
class MyView(View): | ||
@asyncio.coroutine | ||
def get(self): | ||
return web.Response(text='OK') | ||
|
||
request = mock.Mock() | ||
request.method = 'POST' | ||
with pytest.raises(web.HTTPMethodNotAllowed) as ctx: | ||
yield from MyView(request) | ||
assert ctx.value.status == 405 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what's the profit?
MyView
class is used not instance ofMyView
, so no custom initialization could be done. This would lead to some "view factories" like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People asked for "class based views like Django" many times.
Personally I prefer another style of views organizing like http://aiohttp.readthedocs.org/en/stable/web.html#using-plain-coroutines-and-classes-for-web-handlers you know.
'*'
as method name).View.as_view()
etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, agree to 1.
2. looks like a next request after this one.
Anyway, I not sure which is worse -- to have "class based views like Django" in aiohttp.web or to let people reinvent the wheel themselfs...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If people asked me the question I would try to explain why they don't need it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is in this line: https://github.com/KeepSafe/aiohttp/pull/684/files#diff-b41dc5c66bc19f22fe7cc77c002db02cR501
Without it people may do it themselves (while most of them not experienced enough to override
__iter__
or__await__
properly).