I have a form where the user builds a query and on submit, displays the results.

I wanted to also have a link on that page to export the results as CSV, but I 
couldn't get the export action to use the values that the first action (display 
the results) does. To get it to work, I ended up duplicating the view and the 
action and changing it to save the csv file instead of displaying the results. 
This seems like more work than should be necessary and is also not a good 
coding practice.

How can I provide both pieces of functionality in one view?


controller:
def display_optimizer_form():
  # Loops through the MAC Addresses and puts them in a list
  options=[str(my_macaddr[i].MacAddr) for i in range(len(my_macaddr))]
  
  #creates the form for choosing which Optimizers, from which time frame
  
form=SQLFORM.factory(Field('Optimizers',requires=IS_IN_SET(options,zero=T('Choose
 one or 
more'),multiple='multiple')),Field('StartTime',requires=[IS_NOT_EMPTY(),IS_DATETIME(format=T('%Y-%m-%dT%H:%M:%S'),
 error_message=T('must be 
YYYY-MM-DDTHH:MM:SS!'))]),Field('EndTime',requires=[IS_NOT_EMPTY(),IS_DATETIME(format=T('%Y-%m-%dT%H:%M:%S'),
 error_message=T('must be YYYY-MM-DDTHH:MM:SS!'))]))
  records=[]
  if form.accepts(request):
    
dbset=db4((db4.data_table.ReqTime>=form.vars.StartTime.strftime('%Y-%m-%dT%H:%M:%S'))&(db4.data_table.ReqTime<=form.vars.EndTime.strftime('%Y-%m-%dT%H:%M:%S')))
    if form.vars.Optimizers:
      query = reduce(lambda a,b:a|b,[db4.data_table.MacAddr==x for x in 
form.vars.Optimizers])
    dbset=dbset(query)
    records = 
dbset.select(db4.data_table.MacAddr,db4.data_table.ReqTime,db4.data_table.Po, 
orderby=db4.data_table.MacAddr|db4.data_table.ReqTime)
    # print records
    return dict(form=form,records=records,results=SQLTABLE(records, 
truncate=100,_class='results'),vars=form.vars,vars2=request.vars)
  else: results=[]
  
  return 
dict(form=form,records=records,results=results,vars=form.vars,vars2=request.vars)
  

def export_optimizer_form():
  import cStringIO
  stream=cStringIO.StringIO()
  # Loops through the MAC Addresses and puts them in a list
  options=[str(my_macaddr[i].MacAddr) for i in range(len(my_macaddr))]
  
  #creates the form for choosing which Optimizers, from which time frame
  
form=SQLFORM.factory(Field('Optimizers',requires=IS_IN_SET(options,zero=T('Choose
 one or 
more'),multiple='multiple')),Field('StartTime',requires=[IS_NOT_EMPTY(),IS_DATETIME(format=T('%Y-%m-%dT%H:%M:%S'),
 error_message=T('must be 
YYYY-MM-DDTHH:MM:SS!'))]),Field('EndTime',requires=[IS_NOT_EMPTY(),IS_DATETIME(format=T('%Y-%m-%dT%H:%M:%S'),
 error_message=T('must be YYYY-MM-DDTHH:MM:SS!'))]))
  records=[]
  if form.accepts(request):
    
dbset=db4((db4.data_table.ReqTime>=form.vars.StartTime.strftime('%Y-%m-%dT%H:%M:%S'))&(db4.data_table.ReqTime<=form.vars.EndTime.strftime('%Y-%m-%dT%H:%M:%S')))
    if form.vars.Optimizers:
      query = reduce(lambda a,b:a|b,[db4.data_table.MacAddr==x for x in 
form.vars.Optimizers])
      print "query"
      print query
    dbset=dbset(query)
    records = 
dbset.select(db4.data_table.MacAddr,db4.data_table.ReqTime,db4.data_table.Po, 
orderby=db4.data_table.MacAddr|db4.data_table.ReqTime)
    records.export_to_csv_file(stream)
    response.headers['Content-Type']='application/vnd.ms-excel'
    response.write(stream.getvalue(), escape=False)

    return dict(form=form,records=records,results=SQLTABLE(records, 
truncate=100,_class='results'),vars=form.vars,vars2=request.vars)
  else: results=[]
  
  return 
dict(form=form,records=records,results=results,vars=form.vars,vars2=request.vars)

-- 
Lorin Rivers
Mosasaur: Killer Technical Marketing <http://www.mosasaur.com>
<mailto:[email protected]>
512/203.3198 (m)


Reply via email to