Hello all,

I decided to tackle this with the first implementation I saw in the 
documentation. I insert two custom fields, remove the f_measure field from 
the form and use the "SQLForm without Database IO" code from the book to 
process the form. Here's my (somewhat ugly) code, hopefully it will be 
useful to someone.

form = SQLFORM(db.t_exercise_log, fields =  ["f_record_taken", "f_comment", 
"f_scaled"],
            labels = {'f_measure' : EXERCISE_UNIT[exercise.f_unit[0]]})
time_element =  TR(LABEL('Time:'),
                DIV(INPUT(_name='minutes',_type='text',_size='2', _maxlength
='3',
                requires=IS_INT_IN_RANGE(0,181,
                error_message=T('Minutes must be between 0 and 180')),
                _id="t_exercise_log_f_min",
                _style="width: 2em; margin: 2px 2px 2px 5px;"),
                T("m"),
                INPUT(_name='seconds',_type='text',_size='2',
                                # 0 < x < 60
                requires=IS_INT_IN_RANGE(0,60,
                error_message=T('Seconds must be between 0 and 59.')),
                _id="t_exercise_log_f_sec",
                _style="width: 2em; margin: 2px 2px 2px 5px;"),
                T("s")))
        form[0].insert(-3,time_element)
        form.vars.f_measure = 0


Form processing:
if form.validate():
sec = form.vars.minutes * 60 + form.vars.seconds
            form.vars.f_measure = sec
            # delete fields or DB insertion will fail
            del form.vars.minutes
            del form.vars.seconds
            form.vars.id = db.t_exercise_log.insert(**dict(form.vars))
            _updateCurrentExerciseLog(auth.user_id, form.vars.id)
            session.flash = T('your record has been logged')
            redirect(URL("list"))







Am Donnerstag, 7. Februar 2013 14:07:46 UTC+1 schrieb Michael Haas:
>
> Hello all,
>
> I'm writing a web app where people can log their exercise sessions. 
> Basically, sessions are timed, and the faster the better. To this end, I 
> store seconds in my model:
>
>     Field('f_record_taken', type='date',notnull=True,default=datetime.date
> .today(),
>           label=T('Record Taken')),
>     Field('f_user', type='reference auth_user',notnull=True,
>           label=T('User')),
>     Field('f_exercise', type='reference t_exercise_description',notnull=
> True,
>           label=T('Exercise')),
>     ### either repetitions, time or weight
>     Field('f_measure', type='double',notnull=True,
>           label=T('Measurement')),
>     Field('f_comment', type='string',label=T("Comment")),
>
>
>
> I use SQLForm to let users insert new entries:
>
> form = SQLFORM(db.t_exercise_log, fields =  ["f_record_taken", "f_measure"
> , "f_comment"]
>
> Obviously, this will render the f_measure field as a single form entry - 
> but users do not want to enter seconds because it requires them to do the 
> math from minutes to seconds. I could theoretically use a time field 
> instead, but I use the f_measure field for other types of exercise as well 
> (think "weight lifted in kilos").
>
> How would you go about doing this? I assume I could use SQLForm.Factory, 
> but then I might have to validate the form by hand (?). So I'm wondering 
> what would be the best, most web2py-ish solution here, e.g. by modifying 
> some kind of representation logic in the model.
>
>
> Kind regards,
>
> Michael
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to