Skip to content

Commit

Permalink
pylint for utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SPGundewar committed Oct 31, 2024
1 parent 734238a commit 978aa6c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
44 changes: 36 additions & 8 deletions utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 978aa6c

Please sign in to comment.