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

Create script to update geneds #91

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Changes from 27 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0c68d78
add geneds field to course model
nsandler1 Feb 6, 2023
7f61090
add script file
nsandler1 Feb 6, 2023
42a67eb
write script
nsandler1 Feb 7, 2023
4bd952d
Merge branch 'gened-schema' into gened-populate
nsandler1 Feb 7, 2023
bfa444f
add case for no geneds
nsandler1 Feb 7, 2023
6d78421
Merge branch 'gened-schema' into gened-script
nsandler1 Feb 7, 2023
81e2a92
Merge branch 'gened-populate' into gened-script
nsandler1 Feb 7, 2023
5a32330
write script to update geneds for a semester
nsandler1 Feb 7, 2023
9a398fc
amend comment
nsandler1 Feb 7, 2023
ce728cc
Merge branch 'gened-schema' into gened-populate
nsandler1 Feb 7, 2023
f1a2eda
minor changes to work with JSONField
nsandler1 Feb 7, 2023
a8638b2
Merge branch 'gened-populate' into gened-script
nsandler1 Feb 7, 2023
c51b42e
minor changes to work with JSONField
nsandler1 Feb 7, 2023
47cb776
change variable name
nsandler1 Feb 7, 2023
b7930dc
don't update num_updated_coruses in one case
nsandler1 Feb 7, 2023
096b16b
consolidate conditional cases
nsandler1 Feb 7, 2023
53f88c4
explain print statement
nsandler1 Feb 7, 2023
06d2696
Merge branch 'gened-populate' into gened-script
nsandler1 Feb 7, 2023
51f9023
update variable name
nsandler1 Feb 7, 2023
ab6b8e1
consolidate conditional case
nsandler1 Feb 7, 2023
d1ab2c0
Merge branch 'master' into gened-script
nsandler1 Mar 4, 2023
29cd9df
modify script to address discusison in #90
nsandler1 Mar 5, 2023
271ec2b
remove migration file from #90
nsandler1 Mar 5, 2023
9ac0e14
fix argument
nsandler1 Mar 7, 2023
846269d
remove print
nsandler1 Mar 7, 2023
77333e8
process courses alphabetically
nsandler1 Mar 7, 2023
bc307c7
Merge branch 'master' of https://github.com/planetterp/PlanetTerp int…
nsandler1 Mar 7, 2023
2f65880
Merge branch 'master' of https://github.com/planetterp/PlanetTerp int…
nsandler1 Mar 15, 2023
16998f4
don't rely on length of gened
nsandler1 Mar 15, 2023
9146344
come up with two potential solutions to improve runtime
nsandler1 Mar 26, 2023
b39f0bc
change wording of comment
nsandler1 Apr 30, 2023
004469b
use related manager queries where appropriate
nsandler1 Apr 30, 2023
878fe19
Merge branch 'master' into gened-script
nsandler1 Apr 30, 2023
c604562
change/add print statements
nsandler1 Apr 30, 2023
e81f13d
improve threaded approach
nsandler1 May 20, 2023
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
88 changes: 88 additions & 0 deletions home/management/commands/updategeneds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import requests
from datetime import datetime

from django.core.management import BaseCommand
from argparse import RawTextHelpFormatter

from home.models import Course, Gened

class Command(BaseCommand):
help = '''Updates the database with new courses and professors during the provided semester.
The semester argument must be in the numerical form YEAR+SEASON (see ** for exception).
The season codes are as follows:
Spring -> 01
Summer -> 05
Fall -> 08
Winter -> 12
EXAMPLE: Spring 2023 = 202301

** NOTE: Starting from Winter 2021, the values for winter semesters are off by one year. Winter 2021 is actually 202012, not 202112
'''

def __init__(self, *args, **kwargs):
super().__init__()
self.num_geneds_updated = 0

def create_parser(self, *args, **kwargs):
# to format the help text that gets displayed with the -h option
parser = super(Command, self).create_parser(*args, **kwargs)
parser.formatter_class = RawTextHelpFormatter
return parser

def handle(self, *args, **options):
start = datetime.now()
pt_courses = Course.unfiltered.all().order_by('name')

# for each of our courses...
for pt_course in pt_courses:
# see if umdio has this course
umdio_course = requests.get(f"https://api.umd.io/v1/courses/{pt_course.name}").json()

# if umdio does not have this course continue to the next course
if isinstance(umdio_course, dict) and 'error_code' in umdio_course.keys():
continue

print(pt_course.name)
self.update_course_table(pt_course, umdio_course[0])
self.update_geneds_table(pt_course, umdio_course[0])

runtime = datetime.now() - start
print(f"number of geneds updated: {self.num_geneds_updated}")
print(f"total runtime: {round(runtime.seconds / 60, 2)} minutes")

def update_course_table(self, pt_course, umdio_course):
# umdio uses an empty list if a course has no geneds but
# we want to use null because that's what our schema is expecting.
if not umdio_course['gen_ed']:
pt_course.geneds = None
else:
pt_course.geneds = umdio_course['gen_ed']

pt_course.save()

def update_geneds_table(self, pt_course, umdio_course):
pt_geneds = Gened.objects.all()

# create a list of all the umdio geneds for this course.
# This list is considered the "correct" geneds for this course.
umdio_geneds = []
for gened_lst in umdio_course['gen_ed']:
for gened in gened_lst:
# To account for cases that have a pipe |, take the first 4
# characters of the gened value because geneds only have 4
# characters in their name
umdio_geneds.append(gened[:4])
tybug marked this conversation as resolved.
Show resolved Hide resolved

# if we have a gened linked to a course but umdio doesn't agree,
# assume our records are outdated and delete this link.
for gened in pt_geneds.filter(course=pt_course):
tybug marked this conversation as resolved.
Show resolved Hide resolved
if gened.name not in umdio_geneds:
self.num_geneds_updated += 1
gened.delete()

# if we don't have a gened for this course that umdio does have,
# add it to our records.
for gened in umdio_geneds:
if not pt_geneds.filter(name=gened, course=pt_course).exists():
tybug marked this conversation as resolved.
Show resolved Hide resolved
self.num_geneds_updated += 1
Gened(name=gened, course=pt_course).save()