ceys / django-tracking

Automatically exported from code.google.com/p/django-tracking
MIT License
0 stars 0 forks source link

User logged in multiple times: Middleware Exception #11

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Log in with a user account from one machine
2. Log in to that same user account from a different machine/browser
3. Middleware Exception, the site doesn't function for that user anymore

The exception:

MultipleObjectsReturned error in middleware.py line 85 (approx.)

The fix:
Replace the existing block at line 72 with this, which does not assume that
there's a unique Visitor to .get() and uses the first found if there's more
than one:

            # see if there's a visitor with the same IP and user agent
            # within the last 5 minutes
            cutoff = now - timedelta(minutes=5)
            visitors = Visitor.objects.filter(
                            ip_address=ip_address,
                            user_agent=user_agent,
                            last_update__gte=cutoff
                        )
            if visitors.count() >= 1:
                visitor = visitors[0]
            else:
                # it's probably safe to assume that the visitor is brand new
                visitor = Visitor(**attrs)
            visitor.session_key = session_key

Original issue reported on code.google.com by magnus.l...@gmail.com on 16 Nov 2009 at 3:46

GoogleCodeExporter commented 8 years ago
This seems to have been addressed in [9b72f9bb6d36], is that fix considered 
finished? Can anyone reproduce 
this?

Incidentally, that change catches the MultipleObjectsReturned error and retries 
with .filter(**attrs)[0] … is there 
any reason not to just use .filter(**attrs)[0] in the first place? Looking at 
Django code, the get call just calls 
.filter(**attrs) anyway, and here the information that there was more than one 
object is not being used.

Original comment by gunnlau...@gmail.com on 12 May 2010 at 12:04

GoogleCodeExporter commented 8 years ago
I like this suggestion more than the current implementation.  Thanks!

Original comment by wheaties...@gmail.com on 27 Dec 2010 at 4:43