Skip to content

Commit

Permalink
Merge pull request #329 from techstartucalgary/328-deploy-server
Browse files Browse the repository at this point in the history
remove mongodb interaction since the cluster is non-existent now
  • Loading branch information
tim-macphail authored Sep 30, 2024
2 parents f8d8808 + 293f511 commit cf45e8a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
52 changes: 25 additions & 27 deletions server/src/handlers/db_handler.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
"""Handler for the database"""
import json
# """Handler for the database"""
# import json

from dotenv.main import load_dotenv
from pymongo import MongoClient
# from dotenv.main import load_dotenv
# from pymongo import MongoClient

# Getting the connection string
load_dotenv()
# # Getting the connection string
# load_dotenv()
# pylint ignore next line naming and line length
# pylint: disable=C0103, C0301
connection_string = "mongodb+srv://lifelineteamdb:mypassword1@cluster0.ib2qbgg.mongodb.net/?retryWrites=true&w=majority"

# Defining a MongoDB client instance
client = MongoClient(connection_string)
# # Defining a MongoDB client instance
# client = MongoClient(connection_string)

# DB name
db = client["database1"]
# # DB name
# db = client["database1"]

# Referencing the collection
courses = db["courses"]
# # Referencing the collection
# courses = db["courses"]


def insert_course_into_db(sha: str, course_str: str):
"""Inserts a course into the database"""
course = {"sha": sha, "course_str": course_str}
result = courses.insert_one(course)
print("Course inserted with ID:", result.inserted_id)
# def insert_course_into_db(sha: str, course_str: str):
# """Inserts a course into the database"""
# course = {"sha": sha, "course_str": course_str}
# result = courses.insert_one(course)
# print("Course inserted with ID:", result.inserted_id)


def query_course_from_db(sha: str):
"""Queries the database for a course with the given sha"""
course = courses.find_one({"sha": sha})
if course:
print("Found a cached course with ID:", course["_id"])
return json.loads(course["course_str"])
print("Didn't find a cached course")
return None
# def query_course_from_db(sha: str):
# """Queries the database for a course with the given sha"""
# course = courses.find_one({"sha": sha})
# if course:
# print("Found a cached course with ID:", course["_id"])
# return json.loads(course["course_str"])
# print("Didn't find a cached course")
# return None
24 changes: 12 additions & 12 deletions server/src/handlers/file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@
from pdfminer.pdfparser import PDFSyntaxError

from .openai_api_handler import get_assessments_from_text
from .db_handler import insert_course_into_db, query_course_from_db
# from .db_handler import insert_course_into_db, query_course_from_db

def handle_file(file: UploadFile, response: Response, premium: bool = False):
"""Handles one file"""
try:
tmp_path = save_temp_file(file)
print(f"Processing file: {tmp_path}")

print("First checking if a cached version of the course is present")
file_hash = get_file_hash(tmp_path)
print(f"The file hash is: {file_hash}")
# print("First checking if a cached version of the course is present")
# file_hash = get_file_hash(tmp_path)
# print(f"The file hash is: {file_hash}")

print("Querying the DB")
cached_course = query_course_from_db(file_hash)
if cached_course is not None:
print("Found cached course for this file hash. Returning it.")
return cached_course
# print("Querying the DB")
# cached_course = query_course_from_db(file_hash)
# if cached_course is not None:
# print("Found cached course for this file hash. Returning it.")
# return cached_course

print("Couldn't find a cached version. Processing the file as usual.")
# print("Couldn't find a cached version. Processing the file as usual.")
# Processing only if we don't have a cached version available
with pdfplumber.open(tmp_path) as pdf:
course = get_course_info(pdf)
Expand All @@ -54,8 +54,8 @@ def handle_file(file: UploadFile, response: Response, premium: bool = False):
if tmp_path:
tmp_path.unlink()

print("Caching the course for future use")
insert_course_into_db(file_hash, json.dumps(course))
# print("Caching the course for future use")
# insert_course_into_db(file_hash, json.dumps(course))
return course


Expand Down

0 comments on commit cf45e8a

Please sign in to comment.