# Buggy code
def read_book(book_id, book_format):
    book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
    if not book:
        flash(
            _("Error opening eBook. File does not exist or file is not accessible:"),
            category="error",
        )
        return redirect(url_for("index"))

    # check if book was downloaded before
    lbookmark = None
    if current_user.is_authenticated:
        lbookmark = (
            ub.session.query(ub.Bookmark)
            .filter(
                ub.and_(
                    ub.Bookmark.user_id == int(current_user.id),
                    ub.Bookmark.book_id == book_id,
                    ub.Bookmark.format == book_format.upper(),
                )
            )
            .first()
        )
    if book_format.lower() == "epub":
        return render_title_template(
            "read.html", bookid=book_id, title=_("Read a Book"), bookmark=lbookmark
        )
    elif book_format.lower() == "pdf":
        return render_title_template(
            "readpdf.html", pdffile=book_id, title=_("Read a Book")
        )
    elif book_format.lower() == "txt":
        return render_title_template(
            "readtxt.html", txtfile=book_id, title=_("Read a Book")
        )
    else:
        book_dir = os.path.join(config.get_main_dir, "cps", "static", str(book_id))
        if not os.path.exists(book_dir):
            os.mkdir(book_dir)
        for fileext in ["cbr", "cbt", "cbz"]:
            if book_format.lower() == fileext:
                all_name = str(book_id)  # + "/" + book.data[0].name + "." + fileext
                # tmp_file = os.path.join(book_dir, book.data[0].name) + "." + fileext
                # if not os.path.exists(all_name):
                #    cbr_file = os.path.join(config.config_calibre_dir, book.path, book.data[0].name) + "." + fileext
                #    copyfile(cbr_file, tmp_file)
                return render_title_template(
                    "readcbr.html",
                    comicfile=all_name,
                    title=_("Read a Book"),
                    extension=fileext,
                )
        """if rar_support == True:
            extensionList = ["cbr","cbt","cbz"]
        else:
            extensionList = ["cbt","cbz"]
        for fileext in extensionList:
            if book_format.lower() == fileext:
                return render_title_template('readcbr.html', comicfile=book_id, 
                extension=fileext, title=_(u"Read a Book"), book=book)
        flash(_(u"Error opening eBook. File does not exist or file is not accessible."), category="error")
        return redirect(url_for("index"))"""


# Traceback (TypeError)
[2019-04-29 08:55:11,436] ERROR in app: Exception on /read/4/zip [GET]
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1816, in full_dispatch_request
return self.finalize_request(rv)
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1831, in finalize_request
response = self.make_response(rv)
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1957, in make_response
'The view function did not return a valid response. The'
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

# Fixed code
def read_book(book_id, book_format):
    book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
    if not book:
        flash(
            _("Error opening eBook. File does not exist or file is not accessible:"),
            category="error",
        )
        return redirect(url_for("index"))

    # check if book has bookmark
    lbookmark = None
    if current_user.is_authenticated:
        lbookmark = (
            ub.session.query(ub.Bookmark)
            .filter(
                ub.and_(
                    ub.Bookmark.user_id == int(current_user.id),
                    ub.Bookmark.book_id == book_id,
                    ub.Bookmark.format == book_format.upper(),
                )
            )
            .first()
        )
    if book_format.lower() == "epub":
        return render_title_template(
            "read.html", bookid=book_id, title=_("Read a Book"), bookmark=lbookmark
        )
    elif book_format.lower() == "pdf":
        return render_title_template(
            "readpdf.html", pdffile=book_id, title=_("Read a Book")
        )
    elif book_format.lower() == "txt":
        return render_title_template(
            "readtxt.html", txtfile=book_id, title=_("Read a Book")
        )
    else:
        book_dir = os.path.join(config.get_main_dir, "cps", "static", str(book_id))
        if not os.path.exists(book_dir):
            os.mkdir(book_dir)
        for fileext in ["cbr", "cbt", "cbz"]:
            if book_format.lower() == fileext:
                all_name = str(book_id)  # + "/" + book.data[0].name + "." + fileext
                # tmp_file = os.path.join(book_dir, book.data[0].name) + "." + fileext
                # if not os.path.exists(all_name):
                #    cbr_file = os.path.join(config.config_calibre_dir, book.path, book.data[0].name) + "." + fileext
                #    copyfile(cbr_file, tmp_file)
                return render_title_template(
                    "readcbr.html",
                    comicfile=all_name,
                    title=_("Read a Book"),
                    extension=fileext,
                )
        """if rar_support == True:
            extensionList = ["cbr","cbt","cbz"]
        else:
            extensionList = ["cbt","cbz"]
        for fileext in extensionList:
            if book_format.lower() == fileext:
                return render_title_template('readcbr.html', comicfile=book_id, 
                extension=fileext, title=_(u"Read a Book"), book=book)
        flash(_(u"Error opening eBook. File does not exist or file is not accessible."), category="error")
        return redirect(url_for("index"))"""
        flash(_("Error opening eBook. Fileformat is not supported."), category="error")
        return redirect(url_for("index"))