def group_member_save(context, group_dict, member_table_name):
    model = context['model']
    session = context['session']
    group = context['group']
    entity_list = group_dict.get(member_table_name, None)
    if (entity_list is None):
        if context.get('allow_partial_update', False):
            return {'added': [], 'removed': []}
        else:
            entity_list = []
    entities = {}
    Member = model.Member
    classname = member_table_name[:(- 1)].capitalize()
    if (classname == 'Organization'):
        classname = 'Group'
    ModelClass = getattr(model, classname)
    for entity_dict in entity_list:
        name_or_id = (entity_dict.get('id') or entity_dict.get('name'))
        obj = ModelClass.get(name_or_id)
        if (obj and (obj not in entities.values())):
            entities[(obj.id, entity_dict.get('capacity', 'public'))] = obj
    members = session.query(Member).filter_by(table_name=member_table_name[:(- 1)], group_id=group.id).all()
    processed = {'added': [], 'removed': []}
    entity_member = dict((((member.table_id, member.capacity), member) for member in members))
    for entity_id in (set(entity_member.keys()) - set(entities.keys())):
        if (entity_member[entity_id].state != 'deleted'):
            processed['removed'].append(entity_id[0])
        entity_member[entity_id].state = 'deleted'
        session.add(entity_member[entity_id])
    for entity_id in (set(entity_member.keys()) & set(entities.keys())):
        if (entity_member[entity_id].state != 'active'):
            processed['added'].append(entity_id[0])
        entity_member[entity_id].state = 'active'
        session.add(entity_member[entity_id])
    for entity_id in (set(entities.keys()) - set(entity_member.keys())):
        member = Member(group=group, group_id=group.id, table_id=entity_id[0], table_name=member_table_name[:(- 1)], capacity=entity_id[1])
        processed['added'].append(entity_id[0])
        session.add(member)
    return processed
def group_member_save(context, group_dict, member_table_name):
    model = context['model']
    session = context['session']
    group = context['group']
    entity_list = group_dict.get(member_table_name, None)
    if (entity_list is None):
        if context.get('allow_partial_update', False):
            return {'added': [], 'removed': []}
        else:
            entity_list = []
    entities = {}
    Member = model.Member
    classname = member_table_name[:(- 1)].capitalize()
    if (classname == 'Organization'):
        classname = 'Group'
    ModelClass = getattr(model, classname)
    for entity_dict in entity_list:
        name_or_id = (entity_dict.get('id') or entity_dict.get('name'))
        obj = ModelClass.get(name_or_id)
        if (obj and (obj not in entities.values())):
            entities[(obj.id, entity_dict.get('capacity', 'public'))] = obj
    members = session.query(Member).filter_by(table_name=member_table_name[:(- 1)], group_id=group.id).all()
    processed = {'added': [], 'removed': []}
    entity_member = dict((((member.table_id, member.capacity), member) for member in members))
    for entity_id in (set(entity_member.keys()) - set(entities.keys())):
        if (entity_member[entity_id].state != 'deleted'):
            processed['removed'].append(entity_id[0])
        entity_member[entity_id].state = 'deleted'
        session.add(entity_member[entity_id])
    for entity_id in (set(entity_member.keys()) & set(entities.keys())):
        if (entity_member[entity_id].state != 'active'):
            processed['added'].append(entity_id[0])
        entity_member[entity_id].state = 'active'
        session.add(entity_member[entity_id])
    for entity_id in (set(entities.keys()) - set(entity_member.keys())):
        member = Member(group=group, group_id=group.id, table_id=entity_id[0], table_name=member_table_name[:(- 1)], capacity=entity_id[1])
        processed['added'].append(entity_id[0])
        session.add(member)
    return processed