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
def tag_a_file(file_name, tag, new_tag): """Associates a file name with a tag""" if not new_tag: files_with_tag = (File .select() .join(FileTag) .join(Tag) .where(FileTag.tag_id == Tag.get(Tag.tag_name == tag.tag_name))) for f in files_with_tag: if file_name.file_name == f.file_name: return try: ft = FileTag(file_id=file_name, tag_id=tag) ft.save() except IntegrityError: return print "'%s' added to %s tags!" % (tag.tag_name, file_name.file_name)
def delete_entry(kind, entry, from_file=None): """Deletes a file name entry or tag entry from the catalog""" if kind == 'files': to_delete = File.get(File.file_name == entry) to_delete.delete_instance() print "'%s' deleted from catalog!" % entry elif kind == 'tag' and from_file: try: existing_file = File.get(File.file_name == from_file) if existing_file: try: association = FileTag.get(FileTag.file_id == existing_file, FileTag.tag_id == Tag.get(Tag.tag_name == entry)) association.delete_instance() print "Deleted '%s' from '%s'" % (entry, from_file) except: print "'%s' no longer tagged to %s" % (entry, from_file) except: print "'%s' not in catalog" % from_file elif kind == 'tag': tag_item = Tag.get(Tag.tag_name == entry) tag_item.delete_instance() print "'%s' deleted from catalog" % entry else: pass