Skip to content

Commit

Permalink
Merge pull request #83 from planetterp/improve-courseupdate
Browse files Browse the repository at this point in the history
Improve courseupdate script
  • Loading branch information
tybug authored Feb 20, 2023
2 parents 675feff + 5150936 commit a3321a7
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions home/management/commands/updatecourses.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def handle(self, *args, **options):
course_data = requests.get("https://api.umd.io/v1/courses", params=kwargs).json()

# if no courses were found during semester, skip.
if "error_code" in course_data.keys():
if isinstance(course_data, dict) and 'error_code' in course_data.keys():
print(f"umd.io doesn't have data for {semester.name()}!")
continue

Expand Down Expand Up @@ -102,42 +102,41 @@ def _professors(self, course: Course, semester: Semester):
continue

professor = self.non_rejected_professors.filter(name=professor_name)
alias = self.aliases.filter(alias=professor_name)

# if there's only one matching professor, use that professor.
if professor.count() == 1:
professor = professor.first()
else:
alias = self.aliases.filter(alias=professor_name)

# if there's more than one matching professor but
# we have an alias that narrows the query down to one,
# use the professor associated with that alias.
if professor.count() > 1 and alias.count() == 1:
professor = alias.first()
else:
# Otherwise, we either don't have this professor or we couldn't
# narrow down the query enough. So, create a new professor and
# attempt to automatically verify it following a process similar
# to that in admin.py.
professor = Professor(name=professor_name, type=Professor.Type.PROFESSOR)
similar_professors = Professor.find_similar(professor.name, 70)
split_name = professor.name.strip().split()
new_slug = split_name[-1].lower()
valid_slug = True

# if there are no matching professors but we have an alias
# for this name, use the professor associated with that alias.
elif professor.count() == 0 and alias.exists():
professor = alias.first().professor

# Otherwise, we either don't recognize this professor or there is
# more than one professor with this exact same name. So we create a
# new professor and attempt to automatically verify it following
# a process similar to that in admin.py.
else:
professor = Professor(name=professor_name, type=Professor.Type.PROFESSOR)
similar_professors = Professor.find_similar(professor.name, 70)
split_name = professor.name.strip().split()
new_slug = split_name[-1].lower()
valid_slug = True

if self.verified_professors.filter(slug=new_slug).exists():
new_slug = f"{split_name[-1]}_{split_name[0]}".lower()
if self.verified_professors.filter(slug=new_slug).exists():
new_slug = f"{split_name[-1]}_{split_name[0]}".lower()
if self.verified_professors.filter(slug=new_slug).exists():
valid_slug = False

# if there are no similarly named professors and there's no
# issues with the auto generated slug, verify the professor.
if len(similar_professors) == 0 and valid_slug:
professor.slug = new_slug
professor.status = Professor.Status.VERIFIED

professor.save()
self.total_num_new_professors += 1
valid_slug = False

# if there are no similarly named professors and there's no
# issues with the auto generated slug, verify the professor.
if len(similar_professors) == 0 and valid_slug:
professor.slug = new_slug
professor.status = Professor.Status.VERIFIED

professor.save()
self.total_num_new_professors += 1

# for every course taught by `professor`...
for entry in umdio_professor['taught']:
Expand Down

0 comments on commit a3321a7

Please sign in to comment.