Beispiel #1
0
def fetch_and_create_tags(tag_names):
    # copy the tag_names list
    new_tag_names = tag_names[:]

    # find tag names that already exist (case insensitive match)
    # and remove those names from our list
    lower_case_tags = [t.lower() for t in new_tag_names]
    existing_tags = DBSession.query(Tag).\
        filter(
            func.lower(Tag.name).in_(lower_case_tags)
        ).all()
    for t in existing_tags:
        for n in new_tag_names[:]:
            if n.lower() == t.name.lower():
                new_tag_names.remove(n)
                break

    # create the tags that don't yet exist
    if new_tag_names:
        new_tags = [{'name': n, 'slug': slugify(n)} for n in new_tag_names]
        DBSession.connection().execute(tags.insert(), new_tags)
        DBSession.flush()
        existing_tags += DBSession.query(Tag)\
            .filter(
                Tag.slug.in_([t['slug'] for t in new_tags])
            ).all()

    return existing_tags