from atlas.models import Case

def get_cases_available_to_user(user, collection=None):
    """Returns a queryset of cases available to the user"""
    queryset = Case.objects.filter(author=user) | Case.objects.filter(open_access=True)

    if collection is not None:
        queryset = queryset | collection.cases.all()
    return queryset.order_by("pk").distinct()
    if user.is_superuser:
        return Case.objects.all()
    else:
        return Case.objects.filter(author=user) | Case.objects.filter(open_access=True)