def case_safe_sf_id(id_15):
    """
    Equivalent to Salesforce CASESAFEID()

    Convert a 15 char case-sensitive Id to 18 char case-insensitive Salesforce Id
    or check the long 18 char ID.

    Long  18 char Id are from SFDC API and from Apex. They are recommended by SF.
    Short 15 char Id are from SFDC formulas if omitted to use func CASESAFEID(),
    from reports or from parsed URLs in HTML.
    The long and short form are interchangable as the input to Salesforce API or
    to django-salesforce. They only need to be someway normalized if they are
    used as dictionary keys in a Python application code.
    """
    if not id_15:
        return None
    if len(id_15) not in (15, 18):
        raise TypeError("The string %r is not a valid Force.com ID")
    suffix = []
    for i in range(0, 15, 5):
        weight = 1
        digit = 0
        for char in id_15[i:i + 5]:
            if char not in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz':
                raise TypeError("The string %r is not a valid Force.com ID")
            if char.isupper():
                digit += weight
            weight *= 2
        suffix.append(chr(ord('A') + digit) if digit < 26 else str(digit - 26))
    out = ''.join(suffix)
    if len(id_15) == 18 and out != id_15[15:]:
        raise TypeError("The string %r is not a valid Force.com ID")
    return id_15[:15] + out