On Thursday 08 March 2007 00:14, steev wrote:
> Hi all, sorry for the inane question, but I think I must have lost sight
> of the woods with all these trees all over the place. I am new to TG,
> very puzzled, and I think I'm missing something simple.
>
> I've been trying to use the CalendarDateTimePicker to store an value in
> a DateTime column (just as in CatWalk). I get a 500 error when trying to
> add a new row using my code. Test data renders OK, and I've checked that
> the default format for validator.DateTimeConverter matches up. I think
> 'Name' (see below) is a red herring...
>
> If anyone could point me at any docs or earlier posts that might shed a
> light (SQLObject? FormEncode?) I'd be very grateful. Currently reading
> the source for CatWalk to see the Right Way to do it, but i'm relatively
> unfamiliar with javascript and a 1000+ line module may take me a while
> to fully grasp :-/.
>
> Here's what I'm doing, (using 1.01, BTW)
>
> ---> in model.py
>
> class Thing(SLObject):
>       Ends = DateTimeCol(title="Ends at")
>       Name = StringCol(title="Name",notNone=True)
>
> ---> in controllers.py:
>
> class AddItemFormSchema(validators.Schema):
>       # ...
>       finish = validators.DateTimeConverter()
>       what = validators.NotEmpty()
>       # ...
>
> class AddItemFormFields(widgets.WidgetsList):
>       # ...
>       finish = widgets.CalendarDateTimePicker()
>       what = widgets.TextField()
>       # ...
>
> class Root:
>       # template1 contains "${sa(action="new_thing")}"
>       @expose(.templates.template1)
>       def function(self):
>               # ...
>               myForm = widgets.TableForm(fields=AddItemFormFields(),
>                                       validator=AddItemFormSchema())
>               # ...
>               return dict(theForm=myForm)
>
>       @expose(.templates.template2)
>       def new_thing():
>               # ...
>               item = Thing(Name=['what'], Ends=kw['finish'])
>               # ...
>
>
> ---> The 500 error:
>
>   File
> "/usr/lib/python2.4/site-packages/SQLObject-0.7.3-py2.4.egg/sqlobject/decla
>rative.py", line 93, in _wrapper
>     return fn(self, *args, **kwargs)
>   File
> "/usr/lib/python2.4/site-packages/SQLObject-0.7.3-py2.4.egg/sqlobject/main.
>py", line 1205, in __init__
>     self._create(id, **kw)
>   File
> "/usr/lib/python2.4/site-packages/SQLObject-0.7.3-py2.4.egg/sqlobject/main.
>py", line 1229, in _create
>     self.set(**kw)
>   File
> "/usr/lib/python2.4/site-packages/SQLObject-0.7.3-py2.4.egg/sqlobject/main.
>py", line 1088, in set
>     kw[name] = dbValue = from_python(value, self._SO_validatorState)
>   File
> "/usr/lib/python2.4/site-packages/SQLObject-0.7.3-py2.4.egg/sqlobject/col.p
>y", line 966, in from_python
>     (self.name, type(value), value), value, state)
> Invalid: expected a datetime in the DateTimeCol 'Ends', got <type
> 'unicode'> u'2007/03/23 22:30' instead

That's a bug - I've been wanting to file that for a while, but didn't come 
around to do so. The problem is that for whatever reason, 
DateTimePicker-fields aren't converted - what you end up with is a string.

As a workaround, I put an additional validator or schema to either the form or 
the @validate-decorator, like this:


class UntilSchema(validators.Schema):
    until = validators.DateTimeConverter(not_empty=True, 
format=i18n.get_date_format)

class WeeklyRecurrenceFields(w.WidgetsList):
    id = w.HiddenField(validator=validators.Int(not_empty=True))                
                               
    until = w.CalendarDatePicker(format=i18n.get_date_format, default='', 
validator=validators.DateTimeConverter(format=i18n.get_date_format), 
not_empty=True, label=_("Until"), button_text=_("Choose"))
    day = w.SingleSelectField(options=[(i, name) for i, name in 
enumerate(get_weekday_names())])
                                    
form=vw.VarmsForm('weekly_recurrence_form',
                                                      
action='add_weekly_recurrences',
                                                      submit_text=_("Create"),
                                                      validator=UntilSchema(), 
# this seems to be needed to really get date values                             
                         
fields=WeeklyRecurrenceFields()))


Diez

--~--~---------~--~----~------------~-------~--~----~
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