Skip to content

Commit

Permalink
feat: Unify mappings for entity names
Browse files Browse the repository at this point in the history
  • Loading branch information
anshg1214 committed Aug 22, 2022
1 parent 6fbc552 commit d6f6708
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
42 changes: 22 additions & 20 deletions critiquebrainz/db/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sqlalchemy
from brainzutils import cache
from flask import current_app
from flask_babel import gettext

from critiquebrainz import db
from critiquebrainz.db import (exceptions as db_exceptions,
Expand All @@ -21,24 +22,25 @@
DEFAULT_LANG = "en"

#: list of allowed entity_type's for writing/querying a review
MUSICBRAINZ_ENTITY_TYPES = [
"event",
"place",
"release_group",
"work",
"artist",
"label",
"recording"
]

BOOKBRAINZ_ENTITY_TYPES = [
"bb_edition_group",
"bb_literary_work",
"bb_author",
"bb_series",
]

ENTITY_TYPES = MUSICBRAINZ_ENTITY_TYPES + BOOKBRAINZ_ENTITY_TYPES
MUSICBRAINZ_ENTITY_TYPES = {
"event": gettext("Event"),
"place": gettext("Place"),
"release_group": gettext("Release Group"),
"work": gettext("Work"),
"artist": gettext("Artist"),
"label": gettext("Label"),
"recording": gettext("Recording"),
}

BOOKBRAINZ_ENTITY_TYPES = {
"bb_edition_group": gettext("Edition Group"),
"bb_literary_work": gettext("Literary Work"),
"bb_author": gettext("Author"),
"bb_series": gettext("Series"),
}

ENTITY_TYPES_MAPPING = {**MUSICBRAINZ_ENTITY_TYPES, **BOOKBRAINZ_ENTITY_TYPES}
ENTITY_TYPES = list(ENTITY_TYPES_MAPPING.keys())

supported_languages = []
for lang in list(pycountry.languages):
Expand Down Expand Up @@ -427,10 +429,10 @@ def get_reviews_list(connection, *, inc_drafts=False, inc_hidden=False, entity_i
if entity_type is not None:
if entity_type == 'musicbrainz':
filters.append("entity_type in :entity_type")
filter_data["entity_type"] = tuple(MUSICBRAINZ_ENTITY_TYPES)
filter_data["entity_type"] = tuple(MUSICBRAINZ_ENTITY_TYPES.keys())
elif entity_type == 'bookbrainz':
filters.append("entity_type in :entity_type")
filter_data["entity_type"] = tuple(BOOKBRAINZ_ENTITY_TYPES)
filter_data["entity_type"] = tuple(BOOKBRAINZ_ENTITY_TYPES.keys())
else:
filters.append("entity_type = :entity_type")
filter_data["entity_type"] = entity_type
Expand Down
16 changes: 2 additions & 14 deletions critiquebrainz/frontend/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,8 @@ def reviews(user_id):
# happens so infrequently that we don't bother to back-fill it.
retrieved_entity_mbids = entities_info.keys()
reviews = [r for r in reviews if str(r["entity_id"]) in retrieved_entity_mbids]

entity_names = {
'artist': gettext('Artist'),
'release_group': gettext('Release Group'),
'label': gettext('Label'),
'recording': gettext('Recording'),
'place': gettext('Place'),
'event': gettext('Event'),
'work': gettext('Work'),
'bb_edition_group': gettext('Edition Group'),
'bb_literary_work': gettext('Literary Work'),
'bb_author': gettext('Author'),
'bb_series': gettext('Series'),
}

entity_names = db_review.ENTITY_TYPES_MAPPING


return render_template('user/reviews.html', section='reviews', user=user,
Expand Down
14 changes: 6 additions & 8 deletions critiquebrainz/ws/review/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ def review_list_handler():

reviews = [db_review.to_dict(p) for p in reviews]


if include_metadata == 'true':
entities = [(str(review["entity_id"]), review["entity_type"]) for review in reviews]
entities_info = mbstore.get_multiple_entities(entities)
Expand All @@ -440,11 +439,10 @@ def review_list_handler():
for review in reviews:
review[review['entity_type']] = entities_info[str(review["entity_id"])]


if include_avg_rating:
if reviews and not entity_type:
entity_type = reviews[0]["entity_type"] if reviews else None
entity_type = reviews[0]["entity_type"] if reviews else None

if entity_type:
avg_rating_data = get_avg_rating(entity_id, entity_type)
if avg_rating_data:
Expand Down Expand Up @@ -481,10 +479,9 @@ def review_list_handler():
expirein=REVIEW_CACHE_TIMEOUT,
namespace=REVIEW_CACHE_NAMESPACE)


result = {"limit": limit, "offset": offset, "count": count, "reviews": reviews}
result = {"limit": limit, "offset": offset, "count": count, "reviews": reviews}
if include_avg_rating:
result["average_rating"] = avg_rating_data
result["average_rating"] = avg_rating_data
return jsonify(**result)


Expand Down Expand Up @@ -527,7 +524,8 @@ def fetch_params():
if language and language not in supported_languages:
raise InvalidRequest(desc='Unsupported language')
if db_review.list_reviews(user_id=user.id, entity_id=entity_id)[1]:
raise InvalidRequest(desc='You have already published a review for this {entity_name}'.format(entity_name=entity_name_map[entity_type]))
raise InvalidRequest(desc='You have already published a review for this {entity_name}'.format(
entity_name=db_review.ENTITY_TYPES_MAPPING[entity_type]))
return entity_id, entity_type, text, rating, license_choice, language, is_draft

if user.is_review_limit_exceeded:
Expand Down

0 comments on commit d6f6708

Please sign in to comment.