Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b71393b

Browse files
committed
remove flask reference and fix some tests
1 parent c90a0a1 commit b71393b

File tree

9 files changed

+82
-74
lines changed

9 files changed

+82
-74
lines changed

docs/architecture.rst

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ Components used
2828

2929
* ``python 2.7``: the main python version supported for now
3030
* ``IoC``: it is a dependency container used to handle Element configuration and to instantiate all required services
31-
* ``Flask``: it is used to handle request and render response, Element also register custom routes to render nodes.
32-
The jinja version is not the default one, the instance is handled by the IoC.
31+
* ``Tornado``: it is used to handle request and render response, Element also register custom routes to render nodes.
32+
* ``jinja``: render templates.
3333
* ``unittest2``: used to test the framework
3434
* ``mongodb``: the main datastore for the content.
3535

@@ -68,22 +68,7 @@ Some events are explained in the next section, other events are available on the
6868
Request / Response workflow
6969
~~~~~~~~~~~~~~~~~~~~~~~~~~~
7070

71-
* The wsgi wrapper (Flask application) retrieves the request information
72-
* Element registers a custom function to the ``before_app_request`` hook, the ioc reference is ``@element.dispatcher.request#handle``
73-
and the related class is ``element.event.FlaskRequestElementDispatcher``. The function generates an ``element.request``
74-
event where ``Element`` services can register. (This as been done to limit the usage of the Flask API.)
75-
If an event listener returns a response then Flask will return the response, if no response is returned then the standard
76-
Flask workflow is used.
77-
* Flask's route resolution: this step resolve the current routing and call the matching callback function.
78-
Element's register a route named ``element_path``, this route accepts a ``path`` argument. The route is bound to the service
79-
``element.flask.view.index`` (class: element.views.PathView)
80-
* the PathView class retrieve the targeted node and render it by returning a ``Response`` object
81-
* Element registers a custom function to the ``after_app_request`` hook, the ioc reference is ``@element.dispatcher.response#handle``
82-
and the related class is ``element.event.FlaskResponseElementDispatcher``. The function generates an ``element.response``
83-
event where ``Element`` services can register.
84-
This feature can be used to alter the ``Response`` object (adding custom headers...)
85-
* The response is returned to the wsgi wrapper and then to the client
86-
71+
* rewrite this part to explain tornado usage
8772

8873
Plugins
8974
-------

docs/events.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
Events
22
~~~~~~
33

4-
element.request
4+
handler.request
55
---------------
66

7-
This event is used when a request is received by Flask. Events registered:
7+
This event is generated by the ``ioc.extra.tornado.RouterHandler``. Events registered:
88

99
* ``element.plugins.security.firewall`` : this is the security firewall used to control resource access depends on user's
1010
credentials and depends role required to access to the resource.
1111

12-
element.response
12+
handler.response
1313
----------------
1414

15-
* ``element.plugins.security.handler.FlaskContextHandler``: this is used to store security information into the user's session
15+
* ``element.plugins.security.handler.TornadoContextHandler``: this is used to store security information into the user's session
16+
17+
18+
handler.terminate
19+
-----------------
20+
21+
* todo
22+
1623

1724
element.nodes.load.success
1825
--------------------------

docs/plugins/action.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Action
44
Features
55
--------
66

7-
This plugin provides a way to attach Flask actions from the datasource.
7+
This plugin provides a way to attach Tornado actions from the datasource.
88

99
Configuration
1010
-------------

docs/plugins/page.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ A page node is very similar to a blog node, however it should be used to render
4040
<div class="tile">
4141
<img class="tile-image" alt="" src="images/illustrations/infinity.png" />
4242
<h3 class="tile-title">Element</h3>
43-
<p>A python CMS based on flask with a bit of IOC. </p>
43+
<p>A python CMS based on Tornado with a bit of IOC. </p>
4444
<a class="btn btn-primary btn-large btn-block" href="https://github.com/rande/python-element">Play</a>
4545
</div>
4646
</div>

element/plugins/action/action.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ def load_action(self, event):
5151

5252

5353
class ActionHandler(NodeHandler):
54-
def __init__(self, container):
54+
def __init__(self, container, application, templating):
5555
self.container = container
56+
self.application = application
57+
self.templating = templating
5658

5759
def get_defaults(self, node):
5860
return {
@@ -65,7 +67,7 @@ def get_name(self):
6567
def execute(self, request_handler, context):
6668
service = self.container.get(context.node.serviceId)
6769

68-
sub_request_handler = SubRequestHandler(self.container.get('ioc.extra.tornado.application'), HTTPRequest(request_handler.request.method, request_handler.request.path))
70+
sub_request_handler = SubRequestHandler(self.application, HTTPRequest(request_handler.request.method, request_handler.request.path))
6971

7072
result = getattr(service, context.node.method)(sub_request_handler, context, **(context.node.kwargs or {}))
7173

@@ -75,13 +77,13 @@ def execute(self, request_handler, context):
7577
if 'context' not in params:
7678
params['context'] = context
7779

78-
self.render(request_handler, self.container.get('ioc.extra.jinja2'), template, params)
79-
request_handler.set_status(status_code)
80+
self.render(request_handler, self.templating, template, params)
8081

81-
return
82+
request_handler.set_status(status_code)
8283

83-
request_handler.set_status(sub_request_handler.get_status())
84-
request_handler.write(sub_request_handler.get_buffer())
84+
else:
85+
request_handler.set_status(sub_request_handler.get_status())
86+
request_handler.write(sub_request_handler.get_buffer())
8587

8688

8789
class DefaultIndex(object):

element/plugins/action/resources/config/handler_action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ services:
33
class: element.plugins.action.action.ActionHandler
44
arguments:
55
- '@service_container'
6+
- '@ioc.extra.tornado.application'
7+
- '@ioc.extra.jinja2'
8+
69
tags:
710
element.handler:
811
- { name: action.node }

tests/__init__.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ def get_default_handler():
1414
return TestHandler(Application(), HTTPRequest("GET", "/"))
1515

1616

17-
class Templating(object):
18-
def render(self, template, **kwargs):
19-
return template
17+
class TemplateEngine(object):
18+
"""
19+
Mock jinja engine ...
20+
"""
21+
def get_template(self, name):
22+
return Template(name)
23+
24+
class Template(object):
25+
def __init__(self, name):
26+
self.name = name
27+
28+
def render(self, params):
29+
return self.name
30+

tests/plugins/action/test_action.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import ioc.component
55
import element.node, element.plugins.action.action
66
import ioc.exceptions
7-
import werkzeug.wrappers
7+
from tornado.web import Application
88
import tests
99

1010
class RedirectHandlerTest(unittest.TestCase):
@@ -20,7 +20,7 @@ def test_execute_relative(self):
2020
element.node.Node('myid', 'mytype', {'redirect': 'to'})
2121
)
2222

23-
handler = tests.get_default_handler();
23+
handler = tests.get_default_handler()
2424

2525
self.handler.execute(handler, context)
2626

@@ -34,7 +34,7 @@ def test_execute_absolute(self):
3434
element.node.Node('myid', 'mytype', {'redirect': '/to'})
3535
)
3636

37-
handler = tests.get_default_handler();
37+
handler = tests.get_default_handler()
3838

3939
self.handler.execute(handler, context)
4040

@@ -47,44 +47,44 @@ def test_execute_absolute_scheme(self):
4747
element.node.Node('myid', 'mytype', {'redirect': 'http://github.com'})
4848
)
4949

50-
handler = tests.get_default_handler();
50+
handler = tests.get_default_handler()
5151

5252
self.handler.execute(handler, context)
5353

5454
self.assertEquals(handler.get_status(), 302)
5555
self.assertEquals(handler.get_header('Location'), 'http://github.com')
5656

57-
# class ActionHandlerTest(unittest.TestCase):
58-
# def setUp(self):
59-
# self.container = ioc.component.Container()
60-
# self.handler = element.plugins.action.action.ActionHandler(self.container)
61-
#
62-
# app = flask.Flask('AAA')
63-
# self.ctx = app.test_request_context()
64-
# self.ctx.push()
65-
#
66-
# def tearDown(self):
67-
# self.ctx.pop()
68-
#
69-
# def test_non_existant_service(self):
70-
# context = element.node.NodeContext(
71-
# element.node.Node("id", "mytype")
72-
# )
73-
#
74-
# self.assertRaises(ioc.exceptions.UnknownService, self.handler.execute, context, flask)
75-
#
76-
#
77-
# def test_return_response(self):
78-
# context = element.node.NodeContext(
79-
# element.node.Node("id", "mytype", {'serviceId': 'fake', 'method': 'foo'})
80-
# )
81-
#
82-
# class Fake(object):
83-
# def foo(self, context, **kwargs):
84-
# return context.flask.make_response("a response")
85-
#
86-
# self.container.add('fake', Fake())
87-
# response = self.handler.execute(context, flask)
88-
#
89-
# self.assertIsInstance(response, werkzeug.wrappers.BaseResponse)
90-
# self.assertEquals(response.status_code, 200)
57+
class ActionHandlerTest(unittest.TestCase):
58+
def setUp(self):
59+
self.handler = element.plugins.action.action.ActionHandler(
60+
ioc.component.Container(),
61+
Application(),
62+
tests.TemplateEngine()
63+
)
64+
65+
def test_non_existant_service(self):
66+
context = element.node.NodeContext(
67+
element.node.Node("id", "mytype")
68+
)
69+
70+
handler = tests.get_default_handler()
71+
72+
self.assertRaises(ioc.exceptions.UnknownService, self.handler.execute, handler, context)
73+
74+
75+
def test_return_tuple(self):
76+
context = element.node.NodeContext(
77+
element.node.Node("id", "mytype", {'serviceId': 'fake', 'method': 'foo'})
78+
)
79+
80+
class Fake(object):
81+
def foo(self, request_context, context, **kwargs):
82+
return 200, "hello", {}
83+
84+
handler = tests.get_default_handler()
85+
86+
self.handler.container.add('fake', Fake())
87+
88+
self.handler.execute(handler, context)
89+
90+
self.assertEquals(handler.get_status(), 200)

tests/plugins/blog/test_blog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import unittest
33
import element.plugins.blog.blog
44

5-
from tests import Templating
5+
from tests import TemplateEngine
66

77
class PostHandlerTest(unittest.TestCase):
88
def test_init(self):
9-
handler = element.plugins.blog.blog.PostHandler(Templating())
9+
handler = element.plugins.blog.blog.PostHandler(TemplateEngine())
1010

0 commit comments

Comments
 (0)