Skip to content

Commit

Permalink
Merge pull request #266 from spellew/work-entity-support
Browse files Browse the repository at this point in the history
CB-270: Implemented the reviewal of works
  • Loading branch information
paramsingh committed Aug 29, 2019
2 parents 6bfb8d4 + 8d72578 commit 595110d
Show file tree
Hide file tree
Showing 25 changed files with 478 additions and 8 deletions.
1 change: 1 addition & 0 deletions admin/schema_changes/19.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TYPE entity_types ADD VALUE 'work' AFTER 'recording';
1 change: 1 addition & 0 deletions admin/sql/create_types.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CREATE TYPE entity_types AS ENUM (
'release_group',
'event',
'place',
'work',
'artist',
'label'
);
1 change: 1 addition & 0 deletions critiquebrainz/db/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"event",
"place",
"release_group",
"work",
"artist",
"label",
]
Expand Down
5 changes: 4 additions & 1 deletion critiquebrainz/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ def create_app(debug=None, config_path=None):
# TODO (code-master5): disabled no-member warnings just as a workaround to deal with failing tests till the
# issue [https://github.com/PyCQA/pylint/issues/2563] with pylint is resolved
app.jinja_env.add_extension('jinja2.ext.do')
from critiquebrainz.utils import reformat_date, reformat_datetime, track_length, parameterize
from critiquebrainz.utils import reformat_date, reformat_datetime, track_length, track_length_ms, parameterize
from critiquebrainz.frontend.external.musicbrainz_db.entities import get_entity_by_id
app.jinja_env.filters['date'] = reformat_date
app.jinja_env.filters['datetime'] = reformat_datetime
app.jinja_env.filters['track_length'] = track_length
app.jinja_env.filters['track_length_ms'] = track_length_ms
app.jinja_env.filters['parameterize'] = parameterize
app.jinja_env.filters['entity_details'] = get_entity_by_id
from flask_babel import Locale, get_locale
Expand All @@ -134,6 +135,7 @@ def create_app(debug=None, config_path=None):
from critiquebrainz.frontend.views.label import label_bp
from critiquebrainz.frontend.views.release_group import release_group_bp
from critiquebrainz.frontend.views.release import release_bp
from critiquebrainz.frontend.views.work import work_bp
from critiquebrainz.frontend.views.event import event_bp
from critiquebrainz.frontend.views.mapping import mapping_bp
from critiquebrainz.frontend.views.user import user_bp
Expand All @@ -156,6 +158,7 @@ def create_app(debug=None, config_path=None):
app.register_blueprint(label_bp, url_prefix='/label')
app.register_blueprint(release_group_bp, url_prefix='/release-group')
app.register_blueprint(release_bp, url_prefix='/release')
app.register_blueprint(work_bp, url_prefix='/work')
app.register_blueprint(event_bp, url_prefix='/event')
app.register_blueprint(place_bp, url_prefix='/place')
app.register_blueprint(mapping_bp, url_prefix='/mapping')
Expand Down
6 changes: 6 additions & 0 deletions critiquebrainz/frontend/external/musicbrainz.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def search_places(query='', limit=None, offset=None):
return api_resp.get('place-count'), api_resp.get('place-list')


def search_works(query='', limit=None, offset=None):
"""Search for works."""
api_resp = musicbrainzngs.search_works(query=query, limit=limit, offset=offset)
return api_resp.get('work-count'), api_resp.get('work-list')


def search_labels(query='', limit=None, offset=None):
"""Search for labels."""
api_resp = musicbrainzngs.search_labels(query=query, limit=limit, offset=offset)
Expand Down
8 changes: 8 additions & 0 deletions critiquebrainz/frontend/external/musicbrainz_db/entities.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from brainzutils.musicbrainz_db.work import fetch_multiple_works
from brainzutils.musicbrainz_db.artist import fetch_multiple_artists
from brainzutils.musicbrainz_db.label import fetch_multiple_labels
from brainzutils.musicbrainz_db.place import fetch_multiple_places
Expand All @@ -8,6 +9,7 @@
from critiquebrainz.frontend.external.musicbrainz_db.event import get_event_by_id
from critiquebrainz.frontend.external.musicbrainz_db.label import get_label_by_id
from critiquebrainz.frontend.external.musicbrainz_db.artist import get_artist_by_id
from critiquebrainz.frontend.external.musicbrainz_db.work import get_work_by_id


def get_multiple_entities(entities):
Expand All @@ -31,6 +33,7 @@ def get_multiple_entities(entities):
label_mbids = [entity[0] for entity in filter(lambda entity: entity[1] == 'label', entities)]
place_mbids = [entity[0] for entity in filter(lambda entity: entity[1] == 'place', entities)]
event_mbids = [entity[0] for entity in filter(lambda entity: entity[1] == 'event', entities)]
work_mbids = [entity[0] for entity in filter(lambda entity: entity[1] == 'work', entities)]
entities_info.update(fetch_multiple_release_groups(
release_group_mbids,
includes=['artists'],
Expand All @@ -47,6 +50,9 @@ def get_multiple_entities(entities):
entities_info.update(fetch_multiple_events(
event_mbids,
))
entities_info.update(fetch_multiple_works(
work_mbids,
))
return entities_info


Expand All @@ -62,4 +68,6 @@ def get_entity_by_id(id, type='release_group'):
entity = get_place_by_id(str(id))
elif type == 'event':
entity = get_event_by_id(str(id))
elif type == 'work':
entity = get_work_by_id(str(id))
return entity
22 changes: 22 additions & 0 deletions critiquebrainz/frontend/external/musicbrainz_db/work.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from brainzutils import cache
from brainzutils.musicbrainz_db.work import fetch_multiple_works
from critiquebrainz.frontend.external.musicbrainz_db import DEFAULT_CACHE_EXPIRATION


def get_work_by_id(mbid):
"""Get work with MusicBrainz ID.
Args:
mbid (uuid): MBID(gid) of the work.
Returns:
Dictionary containing the work information
"""
key = cache.gen_key(mbid)
work = cache.get(key)
if not work:
work = fetch_multiple_works(
[mbid],
includes=['artist-rels', 'recording-rels'],
).get(mbid)
cache.set(key=key, val=work, time=DEFAULT_CACHE_EXPIRATION)
return work
21 changes: 17 additions & 4 deletions critiquebrainz/frontend/static/styles/main.less
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@artist-color: @blue;
@event-color: @green;
@place-color: @yellow;
@work-color: @blue;
@label-color: @blue;

body {
Expand Down Expand Up @@ -126,11 +127,17 @@ ul.sharing {
&.release-group {
background-color: fade(@rg-color, 70%);
}
&.place {
background-color: fade(@place-color, 70%);
}
&.event {
background-color: fade(@event-color, 70%);
}
&.artist {
background-color: fade(@artist-color, 70%);
}
&.place {
background-color: fade(@place-color, 70%);
&.work {
background-color: fade(@work-color, 70%);
}
&.label {
background-color: fade(@label-color, 70%);
Expand Down Expand Up @@ -491,11 +498,17 @@ a#edit-review { margin-top: 20px; }
&.release-group {
background-color: fade(@rg-color, 70%);
}
&.place {
background-color: fade(@place-color, 70%);
}
&.event {
background-color: fade(@event-color, 70%);
}
&.artist {
background-color: fade(@artist-color, 70%);
}
&.place {
background-color: fade(@place-color, 70%);
&.work {
background-color: fade(@work-color, 70%);
}
&.label {
background-color: fade(@label-color, 70%);
Expand Down
2 changes: 2 additions & 0 deletions critiquebrainz/frontend/templates/entity_review.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
{{ _('%(event)s', event = '<b>'|safe + entity.name | default(_('[Unknown event]')) + '</b>'|safe) }}
{% elif review.entity_type == 'place' %}
{{ _('%(place)s', place = '<b>'|safe + entity.name | default(_('[Unknown place]')) + '</b>'|safe) }}
{% elif review.entity_type == 'work' %}
{{ _('%(work)s', work = '<b>'|safe + entity.name | default(_('[Unknown work]')) + '</b>'|safe) }}
{% endif %}
</a>
7 changes: 7 additions & 0 deletions critiquebrainz/frontend/templates/macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
{{ _('Place') }}
</span>
{% endif %}
{% elif entity_type == 'work' %}
<img src="{{ get_static_path('images/placeholder_place.svg') }}" {{ attributes }} />
{% if overlay_type %}
<span class="entity-type work">
{{ _('Work') }}
</span>
{% endif %}
{% elif entity_type == 'artist' %}
<img src="{{ get_static_path('images/placeholder_place.svg') }}" {{ attributes }} />
{% if overlay_type %}
Expand Down
1 change: 1 addition & 0 deletions critiquebrainz/frontend/templates/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<div class="form-group">
<select id="type-selector" name="type" class="form-control input-sm">
<option value="artist">{{ _('Artist') }}</option>
<option value="work">{{ _('Work') }}</option>
<option value="label">{{ _('Label') }}</option>
<option value="event">{{ _('Event') }}</option>
<option value="place">{{ _('Place') }}</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ <h4>{{ _('Tracklist') }}</h4>
</td>
<td>
{% if track.length %}
{{ track.length | track_length }}
{{ track.length | track_length_ms }}
{% else %}
-
{% endif %}
Expand Down
2 changes: 2 additions & 0 deletions critiquebrainz/frontend/templates/review/browse.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ <h2>{{ _('Reviews') }}</h2>
<a href="{{ url_for('review.browse', entity_type='event') }}">{{ _('Event') }}</a></li>
<li role="presentation" {{ "class=active" if entity_type == 'place' }}>
<a href="{{ url_for('review.browse', entity_type='place') }}">{{ _('Place') }}</a></li>
<li role="presentation" {{ "class=active" if entity_type == 'work' }}>
<a href="{{ url_for('review.browse', entity_type='work') }}">{{ _('Work') }}</a></li>
</ul>

<div id="reviews-browse" class="row">
Expand Down
24 changes: 24 additions & 0 deletions critiquebrainz/frontend/templates/review/entity/work.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends 'review/entity/base.html' %}

{% set work = review.entity_id | entity_details(type='work') %}

{% block title %}
{% set work_title = work.name | default(_('[Unknown work]')) %}
{{ _('Review of "%(work)s" by %(user)s', work=work_title, user=review.user.display_name) }} - CritiqueBrainz
{% endblock %}

{% block entity_title %}
<h2 id="title">
{% if work %}
{% set work_name = '<a href="%s">' | safe % url_for('work.entity', id=review.entity_id) ~ work.name ~ '</a>'|safe %}
{% else %}
{% set work_name = _('[Unknown work]') %}
{% endif %}

{{ _('%(work)s', work=work_name) }}

{% if work['life-span'] %}
<small>{{ work['life-span']['begin'][:4] }}</small>
{% endif %}
</h2>
{% endblock %}
28 changes: 28 additions & 0 deletions critiquebrainz/frontend/templates/review/modify/work.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% if entity is not defined %}
{% set entity = review.entity_id | entity_details(type=entity_type) %}
{% endif %}
<div class="col-md-12">
<dl class="dl-horizontal">
<dt>{{ _('Work') }}</dt>
<dd>
{{ entity['name'] | default(_('[Unknown work]')) }}
</dd>
<dt>{{ _('Type') }}</dt>
<dd>{{ entity['type'] or '-' }}</dd>
<dt>{{ _('Artists') }}</dt>
<dd>
{% if entity['artist-rels'] %}
{{ entity['artist-rels'][0]['artist']['name'] or '-' }}
{% set count = entity['artist-rels'] | length %}
{% if count > 1 %}
+ {{ count - 1 }} {{ _("more") }}
{% endif %}
{% else %}
-
{% endif %}
</dd>
{% block more_info %}
{# Information like creation date, votes etc. #}
{% endblock %}
</dl>
</div>
8 changes: 8 additions & 0 deletions critiquebrainz/frontend/templates/search/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ <h3>{{ _('Search') }}</h3>
<option value="event" {% if type=="event" %}selected="selected"{% endif %}>{{ _('Event') }}</option>
<option value="label" {% if type=="label" %}selected="selected"{% endif %}>{{ _('Label') }}</option>
<option value="place" {% if type=="place" %}selected="selected"{% endif %}>{{ _('Place') }}</option>
<option value="work" {% if type=="work" %}selected="selected"{% endif %}>{{ _('Work') }}</option>
</select>
</div>
</div>
Expand Down Expand Up @@ -77,6 +78,13 @@ <h3>{{ _('Search') }}</h3>
<th>{{ _('Type') }}</th>
<th>{{ _('Country') }}</th>
</tr>
{% elif type=="work" %}
<tr>
<th>{{ _('Title') }}</th>
<th>{{ _('Artists') }}</th>
<th>{{ _('Type') }}</th>
<th>{{ _('Language') }}</th>
</tr>
{% endif %}
</thead>
<tbody id="results">{% include 'search/results.html' %}</tbody>
Expand Down
24 changes: 24 additions & 0 deletions critiquebrainz/frontend/templates/search/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@
</td>
</tr>

{% elif type=="work" %}
<tr>
<td>
<a href="{{ url_for('work.entity', id=result.id) }}">{{ result['title'] }}</a>
</td>
<td>
{% if result['artist-relation-list'] %}
<a href="{{ url_for('artist.entity', mbid=result['artist-relation-list'][0].artist.id) }}">{{ result['artist-relation-list'][0]['artist']['name'] or '-' }}</a>
{% set count = result['artist-relation-list'] | length %}
{% if count > 1 %}
+ {{ count - 1 }} {{ _("more") }}
{% endif %}
{% else %}
-
{% endif %}
</td>
<td>
{{ result['type'] or '-' }}
</td>
<td>
{{ result['language'] or '-' }}
</td>
</tr>

{% elif type=="label" %}
<tr>
<td>
Expand Down
32 changes: 32 additions & 0 deletions critiquebrainz/frontend/templates/search/selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<li {% if type=="place" %}class="active"{% endif %}>
<a href="#place" data-toggle="tab">Place</a>
</li>
<li {% if type=="work" %}class="active"{% endif %}>
<a href="#work" data-toggle="tab">{{ _('Work') }}</a>
</li>
<li {% if type=="label" %}class="active"{% endif %}>
<a href="#label" data-toggle="tab">{{ _('Label') }}</a>
</li>
Expand Down Expand Up @@ -104,6 +107,26 @@ <h3>{{ _('Place selection') }}</h3>
</form>
</div>

<div id="work" class="tab-pane {% if type=="work" %}active{% endif %}">
<h3>{{ _('Work selection') }}</h3>
<form method="GET" class="form-horizontal" role="form">
<div class="form-group">
<label for="input-release-group" class="col-sm-2 control-label">{{ _('Work') }}</label>
<div class="col-sm-4">
<input id="input-release-group" class="form-control" name="work" type="text"
value="{{ request.args.get('work', default='') }}">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">{{ _('Search') }}</button>
</div>
</div>
<input type="hidden" name="next" value="{{ next }}" />
<input type="hidden" name="type" value="work" />
</form>
</div>

<div id="label" class="tab-pane {% if type=="label" %}active{% endif %}">
<h3>{{ _('Label selection') }}</h3>
<form method="GET" class="form-horizontal" role="form">
Expand Down Expand Up @@ -132,6 +155,7 @@ <h3>{{ _('Label selection') }}</h3>
or request.args.get('release_group', default=False)
or request.args.get('event', default=False)
or request.args.get('place', default=False)
or request.args.get('work', default=False)
or request.args.get('label', default=False)
%}
<hr />
Expand Down Expand Up @@ -177,6 +201,14 @@ <h3>{{ _('Label selection') }}</h3>
<th>{{ _('Country') }}</th>
<th></th>
</tr>
{% elif type=="work" %}
<tr>
<th>{{ _('Title') }}</th>
<th>{{ _('Artists') }}</th>
<th>{{ _('Type') }}</th>
<th>{{ _('Language') }}</th>
<th></th>
</tr>
{% endif %}
</thead>
<tbody id="results">{% include 'search/selector_results.html' %}</tbody>
Expand Down
Loading

0 comments on commit 595110d

Please sign in to comment.