Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pylint for utils.py #27

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading