user activity monitornig

30 views
Skip to first unread message

Maxim Oganesyan

unread,
Mar 2, 2012, 7:45:15 AM3/2/12
to TurboGears
Hi all.
I'm looking for easier way for monitoring user activity on my site.

I have a record in User class:

class User(DeclarativeBase):
...
last_activity = Column(DateTime)
...

...and I have a code:

if 'REMOTE_USER' in request.environ:

DBSession.query( User ).filter( User.user_name==request.environ['REMOTE_USER'] ).one().last_activity
= datetime.now()

Where should I insert this code to be called each time when logged-in-
user visits my pages.

I could insert it into each function, but it must be easier way.

Thank You.
Maxim.

Maxim Oganesyan

unread,
Mar 3, 2012, 5:48:46 AM3/3/12
to TurboGears
Here is solution I found using decorators.

...
from tg.decorators import before_call
...
def updateactivity(*l, **kw):
now = datetime.now()

DBSession.query( User ).filter( User.user_name==request.environ['REMOTE_USER'] ).one().last_activity
= datetime(now.year, now.month, now.day, now.hour, now.minute,
now.second)
...
class RootController(BaseController):
...
@expose('myproject.templates.mytemplate')
@require(predicates.not_anonymous)
@before_call(updateactivity)
def mymethod(self, **kw):
...
return dict(page='mypage')
...
Is there any way to use it for whole controller? Like using
"allow_only" for controller instead of "@require" for each method.

Best Regards.
Maxim.

Christoph Zwerschke

unread,
Mar 3, 2012, 7:22:14 AM3/3/12
to turbo...@googlegroups.com
Am 03.03.2012 11:48, schrieb Maxim Oganesyan:
> Is there any way to use it for whole controller? Like using
> "allow_only" for controller instead of "@require" for each method.


You can overwrite the __call__ method of your controller or the base
controller, see her:
http://turbogears.org/2.1/docs/main/explorequickstart.html#base-py

-- Christoph

NiL

unread,
Mar 7, 2012, 3:05:57 AM3/7/12
to turbo...@googlegroups.com
I've been doing something similar, my code is in a custom repoze.who plugin (which I needed anyway)
Reply all
Reply to author
Forward
0 new messages