diff --git a/app/routes.py b/app/routes.py index 1492269..0a46fcb 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,7 +4,7 @@ from flask_paginate import Pagination, get_page_args from app import app, db from app.models import Reviews -from utils import get_DB +from utils import get_db import time jobsDB = None @@ -19,7 +19,7 @@ def setTest(test): def intializeDB(): global jobsDB, db, usersDb, forumDB - db = get_DB(isTest) + db = get_db(isTest) usersDb = db.Users jobsDB = db.Jobs forumDB = db.Forum diff --git a/utils.py b/utils.py index e808768..81a23e1 100644 --- a/utils.py +++ b/utils.py @@ -1,15 +1,43 @@ +""" +utils.py + +This module provides utility functions for interacting with a MongoDB database. + +Functions: +- get_db(is_test=False): Connects to the MongoDB database and returns the appropriate + database instance based on the test flag. + +Usage: + Call `get_db()` to retrieve the main project database, or `get_db(is_test=True)` to retrieve the + test database. The MongoDB password should be stored in a configuration file named `config.ini` + located in the same directory as this script. + +Notes: + Ensure that the `config.ini` file contains the correct password for the MongoDB connection. +""" + import os import sys from pymongo import MongoClient -def get_DB(isTest=False): - with open(os.path.join(sys.path[0], "config.ini"), "r") as f: +def get_db(is_test=False): + """Connect to the MongoDB database and return the appropriate database based on the test flag. + + Args: + is_test (bool): If True, return the test database. Defaults to False. + + Returns: + Database: The MongoDB database instance. + """ + with open(os.path.join(sys.path[0], "config.ini"), "r", encoding="utf-8") as f: content = f.readlines() - client = MongoClient("mongodb+srv://anishd1910:" + - content[0]+"@cluster0.oagwk.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0") - if (isTest): - return client.SETestProj - else: - return client.SEProj2 + mongo_uri = ( + "mongodb+srv://anishd1910:" + f"{content[0].strip()}@cluster0.oagwk.mongodb.net/" + "?retryWrites=true&w=majority&appName=Cluster0" + ) + client = MongoClient(mongo_uri) + + return client.SETestProj if is_test else client.SEProj2