DivmodNevow/FormHandling: Example1.2.tac.py

File Example1.2.tac.py, 2.3 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 import forms
4
5 class NewsEditPage(forms.ResourceMixin, 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="form saveNewsItem" />
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 = forms.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, ctx, form, newsItemData):
34         self.store.append(newsItemData)
35         return url.here.click('confirmation')
36
37     def form_saveNewsItem(self, ctx):
38         form = forms.Form()
39         form.addField('title', forms.String(required=True))
40         form.addField('description', forms.String(required=True), forms.widgetFactory(forms.TextArea))
41         form.addAction(self.saveNewsItem)
42         return form
43        
44     def data_newsItems(self, ctx, name):
45         return self.store
46
47 class ConfirmationPage(rend.Page):
48     docFactory = loaders.xmlstr("""
49 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
50            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
51 <html>
52     <body>
53         <h1>Your item has been saved</h1>
54         <ul>
55             <li><a href="./">Go back</a></li>
56         </ul>
57     </body>
58 </html>
59 """)
60
61 # A place to store news items. A list of dicts in this simple case.
62 store = [dict(title="Lorum Ipsum", description="""Lorem ipsum dolor sit amet,
63 consectetuer adipiscing elit. Sed sed enim mollis nulla faucibus aliquet.""")]
64
65 rootResource = NewsEditPage(store=store)
66 rootResource.putChild('confirmation', ConfirmationPage())
67
68 application = service.Application("News item editor")
69 strports.service("8080", appserver.NevowSite(rootResource)).setServiceParent(application)
jethro@divmod.org