forked from YACS-RCOS/yacs.n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'finals-database-support' into semester-delete
- Loading branch information
Showing
9 changed files
with
660 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ courses20.xml | |
compose-dev.yaml | ||
rpi_data/get-summer-2023-2.sh | ||
rpi_data/summer-20232.csv | ||
.venv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import requests | ||
import base64 | ||
import os | ||
|
||
url = os.environ.get('yacs_url') | ||
url = "http://localhost:5000" | ||
api_location = url + "/api/bulkCourseUpload" | ||
__location__ = os.path.realpath(os.path.join( | ||
os.getcwd(), os.path.dirname(__file__))) | ||
|
||
|
||
def csvUpload(fileName): | ||
endpath = os.path.join(__location__, fileName) | ||
endpath = os.path.dirname(os.path.dirname(endpath)) + "\\" + fileName | ||
print(endpath) | ||
uploadFile = {'file': open(endpath, 'rb')} | ||
data = {'isPubliclyVisible': 'on'} | ||
|
||
r = requests.post(api_location, files=uploadFile, data=data) | ||
print(r.reason, r.status_code) | ||
|
||
|
||
if __name__ == "__main__": | ||
csvUpload("spring-2022.csv") |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import csv | ||
import re | ||
from psycopg2.extras import RealDictCursor | ||
from ast import literal_eval | ||
import asyncio | ||
|
||
# https://stackoverflow.com/questions/54839933/importerror-with-from-import-x-on-simple-python-files | ||
if __name__ == "__main__": | ||
import connection | ||
else: | ||
from . import connection | ||
|
||
|
||
class Finals: | ||
def __init__(self, db_wrapper, cache): | ||
self.db = db_wrapper | ||
self.cache = cache | ||
|
||
def populate_from_csv(self, csv_text): | ||
conn = 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: | ||
for row in reader: | ||
try: | ||
# finals | ||
transaction.execute( | ||
""" | ||
INSERT INTO | ||
final( | ||
semester, | ||
course, | ||
section, | ||
"start", | ||
"end", | ||
room_assignment | ||
) | ||
VALUES ( | ||
%(Semester)s, | ||
%(Course)s, | ||
%(Section)s, | ||
TO_TIMESTAMP(%(Start)s, 'YYYY-MM-DD HH24:MI:SS'), | ||
TO_TIMESTAMP(%(End)s, 'YYYY-MM-DD HH24:MI:SS'), | ||
%(Room_Assignment)s | ||
) | ||
ON CONFLICT (semester, course, section, start, room_assignment) DO NOTHING; | ||
""", | ||
{ | ||
"Semester": row['Season'] + ' ' + row['Year'], | ||
"Course": row['Major'] + '-' + row['Course'], | ||
"Section": "1" if row['Section'] == '' else row['Section'], | ||
"Start": row['Start'], | ||
"End": row['End'], | ||
"Room_Assignment": row['Building'] + '-' + row['Room_Number'] | ||
} | ||
) | ||
except Exception as e: | ||
print(e) | ||
conn.rollback() | ||
return e | ||
conn.commit() | ||
self.clear_cache() | ||
return None | ||
|
||
def get_by_semester(self, semester): | ||
return self.db.execute(""" | ||
SELECT * FROM final | ||
WHERE semester=%(Semester)s | ||
ORDER BY start ASC; | ||
""", { | ||
"Semester": semester | ||
}, isSELECT=True) | ||
|
||
def delete_by_semester(self, semester): | ||
self.clear_cache() | ||
return self.db.execute(""" | ||
BEGIN TRANSACTION; | ||
DELETE FROM final | ||
WHERE semester=%(Semester)s; | ||
COMMIT; | ||
""", { | ||
"Semester": semester | ||
}, isSELECT=False) | ||
|
||
def bulk_delete(self, semesters): | ||
for semester in semesters: | ||
_, error = self.delete_by_semester(semester) | ||
if error: | ||
print(error) | ||
return error | ||
self.clear_cache() | ||
return None | ||
|
||
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")) | ||
|
||
if __name__ == "__main__": | ||
# os.chdir(os.path.abspath("../rpi_data")) | ||
# fileNames = glob.glob("*.csv") | ||
csv_text = open('../../../rpi_data/out.csv', 'r') | ||
finals = Finals(connection.db) | ||
finals.populate_from_csv(csv_text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""adding finals | ||
Revision ID: 1ff4f05483ad | ||
Revises: 6c9a1ffc58a5 | ||
Create Date: 2024-10-15 21:38:59.576120 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '6c9a1ffc58a5' | ||
down_revision = 'c959c263997f' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('final', | ||
sa.Column('semester', sa.VARCHAR(length=255), nullable=False), | ||
sa.Column('course', sa.VARCHAR(length=255), nullable=False), | ||
sa.Column('section', sa.INTEGER(), nullable=False), | ||
sa.Column('start', postgresql.TIMESTAMP(), nullable=False), | ||
sa.Column('end', postgresql.TIMESTAMP(), nullable=False), | ||
sa.Column('room_assignment', sa.VARCHAR(length=255), nullable=False), | ||
sa.PrimaryKeyConstraint('semester', 'course', 'section', 'start', 'room_assignment') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('final') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from sqlalchemy import Column, PrimaryKeyConstraint | ||
from sqlalchemy.dialects.postgresql import INTEGER, VARCHAR, TIME | ||
|
||
from .database import Base | ||
|
||
class Final(Base): | ||
__tablename__ = "final" | ||
|
||
semester = Column(VARCHAR(length=255)) | ||
course = Column(VARCHAR(length=255)) | ||
section = Column(INTEGER) | ||
start = Column(TIME) | ||
end = Column(TIME) | ||
room_assignment = Column(VARCHAR(length=255)) | ||
|
||
__table_args__ = ( | ||
PrimaryKeyConstraint('semester', 'course', 'section', 'start', 'room_assignment'), | ||
) |