-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #456 from anshg1214/handle_deleted_and_redirected_…
…entities Handle Deleted and Redirected BB Entities
- Loading branch information
Showing
7 changed files
with
100 additions
and
4 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
49 changes: 49 additions & 0 deletions
49
critiquebrainz/frontend/external/bookbrainz_db/redirects.py
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,49 @@ | ||
import uuid | ||
from brainzutils import cache | ||
import sqlalchemy | ||
import critiquebrainz.frontend.external.bookbrainz_db as db | ||
from critiquebrainz.frontend.external.bookbrainz_db import DEFAULT_CACHE_EXPIRATION | ||
|
||
|
||
def get_redirected_bbid(bbid: uuid.UUID) -> str: | ||
""" | ||
Get the redirected BBID for a given BBID. | ||
Args: | ||
bbid: The BBID to get the redirected BBID for. | ||
Returns: | ||
The redirected BBID. | ||
Returns None if the BBID is not redirected. | ||
""" | ||
bb_redirect_key = cache.gen_key('bb_redirects', bbid) | ||
results = cache.get(bb_redirect_key) | ||
|
||
if not results: | ||
with db.bb_engine.connect() as connection: | ||
result = connection.execute(sqlalchemy.text(""" | ||
WITH RECURSIVE redirects AS ( | ||
SELECT source_bbid, target_bbid | ||
FROM entity_redirect | ||
WHERE source_bbid = :bbid | ||
UNION | ||
SELECT er.source_bbid, er.target_bbid | ||
FROM entity_redirect er | ||
INNER JOIN redirects rd | ||
ON rd.target_bbid = er.source_bbid | ||
) | ||
SELECT target_bbid::text | ||
FROM redirects | ||
"""), {'bbid': bbid}) | ||
|
||
redirect_bbids = result.fetchall() | ||
|
||
results = [] | ||
for redirect_bbid in redirect_bbids: | ||
redirect_bbids = dict(redirect_bbid) | ||
results.append(redirect_bbid['target_bbid']) | ||
|
||
cache.set(bb_redirect_key, results, DEFAULT_CACHE_EXPIRATION) | ||
|
||
if results: | ||
return results[-1] | ||
else: | ||
return None |
26 changes: 26 additions & 0 deletions
26
critiquebrainz/frontend/external/bookbrainz_db/test/bb_redirects_test.py
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,26 @@ | ||
from critiquebrainz.frontend.external.bookbrainz_db import redirects | ||
from critiquebrainz.data.testing import DataTestCase | ||
|
||
|
||
class BBRedirectsTestCase(DataTestCase): | ||
|
||
def setUp(self): | ||
super(BBRedirectsTestCase, self).setUp() | ||
self.bbid1 = '63a40e3d-54ff-4549-9637-24959ad89241' | ||
self.bbid2 = 'dd40b465-931f-46ee-b2ae-28685b19f8d8' | ||
self.bbid3 = 'e5c4e68b-bfce-4c77-9ca2-0f0a2d4d09f0' | ||
|
||
def test_single_bb_redirects(self): | ||
# Test single redirect | ||
redirected_bbid = redirects.get_redirected_bbid(self.bbid1) | ||
self.assertEqual(redirected_bbid, 'ecdeb45d-c432-4347-94c3-f01acc799d4a') | ||
|
||
def test_multiple_bb_redirects(self): | ||
# Test multiple redirects | ||
redirected_bbid = redirects.get_redirected_bbid(self.bbid2) | ||
self.assertEqual(redirected_bbid, '7e691222-6f78-4ad6-ad5d-1a671a319fbd') | ||
|
||
def test_no_bb_redirects(self): | ||
# Test no redirects | ||
redirected_bbid = redirects.get_redirected_bbid(self.bbid3) | ||
self.assertEqual(redirected_bbid, None) |
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
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