from peewee import fn from db import db from db import Video, User, Tag, VideoTag from video import serialize_video from tools import parse_tags, parse_tag, parse_offset async def search_videos(tags, offset=0): tags = parse_tags(tags) videos = await db.execute(Video .select(User, Video) .join(User) .switch(Video) .join(VideoTag) .join(Tag) .where( (Tag.tag << tags) & (~Video.is_hidden) ) .group_by(User, Video) .having(fn.Count(Tag.id.distinct()) == len(tags)) .order_by(Video.upload_date.desc()) .offset(parse_offset(offset)) .limit(6)) return [await serialize_video(video) for video in videos] async def suggest_tag(tag): tags = await db.execute(Tag .select() .where(Tag.tag.startswith(parse_tag(tag)))) return [tag.tag for tag in tags]