postgresqlflaskadminer

TypeError: unsupported operand type(s) for +: 'int' and 'RowProxy'


The purpose of this view is to extract and return into JSON format after reading from goodread.com and my database by some calculation.

I tried so many ways to render the API but still no hope

I tried changing user input oneIsbn with 'oneIsbn' while querying in each listed SQL queries. what I got on my browser is just like this

{ "Error": "Invalid ISBN 0380795272" }

My code snippet

@app.route("/api/<oneIsbn>", methods=["GET", "POST"])
@login_required
def api(oneIsbn):
    """Returns in JSON format for a single Book"""

    if request.method == "GET":

        check = db.execute("SELECT * FROM books WHERE isbn= :isbn",
                           {"isbn": oneIsbn}).fetchone()
        if check is None:
               return jsonify({"Error": f"Invalid ISBN {oneIsbn}"}), 405
        else:
            res = requests.get(
                "https://www.goodreads.com/book/review_counts.json",
                params={
                    "key": "x9fJg",
                    "isbns": oneIsbn})
            if res.status_code != 200:
                raise Exception("ERROR: API request unsuccessful.")
            else:
                data = res.json()
                y = data["books"][0]["work_reviews_count"]
                r = data["books"][0]["average_rating"]

            isbn = db.execute("SELECT isbn FROM books WHERE isbn = :isbn",
                                {"isbn": oneIsbn}).fetchone()
            title = db.execute("SELECT title FROM books WHERE isbn = :isbn",
                                {"isbn": oneIsbn}).fetchone()
            author = db.execute("SELECT author FROM books WHERE isbn = :isbn",
                                {"isbn": oneIsbn}).fetchone()
            year = db.execute("SELECT year FROM books WHERE isbn = :isbn",
                                {"isbn": oneIsbn}).fetchone()
            x = db.execute("SELECT COUNT(reviews) FROM reviews WHERE isbn = :isbn",
                                    {"isbn": 'oneIsbn'}).fetchone()
            z = db.execute("SELECT rating FROM reviews WHERE isbn = :isbn",
                                    {"isbn": oneIsbn}).fetchone()

            rev = int(y)
            revCount = int(x.count)
            bothReviewValue = sum((revCount,rev))

            # listRate = float(z)
            rat = float(r)
            bothRatingValue = sum([z,rat]) / 2

            return jsonify(
                ISBN=isbn,
                TITLE=title,
                AUTHOR=author,
                YEAR=year,
                REVIEWS=bothReviewValue,
                RATING=bothRatingValue
                ), 422 

TRACEBACK

TypeError
TypeError: unsupported operand type(s) for +: 'int' and 'RowProxy'

Traceback (most recent call last)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
Open an interactive python shell in this frameraise value
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Beacon\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Beacon\Desktop\THINKFUL DOCS\PYTHON\PYTHON WORKSPACE\project1\application.py", line 39, in wrapped_view
return view(**kwargs)
File "C:\Users\Beacon\Desktop\THINKFUL DOCS\PYTHON\PYTHON WORKSPACE\project1\application.py", line 233, in api
bothRatingValue = sum([z,rat]) / 2
TypeError: unsupported operand type(s) for +: 'int' and 'RowProxy'
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter.

Solution

  • This method I used really works great. I already missed something like

    The sum method should be put in tuple not in list and the pulled string rate should cast to float.

                rev = int(y)
                revCount = int(x.count)
                bothReviewValue = sum((revCount,rev))
    
                listRate = float(z)
                rat = float(r)
                bothRatingValue = sum((listRate,rat)) / 2