Hey there

I have a small problem with using widgets for my form. I think the
problem lies in the fact that the form contains a select field which is
pulled from the database, and therefore needs to be "refreshed" every
time that data changes - this breaks links with
@validate(form=forms.myform), and from then on every time I submit an
invalid form I don't get messages and my data isn't populated.

Below is the code for my app. You'll notice a call to forms.refresh
after I save an adcode. It's saving an adcode which breaks everything.

The only solution I can think of is reloading the entire controller, but
it doesn't sound optimal to me. Could someone suggest another approach?

Thanks

-Rob

# controllers.py

import cherrypy
import turbogears
from turbogears.toolbox.catwalk import CatWalk
from turbogears import controllers
from turbogears import identity
from turbogears import widgets
from turbogears import validators
from turbogears.fastdata import *

import model
from model import *

class Forms:

        def __init__(self):
                self.refresh()

        def refresh(self):
                self.allprojects = widgets.TableForm(fields=[
                        widgets.SingleSelectField(name="projectid",
label="Project",
                                options=[(p.id, p.title) for p in
Project.select()], validator=validators.NotEmpty)
                ], submit_text="Edit")
                self.project = widgets.TableForm(fields=[
                        widgets.TextField(name="title", label="Project
title", validator=validators.NotEmpty),
                        widgets.TextField(name="domain", label="Domain",
validator=validators.NotEmpty),
                        widgets.SingleSelectField(name="adcode",
label="Advertisements",
                                options=[(a.id, a.name) for a in
Adcode.select()], validator=validators.NotEmpty),
                        widgets.TextArea(name="copy", label="Copy",
validator=validators.NotEmpty),
                        widgets.TextArea(name="keywords",
label="Keywords", validator=validators.NotEmpty),
                        widgets.HiddenField(name="projectid")
                ], submit_text="Save")
                self.alladcodes = widgets.TableForm(fields=[
                        widgets.SingleSelectField(name="adcodeid",
label="Adcode", validator=validators.NotEmpty,
                                options=[(a.id, a.name) for a in
Adcode.select()])
                ], submit_text="Edit")
                self.adcodes = widgets.TableForm(fields=[
                        widgets.TextField(name="name",
validator=validators.NotEmpty),
                        widgets.TextArea(name="adcode",
validator=validators.NotEmpty),
                        widgets.HiddenField(name="adcodeid")
                ], submit_text="Save")

forms = Forms()

class Pyrgen(controllers.RootController):

        catwalk = CatWalk(model, allowedHosts=["192.168.0.0/24"])

        @turbogears.expose(template=".templates.index")
        def index(self, tg_errors=None):
                return dict(
                        title = "Manage Projects",
                        editprojectform = forms.allprojects,
                        projectform = forms.project
                )
        @turbogears.expose(template=".templates.adcodes")
        def adcodes(self):
                return dict(
                        title="Manage Adcodes",
                        all=forms.alladcodes,
                        form=forms.adcodes,
                        value={},
                )

        @turbogears.expose(template=".templates.index")
        @turbogears.validate(form=forms.project)
        @turbogears.error_handler(index)
        def SaveProject(self, projectid, title, domain, adcode, copy,
keywords):
                try:
                        p = Project.get(projectid)
                        p.title = title
                        p.domain = domain
                        p.adcode = adcode
                        p.copy = copy
                except ValueError:
                        p = Project(
                                title=title,
                                domain=domain,
                                adcode=adcode,
                                copy=copy)
                keywords = keywords.split("\n")
                keywords = [ keyword.strip() for keyword in keywords if
keyword.strip() ]
                for keyword in p.keywords:
                        p.removeKeyword(keyword)
                for keyword in keywords:
                        try:
                                kw = Keyword.byKeyword(keyword)
                        except SQLObjectNotFound:
                                kw = Keyword(keyword=keyword)
                        p.addKeyword(kw)
                turbogears.flash("Project saved")
                return dict(
                        title="Project Saved",
                        editprojectform = forms.allprojects,
                        projectform = forms.project
                        )
        @turbogears.expose(template=".templates.editproject")
        def EditProject(self, projectid):
                p = Project.get(projectid)
                values = dict(
                        projectid=p.id,
                        title=p.title,
                        domain=p.domain,
                        adcode=p.adcode,
                        copy=p.copy,
                        keywords="\n".join([ kw.keyword for kw in
p.keywords ])
                )
                return dict(
                        title="Edit Project: "+p.title,
                        form=forms.project,
                        values=values
                )

        @turbogears.expose(template=".templates.adcodes")
        @turbogears.validate(form=forms.adcodes)
        def SaveAdcode(self, adcodeid, name, adcode):
                try:
                        a = Adcode.get(adcodeid)
                        a.name = name
                        a.adcode = adcode
                except:
                        a = Adcode(
                                name=name,
                                adcode=adcode,
                        )
                turbogears.flash("Adcode Saved")
                forms.refresh()
                return dict(
                        title="Adcode Saved",
                        all=forms.alladcodes,
                        form=forms.adcodes,
                        value={}
                )

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to