Hi guys!

I'm on my way to learning web2py and I need some help. I'm trying to build
a form to populate or modify a table with several rows at the same time.
The idea is this: I have a table which stores dates, more precisely, annual
dates (like holidays, for example). And I want to create a form that can be
capable of populate a whole set of dates by year.
I've built a form based on my model and then I've added several fields to
that form based on the fields that I need by row. Finally when my form is
validated, the data is processed and then inserted with an update_or_insert
on the DB.

When the page is loaded the form automatically selects the current year (it
would be the most common use), but I want to add the possibility to view or
change the dates for another year. I imagine for this I have to use jQuery
or AJAX but my experience with those are almost none, and the manual wasn't
been very helpfull for me.
I want that when the user change the year, on the dropdown list, the form
automatically updates the data belonging to that year. Then, when the user
hits the submit button, updates or insert the data normally.

Here is my model:
GENERAL_DATE_TYPE = ['reunion1', 'reunion2', 'meeting', 'exam', 'poll']
db.define_table('general_date',
                Field('type', required=True,
requires=IS_IN_SET(GENERAL_DATE_TYPE), label=T("Date Type")),
                Field('year', 'integer', length=4, required=True,
requires=IS_IN_SET(['2013', '2014', '2015', '2016', '2017', '2018']),
label=T("Year")),
                Field('date', 'date', required=True,
requires=IS_DATE(DATE_FORMAT), label=T("Date")),
                Field('start_time', 'time',
requires=IS_EMPTY_OR(IS_TIME()), label=T("Start Time")),
                Field('end_time', 'time', requires=IS_EMPTY_OR(IS_TIME()),
label=T("End Time")),
                auth.signature
                )

Here is my controller:
def new_dates():
    form = SQLFORM.factory(db.general_date.year)

    for date_type in GENERAL_DATE_TYPE:
        fila = TR(LABEL(B(date_type)))
        fila += TR(LABEL(T("Date")+":"), INPUT(_name=date_type.replace('
','_')+'_date', _class="date", requires=IS_EMPTY_OR(IS_DATE(DATE_FORMAT))),
_id=date_type.replace(' ','_')+"_date__row")
        fila += TR(LABEL(T("Start Time")+":"),
INPUT(_name=date_type.replace(' ','_')+'_start_time', _class="time",
requires=IS_EMPTY_OR(IS_TIME())), _id=date_type.replace('
','_')+"_start_time__row")
        fila += TR(LABEL(T("End Time")+":"),
INPUT(_name=date_type.replace(' ','_')+'_end_time', _class="time",
requires=IS_EMPTY_OR(IS_TIME())), _id=date_type.replace('
','_')+"_end_time__row")
        form[0].insert(-1, fila)

    if not request.vars.year:
        form.vars.year = request.now.year
    else:
        form.vars.year = request.vars.year

    data =
db(db.general_date.year==form.vars.year).select(db.general_date.type,
db.general_date.date, db.general_date.start_time, db.general_date.end_time)

    for row in data:
        fieldtype = row.type
        fieldtype = fieldtype.replace(' ', '_')
        form.vars[fieldtype+'_date'] = row.date.strftime(DATE_FORMAT)
        if not row.start_time:
            form.vars[fieldtype+'_start_time'] = row.start_time
        if not row.end_time:
            form.vars[fieldtype+'_end_time'] = row.end_time

    if form.validate():
        for field, value in form.vars.items():
            fieldtype = ""
            date = ""
            start_time = ""
            end_time = ""
            if not value:
                continue
            elif "_date" in field:
                fieldname = field.split("_date")
                fieldtype = fieldname[0].replace('_', ' ')
                date = value
                start_time = form.vars[fieldname[0]+"_start_time"]
                end_time = form.vars[fieldname[0]+"_end_time"]
            elif "_start_time" in field:
                continue
            elif "_end_time" in field:
                continue
            elif "year" in field:
                continue
            else:
                pass


db.general_date.update_or_insert((db.general_date.type==fieldtype)&(db.general_date.year==form.vars.year),
type=fieldtype, year=form.vars.year, date=date, start_time=start_time,
end_time=end_time)
        response.flash = T("new record inserted")
    return dict(form=form)

The view is just the generic one for now.

Thanks in advance!!!
LEO.

PS: sorry my english isn't my native language... I hope I made myself clear.

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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