def package_tag_list_save(tag_dicts, package, context):
    allow_partial_update = context.get('allow_partial_update', False)
    if ((tag_dicts is None) and allow_partial_update):
        return
    model = context['model']
    session = context['session']
    tag_package_tag = dict(((package_tag.tag, package_tag) for package_tag in package.package_tag_all))
    tag_package_tag_inactive = {tag: pt for (tag, pt) in tag_package_tag.items() if (pt.state in ['deleted'])}
    tag_name_vocab = set()
    tags = set()
    for tag_dict in (tag_dicts or []):
        if ((tag_dict.get('name'), tag_dict.get('vocabulary_id')) not in tag_name_vocab):
            tag_obj = d.table_dict_save(tag_dict, model.Tag, context)
            tags.add(tag_obj)
            tag_name_vocab.add((tag_obj.name, tag_obj.vocabulary_id))
    for tag in (set(tag_package_tag.keys()) - tags):
        package_tag = tag_package_tag[tag]
        package_tag.state = 'deleted'
    for tag in (tags - set(tag_package_tag.keys())):
        state = 'active'
        package_tag_obj = model.PackageTag(package, tag, state)
        session.add(package_tag_obj)
        tag_package_tag[tag] = package_tag_obj
    for tag in tags.intersection(set(tag_package_tag_inactive.keys())):
        state = 'active'
        package_tag = tag_package_tag[tag]
        package_tag.state = state
    package.package_tag_all[:] = tag_package_tag.values()
def package_tag_list_save(tag_dicts, package, context):
    allow_partial_update = context.get('allow_partial_update', False)
    if ((tag_dicts is None) and allow_partial_update):
        return
    model = context['model']
    session = context['session']
    tag_package_tag = dict(((package_tag.tag, package_tag) for package_tag in package.package_tag_all))
    tag_package_tag_inactive = {tag: pt for (tag, pt) in tag_package_tag.items() if (pt.state in ['deleted'])}
    tag_name_vocab = set()
    tags = set()
    for tag_dict in (tag_dicts or []):
        if ((tag_dict.get('name'), tag_dict.get('vocabulary_id')) not in tag_name_vocab):
            tag_obj = d.table_dict_save(tag_dict, model.Tag, context)
            tags.add(tag_obj)
            tag_name_vocab.add((tag_obj.name, tag_obj.vocabulary_id))
    for tag in (set(tag_package_tag.keys()) - tags):
        package_tag = tag_package_tag[tag]
        package_tag.state = 'deleted'
    for tag in (tags - set(tag_package_tag.keys())):
        state = 'active'
        package_tag_obj = model.PackageTag(package, tag, state)
        session.add(package_tag_obj)
        tag_package_tag[tag] = package_tag_obj
    for tag in tags.intersection(set(tag_package_tag_inactive.keys())):
        state = 'active'
        package_tag = tag_package_tag[tag]
        package_tag.state = state
    package.package_tag_all[:] = tag_package_tag.values()