def package_membership_list_save(group_dicts, package, context):
    allow_partial_update = context.get('allow_partial_update', False)
    if ((group_dicts is None) and allow_partial_update):
        return
    capacity = 'public'
    model = context['model']
    session = context['session']
    user = context.get('user')
    members = session.query(model.Member).filter((model.Member.table_id == package.id)).filter((model.Member.capacity != 'organization'))
    group_member = dict(((member.group, member) for member in members))
    groups = set()
    for group_dict in (group_dicts or []):
        id = group_dict.get('id')
        name = group_dict.get('name')
        capacity = group_dict.get('capacity', 'public')
        if (capacity == 'organization'):
            continue
        if id:
            group = session.query(model.Group).get(id)
        else:
            group = session.query(model.Group).filter_by(name=name).first()
        if group:
            groups.add(group)
    model.Session.flush()
    for group in (set(group_member.keys()) - groups):
        member_obj = group_member[group]
        if (member_obj and (member_obj.state == 'deleted')):
            continue
        if authz.has_user_permission_for_group_or_org(member_obj.group_id, user, 'read'):
            member_obj.capacity = capacity
            member_obj.state = 'deleted'
            session.add(member_obj)
    for group in groups:
        member_obj = group_member.get(group)
        if (member_obj and (member_obj.state == 'active')):
            continue
        if authz.has_user_permission_for_group_or_org(group.id, user, 'read'):
            member_obj = group_member.get(group)
            if member_obj:
                member_obj.capacity = capacity
                member_obj.state = 'active'
            else:
                member_obj = model.Member(table_id=package.id, table_name='package', group=group, capacity=capacity, group_id=group.id, state='active')
            session.add(member_obj)
def package_membership_list_save(group_dicts, package, context):
    allow_partial_update = context.get('allow_partial_update', False)
    if ((group_dicts is None) and allow_partial_update):
        return
    capacity = 'public'
    model = context['model']
    session = context['session']
    user = context.get('user')
    members = session.query(model.Member).filter((model.Member.table_id == package.id)).filter((model.Member.capacity != 'organization'))
    group_member = dict(((member.group, member) for member in members))
    groups = set()
    for group_dict in (group_dicts or []):
        id = group_dict.get('id')
        name = group_dict.get('name')
        capacity = group_dict.get('capacity', 'public')
        if (capacity == 'organization'):
            continue
        if id:
            group = session.query(model.Group).get(id)
        else:
            group = session.query(model.Group).filter_by(name=name).first()
        if group:
            groups.add(group)
    model.Session.flush()
    for group in (set(group_member.keys()) - groups):
        member_obj = group_member[group]
        if (member_obj and (member_obj.state == 'deleted')):
            continue
        if authz.has_user_permission_for_group_or_org(member_obj.group_id, user, 'read'):
            member_obj.capacity = capacity
            member_obj.state = 'deleted'
            session.add(member_obj)
    for group in groups:
        member_obj = group_member.get(group)
        if (member_obj and (member_obj.state == 'active')):
            continue
        if authz.has_user_permission_for_group_or_org(group.id, user, 'read'):
            member_obj = group_member.get(group)
            if member_obj:
                member_obj.capacity = capacity
                member_obj.state = 'active'
            else:
                member_obj = model.Member(table_id=package.id, table_name='package', group=group, capacity=capacity, group_id=group.id, state='active')
            session.add(member_obj)