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
-~----------~----~----~----~------~----~------~--~---

Reply via email to