this seems more accurate:

    def process_request( self, request ):
        now = datetime.datetime.now()
        # get last_request, defaults to now, when he was never seen before
        # you may wish to omit setting last_seen in that case (he
wasn't ever seen)
        last_request = request.get( 'last_request', now )
        # when did you last see him? when he last requested something! ::
        # if you really want to, you can add the 4-hour waiting time
here (only for this line though !!)
        request.session['last_seen'] = last_request
        # now is the time he is making his last request
        request.session['last_request'] = now

no tries, no catches...

On 1/31/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> I think this MAY be working now and I think I even finally wrapped my
> head around what's going on. So, in hopes of helping someone else some
> day (or, alternately, someone pointing out any trouble spots
> remaining), the last_visit middleware:
>
> import datetime
>
> class LastSeen (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()
>
> And I'd like to thank Honza, Doug, and everyone else who tried so hard
> to pound this simple thing through my thick skull.
>
> On Jan 31, 8:42 am, "Honza Kr�l" <[EMAIL PROTECTED]> wrote:
> > On 1/31/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> > > I know I'm dense, and I'm just not seeing this, but isn't that what
> > > I'm doing?
> >
> > > now = datetime.datetime.now()
> > > last_request = request.session['last_request']
> >
> > > if (now - last_request).seconds > (60 * 60 *4):
> > > ...
> >
> > but this line:
> >
> > >                      request.session['last_request'] = now
> >
> > is only executed when last_request is lder than 4 hours... hardly
> > seems like always, does it?
> >
> >
> >
> >
> >
> > > On Jan 31, 7:47 am, "Honza Kr?l" <[EMAIL PROTECTED]> wrote:
> > > > On 1/31/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >
> > > > > Ok, but if I update last_request at every request, then won't (now -
> > > > > last_request) ALWAYS be more or less 0?
> >
> > > > not if you update it AFTER the comparison...
> >
> > > > > On Jan 31, 4:16 am, "Honza Kr?l" <[EMAIL PROTECTED]> wrote:
> > > > > > On 1/31/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >
> > > > > > > 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
> >
> > > > > > you have to update last request at every request, not only when its
> > > > > > too old... if you do it like this it is EXACTLY what you did before
> >
> > > > > > >         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/
> >
> > > > > > --
> > > > > > Honza Kr?l
> > > > > > E-Mail: [EMAIL PROTECTED]
> > > > > > ICQ#:   107471613
> > > > > > Phone:  +420 606 678585
> >
> > > > --
> > > > Honza Kr?l
> > > > E-Mail: [EMAIL PROTECTED]
> > > > ICQ#:   107471613
> > > > Phone:  +420 606 678585
> >
> > --
> > Honza Kr?l
> > E-Mail: [EMAIL PROTECTED]
> > ICQ#:   107471613
> > Phone:  +420 606 678585
>
>
> >
>


-- 
Honza Kr�l
E-Mail: [EMAIL PROTECTED]
ICQ#:   107471613
Phone:  +420 606 678585

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to