예제 #1
0
 def fetch_params(review):
     try:
         text = Parser.string('json', 'text', min=REVIEW_TEXT_MIN_LENGTH, max=REVIEW_TEXT_MAX_LENGTH)
     except MissingDataError:
         text = review['text']
     try:
         rating = Parser.int('json', 'rating', min=REVIEW_RATING_MIN, max=REVIEW_RATING_MAX)
     except MissingDataError:
         rating = review['rating']
     if text is None and rating is None:
         raise InvalidRequest(desc='Review must have either text or rating')
     return text, rating
예제 #2
0
 def fetch_params(review):
     try:
         text = Parser.string('json', 'text', min=REVIEW_TEXT_MIN_LENGTH, max=REVIEW_TEXT_MAX_LENGTH)
     except MissingDataError:
         text = review['text']
     try:
         rating = Parser.int('json', 'rating', min=REVIEW_RATING_MIN, max=REVIEW_RATING_MAX)
     except MissingDataError:
         rating = review['rating']
     if text is None and rating is None:
         raise InvalidRequest(desc='Review must have either text or rating')
     return text, rating
예제 #3
0
def user_entity_handler(user_id):
    """Get profile of a user with a specified UUID.

    **Request Example:**

    .. code-block:: bash

        $ curl https://critiquebrainz.org/ws/1/user/ae5a003f-292c-497e-afbd-8076e9626f2e \\
               -X GET

    **Response Example:**

    .. code-block:: json

        {
          "user": {
            "created": "Wed, 07 May 2014 14:47:03 GMT",
            "display_name": "User's Name comes here",
            "id": "ae5a003f-292c-497e-afbd-8076e9626f2e",
            "karma": 0,
            "user_type": "Noob"
          }
        }

    :resheader Content-Type: *application/json*
    """
    user = db_users.get_by_id(str(user_id))
    if not user:
        raise NotFound(
            "Can't find a user with ID: {user_id}".format(user_id=user_id))
    inc = Parser.list('uri', 'inc', User.allowed_includes, optional=True) or []
    return jsonify(user=User(user).to_dict(inc))
예제 #4
0
def user_me_handler(user):
    """Get your profile information.

    **Request Example:**

    .. code-block:: bash

        $ curl "https://critiquebrainz.org/ws/1/user/me" \\
               -X GET \\
               -H "Authorization: Bearer <access token>"

    **Response Example:**

    .. code-block:: json

        {
          "user": {
            "display_name": "your_display_name",
            "created": "Fri, 02 Dec 2016 19:02:47 GMT",
            "show_gravatar": true,
            "user_type": "Noob",
            "email": "your_email_id",
            "karma": 0,
            "musicbrainz_username": "******",
            "id": "your-unique-user-id",
            "avatar": "https://gravatar.com/your-gravatar-link"
          }
        }

    :query inc: includes

    :resheader Content-Type: *application/json*
    """
    inc = Parser.list('uri', 'inc', User.allowed_includes, optional=True) or []
    return jsonify(user=user.to_dict(inc, confidential=True))
예제 #5
0
def user_entity_handler(user_id):
    """Get profile of a user with a specified UUID.

    **Request Example:**

    .. code-block:: bash

        $ curl https://critiquebrainz.org/ws/1/user/ae5a003f-292c-497e-afbd-8076e9626f2e \\
               -X GET

    **Response Example:**

    .. code-block:: json

        {
          "user": {
            "created": "Wed, 07 May 2014 14:47:03 GMT",
            "display_name": "User's Name comes here",
            "id": "ae5a003f-292c-497e-afbd-8076e9626f2e",
            "karma": 0,
            "user_type": "Noob"
          }
        }

    :resheader Content-Type: *application/json*
    """
    user = db_users.get_by_id(str(user_id))
    if not user:
        raise NotFound("Can't find a user with ID: {user_id}".format(user_id=user_id))
    inc = Parser.list('uri', 'inc', User.allowed_includes, optional=True) or []
    return jsonify(user=User(user).to_dict(inc))
예제 #6
0
def user_me_handler(user):
    """Get your profile information.

    **Request Example:**

    .. code-block:: bash

        $ curl "https://critiquebrainz.org/ws/1/user/me" \\
               -X GET \\
               -H "Authorization: Bearer <access token>"

    **Response Example:**

    .. code-block:: json

        {
          "user": {
            "display_name": "your_display_name",
            "created": "Fri, 02 Dec 2016 19:02:47 GMT",
            "show_gravatar": true,
            "user_type": "Noob",
            "email": "your_email_id",
            "karma": 0,
            "musicbrainz_username": "******",
            "id": "your-unique-user-id",
            "avatar": "https:\/\/gravatar.com\/your-gravatar-link"
          }
        }

    :query inc: includes

    :resheader Content-Type: *application/json*
    """
    inc = Parser.list('uri', 'inc', User.allowed_includes, optional=True) or []
    return jsonify(user=user.to_dict(inc, confidential=True))
예제 #7
0
def user_entity_handler(user_id):
    """Get profile of a user with a specified UUID.

    :resheader Content-Type: *application/json*
    """
    user = User.query.get_or_404(str(user_id))
    inc = Parser.list('uri', 'inc', User.allowed_includes, optional=True) or []
    return jsonify(user=user.to_dict(inc))
예제 #8
0
def review_list_handler():
    """Get list of reviews.

    :json uuid entity_id: UUID of the release group that is being reviewed
    :json string entity_type: One of the supported reviewable entities. 'release_group' or 'event' etc. **(optional)**
    :query user_id: user's UUID **(optional)**
    :query sort: ``rating`` or ``created`` **(optional)**
    :query limit: results limit, min is 0, max is 50, default is 50 **(optional)**
    :query offset: result offset, default is 0 **(optional)**
    :query language: language code (ISO 639-1) **(optional)**

    :resheader Content-Type: *application/json*
    """
    # TODO: This checking is added to keep old clients working and needs to be removed.
    release_group = Parser.uuid('uri', 'release_group', optional=True)
    if release_group:
        entity_id = release_group
        entity_type = 'release_group'
    else:        
        entity_id = Parser.uuid('uri', 'entity_id', optional=True)
        entity_type = Parser.string('uri', 'entity_type', valid_values=ENTITY_TYPES, optional=True)

    user_id = Parser.uuid('uri', 'user_id', optional=True)
    sort = Parser.string('uri', 'sort', valid_values=['rating', 'created'], optional=True)
    limit = Parser.int('uri', 'limit', min=1, max=50, optional=True) or 50
    offset = Parser.int('uri', 'offset', optional=True) or 0
    language = Parser.string('uri', 'language', min=2, max=3, optional=True)
    if language and language not in supported_languages:
        raise InvalidRequest(desc='Unsupported language')

    # TODO(roman): Ideally caching logic should live inside the model. Otherwise it
    # becomes hard to track all this stuff.
    cache_key = cache.gen_key('list', entity_id, user_id, sort, limit, offset, language)
    cached_result = cache.get(cache_key, Review.CACHE_NAMESPACE)
    if cached_result:
        reviews = cached_result['reviews']
        count = cached_result['count']

    else:
        reviews, count = Review.list(
            entity_id=entity_id,
            entity_type=entity_type,
            user_id=user_id,
            sort=sort,
            limit=limit,
            offset=offset,
            language=language,
        )
        reviews = [p.to_dict() for p in reviews]
        cache.set(cache_key, {
            'reviews': reviews,
            'count': count,
        }, namespace=Review.CACHE_NAMESPACE)

    return jsonify(limit=limit, offset=offset, count=count, reviews=reviews)
예제 #9
0
def review_list_handler():
    """Get list of reviews.

    :json uuid entity_id: UUID of the release group that is being reviewed
    :json string entity_type: One of the supported reviewable entities. 'release_group' or 'event' etc. **(optional)**
    :query user_id: user's UUID **(optional)**
    :query sort: ``rating`` or ``created`` **(optional)**
    :query limit: results limit, min is 0, max is 50, default is 50 **(optional)**
    :query offset: result offset, default is 0 **(optional)**
    :query language: language code (ISO 639-1) **(optional)**

    :resheader Content-Type: *application/json*
    """
    # TODO: This checking is added to keep old clients working and needs to be removed.
    release_group = Parser.uuid('uri', 'release_group', optional=True)
    if release_group:
        entity_id = release_group
        entity_type = 'release_group'
    else:        
        entity_id = Parser.uuid('uri', 'entity_id', optional=True)
        entity_type = Parser.string('uri', 'entity_type', valid_values=ENTITY_TYPES, optional=True)

    user_id = Parser.uuid('uri', 'user_id', optional=True)
    sort = Parser.string('uri', 'sort', valid_values=['rating', 'created'], optional=True)
    limit = Parser.int('uri', 'limit', min=1, max=50, optional=True) or 50
    offset = Parser.int('uri', 'offset', optional=True) or 0
    language = Parser.string('uri', 'language', min=2, max=3, optional=True)
    if language and language not in supported_languages:
        raise InvalidRequest(desc='Unsupported language')

    # TODO(roman): Ideally caching logic should live inside the model. Otherwise it
    # becomes hard to track all this stuff.
    cache_key = cache.gen_key('list', entity_id, user_id, sort, limit, offset, language)
    cached_result = cache.get(cache_key, Review.CACHE_NAMESPACE)
    if cached_result:
        reviews = cached_result['reviews']
        count = cached_result['count']

    else:
        reviews, count = Review.list(
            entity_id=entity_id,
            entity_type=entity_type,
            user_id=user_id,
            sort=sort,
            limit=limit,
            offset=offset,
            language=language,
        )
        reviews = [p.to_dict() for p in reviews]
        cache.set(cache_key, {
            'reviews': reviews,
            'count': count,
        }, namespace=Review.CACHE_NAMESPACE)

    return jsonify(limit=limit, offset=offset, count=count, reviews=reviews)
예제 #10
0
 def fetch_params():
     is_draft = Parser.bool('json', 'is_draft', optional=True) or False
     if is_draft:
         REVIEW_TEXT_MIN_LENGTH = None
     entity_id = Parser.uuid('json', 'entity_id')
     entity_type = Parser.string('json',
                                 'entity_type',
                                 valid_values=ENTITY_TYPES)
     text = Parser.string('json',
                          'text',
                          min=REVIEW_TEXT_MIN_LENGTH,
                          max=REVIEW_TEXT_MAX_LENGTH,
                          optional=True)
     rating = Parser.int('json',
                         'rating',
                         min=REVIEW_RATING_MIN,
                         max=REVIEW_RATING_MAX,
                         optional=True)
     license_choice = Parser.string('json', 'license_choice')
     language = Parser.string(
         'json', 'language', min=2, max=3, optional=True) or 'en'
     if text is None and rating is None:
         raise InvalidRequest(desc='Review must have either text or rating')
     if language and language not in supported_languages:
         raise InvalidRequest(desc='Unsupported language')
     if db_review.list_reviews(user_id=user.id, entity_id=entity_id)[1]:
         raise InvalidRequest(
             desc='You have already published a review for this album')
     return entity_id, entity_type, text, rating, license_choice, language, is_draft
예제 #11
0
def user_me_handler(user):
    """Get your profile information.

    :query inc: includes

    :resheader Content-Type: *application/json*
    """
    inc = Parser.list('uri', 'inc', User.allowed_includes, optional=True) or []
    return jsonify(user=user.to_dict(inc, confidential=True))
예제 #12
0
 def fetch_params():
     is_draft = Parser.bool('json', 'is_draft', optional=True) or False
     if is_draft:
         REVIEW_TEXT_MIN_LENGTH = None
     entity_id = Parser.uuid('json', 'entity_id')
     entity_type = Parser.string('json', 'entity_type', valid_values=ENTITY_TYPES)
     text = Parser.string('json', 'text', min=REVIEW_TEXT_MIN_LENGTH, max=REVIEW_TEXT_MAX_LENGTH, optional=True)
     rating = Parser.int('json', 'rating', min=REVIEW_RATING_MIN, max=REVIEW_RATING_MAX, optional=True)
     license_choice = Parser.string('json', 'license_choice')
     language = Parser.string('json', 'language', min=2, max=3, optional=True) or 'en'
     if text is None and rating is None:
         raise InvalidRequest(desc='Review must have either text or rating')
     if language and language not in supported_languages:
         raise InvalidRequest(desc='Unsupported language')
     if db_review.list_reviews(user_id=user.id, entity_id=entity_id)[1]:
         raise InvalidRequest(desc='You have already published a review for this album')
     return entity_id, entity_type, text, rating, license_choice, language, is_draft
예제 #13
0
 def fetch_params():
     is_draft = Parser.bool('json', 'is_draft', optional=True) or False
     if is_draft:
         REVIEW_MIN_LENGTH = None
     entity_id = Parser.uuid('json', 'entity_id')
     entity_type = Parser.string('json', 'entity_type', valid_values=ENTITY_TYPES)
     text = Parser.string('json', 'text', min=REVIEW_MIN_LENGTH, max=REVIEW_MAX_LENGTH)
     license_choice = Parser.string('json', 'license_choice')
     language = Parser.string('json', 'language', min=2, max=3, optional=True) or 'en'
     if language and language not in supported_languages:
         raise InvalidRequest(desc='Unsupported language')
     if Review.query.filter_by(user=user, entity_id=entity_id).count():
         raise InvalidRequest(desc='You have already published a review for this album')
     return entity_id, entity_type, text, license_choice, language, is_draft
예제 #14
0
 def fetch_params():
     is_draft = Parser.bool('json', 'is_draft', optional=True) or False
     if is_draft:
         REVIEW_MIN_LENGTH = None
     entity_id = Parser.uuid('json', 'entity_id')
     entity_type = Parser.string('json', 'entity_type', valid_values=ENTITY_TYPES)
     text = Parser.string('json', 'text', min=REVIEW_MIN_LENGTH, max=REVIEW_MAX_LENGTH)
     license_choice = Parser.string('json', 'license_choice')
     language = Parser.string('json', 'language', min=2, max=3, optional=True) or 'en'
     if language and language not in supported_languages:
         raise InvalidRequest(desc='Unsupported language')
     if Review.query.filter_by(user=user, entity_id=entity_id).count():
         raise InvalidRequest(desc='You have already published a review for this album')
     return entity_id, entity_type, text, license_choice, language, is_draft
예제 #15
0
 def fetch_params():
     vote = Parser.bool('json', 'vote')
     return vote
예제 #16
0
def review_list_handler():
    """Get list of reviews.

    **Request Example:**

    .. code-block:: bash

        $ curl "https://critiquebrainz.org/ws/1/review/?limit=1&offset=50" \\
                -X GET

    **Response Example:**

    .. code-block:: json

        {
          "count": 9197,
          "limit": 1,
          "offset": 50,
          "reviews": [
            {
              "created": "Fri, 16 May 2008 00:00:00 GMT",
              "edits": 0,
              "entity_id": "09259937-6477-3959-8b10-af1cbaea8e6e",
              "entity_type": "release_group",
              "id": "c807d0b4-0dd0-43fe-a7c4-d29bb61f389e",
              "language": "en",
              "last_updated": "Fri, 16 May 2008 00:00:00 GMT",
              "license": {
                "full_name": "Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported",
                "id": "CC BY-NC-SA 3.0",
                "info_url": "https://creativecommons.org/licenses/by-nc-sa/3.0/"
              },
              "popularity": 0,
              "source": "BBC",
              "source_url": "http://www.bbc.co.uk/music/reviews/vh54",
              "text": "TEXT CONTENT OF REVIEW",
              "rating": 5,
              "user": {
                "created": "Wed, 07 May 2014 16:20:47 GMT",
                "display_name": "Jenny Nelson",
                "id": "3bf3fe0c-6db2-4746-bcf1-f39912113852",
                "karma": 0,
                "user_type": "Noob"
              },
              "votes": {
                "positive": 0,
                "negative": 0
              }
            }
          ]
        }

    :json uuid entity_id: UUID of the release group that is being reviewed
    :json string entity_type: One of the supported reviewable entities. 'release_group' or 'event' etc. **(optional)**
    :query user_id: user's UUID **(optional)**
    :query sort: ``popularity`` or ``published_on`` **(optional)**
    :query limit: results limit, min is 0, max is 50, default is 50 **(optional)**
    :query offset: result offset, default is 0 **(optional)**
    :query language: language code (ISO 639-1) **(optional)**

    :resheader Content-Type: *application/json*
    """
    # TODO: This checking is added to keep old clients working and needs to be removed.
    release_group = Parser.uuid('uri', 'release_group', optional=True)
    if release_group:
        entity_id = release_group
        entity_type = 'release_group'
    else:
        entity_id = Parser.uuid('uri', 'entity_id', optional=True)
        entity_type = Parser.string('uri',
                                    'entity_type',
                                    valid_values=ENTITY_TYPES,
                                    optional=True)

    user_id = Parser.uuid('uri', 'user_id', optional=True)
    sort = Parser.string(
        'uri',
        'sort',
        valid_values=['popularity', 'published_on', 'rating', 'created'],
        optional=True)

    # "rating" and "created" sort values are deprecated and but allowed here for backward compatibility
    if sort == 'created':
        sort = 'published_on'
    if sort == 'rating':
        sort = 'popularity'

    limit = Parser.int('uri', 'limit', min=1, max=50, optional=True) or 50
    offset = Parser.int('uri', 'offset', optional=True) or 0
    language = Parser.string('uri', 'language', min=2, max=3, optional=True)
    if language and language not in supported_languages:
        raise InvalidRequest(desc='Unsupported language')

    # TODO(roman): Ideally caching logic should live inside the model. Otherwise it
    # becomes hard to track all this stuff.
    cache_key = cache.gen_key('list', entity_id, user_id, sort, limit, offset,
                              language)
    cached_result = cache.get(cache_key, REVIEW_CACHE_NAMESPACE)
    if cached_result:
        reviews = cached_result['reviews']
        count = cached_result['count']

    else:
        reviews, count = db_review.list_reviews(
            entity_id=entity_id,
            entity_type=entity_type,
            user_id=user_id,
            sort=sort,
            limit=limit,
            offset=offset,
            language=language,
        )
        reviews = [db_review.to_dict(p) for p in reviews]
        cache.set(cache_key, {
            'reviews': reviews,
            'count': count,
        },
                  namespace=REVIEW_CACHE_NAMESPACE)

    return jsonify(limit=limit, offset=offset, count=count, reviews=reviews)
예제 #17
0
 def fetch_params():
     text = Parser.string('json',
                          'text',
                          min=REVIEW_MIN_LENGTH,
                          max=REVIEW_MAX_LENGTH)
     return text
예제 #18
0
 def fetch_params():
     limit = Parser.int('uri', 'limit', min=1, max=50, optional=True) or 50
     offset = Parser.int('uri', 'offset', optional=True) or 0
     return limit, offset
예제 #19
0
 def fetch_params():
     display_name = Parser.string('json', 'display_name', optional=True)
     email = Parser.email('json', 'email', optional=True)
     show_gravatar = Parser.bool('json', 'show_gravatar', optional=True)
     return display_name, email, show_gravatar
예제 #20
0
 def fetch_params():
     text = Parser.string('json', 'text', min=REVIEW_MIN_LENGTH, max=REVIEW_MAX_LENGTH)
     return text
예제 #21
0
 def fetch_params():
     limit = Parser.int('uri', 'limit', min=1, max=50, optional=True) or 50
     offset = Parser.int('uri', 'offset', optional=True) or 0
     return limit, offset
예제 #22
0
 def fetch_params():
     display_name = Parser.string('json', 'display_name', optional=True)
     email = Parser.email('json', 'email', optional=True)
     show_gravatar = Parser.bool('json', 'show_gravatar', optional=True)
     return display_name, email, show_gravatar
예제 #23
0
 def fetch_params():
     vote = Parser.bool('json', 'vote')
     return vote
예제 #24
0
def review_list_handler():
    """Get list of reviews.

    **Request Example:**

    .. code-block:: bash

        $ curl "https://critiquebrainz.org/ws/1/review/?limit=1&offset=50" \\
                -X GET

    **Response Example:**

    .. code-block:: json

        {
          "count": 9197,
          "limit": 1,
          "offset": 50,
          "reviews": [
            {
              "created": "Fri, 16 May 2008 00:00:00 GMT",
              "edits": 0,
              "entity_id": "09259937-6477-3959-8b10-af1cbaea8e6e",
              "entity_type": "release_group",
              "id": "c807d0b4-0dd0-43fe-a7c4-d29bb61f389e",
              "language": "en",
              "last_updated": "Fri, 16 May 2008 00:00:00 GMT",
              "license": {
                "full_name": "Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported",
                "id": "CC BY-NC-SA 3.0",
                "info_url": "https://creativecommons.org/licenses/by-nc-sa/3.0/"
              },
              "popularity": 0,
              "source": "BBC",
              "source_url": "http://www.bbc.co.uk/music/reviews/vh54",
              "text": "TEXT CONTENT OF REVIEW",
              "rating": 5,
              "user": {
                "created": "Wed, 07 May 2014 16:20:47 GMT",
                "display_name": "Jenny Nelson",
                "id": "3bf3fe0c-6db2-4746-bcf1-f39912113852",
                "karma": 0,
                "user_type": "Noob"
              },
              "votes": {
                "positive": 0,
                "negative": 0
              }
            }
          ]
        }

    :json uuid entity_id: UUID of the release group that is being reviewed
    :json string entity_type: One of the supported reviewable entities. 'release_group' or 'event' etc. **(optional)**
    :query user_id: user's UUID **(optional)**
    :query sort: ``popularity`` or ``published_on`` **(optional)**
    :query limit: results limit, min is 0, max is 50, default is 50 **(optional)**
    :query offset: result offset, default is 0 **(optional)**
    :query language: language code (ISO 639-1) **(optional)**

    :resheader Content-Type: *application/json*
    """
    # TODO: This checking is added to keep old clients working and needs to be removed.
    release_group = Parser.uuid('uri', 'release_group', optional=True)
    if release_group:
        entity_id = release_group
        entity_type = 'release_group'
    else:
        entity_id = Parser.uuid('uri', 'entity_id', optional=True)
        entity_type = Parser.string('uri', 'entity_type', valid_values=ENTITY_TYPES, optional=True)

    user_id = Parser.uuid('uri', 'user_id', optional=True)
    # TODO: "rating" sort value is deprecated and needs to be removed.
    sort = Parser.string('uri', 'sort', valid_values=['popularity', 'published_on', 'rating'], optional=True)
    if sort == 'rating':
        sort = 'popularity'
    limit = Parser.int('uri', 'limit', min=1, max=50, optional=True) or 50
    offset = Parser.int('uri', 'offset', optional=True) or 0
    language = Parser.string('uri', 'language', min=2, max=3, optional=True)
    if language and language not in supported_languages:
        raise InvalidRequest(desc='Unsupported language')

    # TODO(roman): Ideally caching logic should live inside the model. Otherwise it
    # becomes hard to track all this stuff.
    cache_key = cache.gen_key('list', entity_id, user_id, sort, limit, offset, language)
    cached_result = cache.get(cache_key, REVIEW_CACHE_NAMESPACE)
    if cached_result:
        reviews = cached_result['reviews']
        count = cached_result['count']

    else:
        reviews, count = db_review.list_reviews(
            entity_id=entity_id,
            entity_type=entity_type,
            user_id=user_id,
            sort=sort,
            limit=limit,
            offset=offset,
            language=language,
        )
        reviews = [db_review.to_dict(p) for p in reviews]
        cache.set(cache_key, {
            'reviews': reviews,
            'count': count,
        }, namespace=REVIEW_CACHE_NAMESPACE)

    return jsonify(limit=limit, offset=offset, count=count, reviews=reviews)