Commits

Josh VanderLinden committed f76b3d9

Used the suggestion from GC #11 instead of my solution

Comments (0)

Files changed (3)

tracking/__init__.py

-VERSION = (0, 2, 11)
+VERSION = (0, 2, 12)
 
 def get_version():
     "Returns the version as a human-format string."

tracking/middleware.py

         now = datetime.now()
 
         attrs = {
-                    'session_key': session_key,
-                    'ip_address': ip_address
-                }
+            'session_key': session_key,
+            'ip_address': ip_address
+        }
 
         # for some reason, Visitor.objects.get_or_create was not working here
         try:
             visitor = Visitor.objects.get(**attrs)
         except Visitor.DoesNotExist:
-            # see if there's a visitor with the same IP and user agent
+            # see if there's a visitor with the same IP and session key
             # within the last 5 minutes
             cutoff = now - timedelta(minutes=5)
-            filter_params = {
-                    'ip_address': ip_address,
-                    'user_agent': user_agent,
-                    'last_update__gte': cutoff
-                }
-            try:
-                visitor = Visitor.objects.get(**filter_params)
+            visitors = Visitor.objects.filter(
+                ip_address=ip_address,
+                user_agent=user_agent,
+                last_update__gte=cutoff
+            )
+
+            if len(visitors):
+                visitor = visitors[0]
                 visitor.session_key = session_key
-            except Visitor.MultipleObjectsReturned:
-                # just get the first match
-                visitor = Visitor.objects.filter(**filter_params)[0]
-                visitor.session_key = session_key
-            except Visitor.DoesNotExist:
+            else:
                 # it's probably safe to assume that the visitor is brand new
                 visitor = Visitor(**attrs)
         except:

tracking/utils.py

 
 def get_timeout():
     """
-    gets any specified timeout from the settings file, or use 10 minutes by
+    Gets any specified timeout from the settings file, or use 10 minutes by
     default
     """
     return getattr(settings, 'TRACKING_TIMEOUT', 10)
 
 def get_cleanup_timeout():
     """
-    gets any specified visitor clean-up timeout from the settings file, or
+    Gets any specified visitor clean-up timeout from the settings file, or
     use 24 hours by default
     """
     return getattr(settings, 'TRACKING_CLEANUP_TIMEOUT', 24)
 
 def get_untracked_prefixes():
-    """
-    gets a list of prefixes that shouldn't be tracked
-    """
+    """Gets a list of prefixes that shouldn't be tracked"""
+
     return getattr(settings, 'NO_TRACKING_PREFIXES', [])
 
 def u_clean(s):