def group_permissions(permissions):
    """Return the given object permissions grouped by the model name and permission level."""
    grouped_permissions = {"connection (level 1)": [], "connection (level 2)": [],
                           "dataset (level 1)": [], "dataset (level 2)": [],
                           "job (level 1)": [], "job (level 2)": []}

    for permission in permissions:
        group = f"{permission.get_object()._meta.model_name} (level {2 if permission.can_update else 1})"
        grouped_permissions[group].append(permission)

    # Order the permissions for each model.
    for _group_name, group_permissions in grouped_permissions.items():
        group_permissions.sort(key=lambda x: str(x))

    # Remove potentially empty permission lists.
    return {k: v for k, v in grouped_permissions.items() if v}