def get_user_info(self, request): user_info = { 'ip_address': get_client_ip(request.META), } user = getattr(request, 'user', None) if user is None: return user_info try: if hasattr(user, 'is_authenticated'): # is_authenticated was a method in Django < 1.10 if callable(user.is_authenticated): authenticated = user.is_authenticated() else: authenticated = user.is_authenticated if not authenticated: return user_info user_info['id'] = user.pk if hasattr(user, 'email'): user_info['email'] = user.email if hasattr(user, 'get_username'): user_info['username'] = user.get_username() elif hasattr(user, 'username'): user_info['username'] = user.username except Exception: # We expect that user objects can be somewhat broken at times # and try to just handle as much as possible and ignore errors # as good as possible here. pass return user_info
def get_user_info(self, request): user_info = { 'ip_address': get_client_ip(request.META), } user = getattr(request, 'user', None) if user is None: return user_info try: authenticated = is_authenticated(user) if not authenticated: return user_info user_info['id'] = user.pk if hasattr(user, 'email'): user_info['email'] = user.email if hasattr(user, 'get_username'): user_info['username'] = user.get_username() elif hasattr(user, 'username'): user_info['username'] = user.username except Exception: # We expect that user objects can be somewhat broken at times # and try to just handle as much as possible and ignore errors # as good as possible here. pass return user_info
def test_xff(self): result = get_client_ip({'HTTP_X_FORWARDED_FOR': '1.1.1.1, 127.0.0.1'}) self.assertEquals(result, '1.1.1.1')
def test_has_remote_addr(self): result = get_client_ip({'REMOTE_ADDR': '127.0.0.1'}) self.assertEquals(result, '127.0.0.1')