# MODEL
# Ex.: usage_statistics.py
# Statistics of utilisation
db.define_table('usage_statistics',
Field('time_stamp','datetime', default=request.now),
Field('client_ip','string', default=request.client),
Field('user_id', 'reference auth_user', default=auth.user and auth.user.id),
Field('request_controller','string', default=request.controller),
Field('request_function','string', default=request.function),
Field('request_extension','string', default=request.extension),
Field('request_ajax','string', default=request.ajax),
Field('request_args','string', default=request.args),
Field('request_vars','string', default=request.vars),
Field('request_view','string', default=request.view),
Field('request_http_user_agent','string', default=request.env.http_user_agent),
Field('request_language','string', default=request.env.http_accept_language),
Field('description', 'text'),
migrate=False,
)
# CONTROLLER
def select():
"""
Display log event into usage_statistics table.
"""
db.usage_statistics.id.readable = False
table = SQLFORM.grid(db.usage_statistics, orderby=~db.usage_statistics.time_stamp, ui='jquery-ui', formstyle = 'divs')
return dict(table=table)
# VIEWS
# Just put these two lines in ALL your views or only the views you want to log and you can log what your users do!!
{{db.usage_statistics.insert()}}
{{db.commit()}}
NOTE: You may put db.usage_statistics.insert() in your controller functions and you may also just put it only in a model file.
NOTE2: db.commit() is not required
NOTE3: Some have said that this approach maybe impraticcable because it requires a db insert for each request... You are advised!
Comments (3)
1
calvin-sim-10021 11 years ago
This is very neat! How about implementing this as a decorator?
0
bilal-hasan-10946 9 years ago
Awesome! This just gave me such a quick fix rather than doing all this manually.
Thanks so much!
0
mlrichardvezina 10 years ago
Do it if you want!
For high load app this could be impracticable, because it makes a insert at each request... There is better solution, but for small app it works well...
:)
Richard