It would probably make more sense to do the inserts in the model or
controller. Also, no need to call db.commit(), unless you want to count the
visit even if there is an error later in the request.
Anthony
On Monday, April 22, 2013 6:08:02 PM UTC-4, Richard wrote:
>
> Hello,
>
> I want to share this little snippet :
>
> # MODEL
>
> # 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()}}
>
> *Only thing I didn't figured out yet is what the best way to log delete
> event... I thought about that :*
>
> # Not tested and pseudo code
> def myeventlogcall(form)
> if form.delete:
> db.usage_statistics.insert()
> db.commit()
>
> def update_functions():
> ...
> form.process(ondelete=myeventlogcall)
> ...
>
> But, it makes things complicated if case there is already ondelete in an
> update_function...
>
> Any suggestion is welcome.
>
> Thanks
>
> Richard
>
>
--
---
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.