I thing you can also get the paginate parameters from the original
http request stored by cherrypy.
I thing this is called
cherrypy.request.params
look the cherrypy doc to be sure.
Then extract the paginate parameters and do something like
for i, entry in enumerate(entries):
idx=i-(paginate_no-1)*paginate_limit
if 0<=idx and idx<=paginate_limit:
entry.viewed = 1
return dict(entries=entries)
Remi, I like your solution, but prefere to use use a callable this
way :
@expose(...)
@stats('entries', lambda x: x.viewed = 1)
@paginate('entries', ...)
with stats looking like :
def stats(var_name, callable):
def decorator(func):
def wrapper(*args, **kw):
output = func(*args, **kw)
if var_name not in output:
log.info(u"%s not in the result dict" % var_name)
else:
resultat = output[var_name]
for r in resultat:
callable(r)
return output
return wrapper
return decorator
On 7 avr, 11:08, remi jolin <[EMAIL PROTECTED]> wrote:
> Hello,
> Todd Martin a écrit :
>
> > I would like to use the paginate decorator and update the database at
> > the same time, but I'm having trouble making this work correctly. I'm
> > using SQLAlchemy. Here's an example.
>
> > @paginate('entries', allow_limit_override='True', limit=25)
> > def index(self):
> > entries = model.Entry.select(model.Entry.c.unread==1)
> > for entry in entries:
> > entry.viewed = 1
> > return dict(entries=entries)
>
> > The problem with this is it sets viewed to 1 for all entries, not just
> > the page that is returned by paginate. It seems like this is probably
> > the correct behavior for paginate, but how can I use paginate and
> > still update the database?
>
> As it is paginate that "extracts" the displayed subarray from the
> dict/iterator, the solution I use to do something similar (counting the
> number of times an item is displayed) is to define a decorator and call
> it between @expose and @paginate like this
>
> @expose(...)
> @stats('entries')
> @paginate('entries', ...)
>
> with stats looking like :
>
> def stats(var_name):
> def decorator(func):
> def wrapper(*args, **kw):
> output = func(*args, **kw)
> if var_name not in output:
> log.info(u"%s not in the result dict" % var_name)
> else:
> resultat = output[var_name]
> for r in resultat:
> r.viewed = 1
> return output
> return wrapper
> return decorator
>
> > Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---