Hi,

I've got a controller that is serving up a searchable SQLFORM grid of some 
map locations along with a Leaflet map containing the locations. The 
underlying table looks like this:

db.define_table('gazeteer',
    Field('location', 'string', unique=True),
    Field('type', 'string', requires=IS_IN_SET(gaz_types)),
    Field('parent', 'string'),
    Field('display_order', 'integer'),
    Field('region','string', requires=IS_IN_SET(gaz_regions)),
    Field('plot_size', 'string'),
    Field('fractal_order', 'integer'),
    Field('transect_order', 'integer'),
    Field('centroid_x', 'float'),
    Field('centroid_y', 'float'),
    Field('geom_type', 'string'),
    Field('geom_coords', 'json'))

I only want users to see a small number of those fields in the grid 
(location, type and a couple more) so I make the others unreadable. I want 
to do the following:

1) Populate the Leaflet map with features by passing GeoJSON (mostly just 
geom_type and geom_coords) for rows selected in the SQLFORM. The form 
object returned by SQLFORM.grid contains all the fields (even the ones with 
readable set to FALSE), but it only returns the first page of rows. This 
makes sense, since this is what is rendered, but I'd like to display all 
the features selected not just those in the current page. Options:

a) I could turn off pagination (but there are hundreds of rows) 
b) I can work around by intercepting the keywords, using 
SQLFORM.build_query and running a separate query to capture the row set 
used by SQLFORM.grid (which seems really clunky and does the same database 
query twice).


2) I want to provide a GPX file export, which requires the centroid_x and 
centroid_y fields. That's basically fine, I can provide a new Exporter 
class for the SQLFORM.grid. The Exporter does get all the rows (pagination 
presumably happens just before returning from SQLFORM.grid) but the problem 
now is that the unreadable fields are not present in form.rows. Options:

a) I could make centroid_x and centroid_y readable (looks cluttered, not 
really useful for users)
b) I could scan all the ids out of form.rows and go and look up the details 
(more DBIO).


The version below uses 1b and 2a but am I missing something simple that 
allows me to get at all the data in both cases? The CSV_hidden exporter 
suggests that Exporters can access hidden fields.


def gazeteer():
    
    """
    Controller to provide a map view of the gazeteer data and a searchable
    interface with GPX download.
    """
    
    # If the grid has set up some search keywords, and the keywords aren't 
an empty 
    # string then use them to select those rows, otherwise get all rows
    sfields = [db.gazeteer.location, db.gazeteer.type, db.gazeteer.plot_size
, 
               db.gazeteer.fractal_order, db.gazeteer.transect_order]
    
    if 'keywords' in request.get_vars and request.vars.keywords != '':
        qry = SQLFORM.build_query(sfields, keywords=request.vars.keywords)
    else:
        qry = db.gazeteer
    
    # get the (selected) rows and turn them into geojson, ordering them
    # so that the bottom ones get added to the leaflet map first
    rws = db(qry).select(orderby=db.gazeteer.display_order)
    # removed formating code not relevant to question
    
    # provide a single export format - GPX
    export = dict(gpx=(ExporterGPX, 'GPX'), csv_with_hidden_cols=False,
                  csv=False, xml=False, html=False, json=False,
                  tsv_with_hidden_cols=False, tsv=False)
    
    # hide these fields - except that we need them for GPX
    # output and populating the leaflet map
    db.gazeteer.id.readable = False
    db.gazeteer.centroid_x.readable = True
    db.gazeteer.centroid_y.readable = True
    db.gazeteer.display_order.readable = False
    db.gazeteer.geom_type.readable = False
    db.gazeteer.geom_coords.readable = False
    db.gazeteer.region.readable = False
    db.gazeteer.parent.readable = False
    
    form = SQLFORM.grid(db.gazeteer,
                        csv=True,
                        exportclasses=export,
                        maxtextlength=250,
                        deletable=False,
                        editable=False,
                        create=False,
                        details=False)
        
    return dict(form=form, sitedata=json(rws))


-- 
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/d/optout.

Reply via email to