-
Notifications
You must be signed in to change notification settings - Fork 25
Create script to update geneds #91
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
Open
nsandler1
wants to merge
35
commits into
master
Choose a base branch
from
gened-script
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
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 7f61090
add script file
nsandler1 42a67eb
write script
nsandler1 4bd952d
Merge branch 'gened-schema' into gened-populate
nsandler1 bfa444f
add case for no geneds
nsandler1 6d78421
Merge branch 'gened-schema' into gened-script
nsandler1 81e2a92
Merge branch 'gened-populate' into gened-script
nsandler1 5a32330
write script to update geneds for a semester
nsandler1 9a398fc
amend comment
nsandler1 ce728cc
Merge branch 'gened-schema' into gened-populate
nsandler1 f1a2eda
minor changes to work with JSONField
nsandler1 a8638b2
Merge branch 'gened-populate' into gened-script
nsandler1 c51b42e
minor changes to work with JSONField
nsandler1 47cb776
change variable name
nsandler1 b7930dc
don't update num_updated_coruses in one case
nsandler1 096b16b
consolidate conditional cases
nsandler1 53f88c4
explain print statement
nsandler1 06d2696
Merge branch 'gened-populate' into gened-script
nsandler1 51f9023
update variable name
nsandler1 ab6b8e1
consolidate conditional case
nsandler1 d1ab2c0
Merge branch 'master' into gened-script
nsandler1 29cd9df
modify script to address discusison in #90
nsandler1 271ec2b
remove migration file from #90
nsandler1 9ac0e14
fix argument
nsandler1 846269d
remove print
nsandler1 77333e8
process courses alphabetically
nsandler1 bc307c7
Merge branch 'master' of https://github.com/planetterp/PlanetTerp int…
nsandler1 2f65880
Merge branch 'master' of https://github.com/planetterp/PlanetTerp int…
nsandler1 16998f4
don't rely on length of gened
nsandler1 9146344
come up with two potential solutions to improve runtime
nsandler1 b39f0bc
change wording of comment
nsandler1 004469b
use related manager queries where appropriate
nsandler1 878fe19
Merge branch 'master' into gened-script
nsandler1 c604562
change/add print statements
nsandler1 e81f13d
improve threaded approach
nsandler1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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]) | ||
|
|
||
| # 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): | ||
|
Liam-DeVoe marked this conversation as resolved.
Outdated
|
||
| 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(): | ||
|
Liam-DeVoe marked this conversation as resolved.
Outdated
|
||
| self.num_geneds_updated += 1 | ||
| Gened(name=gened, course=pt_course).save() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.