コード例 #1
0
ファイル: catalog.py プロジェクト: mosegontar/ScratchTrack
def view_tags(sort_by):
    """Retreives a list of all the tags in the catalog"""

    if sort_by == 'alpha':

        tags_in_use = (Tag
                       .select(Tag)
                       .join(FileTag)
                       .join(File)
                       .group_by(Tag))

        tags_in_use_list = sorted([t.tag_name for t in tags_in_use])

        return tags_in_use_list

    elif sort_by == 'count':

        count = fn.COUNT(FileTag.id)

        tags_with_counts = (Tag
                            .select(Tag, count.alias('entry_count'))
                            .join(FileTag)
                            .join(File)
                            .group_by(Tag)
                            .order_by(count.desc(), Tag.tag_name))

        tags_and_counts = []

        for tag in tags_with_counts:
            tag_name, count = (tag.tag_name, FileTag.select().where(FileTag.tag_id == tag).count())
            tags_and_counts.append(tag_name+' '+'(%d)' % count)

        return tags_and_counts