comments and README.md

This commit is contained in:
2023-09-28 17:08:50 +02:00
parent 68311135bf
commit 945b9c2195
6 changed files with 298 additions and 58 deletions

View File

@@ -1,10 +1,15 @@
from flask import g
from werkzeug.local import LocalProxy
from pymongo import MongoClient
from .config import settings
def connect_db():
"""
Creates the connection to the MongoDB using the config file data.
Thos function is used by flask application as well as the scraper script
:return: connection to the collection in the mongodb
"""
client = MongoClient(settings["mongodb_uri"])
db = client[settings["mongodb_db"]]
collection = db[settings["mongodb_collection"]]
@@ -12,12 +17,17 @@ def connect_db():
def disconnect_db(conn):
"""
Disconnects open connection
:param conn: open db connection
"""
conn.database.client.close()
def get_db():
"""
Configuration method to return db instance
Get collection instance for flask application. If no instance stored in global variables, then create connection
and store it in g
"""
collection = getattr(g, "_database", None)
@@ -25,7 +35,3 @@ def get_db():
collection = g._database = connect_db()
return collection
# Use LocalProxy to read the global db instance with just `db`
db = LocalProxy(get_db)