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

Sometime it's critical to keep tracking who updated the records. Built-in CSV import function is great but doesn't update the field such as updated_on, updated_by fields. Here's how I solved the problem.


Let's create a sample app.


1. Create new app called "who_imports_csv"

2. Model - create db_tables.py

# coding: utf8

    Field('flag', length=1, default=""),
    Field('updated_by', update=auth.user.first_name + " " + auth.user.last_name if auth.user else None),
    Field('updated_on', 'datetime', update=request.now))

db.friends.updated_by.writable = False
db.friends.updated_on.writable = False


Change titles on menu.py if you want.

response.title = "Who imports CSV ?"
response.subtitle = "I know who you are"

3. Controller - replace index action with this

def index():
    if request.vars.csvfile != None:
        # set values
        table = db[request.vars.table]
        file = request.vars.csvfile.file
        # import csv file
        # update who imported
        query = db.friends.flag==""
        response.flash = 'Data uploaded'
    return dict()


4. View - replace default/index.html with this

{{extend 'layout.html'}}


5. Done !


6. Try importing the csv file

7. It's showing who/when updated.

Related slices

Comments (1)

Hosting graciously provided by:
Python Anywhere