Differences between revisions 4 and 7 (spanning 3 versions)
Revision 4 as of 2012-04-18 02:50:17
Size: 2786
Comment:
Revision 7 as of 2012-05-01 02:58:55
Size: 5956
Comment:
Deletions are marked like this. Additions are marked like this.
Line 89: Line 89:
=== widget ===
 *Adding toscawidget to pyramid:
 *http://toscawidgets.org/documentation/tw2.core/pyramid.html
 *http://pythonpaste.org/deploy/#filter-composition
 *First add these to setup.py:
{{{
requires=[

    ...

    "tw2.core",
    "tw2.forms",
    "tw2.dynforms",
    "tw2.sqla",
    "tw2.jqplugins.jqgrid",
    "formencode",
    ],
}}}
 *Then run:
{{{
python setup.py develop
}}}
 *Now edit development.ini and add this on first line:
{{{
[pipeline:main]
pipeline =
    tw2.core
    myapp

[filter:tw2.core]
use = egg:tw2.core#middleware
}}}
 *And right below change app:main
from
{{{
[app:main]
use = egg:...
}}}
to
{{{
[app:myapp]
use = egg:...
}}}

 *Now we are ready to add the widget. Go to the view.py and add above your view
{{{
from tw2.forms import TableForm, TextField, CalendarDatePicker, SingleSelectField, TextArea
import tw2
#Validator
from formencode.validators import Int, NotEmpty, DateConverter, DateValidator,PostalCode,String,Email
Line 91: Line 141:
class AddressForm(TableForm):
    title='MyApp Add Address Form'
    # This WidgetsList is just a container
    #class fields(WidgetsList):
    FirstName = TextField(validator=tw2.core.Required)
    LastName = TextField(validator=tw2.core.Required)
    MaidenLastName = TextField(validator=String)
    Email = TextField(validator=tw2.core.EmailValidator)
    Address = TextField(validator=tw2.core.Required)
    City = TextField(validator=tw2.core.Required)
    State = TextField(validator=tw2.core.Required)
    #Or you could do:
    StateChoices = (("IL"),
                     ("IN"),
                     ("MS"),
                    )
    State = SingleSelectField(options=StateChoices, validator=NotEmpty)
    ZipCode = TextField(size=5, validator=PostalCode())
    DOB = CalendarDatePicker(validator=tw2.core.DateValidator)
    GenderChoices = (("Female"),
                     ("Male"),
                    )
    Gender = SingleSelectField(options=GenderChoices)
    Description = TextArea(rows=3, cols=25)
}}}
Line 92: Line 167:
* Now in the view.py add the following in your add_view. The first line checks if its a post request, then checks if all validators passed if it didn't pass then you need to catch the exception and send it back to the template. When all validators pass you can do what you need with the data.
{{{
import tw2.core as twc
@view_config(route_name='add', renderer='templates/add.pt')
def add_view(request):
    #address_widget = AddressForm(action='saveaddress')
    from myapp.widgets import AddressForm
    #context='myapp.widget.AddressForm'
    if request.method=='POST':
        # First, validate the posted data
        try:
            form_result = AddressForm.validate(request.POST)
        except twc.ValidationError, e:
            return {'widget': e.widget,'project':'myapp'}
        print request
    return {'widget':AddressForm(),'project':'myapp'}
}}}

* The last part is to add our 'widget' to our template.Update your add.pt to:
{{{
   <div id="left" class="align-right">
 <p tal:content="structure widget.display()"></p>
        </div>
}}}
{{attachment:add_toscawidget2.png}}

Addressbook

structure

You probably created your alchemy scaffold, and here is your folder structure:

myapp/
├── CHANGES.txt
├── development.ini
├── MANIFEST.in
├── myapp
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── scripts
│   │   ├── initializedb.py
│   │   ├── initializedb.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── static
│   │   ├── favicon.ico
│   │   ├── footerbg.png
│   │   ├── headerbg.png
│   │   ├── ie6.css
│   │   ├── middlebg.png
│   │   ├── pylons.css
│   │   ├── pyramid.png
│   │   ├── pyramid-small.png
│   │   └── transparent.gif
│   ├── templates
│   │   └── mytemplate.pt
│   ├── tests.py
│   ├── tests.pyc
│   ├── views.py
│   └── views.pyc
├── myapp.db
├── myapp.egg-info
│   ├── dependency_links.txt
│   ├── entry_points.txt
│   ├── not-zip-safe
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
├── production.ini
├── README.txt
├── setup.cfg
└── setup.py

views.py

  • This will be the primary file where we will be working on our web application
  • Lets add our add function that will allow you to enter new addressbook.
  • Edit init.py and add the new add_route.

config.add_route('add', '/add')
  • This will add a route "add" and the url that it will use is "/add"
  • Now lets create a view that will handle our form.
  • Edit views.py and add our new "add view handler.

@view_config(route_name='add', renderer='templates/add.pt')
def add_view(request):
    '''This is an add view. Here is where we define and pass any information to the template.'''
    #Code here
    return {'project':'myapp'}

add template

  • Lets go to templates folder and create our add.pt
  • To simplify our coding we will copy an existing template and name it add.pt

cd templates
cp mytemplate.pt add.pt
  • Then replace the text:

#From:

Welcome to <span class="app-name">${project}</span>, an application generated by<br/>the Pyramid web application development framework.

#To:

Welcome to <span class="app-name">${project}</span>.<br/> Please add your address.
  • Lets check if it works, and our next step will be to add the form.

add1.png

widget

requires=[

    ...

    "tw2.core",
    "tw2.forms",
    "tw2.dynforms",
    "tw2.sqla",
    "tw2.jqplugins.jqgrid",
    "formencode",
    ],
  • Then run:

python setup.py develop
  • Now edit development.ini and add this on first line:

[pipeline:main]
pipeline =
    tw2.core
    myapp

[filter:tw2.core]
use = egg:tw2.core#middleware
  • And right below change app:main

from

[app:main]
use = egg:...

to

[app:myapp]
use = egg:...
  • Now we are ready to add the widget. Go to the view.py and add above your view

from tw2.forms import TableForm, TextField, CalendarDatePicker, SingleSelectField, TextArea
import tw2
#Validator
from formencode.validators import Int, NotEmpty, DateConverter, DateValidator,PostalCode,String,Email


class AddressForm(TableForm):
    title='MyApp Add Address Form'
    # This WidgetsList is just a container
    #class fields(WidgetsList):
    FirstName = TextField(validator=tw2.core.Required)
    LastName = TextField(validator=tw2.core.Required)
    MaidenLastName = TextField(validator=String)
    Email = TextField(validator=tw2.core.EmailValidator)
    Address = TextField(validator=tw2.core.Required)
    City = TextField(validator=tw2.core.Required)
    State = TextField(validator=tw2.core.Required)
    #Or you could do:
    StateChoices = (("IL"),
                     ("IN"),
                     ("MS"),
                    )
    State = SingleSelectField(options=StateChoices, validator=NotEmpty)
    ZipCode = TextField(size=5, validator=PostalCode())
    DOB = CalendarDatePicker(validator=tw2.core.DateValidator)
    GenderChoices = (("Female"),
                     ("Male"),
                    )
    Gender = SingleSelectField(options=GenderChoices)
    Description = TextArea(rows=3, cols=25)

* Now in the view.py add the following in your add_view. The first line checks if its a post request, then checks if all validators passed if it didn't pass then you need to catch the exception and send it back to the template. When all validators pass you can do what you need with the data.

import tw2.core as twc
@view_config(route_name='add', renderer='templates/add.pt')
def add_view(request):
    #address_widget = AddressForm(action='saveaddress')
    from myapp.widgets import AddressForm
    #context='myapp.widget.AddressForm'
    if request.method=='POST':
        # First, validate the posted data
        try:
            form_result = AddressForm.validate(request.POST)
        except twc.ValidationError, e:
            return {'widget': e.widget,'project':'myapp'}
        print request
    return {'widget':AddressForm(),'project':'myapp'}

* The last part is to add our 'widget' to our template.Update your add.pt to:

   <div id="left" class="align-right">
 <p tal:content="structure widget.display()"></p>
        </div>

add_toscawidget2.png

create database model

MyWiki: Pyramid/addressbook (last edited 2012-05-23 03:02:26 by LukaszSzybalski)