diff --git a/critiquebrainz/db/review.py b/critiquebrainz/db/review.py index ac9b45810..b7f8805bc 100644 --- a/critiquebrainz/db/review.py +++ b/critiquebrainz/db/review.py @@ -406,10 +406,14 @@ def invalidate_ws_entity_cache(entity_id, entity_type, user_id): cache_keys_for_top_reviews_key = cache.gen_key('entity_api', entity_type, entity_id, "top_reviews") cache_keys_for_latest_reviews_key = cache.gen_key('entity_api', entity_type, entity_id, "latest_reviews") - cache_keys_for_user_reviews_key = cache.gen_key('entity_api', entity_id, user_id, "user_reviews") cache.delete(cache_keys_for_top_reviews_key, namespace=REVIEW_CACHE_NAMESPACE) cache.delete(cache_keys_for_latest_reviews_key, namespace=REVIEW_CACHE_NAMESPACE) - cache.delete(cache_keys_for_user_reviews_key, namespace=REVIEW_CACHE_NAMESPACE) + + user = db_users.get_by_id(user_id) + username = user["musicbrainz_username"] + if username: + cache_keys_for_user_reviews_key = cache.gen_key('entity_api', entity_id, username, "user_reviews") + cache.delete(cache_keys_for_user_reviews_key, namespace=REVIEW_CACHE_NAMESPACE) # pylint: disable=too-many-branches diff --git a/critiquebrainz/ws/artist/views.py b/critiquebrainz/ws/artist/views.py index 8197389d5..44c3233be 100644 --- a/critiquebrainz/ws/artist/views.py +++ b/critiquebrainz/ws/artist/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.musicbrainz_db import artist as db_artist from critiquebrainz.decorators import crossdomain @@ -223,6 +224,8 @@ def artist_entity_handler(artist_mbid): :statuscode 200: no error :statuscode 404: artist not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -232,23 +235,28 @@ def artist_entity_handler(artist_mbid): user_review = None - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', artist['mbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', artist['mbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=artist['mbid'], - entity_type='artist', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) - else: - user_review = None + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=artist['mbid'], + entity_type='artist', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + + else: + user_review = [] ratings_stats, average_rating = db_rating_stats.get_stats(artist_mbid, "artist") @@ -296,7 +304,8 @@ def artist_entity_handler(artist_mbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + + if username: result['user_review'] = user_review return jsonify(**result) diff --git a/critiquebrainz/ws/bb_author/views.py b/critiquebrainz/ws/bb_author/views.py index 26dda70cf..43c088c3a 100644 --- a/critiquebrainz/ws/bb_author/views.py +++ b/critiquebrainz/ws/bb_author/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.bookbrainz_db import author as db_author from critiquebrainz.decorators import crossdomain @@ -159,6 +160,8 @@ def author_entity_handler(author_bbid): :statuscode 200: no error :statuscode 404: author not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -169,23 +172,29 @@ def author_entity_handler(author_bbid): user_review = [] - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', author['bbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', author['bbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=author['bbid'], - entity_type='bb_author', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=author['bbid'], + entity_type='bb_author', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) + + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + else: user_review = [] - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) ratings_stats, average_rating = db_rating_stats.get_stats(author_bbid, "bb_author") @@ -233,7 +242,7 @@ def author_entity_handler(author_bbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + if username: result['user_review'] = user_review return jsonify(**result) diff --git a/critiquebrainz/ws/bb_edition_group/views.py b/critiquebrainz/ws/bb_edition_group/views.py index ef12b15cd..64bc8a77a 100644 --- a/critiquebrainz/ws/bb_edition_group/views.py +++ b/critiquebrainz/ws/bb_edition_group/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.bookbrainz_db import edition_group as db_edition_group from critiquebrainz.decorators import crossdomain @@ -137,6 +138,8 @@ def edition_group_entity_handler(edition_group_bbid): :statuscode 200: no error :statuscode 404: edition group not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -147,24 +150,29 @@ def edition_group_entity_handler(edition_group_bbid): user_review = [] - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', edition_group['bbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', edition_group['bbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=edition_group['bbid'], - entity_type='bb_edition_group', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=edition_group['bbid'], + entity_type='bb_edition_group', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) + + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + else: user_review = [] - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) - ratings_stats, average_rating = db_rating_stats.get_stats(edition_group_bbid, "bb_edition_group") top_reviews_cache_key = cache.gen_key("entity_api_bb_edition_group", edition_group['bbid'], "top_reviews") @@ -211,7 +219,8 @@ def edition_group_entity_handler(edition_group_bbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + + if username: result['user_review'] = user_review return jsonify(**result) diff --git a/critiquebrainz/ws/bb_literary_work/views.py b/critiquebrainz/ws/bb_literary_work/views.py index ba0c5991b..2ea81848f 100644 --- a/critiquebrainz/ws/bb_literary_work/views.py +++ b/critiquebrainz/ws/bb_literary_work/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.bookbrainz_db import literary_work as db_literary_work from critiquebrainz.decorators import crossdomain @@ -157,6 +158,8 @@ def literary_work_entity_handler(literary_work_bbid): :statuscode 200: no error :statuscode 404: literary work not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -167,24 +170,29 @@ def literary_work_entity_handler(literary_work_bbid): user_review = [] - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', literary_work['bbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', literary_work['bbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=literary_work['bbid'], - entity_type='bb_literary_work', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=literary_work['bbid'], + entity_type='bb_literary_work', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) + + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + else: user_review = [] - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) - ratings_stats, average_rating = db_rating_stats.get_stats(literary_work_bbid, "bb_literary_work") top_reviews_cache_key = cache.gen_key("entity_api_bb_literary_work", literary_work['bbid'], "top_reviews") @@ -231,7 +239,8 @@ def literary_work_entity_handler(literary_work_bbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + + if username: result['user_review'] = user_review return jsonify(**result) diff --git a/critiquebrainz/ws/bb_series/views.py b/critiquebrainz/ws/bb_series/views.py index 0c20a40c1..807e6f277 100644 --- a/critiquebrainz/ws/bb_series/views.py +++ b/critiquebrainz/ws/bb_series/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.bookbrainz_db import series as db_series from critiquebrainz.decorators import crossdomain @@ -129,6 +130,8 @@ def series_entity_handler(series_bbid): :statuscode 200: no error :statuscode 404: series not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -139,23 +142,29 @@ def series_entity_handler(series_bbid): user_review = [] - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', series['bbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', series['bbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=series['bbid'], - entity_type='bb_series', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=series['bbid'], + entity_type='bb_series', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) + + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + else: user_review = [] - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) ratings_stats, average_rating = db_rating_stats.get_stats(series_bbid, "bb_series") @@ -203,7 +212,8 @@ def series_entity_handler(series_bbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + + if username: result['user_review'] = user_review return jsonify(**result) diff --git a/critiquebrainz/ws/event/views.py b/critiquebrainz/ws/event/views.py index 6e199dacd..e0a304b50 100644 --- a/critiquebrainz/ws/event/views.py +++ b/critiquebrainz/ws/event/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.musicbrainz_db import event as db_event from critiquebrainz.decorators import crossdomain @@ -249,6 +250,8 @@ def event_entity_handler(event_mbid): :statuscode 200: no error :statuscode 404: label not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -259,23 +262,28 @@ def event_entity_handler(event_mbid): user_review = None - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', event['mbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', event['mbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=event['mbid'], - entity_type='event', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) - else: - user_review = None + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=event['mbid'], + entity_type='event', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + + else: + user_review = [] ratings_stats, average_rating = db_rating_stats.get_stats(event_mbid, "event") @@ -323,7 +331,8 @@ def event_entity_handler(event_mbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + + if username: result['user_review'] = user_review return jsonify(**result) diff --git a/critiquebrainz/ws/label/views.py b/critiquebrainz/ws/label/views.py index 95dd6cf1e..e91737828 100644 --- a/critiquebrainz/ws/label/views.py +++ b/critiquebrainz/ws/label/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.musicbrainz_db import label as db_label from critiquebrainz.decorators import crossdomain @@ -164,6 +165,8 @@ def label_entity_handler(label_mbid): :statuscode 200: no error :statuscode 404: label not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -174,23 +177,28 @@ def label_entity_handler(label_mbid): user_review = None - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', label['mbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', label['mbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=label['mbid'], - entity_type='label', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) - else: - user_review = None + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=label['mbid'], + entity_type='label', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + + else: + user_review = [] ratings_stats, average_rating = db_rating_stats.get_stats(label_mbid, "label") @@ -238,7 +246,8 @@ def label_entity_handler(label_mbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + + if username: result['user_review'] = user_review return jsonify(**result) diff --git a/critiquebrainz/ws/place/views.py b/critiquebrainz/ws/place/views.py index ee6d9f909..45f4f2e82 100644 --- a/critiquebrainz/ws/place/views.py +++ b/critiquebrainz/ws/place/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.musicbrainz_db import place as db_place from critiquebrainz.decorators import crossdomain @@ -318,6 +319,8 @@ def place_entity_handler(place_mbid): :statuscode 200: no error :statuscode 404: place not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -328,23 +331,28 @@ def place_entity_handler(place_mbid): user_review = None - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', place['mbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', place['mbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=place['mbid'], - entity_type='place', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) - else: - user_review = None + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=place['mbid'], + entity_type='place', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + + else: + user_review = [] ratings_stats, average_rating = db_rating_stats.get_stats(place_mbid, "place") @@ -392,7 +400,8 @@ def place_entity_handler(place_mbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + + if username: result['user_review'] = user_review return jsonify(**result) diff --git a/critiquebrainz/ws/recording/views.py b/critiquebrainz/ws/recording/views.py index eb2865deb..5e0924211 100644 --- a/critiquebrainz/ws/recording/views.py +++ b/critiquebrainz/ws/recording/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.musicbrainz_db import recording as db_recording from critiquebrainz.decorators import crossdomain @@ -132,6 +133,8 @@ def recording_entity_handler(recording_mbid): :statuscode 200: no error :statuscode 404: recording not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -142,23 +145,28 @@ def recording_entity_handler(recording_mbid): user_review = None - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', recording['mbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', recording['mbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=recording['mbid'], - entity_type='recording', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) - else: - user_review = None + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=recording['mbid'], + entity_type='label', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + + else: + user_review = [] ratings_stats, average_rating = db_rating_stats.get_stats(recording_mbid, "recording") @@ -206,7 +214,8 @@ def recording_entity_handler(recording_mbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + + if username: result['user_review'] = user_review return jsonify(**result) diff --git a/critiquebrainz/ws/release_group/views.py b/critiquebrainz/ws/release_group/views.py index 062e38b9c..84f1e3168 100644 --- a/critiquebrainz/ws/release_group/views.py +++ b/critiquebrainz/ws/release_group/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.musicbrainz_db import release_group as db_release_group from critiquebrainz.decorators import crossdomain @@ -145,6 +146,8 @@ def release_group_entity_handler(release_group_mbid): :statuscode 200: no error :statuscode 404: release group not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -155,23 +158,28 @@ def release_group_entity_handler(release_group_mbid): user_review = None - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', release_group['mbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', release_group['mbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=release_group['mbid'], - entity_type='release_group', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) - else: - user_review = None + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=release_group['mbid'], + entity_type='release_group', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + + else: + user_review = [] ratings_stats, average_rating = db_rating_stats.get_stats(release_group_mbid, "release_group") @@ -219,7 +227,8 @@ def release_group_entity_handler(release_group_mbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + + if username: result['user_review'] = user_review return jsonify(**result) diff --git a/critiquebrainz/ws/work/views.py b/critiquebrainz/ws/work/views.py index 510a36e85..a1a9bb0d6 100644 --- a/critiquebrainz/ws/work/views.py +++ b/critiquebrainz/ws/work/views.py @@ -1,5 +1,6 @@ from flask import Blueprint, jsonify import critiquebrainz.db.review as db_review +import critiquebrainz.db.users as db_users import critiquebrainz.db.rating_stats as db_rating_stats from critiquebrainz.frontend.external.musicbrainz_db import work as db_work from critiquebrainz.decorators import crossdomain @@ -208,6 +209,8 @@ def work_entity_handler(work_mbid): :statuscode 200: no error :statuscode 404: work not found + :query username: User's username **(optional)** + :resheader Content-Type: *application/json* """ @@ -218,23 +221,28 @@ def work_entity_handler(work_mbid): user_review = None - user_id = Parser.uuid('uri', 'user_id', optional=True) - if user_id: - user_review_cache_key = cache.gen_key('entity_api', work['mbid'], user_id, "user_review") + username = Parser.string('uri', 'username', optional=True) + if username: + user_review_cache_key = cache.gen_key('entity_api', work['mbid'], username, "user_review") user_review = cache.get(user_review_cache_key) if not user_review: - user_review, _ = db_review.list_reviews( - entity_id=work['mbid'], - entity_type='work', - user_id=user_id - ) - if user_review: - user_review = db_review.to_dict(user_review[0]) - else: - user_review = None + user = db_users.get_by_mbid(username) + if user: + user_id = user['id'] + + user_review, _ = db_review.list_reviews( + entity_id=work['mbid'], + entity_type='work', + user_id=user_id + ) + if user_review: + user_review = db_review.to_dict(user_review[0]) - cache.set(user_review_cache_key, user_review, - expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + cache.set(user_review_cache_key, user_review, + expirein=REVIEW_CACHE_TIMEOUT, namespace=REVIEW_CACHE_NAMESPACE) + + else: + user_review = [] ratings_stats, average_rating = db_rating_stats.get_stats(work_mbid, "work") @@ -282,7 +290,8 @@ def work_entity_handler(work_mbid): "top_reviews": top_reviews, "latest_reviews": latest_reviews } - if user_id: + + if username: result['user_review'] = user_review return jsonify(**result)