'Change membership status for a group.
If user is a member of an open group, then the user is removed.
If a user is a member of a reviewed or closed group,
then the membership is in a pending state.'

def remove_member(self, userprofile, status=None, send_email=False):

    try:
        membership = GroupMembership.objects.get(group=self, userprofile=userprofile)
    except GroupMembership.DoesNotExist:
        return
    old_status = membership.status
    if ((not status) or (self.accepting_new_members == Group.OPEN) or (old_status != GroupMembership.MEMBER)):
        membership.delete()
        Invite.objects.filter(group=self, redeemer=userprofile).delete()
        send_email = True
    else:
        membership.status = status
        membership.needs_renewal = False
        membership.save()
        send_email = True
    if (self.name == settings.NDA_GROUP):
        unsubscribe_from_basket_task.delay(userprofile.email, [settings.BASKET_NDA_NEWSLETTER])
    if send_email:
        email_membership_change.delay(self.pk, userprofile.user.pk, old_status, status)
