def can_view_group(group, user, level="member"):
    """
    See if the user is allowed to view the group, or if they own it

    Params:
    =======
    group: EAPGroup instance, as obtained from EAPGroup.objects.get(pk)
    user: EAPUser instance, as returned from request.user
    level: str, either "member" or "owner", to select level of permission to view

    Returns:
    =======
    True if user is member / owner of the group.
    False otherwise.
    """
    if level not in ["owner", "member"]:
        raise RuntimeError("'level' parameter should be 'owner' or 'member'")
    if not hasattr(user, "all_groups"):
        # probably AnonymousUser
        return False
    if level == "owner" and group.owner and group.owner == user:
        return True
    elif level == "member" and group in user.all_groups.get_queryset():
        return True
    return False