I spent a lot of time for understanding that cases when we can use SQLFORM 
or SQLFORM.factory() 1 or 2 in 100.

[image: 1.png]

[image: 2.png]

[image: 3.png]

[image: 4.png]
I have table in database for report and on picture I show differ between 
what I need and what can supply SQLFORM. I know that we can exclude 
unnecessary fields and change labels in SQLFORM, but I don't know how to 
combine helps type with status and cloned it 18 times.
All code if you want:
@auth.requires_login()
def egisso_load():
    def get_help_id(name):
        result = None
        if len(name) > 5:
            try:
                result = int(name[5:])
            except:
                return result
        return result


    helps_statuses = db().select(db.s_helps_statuses.id, db.s_helps_statuses
.name,
                                 cache=(cache.ram, 3600), cacheable=True)
    helps_types = db(db.s_helps_types.active == 1).select(
        db.s_helps_types.id, db.s_helps_types.name, orderby=db.s_helps_types
.id, cache=(cache.ram, 3600),
        cacheable=True)
    years = [OPTION(request.now.year, _value=request.now.year),
             OPTION(request.now.year - 1, _value=request.now.year - 1)]
    message = ''
    form = FORM(DIV(DIV(LABEL('Год:', _for='year'),
                        SELECT(*years, _name='year', _id='year', _class=
'form-control',
                               requires=IS_INT_IN_RANGE(request.now.year - 1
, request.now.year + 1,
                                                        error_message="Year 
isn't correct!"),
                               **{'_v-model': 'year'}, **{"'v-test'": "test"
}),
                        _class='col-sm-12 col-md-4 col-lg-3'),
                    DIV(LABEL('Месяц:', _for='month'),
                        SELECT(
                            OPTION('Январь', _value=1),
                            OPTION('Февраль', _value=2),
                            OPTION('Март', _value=3),
                            OPTION('Апрель', _value=4),
                            OPTION('Май', _value=5),
                            OPTION('Июнь', _value=6),
                            OPTION('Июль', _value=7),
                            OPTION('Август', _value=8),
                            OPTION('Сентябрь', _value=9),
                            OPTION('Октябрь', _value=10),
                            OPTION('Ноябрь', _value=11),
                            OPTION('Декабрь', _value=12),
                            requires=IS_INT_IN_RANGE(1, 13, 
error_message='Указан 
некорректный месяц!'),
                            _name='month', _id='month', _class=
'form-control'),
                        _class='col-sm-12 col-md-4 col-lg-3'),
                    _class='form-group row'),
                DIV(DIV(P('Виды выплат:', _class='lead mb-0'), _class='col'
), _class='form-group row'),
                DIV(DIV(INPUT(_type='checkbox', _name='chb', _id='chb', **{
'_v-model': 'chb'},
                              _class='form-check-input'),
                       LABEL('Отметить всё как загруженное:', _for='chb', 
_class='form-check-label'),
                       _class='form-check'), _class='form-group'))


    helps_statuses_list = list()
    for row in helps_statuses:
        helps_statuses_list.append(OPTION(row.name, _value=row.id))
    helps_statuses_list.insert(0, OPTION(''))
    columns_list = list()
    for row in helps_types:
        columns_list.append(DIV(LABEL(row.name, _for=f'help_{row.id}'),
                            SELECT(*helps_statuses_list, _id=f
'help_{row.id}',
                                   _name=f'help_{row.id}', _class=
'form-control',
                                   requires=IS_IN_DB(db, 
's_helps_statuses.id',
                                                     error_message='Выберите 
значение!')),
                            _class='col-sm-12 col-md-4 col-lg-3 pb-3'))
    form.append(DIV(*columns_list, _class='form-group row', _id=
'helps_type_row'))
    form.append(DIV(DIV(INPUT(_type='submit', _class='btn btn-dark w-100'), 
_class='col-sm col-md-4 col-lg-3'),
                    _class='form-group row'))
    if form.process().accepted:
        for x in form.vars:
            if x not in ('year', 'month', 'chb'):
                help_id = get_help_id(x)
                if help_id is not None:
                    db.a_egisso_and_divisions.update_or_insert(
                        (db.a_egisso_and_divisions.division == auth.user.
division) &
                        (db.a_egisso_and_divisions.year == form.vars.year) &
                        (db.a_egisso_and_divisions.month == form.vars.month) 
&
                        (db.a_egisso_and_divisions.helps_type == help_id),
                        division=auth.user.division, helps_type=help_id, 
year=form.vars.year,
                        month=form.vars.month, status=form.vars[x])
                else:
                    message = f'Из ключа {x} не может быть извлечено 
значение идентификатора!'
    return dict(form=form, message=message)



-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/e6c10551-db12-4245-b75d-29f158887f39o%40googlegroups.com.

Reply via email to