# Buggy code
def new_user():
    content = ub.User()
    languages = speaking_language()
    translations = [LC("en")] + babel.list_translations()
    if request.method == "POST":
        to_save = request.form.to_dict()
        content.default_language = to_save["default_language"]
        content.mature_content = "show_mature_content" in to_save
        if "locale" in to_save:
            content.locale = to_save["locale"]
        content.sidebar_view = 0
        if "show_random" in to_save:
            content.sidebar_view += ub.SIDEBAR_RANDOM
        if "show_language" in to_save:
            content.sidebar_view += ub.SIDEBAR_LANGUAGE
        if "show_series" in to_save:
            content.sidebar_view += ub.SIDEBAR_SERIES
        if "show_category" in to_save:
            content.sidebar_view += ub.SIDEBAR_CATEGORY
        if "show_hot" in to_save:
            content.sidebar_view += ub.SIDEBAR_HOT
        if "show_read_and_unread" in to_save:
            content.sidebar_view += ub.SIDEBAR_READ_AND_UNREAD
        if "show_best_rated" in to_save:
            content.sidebar_view += ub.SIDEBAR_BEST_RATED
        if "show_author" in to_save:
            content.sidebar_view += ub.SIDEBAR_AUTHOR
        if "show_publisher" in to_save:
            content.sidebar_view += ub.SIDEBAR_PUBLISHER
        if "show_detail_random" in to_save:
            content.sidebar_view += ub.DETAIL_RANDOM
        if "show_sorted" in to_save:
            content.sidebar_view += ub.SIDEBAR_SORTED
        if "show_recent" in to_save:
            content.sidebar_view += ub.SIDEBAR_RECENT

        content.role = 0
        if "admin_role" in to_save:
            content.role = content.role + ub.ROLE_ADMIN
        if "download_role" in to_save:
            content.role = content.role + ub.ROLE_DOWNLOAD
        if "upload_role" in to_save:
            content.role = content.role + ub.ROLE_UPLOAD
        if "edit_role" in to_save:
            content.role = content.role + ub.ROLE_EDIT
        if "delete_role" in to_save:
            content.role = content.role + ub.ROLE_DELETE_BOOKS
        if "passwd_role" in to_save:
            content.role = content.role + ub.ROLE_PASSWD
        if "edit_shelf_role" in to_save:
            content.role = content.role + ub.ROLE_EDIT_SHELFS
        if not to_save["nickname"] or not to_save["email"] or not to_save["password"]:
            flash(_("Please fill out all fields!"), category="error")
            return render_title_template(
                "user_edit.html",
                new_user=1,
                content=content,
                translations=translations,
                title=_("Add new user"),
            )
        content.password = generate_password_hash(to_save["password"])
        content.nickname = to_save["nickname"]
        if config.config_public_reg and not check_valid_domain(to_save["email"]):
            flash(_("E-mail is not from valid domain"), category="error")
            return render_title_template(
                "user_edit.html",
                new_user=1,
                content=content,
                translations=translations,
                title=_("Add new user"),
            )
        else:
            content.email = to_save["email"]
        try:
            ub.session.add(content)
            ub.session.commit()
            flash(
                _("User '%(user)s' created", user=content.nickname), category="success"
            )
            return redirect(url_for("admin"))
        except IntegrityError:
            ub.session.rollback()
            flash(
                _("Found an existing account for this e-mail address or nickname."),
                category="error",
            )
    else:
        content.role = config.config_default_role
        content.sidebar_view = config.config_default_show
        content.mature_content = bool(config.config_default_show & ub.MATURE_CONTENT)
    return render_title_template(
        "user_edit.html",
        new_user=1,
        content=content,
        translations=translations,
        languages=languages,
        title=_("Add new user"),
        page="newuser",
    )


# Traceback (ValueError)
[2019-06-28 22:37:50,052] ERROR in app: Exception on /tasks [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 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/lib/python2.7/site-packages/flask_login/utils.py", line 261, in decorated_view
return func(*args, **kwargs)
File "/calibre-web/app/cps/web.py", line 1646, in get_tasks_status
tasks=helper.global_WorkerThread.get_taskstatus()
File "/calibre-web/app/cps/worker.py", line 225, in get_taskstatus
datetime.now() - self.queue[self.current]['starttime'])
File "/calibre-web/app/cps/worker.py", line 489, in _formatRuntime
if int(v) > 0:
ValueError: invalid literal for int() with base 10: '1 day, 5'
::ffff:172.17.0.1 - - [2019-06-28 22:37:50] "GET /tasks HTTP/1.1" 500 411 0.011915

# Fixed code
def new_user():
    content = ub.User()
    languages = speaking_language()
    translations = [LC("en")] + babel.list_translations()
    if request.method == "POST":
        to_save = request.form.to_dict()
        content.default_language = to_save["default_language"]
        content.mature_content = "show_mature_content" in to_save
        if "locale" in to_save:
            content.locale = to_save["locale"]
        content.sidebar_view = 0
        if "show_random" in to_save:
            content.sidebar_view += ub.SIDEBAR_RANDOM
        if "show_language" in to_save:
            content.sidebar_view += ub.SIDEBAR_LANGUAGE
        if "show_series" in to_save:
            content.sidebar_view += ub.SIDEBAR_SERIES
        if "show_category" in to_save:
            content.sidebar_view += ub.SIDEBAR_CATEGORY
        if "show_hot" in to_save:
            content.sidebar_view += ub.SIDEBAR_HOT
        if "show_read_and_unread" in to_save:
            content.sidebar_view += ub.SIDEBAR_READ_AND_UNREAD
        if "show_best_rated" in to_save:
            content.sidebar_view += ub.SIDEBAR_BEST_RATED
        if "show_author" in to_save:
            content.sidebar_view += ub.SIDEBAR_AUTHOR
        if "show_publisher" in to_save:
            content.sidebar_view += ub.SIDEBAR_PUBLISHER
        if "show_detail_random" in to_save:
            content.sidebar_view += ub.DETAIL_RANDOM
        if "show_sorted" in to_save:
            content.sidebar_view += ub.SIDEBAR_SORTED
        if "show_recent" in to_save:
            content.sidebar_view += ub.SIDEBAR_RECENT

        content.role = 0
        if "admin_role" in to_save:
            content.role = content.role + ub.ROLE_ADMIN
        if "download_role" in to_save:
            content.role = content.role + ub.ROLE_DOWNLOAD
        if "upload_role" in to_save:
            content.role = content.role + ub.ROLE_UPLOAD
        if "edit_role" in to_save:
            content.role = content.role + ub.ROLE_EDIT
        if "delete_role" in to_save:
            content.role = content.role + ub.ROLE_DELETE_BOOKS
        if "passwd_role" in to_save:
            content.role = content.role + ub.ROLE_PASSWD
        if "edit_shelf_role" in to_save:
            content.role = content.role + ub.ROLE_EDIT_SHELFS
        if not to_save["nickname"] or not to_save["email"] or not to_save["password"]:
            flash(_("Please fill out all fields!"), category="error")
            return render_title_template(
                "user_edit.html",
                new_user=1,
                content=content,
                translations=translations,
                title=_("Add new user"),
            )
        content.password = generate_password_hash(to_save["password"])
        existing_user = (
            ub.session.query(ub.User)
            .filter(func.lower(ub.User.nickname) == to_save["nickname"].lower())
            .first()
        )
        existing_email = (
            ub.session.query(ub.User)
            .filter(ub.User.email == to_save["email"].lower())
            .first()
        )
        if not existing_user and not existing_email:
            content.nickname = to_save["nickname"]
            if config.config_public_reg and not check_valid_domain(to_save["email"]):
                flash(_("E-mail is not from valid domain"), category="error")
                return render_title_template(
                    "user_edit.html",
                    new_user=1,
                    content=content,
                    translations=translations,
                    title=_("Add new user"),
                )
            else:
                content.email = to_save["email"]
        else:
            flash(
                _("Found an existing account for this e-mail address or nickname."),
                category="error",
            )
            return render_title_template(
                "user_edit.html",
                new_user=1,
                content=content,
                translations=translations,
                languages=languages,
                title=_("Add new user"),
                page="newuser",
            )
        try:
            ub.session.add(content)
            ub.session.commit()
            flash(
                _("User '%(user)s' created", user=content.nickname), category="success"
            )
            return redirect(url_for("admin"))
        except IntegrityError:
            ub.session.rollback()
            flash(
                _("Found an existing account for this e-mail address or nickname."),
                category="error",
            )
    else:
        content.role = config.config_default_role
        content.sidebar_view = config.config_default_show
        content.mature_content = bool(config.config_default_show & ub.MATURE_CONTENT)
    return render_title_template(
        "user_edit.html",
        new_user=1,
        content=content,
        translations=translations,
        languages=languages,
        title=_("Add new user"),
        page="newuser",
    )