Skip to content

Commit 52656da

Browse files
committed
Refactor search by email to separate function
1 parent ac498d7 commit 52656da

1 file changed

Lines changed: 88 additions & 176 deletions

File tree

checkpoint.py

Lines changed: 88 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -479,215 +479,127 @@ def login() -> Any:
479479
return redirect(session["next"])
480480

481481

482-
@app.post("/search")
483-
def search() -> (
484-
Dict[str, Any]
485-
): # pylint: disable=too-many-return-statements,too-many-branches,too-many-statements
486-
"""
487-
Search for people matching the provided query
488-
"""
489-
if "has_access" not in session:
490-
raise Unauthorized("Not authenticated")
491-
492-
if session["has_access"] is not True:
493-
raise Forbidden("Access denied")
482+
def search_by_email(email_address: Address) -> Dict[str, Any]:
483+
# search crosswalk by email
484+
cursor = db().execute(
485+
"SELECT gt_person_directory_id FROM crosswalk_email_address WHERE email_address = (:email_address)", # noqa
486+
{"email_address": email_address.addr_spec.lower()},
487+
)
488+
row = cursor.fetchone()
494489

495-
query = request.json["query"].strip() # type: ignore
490+
if row is not None:
491+
# found person in crosswalk, return that
492+
buzzapi_account = get_buzzapi_primary_account(gtPersonDirectoryId=row[0])
496493

497-
try: # pylint: disable=too-many-nested-blocks
498-
# check if the query is formatted like an email address
499-
email_address = Address(addr_spec=query)
494+
if buzzapi_account is None:
495+
raise InternalServerError(
496+
"gtPersonDirectoryId from Crosswalk was not found in BuzzAPI"
497+
)
500498

501-
# if yes, check crosswalk
502-
cursor = db().execute(
503-
"SELECT gt_person_directory_id FROM crosswalk_email_address WHERE email_address = (:email_address)", # noqa
504-
{"email_address": email_address.addr_spec.lower()},
505-
)
506-
row = cursor.fetchone()
499+
return {
500+
"results": [
501+
format_search_result(
502+
buzzapi_account,
503+
search_whitepages(uid=buzzapi_account["gtPrimaryGTAccountUsername"]),
504+
),
505+
],
506+
"exactMatch": True,
507+
}
507508

508-
if row is not None:
509-
buzzapi_account = get_buzzapi_primary_account(gtPersonDirectoryId=row[0])
509+
# search whitepages by email
510+
entries = search_whitepages(mail=email_address.addr_spec)
510511

511-
if buzzapi_account is None:
512-
raise InternalServerError(
513-
"gtPersonDirectoryId from Crosswalk was not found in BuzzAPI"
514-
)
512+
uid = None
515513

516-
return {
517-
"results": [
518-
format_search_result(
519-
buzzapi_account,
520-
search_whitepages(uid=buzzapi_account["gtPrimaryGTAccountUsername"]),
521-
),
522-
],
523-
"exactMatch": True,
524-
}
514+
for entry in entries:
515+
this_uid = get_attribute_value("primaryUid", entry)
525516

526-
entries = search_whitepages(mail=email_address.addr_spec)
517+
if this_uid is not None:
518+
uid = this_uid
527519

528-
uid = None
520+
if uid is None:
521+
# whitepages doesn't have email, check if the username looks like a GT username
522+
if (
523+
fullmatch(GEORGIA_TECH_USERNAME_REGEX, email_address.username, IGNORECASE)
524+
is not None
525+
and email_address.domain == "gatech.edu"
526+
):
527+
# search crosswalk by username
528+
cursor = db().execute(
529+
"SELECT gt_person_directory_id FROM crosswalk WHERE primary_username = (:username)", # noqa
530+
{"username": email_address.username},
531+
)
532+
row = cursor.fetchone()
529533

530-
for entry in entries:
531-
this_uid = get_attribute_value("primaryUid", entry)
534+
if row is not None:
535+
# found person in crosswalk, return that
536+
buzzapi_account = get_buzzapi_primary_account(gtPersonDirectoryId=row[0])
532537

533-
if this_uid is not None:
534-
uid = this_uid
538+
if buzzapi_account is None:
539+
raise InternalServerError(
540+
"gtPersonDirectoryId from Crosswalk was not found in BuzzAPI"
541+
)
535542

536-
if uid is None:
537-
if (
538-
fullmatch(GEORGIA_TECH_USERNAME_REGEX, email_address.username, IGNORECASE)
539-
is not None
540-
and email_address.domain == "gatech.edu"
541-
):
542-
cursor = db().execute(
543-
"SELECT gt_person_directory_id FROM crosswalk WHERE primary_username = (:username)", # noqa
544-
{"username": email_address.username},
545-
)
546-
row = cursor.fetchone()
547-
548-
if row is not None:
549-
buzzapi_account = get_buzzapi_primary_account(gtPersonDirectoryId=row[0])
550-
551-
if buzzapi_account is None:
552-
raise InternalServerError(
553-
"gtPersonDirectoryId from Crosswalk was not found in BuzzAPI"
554-
)
555-
556-
return {
557-
"results": [
558-
format_search_result(
559-
buzzapi_account,
560-
search_whitepages(
561-
uid=buzzapi_account["gtPrimaryGTAccountUsername"]
562-
),
543+
return {
544+
"results": [
545+
format_search_result(
546+
buzzapi_account,
547+
search_whitepages(
548+
uid=buzzapi_account["gtPrimaryGTAccountUsername"]
563549
),
564-
],
565-
"exactMatch": True,
566-
}
567-
568-
entries = search_whitepages(uid=email_address.username)
569-
570-
uid = None
571-
mails = set()
572-
573-
for entry in entries:
574-
this_uid = get_attribute_value("primaryUid", entry)
550+
),
551+
],
552+
"exactMatch": True,
553+
}
575554

576-
if this_uid is not None:
577-
uid = this_uid
555+
# search whitepages by username
556+
entries = search_whitepages(uid=email_address.username)
578557

579-
this_mail = get_attribute_value("mail", entry)
558+
uid = None
580559

581-
if this_mail is not None:
582-
mails.add(this_mail)
560+
for entry in entries:
561+
this_uid = get_attribute_value("primaryUid", entry)
583562

584-
if uid is None:
585-
return {
586-
"results": [],
587-
"exactMatch": True,
588-
}
563+
if this_uid is not None:
564+
uid = this_uid
589565

566+
if uid is not None:
567+
# found person in whitepages, return that
590568
buzzapi_account = get_buzzapi_primary_account(uid=email_address.username)
591569

592570
if buzzapi_account is None:
593571
raise InternalServerError("Account found in Whitepages but not BuzzAPI")
594572

595-
db().execute(
596-
(
597-
"INSERT INTO crosswalk (gt_person_directory_id, gtid, primary_username)"
598-
" VALUES (:gt_person_directory_id, :gtid, :primary_username) ON CONFLICT DO NOTHING" # noqa
599-
),
600-
{
601-
"gt_person_directory_id": buzzapi_account["gtPersonDirectoryId"],
602-
"gtid": buzzapi_account["gtGTID"],
603-
"primary_username": buzzapi_account["gtPrimaryGTAccountUsername"],
604-
},
605-
)
606-
db().execute(
607-
(
608-
"INSERT INTO crosswalk_email_address (email_address, gt_person_directory_id)" # noqa
609-
" VALUES (:email_address, :gt_person_directory_id)"
610-
" ON CONFLICT DO UPDATE SET gt_person_directory_id = (:gt_person_directory_id) WHERE email_address = (:email_address)" # noqa
611-
),
612-
{
613-
"email_address": buzzapi_account["mail"],
614-
"gt_person_directory_id": buzzapi_account["gtPersonDirectoryId"],
615-
},
616-
)
617-
618-
for mail in mails:
619-
db().execute(
620-
(
621-
"INSERT INTO crosswalk_email_address (email_address, gt_person_directory_id)" # noqa
622-
" VALUES (:email_address, :gt_person_directory_id)"
623-
" ON CONFLICT DO UPDATE SET gt_person_directory_id = (:gt_person_directory_id) WHERE email_address = (:email_address)" # noqa
624-
),
625-
{
626-
"email_address": mail,
627-
"gt_person_directory_id": buzzapi_account["gtPersonDirectoryId"],
628-
},
629-
)
630-
631573
return {
632574
"results": [
633575
format_search_result(buzzapi_account, entries),
634576
],
635577
"exactMatch": True,
636578
}
637579

638-
return {
639-
"results": [],
640-
"exactMatch": True,
641-
}
580+
return {
581+
"results": [],
582+
"exactMatch": True
583+
}
642584

643-
buzzapi_account = get_buzzapi_primary_account(uid=email_address.username)
585+
@app.post("/search")
586+
def search() -> (
587+
Dict[str, Any]
588+
): # pylint: disable=too-many-return-statements,too-many-branches,too-many-statements
589+
"""
590+
Search for people matching the provided query
591+
"""
592+
if "has_access" not in session:
593+
raise Unauthorized("Not authenticated")
644594

645-
if buzzapi_account is None:
646-
raise InternalServerError("Account found in Whitepages but not BuzzAPI")
595+
if session["has_access"] is not True:
596+
raise Forbidden("Access denied")
647597

648-
db().execute(
649-
(
650-
"INSERT INTO crosswalk (gt_person_directory_id, gtid, primary_username)"
651-
" VALUES (:gt_person_directory_id, :gtid, :primary_username) ON CONFLICT DO NOTHING"
652-
),
653-
{
654-
"gt_person_directory_id": buzzapi_account["gtPersonDirectoryId"],
655-
"gtid": buzzapi_account["gtGTID"],
656-
"primary_username": buzzapi_account["gtPrimaryGTAccountUsername"],
657-
},
658-
)
659-
db().execute(
660-
(
661-
"INSERT INTO crosswalk_email_address (email_address, gt_person_directory_id)"
662-
" VALUES (:email_address, :gt_person_directory_id)"
663-
" ON CONFLICT DO UPDATE SET gt_person_directory_id = (:gt_person_directory_id) WHERE email_address = (:email_address)" # noqa
664-
),
665-
{
666-
"email_address": buzzapi_account["mail"],
667-
"gt_person_directory_id": buzzapi_account["gtPersonDirectoryId"],
668-
},
669-
)
670-
db().execute(
671-
(
672-
"INSERT INTO crosswalk_email_address (email_address, gt_person_directory_id)"
673-
" VALUES (:email_address, :gt_person_directory_id)"
674-
" ON CONFLICT DO UPDATE SET gt_person_directory_id = (:gt_person_directory_id) WHERE email_address = (:email_address)" # noqa
675-
),
676-
{
677-
"email_address": email_address.addr_spec,
678-
"gt_person_directory_id": buzzapi_account["gtPersonDirectoryId"],
679-
},
680-
)
598+
query = request.json["query"].strip() # type: ignore
681599

682-
return {
683-
"results": [
684-
format_search_result(
685-
buzzapi_account,
686-
search_whitepages(uid=buzzapi_account["gtPrimaryGTAccountUsername"]),
687-
),
688-
],
689-
"exactMatch": True,
690-
}
600+
try: # pylint: disable=too-many-nested-blocks
601+
# check if the query is formatted like an email address
602+
return search_by_email(Address(addr_spec=query))
691603
except InvalidHeaderDefect:
692604
# check if the query is formatted like a GT username
693605
if fullmatch(GEORGIA_TECH_USERNAME_REGEX, query, IGNORECASE) is not None:

0 commit comments

Comments
 (0)