~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Nevow/examples/customform/customform.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#################################################################################
2
 
# Example of using patterns to change the appearance of a webform.
3
 
 
4
 
#from twisted.application import internet, service
5
 
#from twisted.web import static
6
 
 
7
 
from zope.interface import implements
8
 
 
9
 
from nevow import rend
10
 
from nevow import url
11
 
from nevow import loaders
12
 
from nevow import tags as T
13
 
 
14
 
from formless import annotate
15
 
from formless import webform
16
 
 
17
 
 
18
 
#################################################################################
19
 
# This beasty defines how I want the form to look. It's a table (eek!).
20
 
# webform looks for patterns to use when rendering parts of the form and fills
21
 
# slots with key information.
22
 
#
23
 
# Key patterns are:
24
 
#   freeform-form   -- the form itself, mostly just the structure
25
 
#   argument        -- the pattern to use for arguments when nothing better
26
 
#                      is found
27
 
#   argument!!fo    -- the pattern to use for the 'fo' argument
28
 
#
29
 
# Inside the patterns the following slots are filled:
30
 
#   freeform-form:
31
 
#     form-action       -- action attribute, where the form will be posted
32
 
#     form-id           -- id of the form
33
 
#     form-name         -- name of the form
34
 
#     form-label        -- form label, extracted from the docstring
35
 
#     form-description  -- description, also extracted from the docstring
36
 
#     form-error        -- "global" error
37
 
#     form-arguments    -- insertion point for the arguments' HTML
38
 
#   argument:
39
 
#     label             -- label
40
 
#     input             -- the form element (input, textarea, etc)
41
 
#     error             -- error message (if any)
42
 
#     description       -- description of argument
43
 
#
44
 
# Note that you do not have to provide slots for all of the above. For
45
 
# instance, you may not want to display the descriptions.
46
 
#
47
 
# Chances are that this block of text would be in a disk template or
48
 
# perhaps defined using stan in a taglib module.
49
 
 
50
 
 
51
 
FORM_LAYOUT = loaders.xmlstr(
52
 
    """<?xml version="1.0"?>
53
 
    <form xmlns:n="http://nevow.com/ns/nevow/0.1" n:pattern="freeform-form">
54
 
    
55
 
      <!-- Replace/fill the form attributes -->
56
 
      <n:attr name="action"><n:slot name="form-action"/></n:attr>
57
 
      <n:attr name="id"><n:slot name="form-id"/></n:attr>
58
 
      <n:attr name="name"><n:slot name="form-name"/></n:attr>
59
 
      
60
 
      <!-- General form information -->
61
 
      <p><strong><n:slot name="form-label"/></strong></p>
62
 
      <p><em><n:slot name="form-description"/></em></p>
63
 
      <p><strong><em><n:slot name="form-error"/></em></strong></p>
64
 
      
65
 
      <!-- Start of the form layout table -->
66
 
      <table style="background: #eee; border: 1px solid #bbb; padding: 1em;" >
67
 
        <!-- Mark location arguments will be added -->
68
 
        <n:slot name="form-arguments"/>
69
 
        <!-- General argument layout pattern -->
70
 
        <n:invisible n:pattern="argument" n:render="remove">
71
 
          <tr>
72
 
            <th><n:slot name="label"/>:</th>
73
 
            <td><n:slot name="input"/><span class="freeform-error"><n:slot name="error"/></span></td>
74
 
          </tr>
75
 
          <tr>
76
 
            <th></th>
77
 
            <td><n:slot name="description"/></td>
78
 
          </tr>
79
 
        </n:invisible>
80
 
        <!-- Argument layout, just for fum -->
81
 
        <n:invisible n:pattern="argument!!fo" n:render="remove">
82
 
          <tr>
83
 
            <th><n:slot name="label"/>:</th>
84
 
            <td>
85
 
              <textarea cols="40" rows="5"><n:attr name="id"><n:slot name="id"/></n:attr><n:attr name="name"><n:slot name="name"/></n:attr><n:slot name="value"/></textarea>
86
 
              <span class="freeform-error"><n:slot name="error"/></span></td>
87
 
          </tr>
88
 
          <tr>
89
 
            <th></th>
90
 
            <td><n:slot name="description"/></td>
91
 
          </tr>
92
 
        </n:invisible>
93
 
        <!-- Button row -->
94
 
        <tr>
95
 
          <td colspan="2">
96
 
            <n:slot name="form-button"/>
97
 
          </td>
98
 
        </tr>
99
 
      </table>
100
 
    </form>
101
 
    """).load()
102
 
 
103
 
 
104
 
#################################################################################
105
 
# ISomething and Page are just something to test the form rendering on.
106
 
 
107
 
class ISomething(annotate.TypedInterface):
108
 
    
109
 
    def doSomething(
110
 
        ctx = annotate.Context(),
111
 
        fee = annotate.String(required=True, description="Wee!"),
112
 
        fi = annotate.Integer(description="Tra-la-la"),
113
 
        fo = annotate.Text(),
114
 
        fum = annotate.String(),
115
 
        ):
116
 
        """Do Something Really Exciting
117
 
 
118
 
        Normally you would put a useful description of the interface here but,
119
 
        since the inteface is useless anyway, I cannot think of anything
120
 
        useful to say about it. Although ... did I mention it is useless?"""
121
 
    doSomething = annotate.autocallable(doSomething)
122
 
    
123
 
 
124
 
class Root(rend.Page):
125
 
    """Render a custom and normal form for an ISomething.
126
 
    """
127
 
    implements(ISomething)
128
 
    addSlash = True
129
 
    
130
 
    child_webform_css = webform.defaultCSS
131
 
    
132
 
    def render_normalForm(self, ctx, data):
133
 
        return webform.renderForms()
134
 
    
135
 
    def render_customForm(self, ctx, data):
136
 
        return webform.renderForms()[FORM_LAYOUT]
137
 
    
138
 
    def doSomething(self, ctx, **kwargs):
139
 
        print '***** doSomething called with:', kwargs
140
 
    
141
 
    docFactory = loaders.stan(
142
 
        T.html[
143
 
            T.head[
144
 
                T.title['Example :: Custom Form Layout'],
145
 
                T.link(rel='stylesheet', type='text/css', href=url.here.child("webform_css")),
146
 
                ],
147
 
            T.body[
148
 
                T.h1['Custom'],
149
 
                render_customForm,
150
 
                T.h1['Default'],
151
 
                render_normalForm,
152
 
                ]
153
 
            ]
154
 
        )
155
 
 
156
 
 
157
 
#application = service.Application('hellostan')
158
 
#webServer = internet.TCPServer(8080, appserver.NevowSite(Root()))
159
 
#webServer.setServiceParent(application)