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

fix semester deletion #886

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
35 changes: 17 additions & 18 deletions src/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
import pandas as pd
from constants import Constants

"""
NOTE: on caching
on add of semester of change of data from GET
do a cache.clear() to ensure data integrity
"""
# NOTE: on caching
# on add of semester of change of data from GET
# do a cache.clear() to ensure data integrity

app = FastAPI()
app.add_middleware(SessionMiddleware,
Expand All @@ -46,7 +44,7 @@
date_range_map = DateMapping.semester_date_mapping(db_conn)
admin_info = AdminInfo.Admin(db_conn)
course_select = CourseSelect.student_course_selection(db_conn)
semester_info = SemesterInfo.semester_info(db_conn)
semester_info = SemesterInfo.semester_info(db_conn, FastAPICache)
professor_info = All_professors.Professor(db_conn, FastAPICache)
users = UserModel.User()

Expand Down Expand Up @@ -143,9 +141,8 @@ def set_defaultSemester(semester_set: DefaultSemesterSetPydantic):
success, error = admin_info.set_semester_default(semester_set.default)
if success:
return Response(status_code=200)
else:
print(error)
return Response(error.__str__(), status_code=500)
print(error)
return Response(error.__str__(), status_code=500)

#Parses the data from the .csv data files
@app.post('/api/bulkCourseUpload')
Expand Down Expand Up @@ -176,9 +173,8 @@ async def uploadHandler(
isSuccess, error = courses.populate_from_csv(csv_file)
if (isSuccess):
return Response(status_code=200)
else:
print(error)
return Response(error.__str__(), status_code=500)
print(error)
return Response(error.__str__(), status_code=500)

@app.post('/api/bulkProfessorUpload')
async def uploadJSON(
Expand Down Expand Up @@ -208,11 +204,15 @@ async def uploadJSON(
if isSuccess:
print("SUCCESS")
return Response(status_code=200)
else:
print("NOT WORKING")
print(error)
return Response(error.__str__(), status_code=500)
print("NOT WORKING")
print(error)
return Response(error.__str__(), status_code=500)

@app.delete('/api/semester/{semester_id}')
async def remove_semester(semester_id: str):
print(semester_id)
semester, error = semester_info.delete_semester(semester=semester_id)
return Response(status_code=200) if not error else Response(str(error), status_code=500)

@app.post('/api/mapDateRangeToSemesterPart')
async def map_date_range_to_semester_part_handler(request: Request):
Expand All @@ -234,8 +234,7 @@ async def map_date_range_to_semester_part_handler(request: Request):
semester_info.upsert(semester_title, is_publicly_visible)
if (not error):
return Response(status_code=200)
else:
return Response(error, status_code=500)
return Response(error, status_code=500)
return Response("Did not receive proper form data", status_code=500)

@app.get('/api/user/course')
Expand Down
8 changes: 3 additions & 5 deletions src/api/db/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def __init__(self, db_conn):
self.interface_name = 'admin_info'

def get_semester_default(self):
# NOTE: COALESCE takes first non-null vaue from the list
# NOTE: COALESCE takes first non-null value from the list
result, error = self.db_conn.execute("""
SELECT admin.semester FROM admin_settings admin
UNION ALL
Expand All @@ -21,8 +21,7 @@ def get_semester_default(self):

if error:
return (None, error)
else:
return (default_semester, error)
return (default_semester, error)

def set_semester_default(self, semester):
try:
Expand All @@ -40,5 +39,4 @@ def set_semester_default(self, semester):

if response != None:
return(True, None)
else:
return (False, error)
return (False, error)
29 changes: 27 additions & 2 deletions src/api/db/semester_info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
class semester_info:
import asyncio

def __init__(self, db_wrapper):
class semester_info:
def __init__(self, db_wrapper, cache):
self.db = db_wrapper
self.cache = cache

def clear_cache(self):
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None

if loop and loop.is_running():
loop.create_task(self.cache.clear(namespace="API_CACHE"))
else:
asyncio.run(self.cache.clear("API_CACHE"))

def upsert(self, semester, isPublic):
self.db.execute("""
Expand All @@ -28,3 +41,15 @@ def is_public(self, semester):
if data is not None and len(data) > 0:
return data[0]['public']
return False

def delete_semester(self, semester):
# clear cache so this semester does not come up again
self.clear_cache()
return self.db.execute("""
BEGIN TRANSACTION;
DELETE FROM semester_info
WHERE semester=%(Semester)s;
COMMIT;
""", {
"Semester": semester
}, isSELECT=False)
8 changes: 6 additions & 2 deletions src/api/tables/course.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sqlalchemy import Column
from sqlalchemy import Column, ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy.dialects.postgresql import TEXT, INTEGER, VARCHAR, DATE, TSVECTOR

from .database import Base
Expand All @@ -8,7 +9,10 @@ class Course(Base):

crn = Column(VARCHAR(length=255), primary_key=True)
section = Column(VARCHAR(length=255))
semester = Column(VARCHAR(length=255))

semester = Column(VARCHAR(length=255), ForeignKey("semester_info.semester", ondelete="CASCADE"))
professor = Column(VARCHAR(length=255))

min_credits = Column(INTEGER)
max_credits = Column(INTEGER)
date_start = Column(DATE)
Expand Down
3 changes: 3 additions & 0 deletions src/api/tables/semester_info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sqlalchemy import Column, PrimaryKeyConstraint
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import VARCHAR, BOOLEAN

from .database import Base
Expand All @@ -8,3 +9,5 @@ class SemesterInfo(Base):

semester = Column(VARCHAR(length=255), primary_key=True)
public = Column(BOOLEAN)

courses = relationship("Course", backref="semester_info", passive_deletes=True)