Skip to content

Commit

Permalink
feat: Order Series items by ordering type
Browse files Browse the repository at this point in the history
  • Loading branch information
anshg1214 committed Aug 18, 2022
1 parent 1944ad0 commit 4fa24f5
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 25 deletions.
75 changes: 60 additions & 15 deletions critiquebrainz/frontend/external/bookbrainz_db/relationships.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def get_mapped_relationships(relation_types):
"""Get relation types mapped to their case sensitive name in bookbrainz.
relationship_type table.
Args:
relation_types (list): List of relation types.
Returns:
Expand All @@ -35,31 +35,76 @@ def get_mapped_relationships(relation_types):
return mapped_relation_types


def fetch_relationships(relationship_set_id: int, relation_types: List) -> List:
def fetch_relationships(relationship_set_id: int, relation_types: List, ordering_type=None) -> List:
"""
Fetch relationships from the database.
Args:
relationship_set_id (int): ID of the relationship set.
relation_types (list): List of relation types.
ordering_type (int): ID of the ordering type. (optional)
Returns:
List of relationships of the given types.
"""
if not relationship_set_id:
return None

relation_types = get_mapped_relationships(relation_types)
key = cache.gen_key('bb_relationship', relationship_set_id, relation_types)
key = cache.gen_key('bb_relationship', relationship_set_id, relation_types, ordering_type)
relationships = cache.get(key)
if not relationships:

query_params = {
'relationship_set_id': relationship_set_id,
'relation_types': tuple(relation_types),
}

order_by_clause = ''
if ordering_type:
order_by_clause = """
ORDER BY CASE
WHEN :ordering_type = 2
THEN (attributes->>'position')::int
ELSE (attributes->>'number')::int
END
"""
query_params['ordering_type'] = ordering_type

with db.bb_engine.connect() as connection:
result = connection.execute(sqlalchemy.text("""
SELECT rel.id as id,
reltype.label as label,
rel.source_bbid::text as source_bbid,
rel.target_bbid::text as target_bbid,
reltype.target_entity_type as target_entity_type,
reltype.source_entity_type as source_entity_type
FROM relationship_set__relationship rels
LEFT JOIN relationship rel on rels.relationship_id = rel.id
LEFT JOIN relationship_type reltype on rel.type_id = reltype.id
WHERE rels.set_id = :relationship_set_id
AND reltype.label in :relation_types
"""), {'relationship_set_id': relationship_set_id, 'relation_types': tuple(relation_types)})
WITH intermediate AS (
SELECT rel.id as id,
reltype.label as label,
rel.source_bbid::text as source_bbid,
rel.target_bbid::text as target_bbid,
reltype.target_entity_type as target_entity_type,
reltype.source_entity_type as source_entity_type,
COALESCE(
jsonb_object_agg(relatttype.name, relatttext.text_value)
FILTER (WHERE relatts IS NOT NULL),
'[]'
) as attributes
FROM relationship_set__relationship rels
LEFT JOIN relationship rel ON rels.relationship_id = rel.id
LEFT JOIN relationship_type reltype ON rel.type_id = reltype.id
LEFT JOIN relationship_attribute_set__relationship_attribute relatts ON rel.attribute_set_id = relatts.set_id
LEFT JOIN relationship_attribute relatt ON relatts.attribute_id = relatt.id
LEFT JOIN relationship_attribute_type relatttype ON relatt.attribute_type = relatttype.id
LEFT JOIN relationship_attribute_text_value relatttext ON relatts.attribute_id = relatttext.attribute_id
WHERE rels.set_id = :relationship_set_id
AND reltype.label in :relation_types
GROUP BY rel.id,
reltype.label,
rel.source_bbid,
rel.target_bbid,
reltype.target_entity_type,
reltype.source_entity_type
) SELECT *
FROM intermediate
{order_by_clause}
""".format(order_by_clause=order_by_clause)
), query_params)
relationships = result.fetchall()
relationships = [dict(relationship) for relationship in relationships]
cache.set(key, relationships, DEFAULT_CACHE_EXPIRATION)
Expand Down
7 changes: 5 additions & 2 deletions critiquebrainz/frontend/external/bookbrainz_db/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def fetch_multiple_series(bbids: List[uuid.UUID]) -> dict:
disambiguation,
identifier_set_id,
relationship_set_id,
series_ordering_type.label as series_ordering_type
series_ordering_type.label as series_ordering_type,
series_ordering_type.id as series_ordering_type_id
FROM series
LEFT JOIN series_ordering_type
ON series.ordering_type_id = series_ordering_type.id
Expand All @@ -87,7 +88,9 @@ def fetch_multiple_series(bbids: List[uuid.UUID]) -> dict:
series = dict(series)
series['bbid'] = str(series['bbid'])
series['identifiers'] = fetch_bb_external_identifiers(series['identifier_set_id'])
series['rels'] = fetch_relationships(series['relationship_set_id'], [SERIES_REL_MAP[series['series_type']]])
series['rels'] = fetch_relationships(series['relationship_set_id'],
[SERIES_REL_MAP[series['series_type']]],
series['series_ordering_type_id'])
results[series['bbid']] = series

cache.set(bb_series_key, results, DEFAULT_CACHE_EXPIRATION)
Expand Down
40 changes: 35 additions & 5 deletions critiquebrainz/frontend/templates/bb_series/entity.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ <h4>{{ _('Author') }}</h4>
<tbody>
{% for rel in series_rels_info %}
<tr>
<td>{{ loop.index }}</td>
<td>
{% if rel['number'] is defined and rel['number'] %}
{{ rel['number'] }}
{% else %}
-
{% endif %}
</td>
<td>
<a href="{{ url_for('bb_author.entity', id=rel.bbid) }}">
{{ rel.name }}
Expand Down Expand Up @@ -117,7 +123,13 @@ <h4>{{ _('Edition') }}</h4>
<tbody>
{% for rel in series_rels_info %}
<tr>
<td>{{ loop.index }}</td>
<td>
{% if rel['number'] is defined and rel['number'] %}
{{ rel['number'] }}
{% else %}
-
{% endif %}
</td>
<td>
<a href="https://bookbrainz.org/edition/{{rel.bbid}}">
{{ rel.name }}
Expand Down Expand Up @@ -161,7 +173,13 @@ <h4>{{ _('Edition Groups') }}</h4>
<tbody>
{% for rel in series_rels_info %}
<tr>
<td>{{ loop.index }}</td>
<td>
{% if rel['number'] is defined and rel['number'] %}
{{ rel['number'] }}
{% else %}
-
{% endif %}
</td>
<td>
<a href="{{ url_for('bb_edition_group.entity', id=rel.bbid) }}">
{{ rel.name }}
Expand Down Expand Up @@ -205,7 +223,13 @@ <h4>{{ _('Publisher') }}</h4>
<tbody>
{% for rel in series_rels_info %}
<tr>
<td>{{ loop.index }}</td>
<td>
{% if rel['number'] is defined and rel['number'] %}
{{ rel['number'] }}
{% else %}
-
{% endif %}
</td>
<td>
<a href="https://bookbrainz.org/publisher/{{rel.bbid}}">
{{ rel.name }}
Expand Down Expand Up @@ -245,7 +269,13 @@ <h4>{{ _('Literary Works') }}</h4>
<tbody>
{% for rel in series_rels_info %}
<tr>
<td>{{ loop.index }}</td>
<td>
{% if rel['number'] is defined and rel['number'] %}
{{ rel['number'] }}
{% else %}
-
{% endif %}
</td>
<td>
<a href="{{ url_for('bb_literary_work.entity', id=rel.bbid) }}">
{{ rel.name }}
Expand Down
20 changes: 17 additions & 3 deletions critiquebrainz/frontend/views/bb_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,28 @@ def entity(id):
rating_form = RatingEditForm(entity_id=id, entity_type='bb_series')
rating_form.rating.data = my_review['rating'] if my_review else None

rels_bbid = [rel['source_bbid'] for rel in series['rels']]
rels_bbid = []
rels_order_number = []
for rel in series['rels']:
rels_bbid.append(rel['source_bbid'])
if 'number' in rel['attributes']:
rels_order_number.append(rel['attributes']['number'])
else:
rels_order_number.append(None)

series_rels_info = bb_series.fetch_series_rels_info(series['series_type'], rels_bbid)
series_rels_info = series_rels_info.values()

series_items = []
for index, bbid in enumerate(rels_bbid):
item = series_rels_info[bbid]
number = rels_order_number[index]
item['number'] = number
series_items.append(item)

return render_template('bb_series/entity.html',
id=series['bbid'],
series=series,
series_rels_info=series_rels_info,
series_rels_info=series_items,
reviews=reviews,
my_review=my_review,
count=count,
Expand Down

0 comments on commit 4fa24f5

Please sign in to comment.