1. Christopher Lee
  2. pyramid_simpleform

Commits

Christopher Lee  committed 30b1222

Slight addition to examples to cover more elements

Addition of minor styles
Slight re-jig of layout
Example of extending the examples further

  • Participants
  • Parent commits 32d8153
  • Branches default

Comments (0)

Files changed (8)

File examples/simplesite/db.sqlite

  • Ignore whitespace
Binary file modified.

File examples/simplesite/simplesite/__init__.py

View file
  • Ignore whitespace
     # Set up routes and views
     config.add_handler('home', '/', 'simplesite.handlers:MainHandler',
                        action='index')
-    
+
     config.add_handler('add', '/submit/', 'simplesite.handlers:MainHandler',
                        action='submit')
 
-    config.add_handler('edit', '/edit/{item_id}/', 
+    config.add_handler('edit', '/edit/{item_id}/',
                        'simplesite.handlers:MainHandler',
                        action='edit')
 
+    # Set up static assets
+    config.add_static_view('static', 'static')
 
     config.add_handler('main', '/{action}', 'simplesite.handlers:MainHandler',
         path_info=r'/(?!favicon\.ico|robots\.txt|w3c)')

File examples/simplesite/simplesite/handlers.py

View file
  • Ignore whitespace
 
 from simplesite.models import MyModel, Session
 
+# Used for the value_type select control
+all_value_types = { 1: 'First value type',
+                    2: 'Second value type',
+                    3: 'Third value type' }
+
+
 class MyModelSchema(colander.MappingSchema):
 
 
         colander.Int()
     )
 
+    value_type = colander.SchemaNode(
+        colander.Int()
+    )
+
 
 class NestedSchema(colander.MappingSchema):
 
     @action(renderer='index.html')
     def index(self):
         items = Session().query(MyModel).all()
-        return dict(items=items)
+        return dict(items=items, value_types=all_value_types)
 
     @action(renderer='edit.html')
     def edit(self):
         form = Form(self.request, schema=MyModelSchema(), obj=item)
 
         if form.validate():
-            
+
             form.bind(item)
             session.merge(item)
             session.flush()
-            
+
             return HTTPFound(location="/")
 
-        return dict(item=item, form=FormRenderer(form))
+        value_types = [ (k,v) for k,v in all_value_types.iteritems() ]
+
+        return dict(item=item, form=FormRenderer(form), value_types=value_types)
 
     @action(renderer='submit.html')
     def submit(self):
-        
+
         form = Form(self.request, schema=MyModelSchema())
 
         if form.validate():
 
             return HTTPFound(location="/")
 
-        return dict(form=FormRenderer(form))
+        value_types = [ (k,v) for k,v in all_value_types.iteritems() ]
+
+        return dict(form=FormRenderer(form), value_types=value_types)
 
     @action(renderer='nested.html')
     def nested(self):

File examples/simplesite/simplesite/models.py

View file
  • Ignore whitespace
 
 
 class MyModel(Base):
+    """ value_type is used to demonstrate the select form control
+    """
     __tablename__ = 'models'
 
     id = sa.Column(sa.Integer, primary_key=True)
     name = sa.Column(sa.Unicode(255), nullable=False)
     value = sa.Column(sa.Integer, nullable=False)
+    value_type = sa.Column(sa.Integer, nullable=False)
 
 
 def initialize_sql(engine):

File examples/simplesite/simplesite/static/stylesheets/example.css

View file
  • Ignore whitespace
+ul {
+    padding: 0px 0px 10px 0px;
+    margin: 0;
+}
+
+ul li {
+    list-style: none;
+}
+
+ul li span.label {
+    display: inline-block;
+    width: 100px;
+    font-weight: bold;
+}
+
+ul.error:before {
+    content: "There were errors with the following:"
+}
+
+ul.error {
+    color: red;
+}
+
+ul.error li {
+    margin-left: 20px;
+}
+
+form label {
+    width: 80px;
+    display: inline-block;
+    font-weight: bold;
+}

File examples/simplesite/simplesite/templates/edit.html

View file
  • Ignore whitespace
 <html lang="en">
 <head>
 	<meta charset="UTF-8">
-	<title></title>
+	<title>Edit an existing item</title>
+    <link rel="stylesheet"
+          href="{{ request.static_url('simplesite:static/stylesheets/example.css')}}"
+          type="text/css" media="screen" />
 </head>
 <body>
     <h2>Edit this item</h2>
     {{ form.begin(request.route_url('edit', item_id=item.id)) }}
     {{ form.csrf() }}
-    <div>
+    <ul>
+      <li>
         {{ form.errorlist("name") }}
         {{ form.label("name") }}
         {{ form.text('name') }}
-    </div>
-    <div>
+      </li>
+      <li>
         {{ form.errorlist("value") }}
         {{ form.label("value") }}
         {{ form.text('value') }}
-    </div> 
+      </li>
+      <li>      {{ form.errorlist("value_type") }}
+        {{ form.label("value_type", "Value Type") }}
+        {{ form.select("value_type", value_types) }}
+      </li>
+    </ul>
     {{ form.submit("submit", "Update") }}
     {{ form.end() }}
 </body>

File examples/simplesite/simplesite/templates/index.html

View file
  • Ignore whitespace
 <html lang="en">
 <head>
 	<meta charset="UTF-8">
-	<title></title>
+	<title>Current Items</title>
+    <link rel="stylesheet"
+          href="{{ request.static_url('simplesite:static/stylesheets/example.css')}}"
+          type="text/css" media="screen" />
 </head>
 <body>
+  <h2>List of current Items</h2>
+  <div>
     {% for item in items %}
-    <div>
-    Name: {{ item.name }} Value: {{ item.value }} <a href="{{ request.route_url('edit', item_id=item.id) }}">edit</a>
-    </div>
+    <ul>
+      <li><span class="label">Name:</span> {{ item.name }}</li>
+      <li><span class="label">Value:</span> {{ item.value }}</li>
+      <li><span class="label">Value Type:</span> {{ value_types[item.value_type] }}</li>
+      <li><a href="{{ request.route_url('edit', item_id=item.id) }}">edit</a></li>
+    </ul>
     {% endfor %}
-    <a href="{{ request.route_url('add') }}">add</a>
+  </div>
+    <a href="{{ request.route_url('add') }}">Add Another Item</a>
 </body>
 </html>

File examples/simplesite/simplesite/templates/submit.html

View file
  • Ignore whitespace
 <html lang="en">
 <head>
 	<meta charset="UTF-8">
-	<title></title>
+	<title>Add a new item</title>
+    <link rel="stylesheet"
+          href="{{ request.static_url('simplesite:static/stylesheets/example.css')}}"
+          type="text/css" media="screen" />
 </head>
 <body>
     <h2>Add a new item</h2>
     {{ form.begin(request.route_url('add')) }}
     {{ form.csrf() }}
-    <div>
+    <ul>
+      <li>
         {{ form.errorlist("name") }}
         {{ form.label("name") }}
         {{ form.text('name') }}
-    </div>
-    <div>
+      </li>
+      <li>
         {{ form.errorlist("value") }}
         {{ form.label("value") }}
         {{ form.text('value') }}
-    </div> 
+      </li>
+      <li>
+        {{ form.errorlist("value_type") }}
+        {{ form.label("value_type", "Value Type") }}
+        {{ form.select("value_type", value_types) }}
+      </li>
+    </ul>
     {{ form.submit("submit", "Add") }}
     {{ form.end() }}
 </body>