Beispiel #1
0
def api_post_rating():
    """
    @api {post} /api/rating Post a song rating
    @apiName api_post_rating
    @apiGroup Song

    @apiParam {string} artist The exact song's artist name on the system.
    @apiParam {string} album The exact song's album name on the system.
    @apiParam {string} title The exact song's title on the system.
    @apiParam {number{1}=1,2,3,4,5} rating The rating given to the song.
    @apiParam {string} email The email of the user that post the rating.
              Note: If a valid cookie that identifies an user is sent, then this parameter is optional.

    @apiSuccess {number} result The result of the POST, being the sent Rating from <code>1</code> to <code>5</code> for Success.
    @apiError {number} result The result of the POST, being
                              <code>-1</code> for incomplete/incorrect parameters, or
                              <code>0</code> for a song not found.
    @apiSuccessExample {json} Success-Response for a 4 start Rating:
                              { "result": 4 }
    @apiErrorExample {json} Error-Response:
                            { "result": 0 }

    @apiDescription
    API note to set a note for a song.
    Returns JSON object.
    """
    if not request.json:
        return jsonify({'result': -1}) #abort(300)

    if request.json['artist'] == None or request.json['album'] == None or request.json['title'] == None or request.json['rating'] == None:
        return jsonify({'result': -1})
    if request.json['artist'] == '' or request.json['album'] == '' or request.json['title'] == '' or request.json['rating'] == '':
        return jsonify({'result': -1})
    if not request.json['rating'].isdigit():
        return jsonify({'result': -1})

    # Check for an email in JSON.
    email = None
    if request.json.has_key('email') and request.json['email'] != '':
        email = request.json['email']
    # Check if email on Session, and overwrite the one in JSON.
    elif session.has_key('email'):
        email = session['email']
    else:
        return jsonify({'result': -1})

    rating = request.json['rating']

    # Check if rating range is Not valid.
    if int(rating) < 1 or int(rating) > 5:
        return jsonify({'result': -1})

    music = DbFunct.song_data_get(request.json['artist'], request.json['album'], request.json['title'])

    # Check if there was any returned song from DB.
    if music == None:
        return jsonify({'result': 0})

    result = DbFunct.song_rate_set_rating(email, music, rating)
    if result == True:
        return jsonify({'result': int(rating)})
    else:
        return jsonify({'result': -1})