Hi people,
I did alredy post this problem on the ToscaWidgets group but no
response.
Could be here ... I'm more lucky.

I'm in truble with validation in TG 1.0.8 ToscaWidgets application.
I did search the discussion to find a solution but seems that I'm
alone with this problem...
So at fisrt my configuration and the code:

the ls of my site-packeges direcory is:

Cheetah-2.0.1-py2.5-linux-i686.egg
CherryPy-2.3.0-py2.5.egg
configobj-4.6.0-py2.5.egg
DBSprockets-0.5dev_r417-py2.5.egg
DecoratorTools-1.7-py2.5.egg
easy-install.pth
Extremes-1.1.1-py2.5.egg
FormEncode-1.2.2-py2.5.egg
Genshi-0.5.1-py2.5-linux-i686.egg
gsquickstart-1.0-py2.5.egg
kid-0.9.6-py2.5.egg
Paste-1.7.2-py2.5.egg
PasteDeploy-1.3.3-py2.5.egg
PasteScript-1.7.3-py2.5.egg
PyProtocols-1.0a0dev_r2302-py2.5-linux-i686.egg
RuleDispatch-0.5a1.dev_r2506-py2.5-linux-i686.egg
setuptools-0.6c9-py2.5.egg
setuptools.pth
simplejson-2.0.9-py2.5-linux-i686.egg
SQLAlchemy-0.5.3-py2.5.egg
ToscaWidgets-0.9.4-py2.5.egg
TurboCheetah-1.0-py2.5.egg
TurboGears-1.0.8-py2.5.egg
TurboJson-1.1.4-py2.5.egg
TurboKid-1.0.4-py2.5.egg
tw.dynforms-0.9.1-py2.5.egg
tw.forms-0.9.3-py2.5.egg
tw.yui
tw.yui-0.9.2.7.0dev-py2.5.egg
WebOb-0.9.6.1-py2.5.egg
WidgetBrowser
WidgetBrowser-0.1dev_20090509-py2.5.egg

controllers.py

def get_activity_form2(role=-1):
    activity_form = twf.TableForm('activity_form',
action='save_activity1', validator=TGSchema, children=[
        twf.HiddenField('id'),
        twf.HiddenField('user'),
        twf.CalendarDatePicker('date', date_format='%d/%m/%Y'),
        twf.SingleSelectField('project', options=get_projects(),
validator=NotEmpty),
        twd.CascadingSingleSelectField('role', options=get_roles
(),cascadeurl='get_activitytypes2',label='Ruolo', validator=NotEmpty),
        twf.SingleSelectField('activitytype',
options=get_activitytypes
(role=role), validator=NotEmpty),
        twf.TextField('minutes', validator=NotEmpty),
    ])
    return activity_form

class Root(controllers.RootController):
    ..............
    @expose('diario_pkg.templates.activity1')
    def activity1(self, activity_id = None, user_id = None,
tg_errors=None, **kw):
        print 'kw',kw
        print 'tg_errors',tg_errors
        if tg_errors:
            activity = {}
            if 'id' in kw:
                activity['id'] = int(kw['id'])
            if 'project' in kw:
                activity['project'] = int(kw['project'])
            if 'date' in kw:
                activity['date'] = kw['date']
            if 'role' in kw:
                activity['role'] = int(kw['role'])
            if 'activitytype' in kw:
                activity['activitytype'] = int(kw['activitytype'])
            if 'minutes' in kw:
                activity['minutes'] = int(kw['minutes'])
        else:
            activity = {'user':user_id}
            if activity_id:
                for a in session.query(model.Activity).filter_by
(activity_id=activity_id):
                    activity['id'] = a.activity_id
                    activity['project'] = a.project_id
                    activity['date'] = a.activity_date
                    activity['role'] = a.role_id
                    activity['activitytype'] = a.activitytype_id
                    activity['minutes'] = a.activity_minutes
        role = activity['role'] if 'role' in activity else -1
        print 'role',role
        print 'activity', activity
        activity_form = get_activity_form2(role=role)
        return dict(activity_form=activity_form,activity=activity)

    @error_handler(activity1)
    @validate(form=get_activity_form2)
    @expose()
    def save_activity1(self, **kw):
        if cp.request.method != 'POST':
            raise Exception('save_activity must be a POST request')
        # TBD: save activity to database
        if kw['id']:
            activity = session.query(model.Activity).filter_by
(activity_id=int(kw['id']))[0]
        else:
            activity = model.Activity()
        activity.user_id = int(kw['user'])
        activity.project_id = int(kw['project'])
        date = map(int,request.params['date'].split('/'))
        activity.activity_date = datetime.date(date[2],date[1],date
[0])
        activity.role_id = int(kw['role'])
        activity.activitytype_id = int(kw['activitytype'])
        activity.activity_minutes = int(kw['minutes'])
        session.add(activity)
        session.commit()

        raise redirect('/activity1',activity_id =
activity.activity_id, user_id = activity.user_id)

The code run and I obtain the 'activty_form' where I can input the
values and save and update.
But when I try to do an error, selecting a role option that have no
activitytype options associated, and than saving,
the error is intercepted by the validate/error_handler couple of lines
correctly but the activity_form is showed not complete.
The form show the the correct values of the date, user, id (if one)
and minutes but the selected values of the select are not selected.
The activitytype is correctly signed like empty with the correct
message but the role and the project filed are not selected with the
values that I choise before the save.
I did try to trace the status of activity dictionary that I pass to
the form and the values are correctly setted. The activitytype field
alone are without value, obviously.

I think to be wrong in something. If I follow the tutorial and the
documentation it seems also superfluous the part of code:
        if tg_errors:
            activity = {}
            if 'id' in kw:
                activity['id'] = int(kw['id'])
            if 'project' in kw:
                activity['project'] = int(kw['project'])
            if 'date' in kw:
                activity['date'] = kw['date']
            if 'role' in kw:
                activity['role'] = int(kw['role'])
            if 'activitytype' in kw:
                activity['activitytype'] = int(kw['activitytype'])
            if 'minutes' in kw:
                activity['minutes'] = int(kw['minutes'])
where I set the values of activity in case of error. From the
documentation it seems to exist a kind of magic solution to do that
but I did'nt able to realise.

Thanks for help.
Best regards
Enrico Secco
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to