ReleaseEngineering/BuildAPI: Difference between revisions

Changed to match controller creation, and added steps on how to make accessible.
m (spelling error, reordered a sentence.)
(Changed to match controller creation, and added steps on how to make accessible.)
Line 82: Line 82:
===Simple code sample===
===Simple code sample===
Now, to write a simple controller that prints "Hello World" using an M-V-C stack, we need three files:
Now, to write a simple controller that prints "Hello World" using an M-V-C stack, we need three files:
* controllers/hello.py
* controllers/hello_world.py
* model/hello.py
* model/hello_world.py
* templates/hello.mako
* templates/hello_world.mako


Our controller lives in: controllers/hello.py
Our controller lives in: controllers/hello_world.py
<pre>
<pre>
import logging
import logging
Line 94: Line 94:


from buildapi.lib.base import BaseController, render
from buildapi.lib.base import BaseController, render
from buildapi.model.hello import GetMessage
from buildapi.model.hello_world import GetMessage


log = logging.getLogger(__name__)
log = logging.getLogger(__name__)
Line 103: Line 103:
         # Return a rendered template
         # Return a rendered template
         c.message = GetMessage()
         c.message = GetMessage()
         return render('/hello.mako')
         return render('/hello_world.mako')
</pre>
</pre>


Our model lives in: model/hello.py
Our model lives in: model/hello_world.py
<pre>
<pre>
def GetMessage():
def GetMessage():
     return "Hello world!"
     return "Hello world!"
</pre>
</pre>


Our view lives in: templates/hello.mako (note that the tmpl_context object is available in the template)
Our view lives in: templates/hello_world.mako
Note that the tmpl_context object is available in the template.
<pre>
<pre>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Line 124: Line 124:
</body>
</body>
</html>
</html>
</pre>
To access this page, we need to set up a route to it. To do this we add to config/routing.py:
<pre>
map.connect('/hello_world', controller='hello_world', action='index')
</pre>
Now restart buildapi, and point your browser to:
<pre>
http://localhost:5000/hello_world
</pre>
</pre>


32

edits