/
/about.html
/acme
/people
/
/about.html
/acme
/people
... and make a series of URLs that accomplish this. Along the way, we create more views and more templates.$ cd ../../creatingux; mkdir step04; cd step04
step04/application.py
:from wsgiref.simple_server import make_serverFurther also here -
from pyramid.config import Configurator
def main ():
config = Configurator ()
config . scan ( "views" )
app = config . make_wsgi_app ()
return app
if __name__ == '__main__' :
app = main ()
server = make_server ( '0.0.0.0' , 8080 , app)
server . serve_forever ()
step04/views.py
:from pyramid.view import view_configAnd here -
@view_config (renderer = "index.pt" )
def index_view (request):
return {}
@view_config (renderer = "about.pt" , name = "about.html" )
def about_view (request):
return {}
@view_config (renderer = "company.pt" , name = "acme" )
def company_view (request):
return { "company" : COMPANY, "projects" : PROJECTS}
@view_config (renderer = "people.pt" , name = "people" )
def people_view (request):
return { "company" : COMPANY, "people" : PEOPLE}
# Dummy data
COMPANY = "ACME, Inc."
PEOPLE = [
{ 'name' : 'sstanton' , 'title' : 'Susan Stanton' },
{ 'name' : 'bbarker' , 'title' : 'Bob Barker' },
]
PROJECTS = [
{ 'name' : 'sillyslogans' , 'title' : 'Silly Slogans' },
{ 'name' : 'meaningless permissions' , 'title' : 'Meaningless Missions' },
]
step04/index.pt
:<html>Further here -
<head>
<title> Projector - Home </ title>
</ head>
<body>
<ul>
<li> <a href= "/"> Home </a> </ li>
<li> <a href= "/about.html"> About Projector </a> </ li>
<li> <a href= "/acme"> ACME, Inc. </a> </ li>
<li> <a href= "/people"> People </a> </ li>
</ ul>
<h1> Projector - Home </ h1>
</ body>
</ html>
step04/about.pt
:<html>Here is
<head>
<title> Projector - About </ title>
</ head>
<body>
<ul>
<li> <a href= "/"> Home </a> </ li>
<li> <a href= "/about.html"> About Projector </a> </ li>
<li> <a href= "/acme"> ACME, Inc. </a> </ li>
<li> <a href= "/people"> People </a> </ li>
</ ul>
<h1> Projector - About </ h1>
<p> Projector is a simple project management tool capable of hosting.
multiple projects for multiple independent companies
sharing a developer pool between autonomous companies. </ p>
</ body>
</ html>
step04/company.pt
:<html>And finally, here is
<head>
<title> Projector - People </ title>
</ head>
<body>
<ul>
<li> <a href= "/"> Home </a> </ li>
<li> <a href= "/about.html"> About Projector </a> </ li>
<li> <a href= "/acme"> ACME Inc. </a> </ li>
<li> <a href= "/people"> People </a> </ li>
</ ul>
<h1> People </ h1>
<ul>
<li tal: repeat = "person people" >
<a href= "$$person.name}"> $ {person.title} </a>
</ li>
</ ul>
</ body>
</ html>
step04/tests.py
:import unittestFurther we write:
class ProjectorViewsUnitTests (unittest . TestCase):
def test_hello_view ( self ):
from views import index_view
result = index_view ({})
self . assertEqual ( len (result . keys ()), 0 )
def test_about_view ( self ):
from views import about_view
result = about_view ({})
self . assertEqual ( len (result . keys ()), 0 )
def test_company_view ( self ):
from views import company_view
result = company_view ({})
self . assertEqual (result [ "company" ], "ACME, Inc." )
self . assertEqual ( len (result [ "projects" ]), 2 )
def test_people_view ( self ):
from views import people_view
result = people_view ({})
self . assertEqual (result [ "company" ], "ACME, Inc." )
self . assertEqual ( len (result [ "people" ]), 2 )
class ProjectorFunctionalTests (unittest . TestCase):
def setUp ( self ):
from application import main
app = main ()
from webtest import TestApp
self . testapp = TestApp (app)
def test_home ( self ):
res = self . testapp . get ( '/' , status = 200 )
self . failUnless ( 'Home' in res . body)
def test_it ( self ):
res = self . testapp . get ( '/' , status = 200 )
self . failUnless ( 'Home' in res . body)
res = self . testapp . get ( '/about.html' , status = 200 )
self . failUnless ( 'autonomous' in res . body)
res = self . testapp . get ( '/ people' , status = 200 )
self . failUnless ( 'Susan' in res . body)
res = self . testapp . get ( '/ acme' , status = 200 )
self . failUnless ( 'Silly Slogans' in res . body)
$ nosetests
$ python application.py
127.0.0.1:8080
Source: https://habr.com/ru/post/136251/
All Articles