If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!
Marked:
Package type:
plugin

Repository

  • Project:plugin_hotsheet
  • Type:Git
  • Project's site
  • Root location:https://gitlab.com/the_pang/plugin_hotsheet/tree/master

Links

License

  • Name:GNU Lesser General Public License
  • Version:Same as web2py
  • Online version

Suppose you have a models file like this:

# -*- coding: utf-8 -*-

from decimal import Decimal

db.define_table('producer',
    Field('name','string', length=255, unique=True),
)

db.define_table('product',
    Field('name','string', length=255,notnull=True),
    Field('producer', db.producer,requires=IS_IN_DB(db,db.producer,'%(name)s')),#, readable=False, writable=False),
    Field('description','string', length=2048),
    Field('price','decimal(9,2)',
          requires = IS_DECIMAL_IN_RANGE(-1e6,1e6,dot=',')),
    format='%(name)s'
)

db.define_table('item',
    Field('product', db.product,
          requires = IS_IN_DB(db, db.product)),
    Field('user_id', db.auth_user),
    Field('order_date', 'date'),
    Field('amount', 'decimal(9,2)',
          requires = IS_EMPTY_OR(IS_DECIMAL_IN_RANGE(0,1e6)),
          default=Decimal(0)),
)

 

then a controller like this one:

 

# -*- coding: utf-8 -*-

def index():
    from plugin_hotsheet import HOTSheet
    
    date = request.vars.date
    iproducer = long(request.vars.producer)
    producer = db.producer(iproducer)
    igroup = long(request.vars.group)
    group = db.auth_group(igroup)

    response.title = T('orders for group %s producer %s and date %s')%(
        group.role, producer.name, date
    )

    #rows
    query_products = (db.product.producer==iproducer)
    ls_products = [(row.id, row.name) 
                    for row in db(query_products)
                        .select(db.product.id, db.product.name,
                                orderby=db.product.name)]
    row_ids = [id for id,_ in ls_products]    
    row_headers = [nproduct for _,nproduct in ls_products]    

    #cols
    people = [(row.id, row.first_name) for row in
                 db((db.auth_user.id==db.auth_membership.user_id) &
                    (db.auth_membership.group_id==igroup))\
                  .select(db.auth_user.id, db.auth_user.first_name,
                          orderby=db.auth_user.first_name)]
    col_ids = [id for id,_ in people]
    col_headers = [m for _,m in people]

    #data
    query = ((db.item.user_id==db.auth_user.id) &
             (db.auth_membership.user_id==db.auth_user.id) &
             (db.auth_membership.group_id==igroup) &
             (db.item.order_date==date))
    
    sheet = HOTSheet(query,
                    db.item.product,
                    db.item.user_id,
                    db.item.amount,
                    col_ids=col_ids,
                    row_ids=row_ids,
                    row_headers=row_headers,
                    col_headers=col_headers,
                    extra_values={'order_date':date}
                    )

    if sheet.accepts(request.vars):
        session.flash='Updated!'

    return dict(sheet=sheet)

 

will output the spreadsheet. This is an example of moderate complexity, in order to show you that you don't have to expose all the table db.item.


 

Related slices

Comments (1)

  • Login to post



  • 0
    pang 8 years ago

    I noticed that if the form is submitted via ajax it doesn't work. The ultimate reason is on the behaviour of jQuery.serialize

    Note: Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.

    I will fix ASAIRFMH (as soon as I return from my holidays), but if you need it now, it can be fixed using the parameter formname as in gluon.html.FORM.accepts.


Hosting graciously provided by:
Python Anywhere