DivmodNevow/FormHandling: Example1.0.tac

File Example1.0.tac, 2.0 kB (added by rwall, 4 years ago)
Line 
1 from twisted.application import service, strports
2 from nevow import appserver, loaders, rend, static, url
3
4 class NewsEditPage(rend.Page):
5     docFactory = loaders.xmlstr("""
6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
7            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
8 <html xmlns:n="http://nevow.com/ns/nevow/0.1">
9     <head>
10         <title>Example 1: A News Item Editor</title>
11         <link rel="stylesheet" href="form_css" type="text/css" />
12     </head>
13     <body>
14         <h1>Example 1: A News Item Editor</h1>
15         <fieldset>
16             <legend>Add / Edit News Item</legend>
17             <p>Form Goes Here</p>
18         </fieldset>
19        
20         <ol n:render="sequence" n:data="newsItems">
21             <li n:pattern="item" n:render="mapping">
22                 <strong><n:slot name="title" /></strong>: <n:slot name="description" />
23             </li>
24         </ol>
25     </body>
26 </html>
27 """)
28
29     def __init__(self, *args, **kwargs):
30         self.store = kwargs.pop('store')
31         super(NewsEditPage, self).__init__(*args, **kwargs)
32    
33     def saveNewsItem(self, newsItemData):
34         self.store.append(newsItemData)
35         return url.here.click('confirmation')
36        
37     def data_newsItems(self, ctx, name):
38         return self.store
39
40 class ConfirmationPage(rend.Page):
41     docFactory = loaders.xmlstr("""
42 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
43            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
44 <html>
45     <body>
46         <h1>Your item has been saved</h1>
47         <ul>
48             <li><a href="./">Go back</a></li>
49         </ul>
50     </body>
51 </html>
52 """)
53
54 # A place to store news items. A list of dicts in this simple case.
55 store = [dict(title="Lorum Ipsum", description="""Lorem ipsum dolor sit amet,
56 consectetuer adipiscing elit. Sed sed enim mollis nulla faucibus aliquet.""")]
57
58 rootResource = NewsEditPage(store=store)
59 rootResource.putChild('confirmation', ConfirmationPage())
60
61 application = service.Application("News item editor")
62 strports.service("8080", appserver.NevowSite(rootResource)).setServiceParent(application)
jethro@divmod.org