-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
806 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
from flask import Blueprint, jsonify | ||
import critiquebrainz.db.review as db_review | ||
import critiquebrainz.db.rating_stats as db_rating_stats | ||
from critiquebrainz.frontend.external.bookbrainz_db import author as db_author | ||
from critiquebrainz.decorators import crossdomain | ||
from critiquebrainz.ws.exceptions import NotFound | ||
from critiquebrainz.ws.parser import Parser | ||
from critiquebrainz.ws import REVIEWS_LIMIT | ||
|
||
author_bp = Blueprint('ws_author', __name__) | ||
|
||
|
||
@author_bp.route('/<uuid:author_bbid>', methods=['GET', 'OPTIONS']) | ||
@crossdomain(headers="Authorization, Content-Type") | ||
def author_entity_handler(author_bbid): | ||
"""Get list of reviews. | ||
**Request Example:** | ||
.. code-block:: bash | ||
$ curl "https://critiquebrainz.org/ws/1/author/e5c4e68b-bfce-4c77-9ca2-0f0a2d4d09f0" \\ | ||
-X GET | ||
**Response Example:** | ||
.. code-block:: json | ||
{ | ||
"author": { | ||
"area_id": null, | ||
"area_info": [], | ||
"author_type": "Person", | ||
"bbid": "e5c4e68b-bfce-4c77-9ca2-0f0a2d4d09f0", | ||
"begin_area_id": 221, | ||
"begin_day": 31, | ||
"begin_month": 7, | ||
"begin_year": 1965, | ||
"disambiguation": null, | ||
"end_area_id": null, | ||
"end_day": null, | ||
"end_month": null, | ||
"end_year": null, | ||
"ended": false, | ||
"gender": "Female", | ||
"identifier_set_id": 7401, | ||
"identifiers": [ | ||
{ | ||
"icon": "wikidata-16.png", | ||
"name": "Wikidata ID", | ||
"url": "https://www.wikidata.org/wiki/Q34660", | ||
"value": "Q34660" | ||
}, | ||
{ | ||
"icon": "viaf-16.png", | ||
"name": "VIAF", | ||
"url": "https://viaf.org/viaf/116796842", | ||
"value": "116796842" | ||
}, | ||
{ | ||
"icon": "wikidata-16.png", | ||
"name": "Wikidata ID", | ||
"url": "https://www.wikidata.org/wiki/Q1190608", | ||
"value": "Q1190608" | ||
} | ||
], | ||
"name": "J. K. Rowling", | ||
"relationship_set_id": 151715, | ||
"sort_name": "Rowling, J. K." | ||
}, | ||
"average_rating": 5.0, | ||
"latest_reviews": [ | ||
{ | ||
"created": "Tue, 16 Aug 2022 11:25:44 GMT", | ||
"edits": 0, | ||
"entity_id": "e5c4e68b-bfce-4c77-9ca2-0f0a2d4d09f0", | ||
"entity_type": "bb_author", | ||
"full_name": "Creative Commons Attribution-ShareAlike 3.0 Unported", | ||
"id": "326e702a-020f-40e7-b369-9142a7af4315", | ||
"info_url": "https://creativecommons.org/licenses/by-sa/3.0/", | ||
"is_draft": false, | ||
"is_hidden": false, | ||
"language": "en", | ||
"last_revision": { | ||
"id": 11785, | ||
"rating": 5, | ||
"review_id": "326e702a-020f-40e7-b369-9142a7af4315", | ||
"text": null, | ||
"timestamp": "Fri, 02 Sep 2022 09:54:13 GMT" | ||
}, | ||
"last_updated": "Fri, 02 Sep 2022 09:54:13 GMT", | ||
"license_id": "CC BY-SA 3.0", | ||
"popularity": 0, | ||
"published_on": "Tue, 16 Aug 2022 11:25:44 GMT", | ||
"rating": 5, | ||
"source": null, | ||
"source_url": null, | ||
"text": null, | ||
"user": { | ||
"created": "Tue, 18 Jan 2022 09:53:49 GMT", | ||
"display_name": "Ansh Goyal", | ||
"id": "11a1160e-d607-4882-8a82-e2e800f664fe", | ||
"karma": 0, | ||
"user_type": "Noob" | ||
}, | ||
"votes_negative_count": 0, | ||
"votes_positive_count": 0 | ||
} | ||
], | ||
"ratings_stats": { | ||
"1": 0, | ||
"2": 0, | ||
"3": 0, | ||
"4": 0, | ||
"5": 1 | ||
}, | ||
"reviews_count": 1, | ||
"top_reviews": [ | ||
{ | ||
"created": "Tue, 16 Aug 2022 11:25:44 GMT", | ||
"edits": 0, | ||
"entity_id": "e5c4e68b-bfce-4c77-9ca2-0f0a2d4d09f0", | ||
"entity_type": "bb_author", | ||
"full_name": "Creative Commons Attribution-ShareAlike 3.0 Unported", | ||
"id": "326e702a-020f-40e7-b369-9142a7af4315", | ||
"info_url": "https://creativecommons.org/licenses/by-sa/3.0/", | ||
"is_draft": false, | ||
"is_hidden": false, | ||
"language": "en", | ||
"last_revision": { | ||
"id": 11785, | ||
"rating": 5, | ||
"review_id": "326e702a-020f-40e7-b369-9142a7af4315", | ||
"text": null, | ||
"timestamp": "Fri, 02 Sep 2022 09:54:13 GMT" | ||
}, | ||
"last_updated": "Fri, 02 Sep 2022 09:54:13 GMT", | ||
"license_id": "CC BY-SA 3.0", | ||
"popularity": 0, | ||
"published_on": "Tue, 16 Aug 2022 11:25:44 GMT", | ||
"rating": 5, | ||
"source": null, | ||
"source_url": null, | ||
"text": null, | ||
"user": { | ||
"created": "Tue, 18 Jan 2022 09:53:49 GMT", | ||
"display_name": "Ansh Goyal", | ||
"id": "11a1160e-d607-4882-8a82-e2e800f664fe", | ||
"karma": 0, | ||
"user_type": "Noob" | ||
}, | ||
"votes_negative_count": 0, | ||
"votes_positive_count": 0 | ||
} | ||
] | ||
} | ||
:statuscode 200: no error | ||
:statuscode 404: author not found | ||
:resheader Content-Type: *application/json* | ||
""" | ||
|
||
author = db_author.get_author_by_bbid(str(author_bbid)) | ||
|
||
if not author: | ||
raise NotFound("Can't find an author with ID: {author_bbid}".format(author_bbid=author_bbid)) | ||
|
||
user_id = Parser.uuid('uri', 'user_id', optional=True) | ||
if user_id: | ||
user_review, _ = db_review.list_reviews( | ||
entity_id=author['bbid'], | ||
entity_type='bb_author', | ||
user_id=user_id | ||
) | ||
if user_review: | ||
user_review = db_review.to_dict(user_review[0]) | ||
else: | ||
user_review = None | ||
|
||
ratings_stats, average_rating = db_rating_stats.get_stats(author_bbid, "bb_author") | ||
|
||
top_reviews, reviews_count = db_review.list_reviews( | ||
entity_id=author['bbid'], | ||
entity_type='bb_author', | ||
sort='popularity', | ||
limit=REVIEWS_LIMIT, | ||
offset=0, | ||
) | ||
|
||
latest_reviews, reviews_count = db_review.list_reviews( | ||
entity_id=author['bbid'], | ||
entity_type='bb_author', | ||
sort='published_on', | ||
limit=REVIEWS_LIMIT, | ||
offset=0, | ||
) | ||
|
||
top_reviews = [db_review.to_dict(review) for review in top_reviews] | ||
latest_reviews = [db_review.to_dict(review) for review in latest_reviews] | ||
|
||
result = { | ||
"author": author, | ||
"average_rating": average_rating, | ||
"ratings_stats": ratings_stats, | ||
"reviews_count": reviews_count, | ||
"top_reviews": top_reviews, | ||
"latest_reviews": latest_reviews | ||
} | ||
if user_id: | ||
result['user_review'] = user_review | ||
|
||
return jsonify(**result) |
Empty file.
Oops, something went wrong.