Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
62 changes: 58 additions & 4 deletions db_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"""

import sys
import requests

from sqlalchemy import create_engine

Check failure on line 12 in db_init.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

db_init.py:12:24: F401 `sqlalchemy.create_engine` imported but unused
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Echoing the ruff comment, this should be removed since it's not needed/used


from datetime import date, datetime

from labconnect import create_app, db
Expand All @@ -28,12 +32,48 @@
UserCourses,
UserDepartments,
UserMajors,
UserSavedOpportunities,
Codes,
Codes
)


def fetch_json_data(json_url):
response = requests.get(json_url)

if response.status_code != 200:
raise ValueError(f"Error: Received status code {response.status_code}")
try:
return response.json()
except requests.exceptions.JSONDecodeError:
raise ValueError("Error: Received invalid JSON response")


def insert_courses_from_json(session, courses_data):
# Fetch existing courses to avoid multiple queries
existing_courses = {course.code: course for course in session.query(Courses).all()}
new_courses = []

for course, course_info in courses_data.items():
course_name = course_info.get("name")
course_code = course_info.get('subj') + course_info.get('crse')

if len(course_code) != 8:
continue
if course_code in existing_courses:
# Update name if changed
existing_course = existing_courses[course_code]
if existing_course.name != course_name:
existing_course.name = course_name
else:
new_courses.append(Courses(code=course_code, name=course_name))

if new_courses:
session.add_all(new_courses)
session.commit()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for session.commit, move that into the if statement since we only need it in the if statement



app = create_app()


if len(sys.argv) < 2:
sys.exit("No argument or exsisting argument found")

Expand All @@ -42,14 +82,28 @@
if db.inspect(db.engine).get_table_names():
print("Tables already exist.")
# clear the codes table
db.session.query(Codes).delete()
db.session.delete(Codes)
db.session.commit()
sys.exit()
db.create_all()

elif sys.argv[1] == "clear":
with app.app_context():
db.drop_all()

elif sys.argv[1] == "addCourses":
with app.app_context():
db.create_all()

json_url = "https://raw.githubusercontent.com/quacs/quacs-data/master/semester_data/202409/catalog.json"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you update this function to intake the URL as an argument, should check if sys.argv[2] is present (add logic to confirm extra arguments exist). Add logic to confirm that it's a URL.


courses_data = fetch_json_data(json_url)
if not courses_data:
sys.exit("Failed to fetch courses data. Exiting...")

insert_courses_from_json(db.session, courses_data)

db.session.close()

elif sys.argv[1] == "create":
with app.app_context():
Expand Down Expand Up @@ -443,10 +497,10 @@
UserCourses,
UserDepartments,
UserMajors,
UserSavedOpportunities,
]

for table in tables:

stmt = db.select(table)
result = db.session.execute(stmt).scalars()

Expand Down
38 changes: 38 additions & 0 deletions labconnect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import sentry_sdk

# Import logging
from logging.config import dictConfig

# Import Flask modules
from flask import Flask
from flask_cors import CORS
Expand All @@ -27,10 +30,35 @@

def create_app() -> Flask:
# Create flask app object

app = Flask(__name__)

app.config.from_object(os.environ.get("CONFIG", "config.TestingConfig"))

# Logging configuration
dictConfig({
'version': 1,
'formatters': {
'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}
},
'handlers': {
'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}
},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})

app.logger.info("App logger initialized.")

# Sentry
sentry_sdk.init(
dsn=app.config["SENTRY_DSN"],
integrations=[FlaskIntegration()],
Expand All @@ -43,6 +71,8 @@ def create_app() -> Flask:
initialize_extensions(app)
register_blueprints(app)

app.logger.info("Returning App")

return app


Expand All @@ -59,8 +89,11 @@ def initialize_extensions(app) -> None:
jwt.init_app(app)
app.json = OrJSONProvider(app)

app.logger.info("Extensions initialized.")

with app.app_context():
db.create_all()
app.logger.info("Database tables created.")

@app.after_request
def refresh_expiring_jwts(response):
Expand All @@ -74,9 +107,11 @@ def refresh_expiring_jwts(response):
if type(data) is dict:
data["access_token"] = access_token
response.data = json.dumps(data)
app.logger.info("Access token refreshed for user.")
return response
except (RuntimeError, KeyError):
# Case where there is not a valid JWT. Just return the original respone
app.logger.debug("No valid JWT found; skipping refresh.")
return response


Expand All @@ -87,4 +122,7 @@ def register_blueprints(app) -> None:
from labconnect.main import main_blueprint

app.register_blueprint(main_blueprint)
app.logger.info("Main blueprint registered.")

app.register_blueprint(error_blueprint)
app.logger.info("Error blueprint registered.")
Loading