DivmodNevow/FormHandling: Example1.1.tac.py

File Example1.1.tac.py, 2.4 kB (added by rwall, 2 years ago)
Line 
1 from twisted.application import service, strports
2 from nevow import appserver, loaders, rend, static, url
3 from formless import annotate, webform
4
5 class NewsEditPage(rend.Page):
6     docFactory = loaders.xmlstr("""
7 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
8            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
9 <html xmlns:n="http://nevow.com/ns/nevow/0.1">
10     <head>
11         <title>Example 1: A News Item Editor</title>
12         <link rel="stylesheet" href="form_css" type="text/css" />
13     </head>
14     <body>
15         <h1>Example 1: A News Item Editor</h1>
16         <n:invisible n:render="newsInputForm" />
17         
18         <ol n:render="sequence" n:data="newsItems">
19             <li n:pattern="item" n:render="mapping">
20                 <strong><n:slot name="title" /></strong>: <n:slot name="description" />
21             </li>
22         </ol>
23     </body>
24 </html>
25 """)
26
27     child_form_css = webform.defaultCSS
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 bind_saveNewsItem(self, ctx):
38         return [
39             ('title', annotate.String(required=True)),
40             ('description', annotate.Text(required=True)),
41         ]
42
43     def render_newsInputForm(self, ctx, data):
44         return ctx.tag.clear()[
45             webform.renderForms()
46         ]
47        
48     def data_newsItems(self, ctx, name):
49         return self.store
50
51 class ConfirmationPage(rend.Page):
52     docFactory = loaders.xmlstr("""
53 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
54            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
55 <html>
56     <body>
57         <h1>Your item has been saved</h1>
58         <ul>
59             <li><a href="./">Go back</a></li>
60         </ul>
61     </body>
62 </html>
63 """)
64
65 # A place to store news items. A list of dicts in this simple case.
66 store = [dict(title="Lorum Ipsum", description="""Lorem ipsum dolor sit amet,
67 consectetuer adipiscing elit. Sed sed enim mollis nulla faucibus aliquet.""")]
68
69 rootResource = NewsEditPage(store=store)
70 rootResource.putChild('confirmation', ConfirmationPage())
71
72 application = service.Application("News item editor")
73 strports.service("8080", appserver.NevowSite(rootResource)).setServiceParent(application)
jethro@divmod.org