# Buggy code
def view_configuration():
    reboot_required = False
    if request.method == "POST":
        to_save = request.form.to_dict()
        content = ub.session.query(ub.Settings).first()
        if "config_calibre_web_title" in to_save:
            content.config_calibre_web_title = to_save["config_calibre_web_title"]
        if "config_columns_to_ignore" in to_save:
            content.config_columns_to_ignore = to_save["config_columns_to_ignore"]
        if "config_read_column" in to_save:
            content.config_read_column = int(to_save["config_read_column"])
        if "config_title_regex" in to_save:
            if content.config_title_regex != to_save["config_title_regex"]:
                content.config_title_regex = to_save["config_title_regex"]
                reboot_required = True
        if "config_random_books" in to_save:
            content.config_random_books = int(to_save["config_random_books"])
        if "config_books_per_page" in to_save:
            content.config_books_per_page = int(to_save["config_books_per_page"])
        # Mature Content configuration
        if "config_mature_content_tags" in to_save:
            content.config_mature_content_tags = to_save[
                "config_mature_content_tags"
            ].strip()

        # Default user configuration
        content.config_default_role = 0
        if "admin_role" in to_save:
            content.config_default_role = content.config_default_role + ub.ROLE_ADMIN
        if "download_role" in to_save:
            content.config_default_role = content.config_default_role + ub.ROLE_DOWNLOAD
        if "upload_role" in to_save:
            content.config_default_role = content.config_default_role + ub.ROLE_UPLOAD
        if "edit_role" in to_save:
            content.config_default_role = content.config_default_role + ub.ROLE_EDIT
        if "delete_role" in to_save:
            content.config_default_role = (
                content.config_default_role + ub.ROLE_DELETE_BOOKS
            )
        if "passwd_role" in to_save:
            content.config_default_role = content.config_default_role + ub.ROLE_PASSWD
        if "passwd_role" in to_save:
            content.config_default_role = (
                content.config_default_role + ub.ROLE_EDIT_SHELFS
            )
        content.config_default_show = 0
        if "show_detail_random" in to_save:
            content.config_default_show = content.config_default_show + ub.DETAIL_RANDOM
        if "show_language" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_LANGUAGE
            )
        if "show_series" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_SERIES
            )
        if "show_category" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_CATEGORY
            )
        if "show_hot" in to_save:
            content.config_default_show = content.config_default_show + ub.SIDEBAR_HOT
        if "show_random" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_RANDOM
            )
        if "show_author" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_AUTHOR
            )
        if "show_publisher" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_PUBLISHER
            )
        if "show_best_rated" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_BEST_RATED
            )
        if "show_read_and_unread" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_READ_AND_UNREAD
            )
        if "show_recent" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_RECENT
            )
        if "show_sorted" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_SORTED
            )
        if "show_mature_content" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.MATURE_CONTENT
            )
        ub.session.commit()
        flash(_("Calibre-Web configuration updated"), category="success")
        config.loadSettings()
        if reboot_required:
            # db.engine.dispose() # ToDo verify correct
            # ub.session.close()
            # ub.engine.dispose()
            # stop Server
            server.Server.setRestartTyp(True)
            server.Server.stopServer()
            app.logger.info("Reboot required, restarting")
    readColumn = (
        db.session.query(db.Custom_Columns)
        .filter(
            db.and_(
                db.Custom_Columns.datatype == "bool",
                db.Custom_Columns.mark_for_delete == 0,
            )
        )
        .all()
    )
    return render_title_template(
        "config_view_edit.html",
        content=config,
        readColumns=readColumn,
        title=_("UI Configuration"),
        page="uiconfig",
    )


# Traceback (OperationalError)
ERROR:cps.web:Exception on /book/125 [GET]
Traceback (most recent call last):
File "/Users/jgillman/Code/calibre-web/vendor/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/jgillman/Code/calibre-web/vendor/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/jgillman/Code/calibre-web/vendor/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/jgillman/Code/calibre-web/vendor/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/jgillman/Code/calibre-web/vendor/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/jgillman/Code/calibre-web/vendor/flask_login.py", line 717, in decorated_view
return func(*args, **kwargs)
File "/Users/jgillman/Code/calibre-web/cps/web.py", line 381, in show_book
return render_template('detail.html', entry=entries,  cc=cc, title=entries.title, books_shelfs=book_in_shelfs)
File "/Users/jgillman/Code/calibre-web/vendor/flask/templating.py", line 128, in render_template
context, ctx.app)
File "/Users/jgillman/Code/calibre-web/vendor/flask/templating.py", line 110, in _render
rv = template.render(context)
File "/Users/jgillman/Code/calibre-web/vendor/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/jgillman/Code/calibre-web/vendor/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/jgillman/Code/calibre-web/cps/templates/detail.html", line 1, in top-level template code
{% extends "layout.html" %}
File "/Users/jgillman/Code/calibre-web/cps/templates/layout.html", line 136, in top-level template code
{% block body %}{% endblock %}
File "/Users/jgillman/Code/calibre-web/cps/templates/detail.html", line 66, in block "body"
{% if entry['custom_column_' ~ c.id]|length > 0 %}
File "/Users/jgillman/Code/calibre-web/vendor/jinja2/environment.py", line 387, in getitem
return getattr(obj, attr)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/attributes.py", line 316, in __get__
return self.impl.get(instance_state(instance), dict_)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/attributes.py", line 613, in get
value = self.callable_(state, passive)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/strategies.py", line 524, in _load_for_state
return self._emit_lazyload(session, state, ident_key, passive)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/strategies.py", line 585, in _emit_lazyload
result = q.all()
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/query.py", line 2241, in all
return list(self)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/query.py", line 2353, in __iter__
return self._execute_and_instances(context)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/query.py", line 2368, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/base.py", line 662, in execute
params)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/base.py", line 761, in _execute_clauseelement
compiled_sql, distilled_params
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/base.py", line 874, in _execute_context
context)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
exc_info
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/util/compat.py", line 196, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/base.py", line 867, in _execute_context
context)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/default.py", line 324, in do_execute
cursor.execute(statement, parameters)
OperationalError: (OperationalError) no such table: books_custom_column_1_link u'SELECT custom_column_1.id AS custom_column_1_id, custom_column_1.value AS custom_column_1_value \
nFROM custom_column_1, books_custom_column_1_link \nWHERE ? = books_custom_column_1_link.book AND custom_column_1.id = books_custom_column_1_link.value' (125,)
ERROR:tornado.access:500 GET /book/125 (::1) 89.60ms

# Fixed code
def view_configuration():
    reboot_required = False
    if request.method == "POST":
        to_save = request.form.to_dict()
        content = ub.session.query(ub.Settings).first()
        if "config_calibre_web_title" in to_save:
            content.config_calibre_web_title = to_save["config_calibre_web_title"]
        if "config_columns_to_ignore" in to_save:
            content.config_columns_to_ignore = to_save["config_columns_to_ignore"]
        if "config_read_column" in to_save:
            content.config_read_column = int(to_save["config_read_column"])
        if "config_theme" in to_save:
            content.config_theme = int(to_save["config_theme"])
        if "config_title_regex" in to_save:
            if content.config_title_regex != to_save["config_title_regex"]:
                content.config_title_regex = to_save["config_title_regex"]
                reboot_required = True
        if "config_random_books" in to_save:
            content.config_random_books = int(to_save["config_random_books"])
        if "config_books_per_page" in to_save:
            content.config_books_per_page = int(to_save["config_books_per_page"])
        # maximum authors to show before we display a 'show more' link
        if "config_authors_max" in to_save:
            content.config_authors_max = int(to_save["config_authors_max"])
        # Mature Content configuration
        if "config_mature_content_tags" in to_save:
            content.config_mature_content_tags = to_save[
                "config_mature_content_tags"
            ].strip()

        # Default user configuration
        content.config_default_role = 0
        if "admin_role" in to_save:
            content.config_default_role = content.config_default_role + ub.ROLE_ADMIN
        if "download_role" in to_save:
            content.config_default_role = content.config_default_role + ub.ROLE_DOWNLOAD
        if "upload_role" in to_save:
            content.config_default_role = content.config_default_role + ub.ROLE_UPLOAD
        if "edit_role" in to_save:
            content.config_default_role = content.config_default_role + ub.ROLE_EDIT
        if "delete_role" in to_save:
            content.config_default_role = (
                content.config_default_role + ub.ROLE_DELETE_BOOKS
            )
        if "passwd_role" in to_save:
            content.config_default_role = content.config_default_role + ub.ROLE_PASSWD
        if "edit_shelf_role" in to_save:
            content.config_default_role = (
                content.config_default_role + ub.ROLE_EDIT_SHELFS
            )
        content.config_default_show = 0
        if "show_detail_random" in to_save:
            content.config_default_show = content.config_default_show + ub.DETAIL_RANDOM
        if "show_language" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_LANGUAGE
            )
        if "show_series" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_SERIES
            )
        if "show_category" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_CATEGORY
            )
        if "show_hot" in to_save:
            content.config_default_show = content.config_default_show + ub.SIDEBAR_HOT
        if "show_random" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_RANDOM
            )
        if "show_author" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_AUTHOR
            )
        if "show_publisher" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_PUBLISHER
            )
        if "show_best_rated" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_BEST_RATED
            )
        if "show_read_and_unread" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_READ_AND_UNREAD
            )
        if "show_recent" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_RECENT
            )
        if "show_sorted" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.SIDEBAR_SORTED
            )
        if "show_mature_content" in to_save:
            content.config_default_show = (
                content.config_default_show + ub.MATURE_CONTENT
            )
        ub.session.commit()
        flash(_("Calibre-Web configuration updated"), category="success")
        config.loadSettings()
        before_request()
        if reboot_required:
            # db.engine.dispose() # ToDo verify correct
            # ub.session.close()
            # ub.engine.dispose()
            # stop Server
            server.Server.setRestartTyp(True)
            server.Server.stopServer()
            app.logger.info("Reboot required, restarting")
    readColumn = (
        db.session.query(db.Custom_Columns)
        .filter(
            db.and_(
                db.Custom_Columns.datatype == "bool",
                db.Custom_Columns.mark_for_delete == 0,
            )
        )
        .all()
    )
    return render_title_template(
        "config_view_edit.html",
        content=config,
        readColumns=readColumn,
        title=_("UI Configuration"),
        page="uiconfig",
    )