#NEWLINE# def process_request( self , request ) : #NEWLINE# #INDENT# ip_address = utils . get_ip ( request ) #NEWLINE# user_agent = unicode ( request . META . get ( 'HTTP_USER_AGENT' , '' ) [ : 255 ] , errors = 'ignore' ) #NEWLINE# ua_key = '_tracking_untracked_uas' #NEWLINE# untracked = cache . get ( ua_key ) #NEWLINE# if ( untracked is None ) : #NEWLINE# #INDENT# log . info ( 'Updating untracked user agent cache' ) #NEWLINE# untracked = UntrackedUserAgent . objects . all ( ) #NEWLINE# cache . set ( ua_key , untracked , 3600 ) #NEWLINE# #UNINDENT# for ua in untracked : #NEWLINE# #INDENT# if ( user_agent . find ( ua . keyword ) != ( - 1 ) ) : #NEWLINE# #INDENT# log . debug ( ( 'Not tracking UA "%s" because of keyword: %s' % ( user_agent , ua . keyword ) ) ) #NEWLINE# return #NEWLINE# #UNINDENT# #UNINDENT# if ( not request . session . session_key ) : #NEWLINE# #INDENT# request . session . save ( ) #NEWLINE# #UNINDENT# session_key = request . session . session_key #NEWLINE# for prefix in self . prefixes : #NEWLINE# #INDENT# if request . path . startswith ( prefix ) : #NEWLINE# #INDENT# log . debug ( ( 'Not tracking request to: %s' % request . path ) ) #NEWLINE# return #NEWLINE# #UNINDENT# #UNINDENT# now = timezone . localtime ( timezone . now ( ) ) #NEWLINE# attrs = { #NEWLINE# #INDENT# 'session_key' : session_key , #NEWLINE# 'ip_address' : ip_address , #NEWLINE# #UNINDENT# } #NEWLINE# try : #NEWLINE# #INDENT# visitor = Visitor . objects . get ( ** attrs ) #NEWLINE# #UNINDENT# except Visitor . DoesNotExist : #NEWLINE# #INDENT# cutoff = ( now - timedelta ( minutes = 5 ) ) #NEWLINE# visitors = Visitor . objects . filter ( ip_address = ip_address , user_agent = user_agent , last_update__gte = cutoff ) #NEWLINE# if len ( visitors ) : #NEWLINE# #INDENT# visitor = visitors [ 0 ] #NEWLINE# visitor . session_key = session_key #NEWLINE# log . debug ( ( 'Using existing visitor for IP %s / UA %s: %s' % ( ip_address , user_agent , visitor . id ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( visitor , created ) = Visitor . objects . get_or_create ( ** attrs ) #NEWLINE# if created : #NEWLINE# #INDENT# log . debug ( ( 'Created a new visitor: %s' % attrs ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# except : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# user = request . user #NEWLINE# if isinstance ( user , AnonymousUser ) : #NEWLINE# #INDENT# user = None #NEWLINE# #UNINDENT# visitor . user = user #NEWLINE# visitor . user_agent = user_agent #NEWLINE# one_hour_ago = ( now - timedelta ( hours = 1 ) ) #NEWLINE# if ( ( not visitor . last_update ) or ( visitor . last_update <= one_hour_ago ) ) : #NEWLINE# #INDENT# visitor . referrer = utils . u_clean ( request . META . get ( 'HTTP_REFERER' , 'unknown' ) [ : 255 ] ) #NEWLINE# visitor . page_views = 0 #NEWLINE# visitor . session_start = now #NEWLINE# #UNINDENT# visitor . url = request . path #NEWLINE# visitor . page_views += 1 #NEWLINE# visitor . last_update = now #NEWLINE# try : #NEWLINE# #INDENT# sid = transaction . savepoint ( ) #NEWLINE# visitor . save ( ) #NEWLINE# transaction . savepoint_commit ( sid ) #NEWLINE# #UNINDENT# except IntegrityError : #NEWLINE# #INDENT# transaction . savepoint_rollback ( sid ) #NEWLINE# #UNINDENT# except DatabaseError : #NEWLINE# #INDENT# log . error ( ( 'There was a problem saving visitor information:\n%s\n\n%s' % ( traceback . format_exc ( ) , locals ( ) ) ) )