HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/unlimited-leads/Unlimited-Leads-Be/utils/utils.py
def get_first_error(error_dict, default=None):
    for field, messages in error_dict.items():
        key, value = field, messages
        while isinstance(value, dict):
            key, value = list(value.items())[0]

        field = key.replace("_", " ").title()
        field_value = value
        if isinstance(value, list):
            field_value = value[0] if value else "has invalid value"

        if key != "non_field_errors":
            return f"{field}: {field_value}"
        else:
            return field_value

    return default


def format_count_to_short(num):
    if num < 1:
        return "0"
    elif num < 1000:
        return f"{num}+"
    elif num < 1_000_000:
        return f"{round(num / 1000)}k+"
    else:
        return f"{str(round(num / 1_000_000))[:4]}M+"