-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into pcp-schedule-sharing-frontend
- Loading branch information
Showing
26 changed files
with
1,661 additions
and
1,685 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 |
---|---|---|
|
@@ -20,3 +20,7 @@ postgres | |
*-test.json | ||
celerybeat-schedule | ||
pcr-backup* | ||
./Pipfile | ||
./Pipfile.lock | ||
./package.json | ||
./yarn.lock |
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,81 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.db import transaction | ||
from django.db.models import Exists, F, OuterRef | ||
|
||
from alert.management.commands.recomputestats import garbage_collect_topics | ||
from courses.management.commands.load_crosswalk import get_crosswalk_s3 | ||
from courses.models import Course, Topic | ||
|
||
|
||
def form_simple_topics(): | ||
Course.objects.all().update(topic=None) | ||
garbage_collect_topics() | ||
|
||
print("Cleared topics.") | ||
|
||
# create topics for each primary listing | ||
primary_listings = Course.objects.filter(primary_listing_id=F("id")) | ||
for primary_listing in primary_listings: | ||
topic = Topic.objects.create(most_recent=primary_listing) | ||
primary_listing.listing_set.all().update(topic=topic) | ||
|
||
print("Created new topics (one per course).") | ||
|
||
# merge topics based on full_code of primary_listing | ||
for full_code in primary_listings.values_list("full_code", flat=True): | ||
Topic.merge_all( | ||
list( | ||
set( | ||
course.topic | ||
for course in ( | ||
primary_listings.filter(full_code=full_code).select_related("topic") | ||
) | ||
) | ||
) | ||
) | ||
|
||
print("Merged topics based on full_code") | ||
|
||
# use crosswalk | ||
crosswalk = get_crosswalk_s3(verbose=True) | ||
for old_code, new_codes in crosswalk.items(): | ||
old_topic = Topic.objects.filter(most_recent__listing_set__full_code=old_code).first() | ||
new_course = ( | ||
Course.objects.filter( | ||
full_code__in=new_codes, | ||
) | ||
.annotate( | ||
title_matches=Exists( # Prefer Course.objects.all().with matching title | ||
Course.objects.all().filter(full_code=old_code, title=OuterRef("title")) | ||
), | ||
) | ||
.order_by("-title_matches") | ||
.select_related("topic") | ||
.first() | ||
) | ||
if old_topic and new_course: | ||
new_course.topic.merge_with(old_topic) | ||
|
||
print("Merged topics based on crosswalk") | ||
|
||
garbage_collect_topics() | ||
|
||
print("Done!") | ||
|
||
|
||
class Command(BaseCommand): | ||
help = ( | ||
"This script deletes all existing topics and re-creates them " | ||
"so that courses with the same full_code are in the same topic. " | ||
"this script also uses the crosswalk. " | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
print( | ||
"This script is atomic, meaning either all Topic changes will be comitted to the " | ||
"database, or otherwise if an error is encountered, all changes will be rolled back " | ||
"and the database will remain as it was before the script was run." | ||
) | ||
|
||
with transaction.atomic(): | ||
form_simple_topics() |
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
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
Oops, something went wrong.