methods.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:mist.api 作者: mistio 项目源码 文件源码
def remove_tags_from_resource(owner, resource_obj, tags, *args, **kwargs):
    """
    This function get a list of tags in the form [{'key': 'joe'}] or
    [{'key': 'joe', 'value': 'schmoe'}] and will delete them from the resource
    :param owner: the resource owner
    :param resource_obj: the resource object where the tags will be added
    :param rtype: resource type
    :param tags: list of tags to be deleted
    """
    # ensure there are no duplicate tag keys because mongoengine will
    # raise exception for duplicates in query
    key_list = list(set(tags))

    # create a query that will return all the tags with
    query = reduce(lambda q1, q2: q1.__or__(q2),
                   map(lambda key: Q(key=key), key_list))

    Tag.objects(Q(owner=owner) & Q(resource=resource_obj) & (query)).delete()

    # I think that the above overly complex query could simply be rewritten as
    # Tag.objects(owner=owner, resource=resource_obj,
    #             key__in=key_list).delete()

    # SEC
    owner.mapper.update(resource_obj)

    rtype = resource_obj._meta["collection"]

    trigger_session_update(owner,
                           [rtype + 's' if not rtype.endswith('s') else rtype])
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号