async def writeups_search(request: HTTPConnection):
    s_query = request.query_params.get("search", "")

    # sorry about this
    query = pg_search(sa.select([Writeup.join(User)]), s_query, sort=True)
    query = query.column(
        sa.func.ts_headline(
            search_manager.options["regconfig"],
            Writeup.content,
            sa.func.tsq_parse(search_manager.options["regconfig"], s_query),
            f"StartSel=**,StopSel=**,MaxWords=70,MinWords=30,MaxFragments=3",
        ).label("headline"))

    writeups = await query.as_scalar().gino.all()

    def build_writeup(r):
        """we get back a RowProxy so manually construct the writeup from it."""

        author = User(discord_id=r.discord_id,
                      username=r.username,
                      email=r.email)

        writeup = Writeup(
            id=r.id,
            author_id=r.author_id,
            title=r.title,
            slug=r.slug,
            tags=r.tags,
            content=r.content,
            creation_date=r.creation_date,
            edit_date=r.edit_date,
        )

        writeup.author = author
        return writeup

    writeups = [(build_writeup(r), r.headline) for r in writeups]

    rendered = [(w,
                 shorten(plaintext_markdown(headline),
                         width=300,
                         placeholder="...")) for (w, headline) in writeups]

    return templates.TemplateResponse(
        "writeups/index.j2",
        {
            "request": request,
            "writeups": rendered,
            "query": s_query
        },
    )
Exemple #2
0
async def blog_search(request: HTTPConnection):
    s_query = request.query_params.get("search", "")

    # sorry about this
    query = pg_search(sa.select([Blog.join(User)]), s_query, sort=True)
    query = query.column(
        sa.func.ts_headline(
            search_manager.options["regconfig"],
            Blog.content,
            sa.func.tsq_parse(search_manager.options["regconfig"], s_query),
            f"StartSel=**,StopSel=**,MaxWords=70,MinWords=30,MaxFragments=3",
        ).label("headline"))

    blog = await query.as_scalar().gino.all()

    def build_blog(r):
        """we get back a RowProxy so manually construct the blog from it."""

        author = User(discord_id=r.discord_id,
                      username=r.username,
                      email=r.email)

        blog = Blog(
            id=r.id,
            author_id=r.author_id,
            title=r.title,
            slug=r.slug,
            tags=r.tags,
            content=r.content,
            creation_date=r.creation_date,
            edit_date=r.edit_date,
        )

        return blog

    blog = [(build_blog(r), r.headline) for r in blog]

    rendered = [(w, length_constrained_plaintext_markdown(headline))
                for (w, headline) in blog]

    return templates.TemplateResponse("blog/index.j2", {
        "request": request,
        "blog": rendered,
        "query": s_query
    })
Exemple #3
0
async def blog_search(request: HTTPConnection):
    s_query = request.query_params.get("search", "")

    # sorry about this
    query = pg_search(sa.select([Blog]), s_query, sort=True)
    query = query.column(
        sa.func.ts_headline(
            search_manager.options["regconfig"],
            Blog.content,
            sa.func.tsq_parse(search_manager.options["regconfig"], s_query),
            f"StartSel=**,StopSel=**,MaxWords=70,MinWords=30,MaxFragments=3",
        ).label("headline")
    )

    blog = await query.as_scalar().gino.all()

    def build_blog(r):
        """we get back a RowProxy so manually construct the blog from it."""

        blog = Blog(
            id=r.id,
            title=r.title,
            slug=r.slug,
            tags=r.tags,
            content=r.content,
            creation_date=r.creation_date,
            edit_date=r.edit_date,
        )

        return blog

    blog = [(build_blog(r), r.headline) for r in blog]

    rendered = [
        (w, shorten(plaintext_markdown(headline), width=800, placeholder="..."))
        for (w, headline) in blog
    ]

    grouped_blogs = await blogs_grouped()

    return templates.TemplateResponse(
        "blog/index.j2", {"request": request, "blog": rendered, "query": s_query, "grouped_blogs": grouped_blogs}
    )