Commit 7dc7d1dc authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

REdesign the tool bow with two methods export_csv and import_csv.

parent e43fba37
......@@ -6,16 +6,58 @@ import os
import re
def load_csv():
"""Load a csv file in a database table.
def export_csv():
"""export a table as csv file.
The csv file is produced by using the sqlitebrowser.
The action is steered via the URL:
http://localhost:8000/track_teams/scripts/export_csv?table=foo&fn=foo.csv
The csv file is located in the web2py main directory.
"""
if 'table' not in request.vars:
return 'Specify the name of the table !!!'
tablename = request.vars.table
if tablename not in db.tables:
return 'Invalid database table name "%s"' % tablename
if 'fn' not in request.vars:
return 'Specify the name of the csv file !!!'
fn = request.vars.fn
fi = open(fn, 'w')
writer = csv.DictWriter(fi,
db[tablename].fields,
quoting=csv.QUOTE_MINIMAL)
# write the header
headers = {}
for el in db[tablename].fields:
headers[el] = el
writer.writerow(headers)
# write rows
for row in db(db[tablename]).select():
writer.writerow(row.as_dict())
# close and exit
fi.close()
return 'Table "%s" export in "%s"' % (tablename, fn)
def import_csv():
"""import a csv file in a database table.
The csv file is produced by using the export_csv method.
This method preserved id for the primary and the foreign keys.
The action is steered via the URL:
http://localhost:8000/track_teams/scripts/load_csv?table=foo&fn=foo.csv
http://localhost:8000/track_teams/scripts/import_csv?table=foo&fn=foo.csv
The csv file is located in the web2py directory.
The csv file is located in the web2py main directory.
"""
if 'table' not in request.vars:
......@@ -36,7 +78,7 @@ def load_csv():
for di in reader:
db[tablename][0] = di
return 'File "%s" loaded in "%s"' % (fn, tablename)
return 'File "%s" import in "%s"' % (fn, tablename)
def test():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment