Skip to content

Commit

Permalink
feat: Allow sorting reviews by MB - BB
Browse files Browse the repository at this point in the history
  • Loading branch information
anshg1214 committed Aug 3, 2022
1 parent c1e37ff commit fbb7cc5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
26 changes: 24 additions & 2 deletions critiquebrainz/db/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@
"bb_literary_work"
]

MUSICBRAINZ_ENTITY_TYPES = [
"event",
"place",
"release_group",
"work",
"artist",
"label",
"recording"
]

BOOKBRAINZ_ENTITY_TYPES = [
"bb_edition_group",
"bb_literary_work"
]

supported_languages = []
for lang in list(pycountry.languages):
if 'iso639_1_code' in dir(lang):
Expand Down Expand Up @@ -418,8 +433,15 @@ def get_reviews_list(connection, *, inc_drafts=False, inc_hidden=False, entity_i
filter_data["entity_id"] = entity_id

if entity_type is not None:
filters.append("entity_type = :entity_type")
filter_data["entity_type"] = entity_type
if entity_type == 'musicbrainz':
filters.append("entity_type in :entity_type")
filter_data["entity_type"] = tuple(MUSICBRAINZ_ENTITY_TYPES)
elif entity_type == 'bookbrainz':
filters.append("entity_type in :entity_type")
filter_data["entity_type"] = tuple(BOOKBRAINZ_ENTITY_TYPES)
else:
filters.append("entity_type = :entity_type")
filter_data["entity_type"] = entity_type

if license_id is not None:
filters.append("license_id = :license_id")
Expand Down
35 changes: 22 additions & 13 deletions critiquebrainz/frontend/templates/review/browse.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ <h2>{{ _('Reviews') }}</h2>
<ul class="nav nav-pills">
<li role="presentation" {{ "class=active" if entity_type == None }}>
<a href="{{ url_for('review.browse', entity_type='all', sort=sort, sort_order=sort_order) }}">{{ _('All') }}</a></li>
<div class="btn-group dropdown pull-right">
<button class="btn btn-default dropdown-toggle" type="button" id="sortDropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ _('Sort by') }}: {{ sort_options[sort, sort_order] }}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="sortDropdown">
<li {% if sort == 'popularity' and sort_order == 'desc' %}class="active"{% endif %}><a href="{{ url_for('review.browse', entity_type=entity_type, sort='popularity', sort_order='desc') }}">{{ sort_options['popularity', 'desc'] }}</a></li>
<li {% if sort == 'popularity' and sort_order == 'asc' %}class="active"{% endif %}><a href="{{ url_for('review.browse', entity_type=entity_type, sort='popularity', sort_order='asc') }}">{{ sort_options['popularity', 'asc'] }}</a></li>
<li {% if sort == 'published_on' and sort_order == 'desc' %}class="active"{% endif %}><a href="{{ url_for('review.browse', entity_type=entity_type, sort='published_on', sort_order='desc') }}">{{ sort_options['published_on', 'desc'] }}</a></li>
<li {% if sort == 'published_on' and sort_order == 'asc' %}class="active"{% endif %}><a href="{{ url_for('review.browse', entity_type=entity_type, sort='published_on', sort_order='asc') }}">{{ sort_options['published_on', 'asc'] }}</a></li>
</ul>
</div>
</ul>
<ul class="nav nav-pills">
<li role="presentation" {{ "class=active" if entity_type == 'musicbrainz' }}>
<a href="{{ url_for('review.browse', entity_type='musicbrainz', sort=sort, sort_order=sort_order) }}">{{ _('MusicBrainz') }}</a></li>
<li role="presentation" {{ "class=active" if entity_type == 'release_group' }}>
<a href="{{ url_for('review.browse', entity_type='release_group', sort=sort, sort_order=sort_order) }}">{{ _('Release group') }}</a></li>
<li role="presentation" {{ "class=active" if entity_type == 'artist' }}>
Expand All @@ -23,21 +39,14 @@ <h2>{{ _('Reviews') }}</h2>
<a href="{{ url_for('review.browse', entity_type='place', sort=sort, sort_order=sort_order) }}">{{ _('Place') }}</a></li>
<li role="presentation" {{ "class=active" if entity_type == 'work' }}>
<a href="{{ url_for('review.browse', entity_type='work', sort=sort, sort_order=sort_order) }}">{{ _('Work') }}</a></li>
</ul>
<ul class="nav nav-pills">
<li role="presentation" {{ "class=active" if entity_type == 'bookbrainz' }}>
<a href="{{ url_for('review.browse', entity_type='bookbrainz', sort=sort, sort_order=sort_order) }}">{{ _('BookBrainz') }}</a></li>
<li role="presentation" {{ "class=active" if entity_type == 'bb_edition_group' }}>
<a href="{{ url_for('review.browse', entity_type='bb_edition_group', sort=sort, sort_order=sort_order) }}">{{ _('Edition Group') }}</a></li>

<div class="btn-group dropdown pull-right">
<button class="btn btn-default dropdown-toggle" type="button" id="sortDropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ _('Sort by') }}: {{ sort_options[sort, sort_order] }}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="sortDropdown">
<li {% if sort == 'popularity' and sort_order == 'desc' %}class="active"{% endif %}><a href="{{ url_for('review.browse', entity_type=entity_type, sort='popularity', sort_order='desc') }}">{{ sort_options['popularity', 'desc'] }}</a></li>
<li {% if sort == 'popularity' and sort_order == 'asc' %}class="active"{% endif %}><a href="{{ url_for('review.browse', entity_type=entity_type, sort='popularity', sort_order='asc') }}">{{ sort_options['popularity', 'asc'] }}</a></li>
<li {% if sort == 'published_on' and sort_order == 'desc' %}class="active"{% endif %}><a href="{{ url_for('review.browse', entity_type=entity_type, sort='published_on', sort_order='desc') }}">{{ sort_options['published_on', 'desc'] }}</a></li>
<li {% if sort == 'published_on' and sort_order == 'asc' %}class="active"{% endif %}><a href="{{ url_for('review.browse', entity_type=entity_type, sort='published_on', sort_order='asc') }}">{{ sort_options['published_on', 'asc'] }}</a></li>
</ul>
</div>
<li role="presentation" {{ "class=active" if entity_type == 'bb_literary_work' }}>
<a href="{{ url_for('review.browse', entity_type='bb_literary_work', sort=sort, sort_order=sort_order) }}">{{ _('Literary Work') }}</a></li>
</ul>

<div id="reviews-browse" class="row">
Expand Down
4 changes: 2 additions & 2 deletions critiquebrainz/frontend/views/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_review_or_404(review_id):

valid_browse_sort = ['published_on', 'popularity']
valid_browse_sort_order = ['asc', 'desc']

valid_entity_types = ENTITY_TYPES + ['musicbrainz', 'bookbrainz']

@review_bp.route('/')
def browse():
Expand All @@ -58,7 +58,7 @@ def browse():
('published_on', 'asc'): gettext('Oldest')
}

if entity_type and entity_type not in ENTITY_TYPES:
if entity_type and entity_type not in valid_entity_types:
raise BadRequest("Not a valid entity type.")

if sort not in valid_browse_sort:
Expand Down

0 comments on commit fbb7cc5

Please sign in to comment.