There's some conceptual thing I'm apparently just not getting. I
attempted to follow Doug's advice and came up with:
class LastSeen (object):
"""Middleware that adds various objects to thread local storage
from the request object."""
def process_request(self, request):
now = datetime.datetime.now()
try:
last_request = request.session['last_request']
# don't update it too often, every 4 hours should be ok
if (now - last_request).seconds > (60 * 60 *4):
request.session['last_seen'] = last_request
request.session['last_request'] = now
except KeyError:
request.session['last_request'] =
datetime.datetime.now()
request.session['last_seen'] = datetime.datetime.now()
except TypeError:
request.session['last_request'] =
datetime.datetime.now()
request.session['last_seen'] = datetime.datetime.now()
Which appears to do the exact same thing I was doing before.
On Jan 30, 1:07 pm, "Doug Van Horn" <[EMAIL PROTECTED]> wrote:
> On Jan 30, 11:23 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
> wrote:
>
> > Well, if I were doing it by hand, every time they came to the site I
> > would set this_visit, and then set last_visit (or last_seen, or
> > whatever) to the previous value of this_visit, and I would only do it
> > once, when they first come to the site.
>
> The question, then, is how to determine "when they first come to the
> site."
>
> Right now, you determine that by saying, "If the last_seen variable is
> older than 4 hours, then this user was last seen right now." Note
> that they may have clicked just a second ago, when the last_seen
> variable was 3:59:59 old. Their next click will bump the 'last_seen'
> variable. Not what you want.
>
> You probably want to store the most recent request timestamp as part
> of the session. Something like:
>
> request.session['last_request'] = datetime.now()
>
> Then, you need to figure out when your 'last_seen' session variable
> should be updated. It might be something like:
>
> if (now - last_request) > (60 * 60 * 4): # if the last request is 4+
> hours old...
> request.session['last_seen'] = last_request
>
> Handle your base case, where there is no 'last_request' (and thus no
> last_seen), and you should be good.
>
> Hope that helps.
>
> And remember the advice listed by an earlier post-er. Design your
> algorithm on paper. Think it through. Write some psuedo code. Run
> some mental 'unit tests'. Then go code it.
>
> Regards,
>
> Doug Van Horn, Presidenthttp://www.maydigital.com/ ~~
> http://www.limapapa.com/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" 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/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---