Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display Relationships on Entity Pages #459

Merged
merged 8 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions critiquebrainz/frontend/external/bookbrainz_db/edition_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,47 @@ def fetch_multiple_edition_groups(bbids: List[uuid.UUID]) -> dict:
if not results:
return {}
return results


def fetch_work_for_edition_group(bbid: uuid.UUID):
anshg1214 marked this conversation as resolved.
Show resolved Hide resolved
"""
Get works linked to an edition group using its BookBrainz ID.

Args:
bbid : BBID of the edition group.
Returns:
A tuple containing the list of work bbids linked to edition group.

"""

bb_edition_group_work = cache.gen_key('bb-edition-groups-works', bbid)
results = cache.get(bb_edition_group_work)
if not results:
with db.bb_engine.connect() as connection:
result = connection.execute(sqlalchemy.text("""
SELECT DISTINCT(rel.target_bbid::text)
FROM edition_group
INNER JOIN edition ON edition.edition_group_bbid = edition_group.bbid
INNER JOIN relationship_set__relationship rels on rels.set_id = edition.relationship_set_id
LEFT JOIN relationship rel on rels.relationship_id = rel.id
WHERE edition_group.bbid = :bbid
AND edition_group.master = 't'
AND edition_group.data_id IS NOT NULL
AND edition.master = 't'
AND edition.data_id IS NOT NULL
AND rel.type_id = 10
anshg1214 marked this conversation as resolved.
Show resolved Hide resolved
"""), {'bbid': str(bbid)})

works = result.fetchall()
work_bbids = []

for work in works:
work = dict(work)
work_bbids.append(work['target_bbid'])

results = work_bbids
cache.set(bb_edition_group_work, results, DEFAULT_CACHE_EXPIRATION)

if not results:
return []
return results
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def fetch_multiple_literary_works(bbids: List[uuid.UUID], work_type=None, limit=
for literary_work in literary_works:
literary_work = dict(literary_work)
literary_work['identifiers'] = fetch_bb_external_identifiers(literary_work['identifier_set_id'])
literary_work['rels'] = fetch_relationships(literary_work['relationship_set_id'], ['Edition'])
alastair marked this conversation as resolved.
Show resolved Hide resolved
literary_work['rels'] = fetch_relationships(literary_work['relationship_set_id'], ['Translation'])
anshg1214 marked this conversation as resolved.
Show resolved Hide resolved
results[literary_work['bbid']] = literary_work

cache.set(bb_literary_work_key, results, DEFAULT_CACHE_EXPIRATION)
Expand Down
47 changes: 46 additions & 1 deletion critiquebrainz/frontend/templates/bb_edition_group/entity.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends 'base.html' %}
{% from 'macros.html' import show_avg_rating, entity_rate_form, show_review_buttons with context %}
{% from 'macros.html' import show_avg_rating, entity_rate_form, show_review_buttons, display_pagination with context %}
{% from 'common.html' import rating_script with context %}

{% block title %}{{ edition_group.name }} - CritiqueBrainz{% endblock %}
Expand Down Expand Up @@ -60,6 +60,51 @@ <h4 style="margin-bottom:0;">{{ _('Reviews') }}</h4>
{% endif %}
</ul>
{% endif %}

{% if works is defined and works %}
<h4>{{ _('Literary Works') }}</h4>
anshg1214 marked this conversation as resolved.
Show resolved Hide resolved
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>{{ _('Name') }}</th>
<th>{{ _('Languages') }}</th>
<th>{{ _('Type') }}</th>
</tr>
</thead>
<tbody>
{% for work in works %}
anshg1214 marked this conversation as resolved.
Show resolved Hide resolved
<tr>
<td>
<a href="{{ url_for('bb_literary_work.entity', id=work.bbid) }}">
{{ work.name }}
</a>
</td>
<td>
{% if work['languages'] is defined and work['languages'] %}
{% if work["languages"]|length > 1 %}
{{ work['languages'][0] }} + {{ work["languages"]|length - 1 }} more
{% else %}
{{ work['languages'][0] }}
{% endif %}
{% else %}
-
{% endif %}
</td>
<td>
{% if work['work_type'] is defined and work['work_type'] %}
{{ work.work_type }}
{% else %}
-
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>

{{ display_pagination(page, works_count, works_limit, 'bb_edition_group.entity', {'id': id}) }}

{% endif %}
</div>

<div class="col-md-3">
Expand Down
48 changes: 47 additions & 1 deletion critiquebrainz/frontend/templates/bb_literary_work/entity.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends 'base.html' %}
{% from 'macros.html' import show_avg_rating, entity_rate_form, show_review_buttons with context %}
{% from 'macros.html' import show_avg_rating, entity_rate_form, show_review_buttons, display_pagination with context %}
{% from 'common.html' import rating_script with context %}

{% block title %}{{ literary_work.name }} - CritiqueBrainz{% endblock %}
Expand Down Expand Up @@ -60,6 +60,52 @@ <h4 style="margin-bottom:0;">{{ _('Reviews') }}</h4>
{% endif %}
</ul>
{% endif %}

{% if work_rels_info is defined and work_rels_info %}
anshg1214 marked this conversation as resolved.
Show resolved Hide resolved
<h4>{{ _('Translations') }}</h4>
anshg1214 marked this conversation as resolved.
Show resolved Hide resolved
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>{{ _('Name') }}</th>
<th>{{ _('Languages') }}</th>
<th>{{ _('Type') }}</th>
</tr>
</thead>
<tbody>
{% for work in work_rels_info %}
anshg1214 marked this conversation as resolved.
Show resolved Hide resolved
<tr>
<td>
<a href="{{ url_for('bb_literary_work.entity', id=work.bbid) }}">
{{ work.name }}
</a>
</td>
<td>
{% if work['languages'] is defined and work['languages'] %}
{% if work["languages"]|length > 1 %}
{{ work['languages'][0] }} + {{ work["languages"]|length - 1 }} more
{% else %}
{{ work['languages'][0] }}
{% endif %}
{% else %}
-
{% endif %}
</td>
<td>
{% if work['work_type'] is defined and work['work_type'] %}
{{ work.work_type }}
{% else %}
-
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>

{{ display_pagination(page, work_rels_count, work_rels_limit, 'bb_literary_work.entity', {'id': id}) }}

{% endif %}

</div>

<div class="col-md-3">
Expand Down
1 change: 1 addition & 0 deletions critiquebrainz/frontend/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SERIES_REVIEWS_LIMIT = 10
WORK_REVIEWS_LIMIT = 5
RECORDING_REVIEWS_LIMIT = 10
BROWSE_LITERARY_WORK_LIMIT = 10
BROWSE_RELEASE_GROUPS_LIMIT = 20
BROWSE_EVENTS_LIMIT = 15
BROWSE_RECORDING_LIMIT = 10
Expand Down
29 changes: 27 additions & 2 deletions critiquebrainz/frontend/views/bb_edition_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import critiquebrainz.frontend.external.bookbrainz_db.edition_group as bb_edition_group
import critiquebrainz.frontend.external.bookbrainz_db.redirects as bb_redirects
from critiquebrainz.frontend.forms.rate import RatingEditForm
from critiquebrainz.frontend.views import get_avg_rating, EDITION_GROUP_REVIEWS_LIMIT

from critiquebrainz.frontend.views import get_avg_rating, EDITION_GROUP_REVIEWS_LIMIT, BROWSE_LITERARY_WORK_LIMIT
import critiquebrainz.frontend.external.bookbrainz_db.literary_work as bb_literary_work

bb_edition_group_bp = Blueprint('bb_edition_group', __name__)

Expand Down Expand Up @@ -56,9 +56,34 @@ def entity(id):
rating_form = RatingEditForm(entity_id=id, entity_type='bb_edition_group')
rating_form.rating.data = my_review['rating'] if my_review else None

page = request.args.get('page')
if page:
try:
page = int(page)
except ValueError:
raise BadRequest("Invalid page number!")
else:
page = 1

if page < 1:
return redirect(url_for('bb_edition_group.entity', id=id))

work_bbids = bb_edition_group.fetch_work_for_edition_group(bbid=id)
works_count = len(work_bbids)

works_info = bb_literary_work.fetch_multiple_literary_works(
bbids=work_bbids,
limit=BROWSE_LITERARY_WORK_LIMIT,
offset=(page - 1) * BROWSE_LITERARY_WORK_LIMIT,
).values()

return render_template('bb_edition_group/entity.html',
id=edition_group['bbid'],
edition_group=edition_group,
works=works_info,
works_count=works_count,
page=page,
works_limit=BROWSE_LITERARY_WORK_LIMIT,
reviews=reviews,
my_review=my_review,
count=count,
Expand Down
31 changes: 30 additions & 1 deletion critiquebrainz/frontend/views/bb_literary_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import critiquebrainz.frontend.external.bookbrainz_db.literary_work as bb_literary_work
import critiquebrainz.frontend.external.bookbrainz_db.redirects as bb_redirects
from critiquebrainz.frontend.forms.rate import RatingEditForm
from critiquebrainz.frontend.views import get_avg_rating, LITERARY_WORK_REVIEWS_LIMIT
from critiquebrainz.frontend.views import get_avg_rating, LITERARY_WORK_REVIEWS_LIMIT, BROWSE_LITERARY_WORK_LIMIT

bb_literary_work_bp = Blueprint('bb_literary_work', __name__)

Expand Down Expand Up @@ -54,9 +54,38 @@ def entity(id):
rating_form = RatingEditForm(entity_id=id, entity_type='bb_literary_work')
rating_form.rating.data = my_review['rating'] if my_review else None

page = request.args.get('page')
if page:
try:
page = int(page)
except ValueError:
raise BadRequest("Invalid page number!")
else:
page = 1

if page < 1:
return redirect(url_for('bb_literary_work.entity', id=id))

if literary_work['rels']:
work_rels_bbid = [rel['source_bbid'] for rel in literary_work['rels']]
work_rels_count = len(work_rels_bbid)
work_rels_info = bb_literary_work.fetch_multiple_literary_works(
bbids=work_rels_bbid,
limit=BROWSE_LITERARY_WORK_LIMIT,
offset=(page - 1) * BROWSE_LITERARY_WORK_LIMIT
).values()
else:
work_rels_bbid = []
work_rels_count = 0
work_rels_info = []

return render_template('bb_literary_work/entity.html',
id=literary_work['bbid'],
literary_work=literary_work,
work_rels_info=work_rels_info,
work_rels_count=work_rels_count,
work_rels_limit=BROWSE_LITERARY_WORK_LIMIT,
page=page,
reviews=reviews,
my_review=my_review,
count=count,
Expand Down