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

Tortoise conversion coursecorequisite #569

Merged
merged 14 commits into from
Apr 11, 2022
10 changes: 0 additions & 10 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ jobs:
run: |
bash src/api/tests/test.sh

- name: Run API unit tests
env:
DB_PORT: 5432
DB_USER: postgres
DB_PASS: postgres
DB_NAME: yacs
DB_HOST: postgres
run: |
bash src/api/tests/test.sh

lighthouse:
name: Lighthouse CI
runs-on: ubuntu-latest
Expand Down
90 changes: 45 additions & 45 deletions src/api/db/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def getDays(self, daySequenceStr):
return set(filter(
lambda day: day, re.split("(?:(M|T|W|R|F))", daySequenceStr)))

def delete_by_semester(self, semester):
async def delete_by_semester(self, semester):
# clear cache so this semester does not come up again
self.clear_cache()
return self.db.execute("""
return await self.db.execute("""
BEGIN TRANSACTION;
DELETE FROM course
WHERE semester=%(Semester)s;
WHERE semester='%(Semester)s';
DELETE FROM course_session
WHERE semester=%(Semester)s;
WHERE semester='%(Semester)s';
COMMIT;
""", {
"Semester": semester
Expand All @@ -59,8 +59,8 @@ def bulk_delete(self, semesters):
self.clear_cache()
return None

def populate_from_csv(self, csv_text):
conn = self.db.get_connection()
async def populate_from_csv(self, csv_text):
conn = await self.db.get_connection()
reader = csv.DictReader(csv_text)
# for each course entry insert sections and course sessions
with conn.cursor(cursor_factory=RealDictCursor) as transaction:
Expand All @@ -84,15 +84,15 @@ def populate_from_csv(self, csv_text):
instructor
)
VALUES (
NULLIF(%(CRN)s, ''),
NULLIF(%(Section)s, ''),
NULLIF(%(Semester)s, ''),
%(StartTime)s,
%(EndTime)s,
%(WeekDay)s,
NULLIF(%(Location)s, ''),
NULLIF(%(SessionType)s, ''),
NULLIF(%(Instructor)s, '')
NULLIF('%(CRN)s', ''),
NULLIF('%(Section)s', ''),
NULLIF('%(Semester)s', ''),
'%(StartTime)s',
'%(EndTime)s',
'%(WeekDay)s',
NULLIF('%(Location)s', ''),
NULLIF('%(SessionType)s', ''),
NULLIF('%(Instructor)s', '')
)
ON CONFLICT DO NOTHING;
""",
Expand Down Expand Up @@ -134,30 +134,30 @@ def populate_from_csv(self, csv_text):
tsv
)
VALUES (
NULLIF(%(CRN)s, ''),
NULLIF(%(Section)s, ''),
NULLIF(%(Semester)s, ''),
%(MinCredits)s,
%(MaxCredits)s,
NULLIF(%(Description)s, ''),
NULLIF(%(Frequency)s, ''),
NULLIF(%(FullTitle)s, ''),
%(StartDate)s,
%(EndDate)s,
NULLIF(%(Department)s, ''),
%(Level)s,
NULLIF(%(Title)s, ''),
NULLIF(%(RawPrecoreqText)s, ''),
%(School)s,
%(SeatsOpen)s,
%(SeatsFilled)s,
%(SeatsTotal)s,
setweight(to_tsvector(coalesce(%(FullTitle)s, '')), 'A') ||
setweight(to_tsvector(coalesce(%(Title)s, '')), 'A') ||
setweight(to_tsvector(coalesce(%(Department)s, '')), 'A') ||
setweight(to_tsvector(coalesce(%(CRN)s, '')), 'A') ||
setweight(to_tsvector(coalesce(%(Level)s, '')), 'B') ||
setweight(to_tsvector(coalesce(%(Description)s, '')), 'D')
NULLIF('%(CRN)s', ''),
NULLIF('%(Section)s', ''),
NULLIF('%(Semester)s', ''),
'%(MinCredits)s',
'%(MaxCredits)s',
NULLIF('%(Description)s', ''),
NULLIF('%(Frequency)s', ''),
NULLIF('%(FullTitle)s', ''),
'%(StartDate)s',
'%(EndDate)s',
NULLIF('%(Department)s', ''),
'%(Level)s',
NULLIF('%(Title)s', ''),
NULLIF('%(RawPrecoreqText)s', ''),
'%(School)s',
'%(SeatsOpen)s',
'%(SeatsFilled)s',
'%(SeatsTotal)s',
setweight(to_tsvector(coalesce('%(FullTitle)s', '')), 'A') ||
setweight(to_tsvector(coalesce('%(Title)s', '')), 'A') ||
setweight(to_tsvector(coalesce('%(Department)s', '')), 'A') ||
setweight(to_tsvector(coalesce('%(CRN)s', '')), 'A') ||
setweight(to_tsvector(coalesce('%(Level)s', '')), 'B') ||
setweight(to_tsvector(coalesce('%(Description)s', '')), 'D')
)
ON CONFLICT DO NOTHING;
""",
Expand Down Expand Up @@ -197,9 +197,9 @@ def populate_from_csv(self, csv_text):
prerequisite
)
VALUES (
NULLIF(%(Department)s, ''),
%(Level)s,
NULLIF(%(Prerequisite)s, '')
NULLIF('%(Department)s', ''),
'%(Level)s',
NULLIF('%(Prerequisite)s', '')
)
ON CONFLICT DO NOTHING;
""",
Expand All @@ -220,9 +220,9 @@ def populate_from_csv(self, csv_text):
corequisite
)
VALUES (
NULLIF(%(Department)s, ''),
%(Level)s,
NULLIF(%(Corequisite)s, '')
NULLIF('%(Department)s', ''),
'%(Level)s',
NULLIF('%(Corequisite)s', '')
)
ON CONFLICT DO NOTHING;
""",
Expand Down
5 changes: 4 additions & 1 deletion src/api/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from .user_account import UserAccount
from .user_session import UserSession
from .event import Event
from .course import Course
from .course_corequisite import CourseCorequisite
from .course_session import CourseSession
from .event import Event
31 changes: 31 additions & 0 deletions src/api/models/course.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import tortoise
from tortoise.contrib.postgres import fields as pfields
from tortoise import fields
from tortoise.models import Model


class Course(Model):
__tablename__ = "course"

crn = fields.CharField(max_length=255, primary_key=True)
section = fields.CharField(max_length=255)
semester = fields.CharField(max_length=255)
min_credits = fields.IntField()
max_credits = fields.IntField()
date_start = fields.DatetimeField()
date_end = fields.DatetimeField()
department = fields.CharField(max_length=255)
level = fields.IntField()
title = fields.CharField(max_length=255)
full_title = fields.TextField()
description = fields.TextField()
raw_precoreqs = fields.TextField()
frequency = fields.CharField(max_length=255)
school = fields.CharField(max_length=255)
seats_open = fields.IntField()
seats_filled = fields.IntField()
seats_total = fields.IntField()
tsv = pfields.TSVectorField()

class Meta:
table = "course"
12 changes: 12 additions & 0 deletions src/api/models/course_corequisite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from tortoise import fields
from tortoise.models import Model

class CourseCorequisite(Model):

department = fields.IntField(length=255)
level = fields.IntField()
corequisite = fields.IntField(length=255)

class Meta:
table = "course_corequisite"
unique_together = (('department', 'level', 'corequisite'),)
18 changes: 18 additions & 0 deletions src/api/models/course_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from tortoise import fields
from tortoise.models import Model


class CourseSession(Model):
crn = fields.CharField(max_length=255)
section = fields.CharField(max_length=255)
semester = fields.CharField(max_length=255)
time_start = fields.TimeField()
time_end = fields.TimeField()
day_of_week = fields.IntField()
location = fields.CharField(max_length=255)
session_type = fields.CharField(max_length=255)
instructor = fields.CharField(max_length=255)

class Meta:
table = "course_session"
unique_together = (('crn', 'section', 'semester', 'day_of_week'),)
1 change: 1 addition & 0 deletions src/api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ fastapi-cache2==0.1.8
itsdangerous==2.0.1
tortoise-orm==0.19.0
asyncpg==0.25.0
requests==2.27.1
2 changes: 0 additions & 2 deletions src/api/tables/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from .admin_settings import AdminSettings
from .course_corequisite import CourseCorequisite
from .course_prerequisite import CoursePrerequisite
from .course_session import CourseSession
from .course import Course
from .semester_date_range import SemesterDateRange
from .semester_info import SemesterInfo
from .student_course_selection import StudentCourseSelection
Expand Down
27 changes: 0 additions & 27 deletions src/api/tables/course.py

This file was deleted.

15 changes: 0 additions & 15 deletions src/api/tables/course_corequisite.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/api/tests/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM python:3.8-slim
RUN mkdir -p /usr/src
WORKDIR /usr/src
COPY ./../requirements.txt /usr/src/
RUN pip install --no-cache-dir -r requirements.txt
RUN pip3 install --no-cache-dir -r requirements.txt
COPY ../ /usr/src/

CMD [ "sh", "tests/test.sh" ]
48 changes: 0 additions & 48 deletions tests/api/db/test_saving.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pytest==6.2.5
pytest_check==0.3.8
regex==2020.4.4
requests==2.26.0
requests==2.27.1
lxml==4.6.5
pandas==1.3.3
beautifulsoup4==4.9.0
Expand Down