import sys
from datetime import datetime
from random import randint


def get_client_ip(request):
    x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
    if x_forwarded_for:
        ip = x_forwarded_for.split(",")[-1].strip()
    else:
        ip = request.META.get("REMOTE_ADDR")
    return ip


def create_version_id():
    """Generate a random ID to use to identify a person version"""
    return "{:016x}".format(randint(0, sys.maxsize))


def get_current_timestamp():
    return datetime.utcnow().isoformat()


def get_change_metadata(request, information_source):
    result = {
        "information_source": information_source,
        "version_id": create_version_id(),
        "timestamp": get_current_timestamp(),
    }
    if request is not None:
        result["username"] = request.user.username
    return result

import sys
from datetime import datetime
from random import randint


def get_client_ip(request):
    x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
    if x_forwarded_for:
        ip = x_forwarded_for.split(",")[-1].strip()
    else:
        ip = request.META.get("REMOTE_ADDR")
    return ip


def create_version_id():
    """Generate a random ID to use to identify a person version"""
    return "{:016x}".format(randint(0, sys.maxsize))


def get_current_timestamp():
    return datetime.utcnow().isoformat()


def get_change_metadata(request, information_source, user=None):
    """

    :type user: django.contrib.auth.models.User
    """
    result = {
        "information_source": information_source,
        "version_id": create_version_id(),
        "timestamp": get_current_timestamp(),
    }
    if request is not None:
        result["username"] = request.user.username
    if user:
        result["username"] = user.username
    return result

Allow passing User directly to get_change_metadata
Allow passing User directly to get_change_metadata

  import sys
  from datetime import datetime
  from random import randint
  
  
  def get_client_ip(request):
      x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
      if x_forwarded_for:
          ip = x_forwarded_for.split(",")[-1].strip()
      else:
          ip = request.META.get("REMOTE_ADDR")
      return ip
  
  
  def create_version_id():
      """Generate a random ID to use to identify a person version"""
      return "{:016x}".format(randint(0, sys.maxsize))
  
  
  def get_current_timestamp():
      return datetime.utcnow().isoformat()
  
  
- def get_change_metadata(request, information_source):
+ def get_change_metadata(request, information_source, user=None):
+     """
+ 
+     :type user: django.contrib.auth.models.User
+     """
      result = {
          "information_source": information_source,
          "version_id": create_version_id(),
          "timestamp": get_current_timestamp(),
      }
      if request is not None:
          result["username"] = request.user.username
+     if user:
+         result["username"] = user.username
      return result
  
Allow passing User directly to get_change_metadata
## Code Before:
import sys
from datetime import datetime
from random import randint


def get_client_ip(request):
    x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
    if x_forwarded_for:
        ip = x_forwarded_for.split(",")[-1].strip()
    else:
        ip = request.META.get("REMOTE_ADDR")
    return ip


def create_version_id():
    """Generate a random ID to use to identify a person version"""
    return "{:016x}".format(randint(0, sys.maxsize))


def get_current_timestamp():
    return datetime.utcnow().isoformat()


def get_change_metadata(request, information_source):
    result = {
        "information_source": information_source,
        "version_id": create_version_id(),
        "timestamp": get_current_timestamp(),
    }
    if request is not None:
        result["username"] = request.user.username
    return result

## Instruction:
Allow passing User directly to get_change_metadata
## Code After:
import sys
from datetime import datetime
from random import randint


def get_client_ip(request):
    x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
    if x_forwarded_for:
        ip = x_forwarded_for.split(",")[-1].strip()
    else:
        ip = request.META.get("REMOTE_ADDR")
    return ip


def create_version_id():
    """Generate a random ID to use to identify a person version"""
    return "{:016x}".format(randint(0, sys.maxsize))


def get_current_timestamp():
    return datetime.utcnow().isoformat()


def get_change_metadata(request, information_source, user=None):
    """

    :type user: django.contrib.auth.models.User
    """
    result = {
        "information_source": information_source,
        "version_id": create_version_id(),
        "timestamp": get_current_timestamp(),
    }
    if request is not None:
        result["username"] = request.user.username
    if user:
        result["username"] = user.username
    return result
