mikeckennedy / markdown-subtemplate

A template engine to render Markdown with external template imports and basic variable replacements. #pypackage
MIT License
63 stars 7 forks source link

Async support #10

Open possnfiffer opened 2 years ago

possnfiffer commented 2 years ago

I want to use this with FastAPI and Beanie

What's your opinion on the best way forward?

Should I work on rewriting the functions in my fork of markdown-subtemplate or would the solution be as simple as using unsync to fix it. I'll re-watch a few of the videos in your async python course and see if I can pull it off either way. If I get something working I'll submit a pull request.

possnfiffer commented 2 years ago

pymongo to the rescue! Just wrote a pymongo_service.py file and passed it the url. It was easily able to do a non async lookup of the page for me and fits the bill for what I'm doing.

import pymongo

from decouple import config

def get_page(url: str):
    username = config("MONGO_USERNAME")
    password = config("MONGO_PASSWORD")
    port = config("MONGO_PORT")
    server = config("MONGO_SERVER")

    conn_str = "mongodb://" + username + ":" + password + "@" + server + ":" + port + "/?tls=true&tlsAllowInvalidCertificates=true"
    client = pymongo.MongoClient(conn_str)
    db_name = "database_name_here"
    page = client.db_name.pages.find_one({"url": url})

    if not page:
        return None

    return page

page comes back as a dictionary not a class so just had to modify my template storage engine to return page.get('contents') rather than page.contents