From 20175bf5d252fbd75c89173f7689ffe96b1057a8 Mon Sep 17 00:00:00 2001 From: nsandler1 Date: Mon, 30 Jan 2023 15:16:56 -0500 Subject: [PATCH 1/7] refactor --- home/management/commands/updatecourses.py | 60 +++++++++++------------ 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/home/management/commands/updatecourses.py b/home/management/commands/updatecourses.py index f856e029..9a9389c4 100644 --- a/home/management/commands/updatecourses.py +++ b/home/management/commands/updatecourses.py @@ -102,42 +102,42 @@ 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'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. + elif professor.count() > 1 and alias.count() == 1: + professor = alias.first() + + # 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. + 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']: From b643c17e1e8b9cb1b34890a0d95967533780dcc3 Mon Sep 17 00:00:00 2001 From: nsandler1 Date: Mon, 30 Jan 2023 15:52:00 -0500 Subject: [PATCH 2/7] modify comment --- home/management/commands/updatecourses.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/home/management/commands/updatecourses.py b/home/management/commands/updatecourses.py index 9a9389c4..9924ebe5 100644 --- a/home/management/commands/updatecourses.py +++ b/home/management/commands/updatecourses.py @@ -114,10 +114,9 @@ def _professors(self, course: Course, semester: Semester): elif professor.count() > 1 and alias.count() == 1: professor = alias.first() - # 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. + # Otherwise, we don't recognize this professor. 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) From 7fd389f01fcfea600e7f1a5d44e480e54a16424f Mon Sep 17 00:00:00 2001 From: nsandler1 Date: Mon, 30 Jan 2023 15:51:54 -0500 Subject: [PATCH 3/7] take advantage of unique aliases --- home/management/commands/updatecourses.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/home/management/commands/updatecourses.py b/home/management/commands/updatecourses.py index 9924ebe5..b6bebab2 100644 --- a/home/management/commands/updatecourses.py +++ b/home/management/commands/updatecourses.py @@ -108,10 +108,9 @@ def _professors(self, course: Course, semester: Semester): if professor.count() == 1: professor = professor.first() - # 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. - elif professor.count() > 1 and alias.count() == 1: + # if we have an alias for this name, use + # the professor associated with that alias. + elif professor.count() > 1 and alias.exists(): professor = alias.first() # Otherwise, we don't recognize this professor. So we From be7788b746d18b1608775c0c973951e7c9a10a99 Mon Sep 17 00:00:00 2001 From: nsandler1 Date: Mon, 30 Jan 2023 16:18:34 -0500 Subject: [PATCH 4/7] don't use alias if there are more than one professors with the exact same name --- home/management/commands/updatecourses.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/home/management/commands/updatecourses.py b/home/management/commands/updatecourses.py index b6bebab2..b81589d7 100644 --- a/home/management/commands/updatecourses.py +++ b/home/management/commands/updatecourses.py @@ -108,9 +108,9 @@ def _professors(self, course: Course, semester: Semester): if professor.count() == 1: professor = professor.first() - # if we have an alias for this name, use - # the professor associated with that alias. - elif professor.count() > 1 and alias.exists(): + # 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() # Otherwise, we don't recognize this professor. So we From 5273e5f71cabe2bd4ce1ea8b58263ae08bb19bc9 Mon Sep 17 00:00:00 2001 From: nsandler1 Date: Mon, 30 Jan 2023 16:35:30 -0500 Subject: [PATCH 5/7] actually handle unavailable UMD.io semesters --- home/management/commands/updatecourses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/home/management/commands/updatecourses.py b/home/management/commands/updatecourses.py index b81589d7..71b7dbdd 100644 --- a/home/management/commands/updatecourses.py +++ b/home/management/commands/updatecourses.py @@ -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 From bfd43d95ea88d072730b0ef8510b10fe5dff0f67 Mon Sep 17 00:00:00 2001 From: nsandler1 Date: Mon, 30 Jan 2023 16:54:41 -0500 Subject: [PATCH 6/7] assign professor variable a Professor, not a ProfessorAlias --- home/management/commands/updatecourses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/home/management/commands/updatecourses.py b/home/management/commands/updatecourses.py index 71b7dbdd..39ab6cfb 100644 --- a/home/management/commands/updatecourses.py +++ b/home/management/commands/updatecourses.py @@ -111,7 +111,7 @@ def _professors(self, course: Course, semester: Semester): # 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 = alias.first().professor # Otherwise, we don't recognize this professor. So we # create a new professor and attempt to automatically From 5150936e7c3f54a777196f089e6c2051f07ad43b Mon Sep 17 00:00:00 2001 From: nsandler1 Date: Mon, 30 Jan 2023 22:45:58 -0500 Subject: [PATCH 7/7] modify comment --- home/management/commands/updatecourses.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/home/management/commands/updatecourses.py b/home/management/commands/updatecourses.py index 39ab6cfb..a6ef82e2 100644 --- a/home/management/commands/updatecourses.py +++ b/home/management/commands/updatecourses.py @@ -113,9 +113,10 @@ def _professors(self, course: Course, semester: Semester): elif professor.count() == 0 and alias.exists(): professor = alias.first().professor - # Otherwise, we don't recognize this professor. So we - # create a new professor and attempt to automatically - # verify it following a process similar to that in admin.py. + # 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)