jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_progress(self, driver, live_server, upload_file, freeze_upload_folder):
    driver.get(live_server + self.url)
    file_input = driver.find_element(By.XPATH, "//input[@name='file']")
    file_input.send_keys(upload_file)
    assert file_input.get_attribute("name") == "file"
    save_button = driver.find_element(By.XPATH, "//input[@name='save']")
    with wait_for_page_load(driver, timeout=10):
        save_button.click()
    assert "save" in driver.page_source

    driver.get(live_server + self.url)
    file_input = driver.find_element(By.XPATH, "//input[@name='file']")
    file_input.send_keys(upload_file)
    assert file_input.get_attribute("name") == "file"
    save_button = driver.find_element(By.XPATH, "//button[@name='save_continue']")
    with wait_for_page_load(driver, timeout=10):
        save_button.click()
    response = json.loads(driver.find_elements(By.CSS_SELECTOR, "pre")[0].text)
    assert response["POST"]["progress"] == "1"
//  [python]
def test_progress(self, driver, live_server, upload_file, freeze):
    driver.get(live_server + self.url)
    file_input = driver.find_element(By.XPATH, "//input[@name='file']")
    file_input.send_keys(upload_file)
    assert file_input.get_attribute("name") == "file"
    save_button = driver.find_element(By.XPATH, "//input[@name='save']")
    with wait_for_page_load(driver, timeout=10):
        save_button.click()
    assert "save" in driver.page_source

    driver.get(live_server + self.url)
    file_input = driver.find_element(By.XPATH, "//input[@name='file']")
    file_input.send_keys(upload_file)
    assert file_input.get_attribute("name") == "file"
    save_button = driver.find_element(By.XPATH, "//button[@name='save_continue']")
    with wait_for_page_load(driver, timeout=10):
        save_button.click()
    response = json.loads(driver.find_elements(By.CSS_SELECTOR, "pre")[0].text)
    assert response["POST"]["progress"] == "1"
//  [python]
def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, datetime.date]:
    a_camel_date_time: Union[datetime.datetime, datetime.date]
    try:
        a_camel_date_time = datetime.datetime.fromisoformat(d["aCamelDateTime"])

        return a_camel_date_time
    except:
        pass
    a_camel_date_time = datetime.date.fromisoformat(d["aCamelDateTime"])

    return a_camel_date_time
//  [python]
def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime, date]:
    a_camel_date_time: Union[datetime, date]
    try:
        a_camel_date_time = datetime.fromisoformat(d["aCamelDateTime"])

        return a_camel_date_time
    except:
        pass
    a_camel_date_time = date.fromisoformat(d["aCamelDateTime"])

    return a_camel_date_time
//  [python]
def to_dict(self) -> Dict[str, Any]:
    an_enum_value = self.an_enum_value.value

    some_dict = self.some_dict

    if isinstance(self.a_camel_date_time, datetime.datetime):
        a_camel_date_time = self.a_camel_date_time.isoformat()

    else:
        a_camel_date_time = self.a_camel_date_time.isoformat()

    a_date = self.a_date.isoformat()

    if self.nested_list_of_enums is None:
        nested_list_of_enums = None
    else:
        nested_list_of_enums = []
        for nested_list_of_enums_item_data in self.nested_list_of_enums:
            nested_list_of_enums_item = []
            for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
                nested_list_of_enums_item_item = nested_list_of_enums_item_item_data.value

                nested_list_of_enums_item.append(nested_list_of_enums_item_item)

            nested_list_of_enums.append(nested_list_of_enums_item)

    return {
        "an_enum_value": an_enum_value,
        "some_dict": some_dict,
        "aCamelDateTime": a_camel_date_time,
        "a_date": a_date,
        "nested_list_of_enums": nested_list_of_enums,
    }
//  [python]
def to_dict(self) -> Dict[str, Any]:
    an_enum_value = self.an_enum_value.value

    if isinstance(self.a_camel_date_time, datetime):
        a_camel_date_time = self.a_camel_date_time.isoformat()

    else:
        a_camel_date_time = self.a_camel_date_time.isoformat()

    a_date = self.a_date.isoformat()

    if self.nested_list_of_enums is None:
        nested_list_of_enums = None
    else:
        nested_list_of_enums = []
        for nested_list_of_enums_item_data in self.nested_list_of_enums:
            nested_list_of_enums_item = []
            for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
                nested_list_of_enums_item_item = nested_list_of_enums_item_item_data.value

                nested_list_of_enums_item.append(nested_list_of_enums_item_item)

            nested_list_of_enums.append(nested_list_of_enums_item)

    some_dict = self.some_dict

    return {
        "an_enum_value": an_enum_value,
        "aCamelDateTime": a_camel_date_time,
        "a_date": a_date,
        "nested_list_of_enums": nested_list_of_enums,
        "some_dict": some_dict,
    }
//  [python]
def format_errormsg(message: str) -> Markup:
    """Match account names in error messages and insert HTML links for them."""
    match = re.search(ACCOUNT_RE, message)
    if not match:
        return Markup(message)
    account = match.group()
    url = url_for("account", name=account)
    return Markup(
        message.replace(account, f'<a href="{url}">{account}</a>')
        .replace("for '", "for ")
        .replace("': ", ": ")
    )
//  [python]
def format_errormsg(message: str) -> str:
    """Match account names in error messages and insert HTML links for them."""
    match = re.search(ACCOUNT_RE, message)
    if not match:
        return message
    account = match.group()
    url = flask.url_for("account", name=account)
    return (
        message.replace(account, f'<a href="{url}">{account}</a>')
        .replace("for '", "for ")
        .replace("': ", ": ")
    )
//  [python]
def _db_configuration_update_helper():
    db_change = False
    to_save = request.form.to_dict()
    gdrive_error = None

    to_save['config_calibre_dir'] = re.sub(r'[\\/]metadata\.db$',
                                           '',
                                           to_save['config_calibre_dir'],
                                           flags=re.IGNORECASE)
    db_valid = False
    try:
        db_change, db_valid = _db_simulate_change()

        # gdrive_error drive setup
        gdrive_error = _configuration_gdrive_helper(to_save)
    except (OperationalError, InvalidRequestError) as e:
        ub.session.rollback()
        log.error_or_exception("Settings Database error: {}".format(e))
        _db_configuration_result(_(u"Database error: %(error)s.", error=e.orig), gdrive_error)
    try:
        metadata_db = os.path.join(to_save['config_calibre_dir'], "metadata.db")
        if config.config_use_google_drive and is_gdrive_ready() and not os.path.exists(metadata_db):
            gdriveutils.downloadFile(None, "metadata.db", metadata_db)
            db_change = True
    except Exception as ex:
        return _db_configuration_result('{}'.format(ex), gdrive_error)

    if db_change or not db_valid or not config.db_configured \
            or config.config_calibre_dir != to_save["config_calibre_dir"]:
        if not calibre_db.setup_db(to_save['config_calibre_dir'], ub.app_DB_path):
            return _db_configuration_result(_('DB Location is not Valid, Please Enter Correct Path'),
                                            gdrive_error)
        config.store_calibre_uuid(calibre_db, db.LibraryId)
        # if db changed -> delete shelfs, delete download books, delete read books, kobo sync...
        if db_change:
            log.info("Calibre Database changed, delete all Calibre-Web info related to old Database")
            ub.session.query(ub.Downloads).delete()
            ub.session.query(ub.ArchivedBook).delete()
            ub.session.query(ub.ReadBook).delete()
            ub.session.query(ub.BookShelf).delete()
            ub.session.query(ub.Bookmark).delete()
            ub.session.query(ub.KoboReadingState).delete()
            ub.session.query(ub.KoboStatistics).delete()
            ub.session.query(ub.KoboSyncedBooks).delete()
            ub.session_commit()
        _config_string(to_save, "config_calibre_dir")
        calibre_db.update_config(config)
        if not os.access(os.path.join(config.config_calibre_dir, "metadata.db"), os.W_OK):
            flash(_(u"DB is not Writeable"), category="warning")
    config.save()
    return _db_configuration_result(None, gdrive_error)
//  [python]
def _db_configuration_update_helper():
    db_change = False
    to_save = request.form.to_dict()
    gdrive_error = None

    to_save['config_calibre_dir'] = re.sub(r'[\\/]metadata\.db$',
                                           '',
                                           to_save['config_calibre_dir'],
                                           flags=re.IGNORECASE)
    try:
        db_change, db_valid = _db_simulate_change()

        # gdrive_error drive setup
        gdrive_error = _configuration_gdrive_helper(to_save)
    except (OperationalError, InvalidRequestError) as e:
        ub.session.rollback()
        log.error_or_exception("Settings Database error: {}".format(e))
        _db_configuration_result(_(u"Database error: %(error)s.", error=e.orig), gdrive_error)
    try:
        metadata_db = os.path.join(to_save['config_calibre_dir'], "metadata.db")
        if config.config_use_google_drive and is_gdrive_ready() and not os.path.exists(metadata_db):
            gdriveutils.downloadFile(None, "metadata.db", metadata_db)
            db_change = True
    except Exception as ex:
        return _db_configuration_result('{}'.format(ex), gdrive_error)

    if db_change or not db_valid or not config.db_configured \
          or config.config_calibre_dir != to_save["config_calibre_dir"]:
        if not calibre_db.setup_db(to_save['config_calibre_dir'], ub.app_DB_path):
            return _db_configuration_result(_('DB Location is not Valid, Please Enter Correct Path'),
                                            gdrive_error)
        config.store_calibre_uuid(calibre_db, db.Library_Id)
        # if db changed -> delete shelfs, delete download books, delete read books, kobo sync...
        if db_change:
            log.info("Calibre Database changed, delete all Calibre-Web info related to old Database")
            ub.session.query(ub.Downloads).delete()
            ub.session.query(ub.ArchivedBook).delete()
            ub.session.query(ub.ReadBook).delete()
            ub.session.query(ub.BookShelf).delete()
            ub.session.query(ub.Bookmark).delete()
            ub.session.query(ub.KoboReadingState).delete()
            ub.session.query(ub.KoboStatistics).delete()
            ub.session.query(ub.KoboSyncedBooks).delete()
            ub.session_commit()
        _config_string(to_save, "config_calibre_dir")
        calibre_db.update_config(config)
        if not os.access(os.path.join(config.config_calibre_dir, "metadata.db"), os.W_OK):
            flash(_(u"DB is not Writeable"), category="warning")
    config.save()
    return _db_configuration_result(None, gdrive_error)
//  [python]
def admin():
    version = updater_thread.get_current_version_info()
    if version is False:
        commit = _(u'Unknown')
    else:
        if 'datetime' in version:
            commit = version['datetime']

            tz = timedelta(seconds=time.timezone if (time.localtime().tm_isdst == 0) else time.altzone)
            form_date = datetime.strptime(commit[:19], "%Y-%m-%dT%H:%M:%S")
            if len(commit) > 19:    # check if string has timezone
                if commit[19] == '+':
                    form_date -= timedelta(hours=int(commit[20:22]), minutes=int(commit[23:]))
                elif commit[19] == '-':
                    form_date += timedelta(hours=int(commit[20:22]), minutes=int(commit[23:]))
            commit = format_datetime(form_date - tz, format='short', locale=get_locale())
        else:
            commit = version['version']

    all_user = ub.session.query(ub.User).all()
    email_settings = config.get_mail_settings()
    kobo_support = feature_support['kobo'] and config.config_kobo_sync
    return render_title_template("admin.html", allUser=all_user, email=email_settings, config=config, commit=commit,
                                 feature_support=feature_support, kobo_support=kobo_support,
                                 title=_(u"Admin page"), page="admin")
//  [python]
def admin():
    version = updater_thread.get_current_version_info()
    if version is False:
        commit = _(u'Unknown')
    else:
        if 'datetime' in version:
            commit = version['datetime']

            tz = timedelta(seconds=time.timezone if (time.localtime().tm_isdst == 0) else time.altzone)
            form_date = datetime.strptime(commit[:19], "%Y-%m-%dT%H:%M:%S")
            if len(commit) > 19:    # check if string has timezone
                if commit[19] == '+':
                    form_date -= timedelta(hours=int(commit[20:22]), minutes=int(commit[23:]))
                elif commit[19] == '-':
                    form_date += timedelta(hours=int(commit[20:22]), minutes=int(commit[23:]))
            commit = format_datetime(form_date - tz, format='short', locale=get_locale())
        else:
            commit = version['version']

    allUser = ub.session.query(ub.User).all()
    email_settings = config.get_mail_settings()
    kobo_support = feature_support['kobo'] and config.config_kobo_sync
    return render_title_template("admin.html", allUser=allUser, email=email_settings, config=config, commit=commit,
                                 feature_support=feature_support, kobo_support=kobo_support,
                                 title=_(u"Admin page"), page="admin")
//  [python]
def _configuration_oauth_helper(to_save):
    active_oauths = 0
    reboot_required = False
    for element in oauthblueprints:
        if to_save["config_" + str(element['id']) + "_oauth_client_id"] != element['oauth_client_id'] \
                or to_save["config_" + str(element['id']) + "_oauth_client_secret"] != element['oauth_client_secret']:
            reboot_required = True
            element['oauth_client_id'] = to_save["config_" + str(element['id']) + "_oauth_client_id"]
            element['oauth_client_secret'] = to_save["config_" + str(element['id']) + "_oauth_client_secret"]
        if to_save["config_" + str(element['id']) + "_oauth_client_id"] \
                and to_save["config_" + str(element['id']) + "_oauth_client_secret"]:
            active_oauths += 1
            element["active"] = 1
        else:
            element["active"] = 0
        ub.session.query(ub.OAuthProvider).filter(ub.OAuthProvider.id == element['id']).update(
            {"oauth_client_id": to_save["config_" + str(element['id']) + "_oauth_client_id"],
             "oauth_client_secret": to_save["config_" + str(element['id']) + "_oauth_client_secret"],
             "active": element["active"]})
    return reboot_required
//  [python]
def _configuration_oauth_helper(to_save):
    active_oauths = 0
    reboot_required = False
    for element in oauthblueprints:
        if to_save["config_" + str(element['id']) + "_oauth_client_id"] != element['oauth_client_id'] \
            or to_save["config_" + str(element['id']) + "_oauth_client_secret"] != element['oauth_client_secret']:
            reboot_required = True
            element['oauth_client_id'] = to_save["config_" + str(element['id']) + "_oauth_client_id"]
            element['oauth_client_secret'] = to_save["config_" + str(element['id']) + "_oauth_client_secret"]
        if to_save["config_" + str(element['id']) + "_oauth_client_id"] \
            and to_save["config_" + str(element['id']) + "_oauth_client_secret"]:
            active_oauths += 1
            element["active"] = 1
        else:
            element["active"] = 0
        ub.session.query(ub.OAuthProvider).filter(ub.OAuthProvider.id == element['id']).update(
            {"oauth_client_id": to_save["config_" + str(element['id']) + "_oauth_client_id"],
             "oauth_client_secret": to_save["config_" + str(element['id']) + "_oauth_client_secret"],
             "active": element["active"]})
    return reboot_required
//  [python]
def check_valid_restricted_column(column):
    if column != "0":
        if not calibre_db.session.query(db.CustomColumns).filter(db.CustomColumns.id == column) \
              .filter(and_(db.CustomColumns.datatype == 'text', db.CustomColumns.mark_for_delete == 0)).all():
            return False
    return True
//  [python]
def check_valid_restricted_column(column):
    if column != "0":
        if not calibre_db.session.query(db.Custom_Columns).filter(db.Custom_Columns.id == column) \
              .filter(and_(db.Custom_Columns.datatype == 'text', db.Custom_Columns.mark_for_delete == 0)).all():
            return False
    return True
//  [python]
def check_valid_read_column(column):
    if column != "0":
        if not calibre_db.session.query(db.CustomColumns).filter(db.CustomColumns.id == column) \
              .filter(and_(db.CustomColumns.datatype == 'bool', db.CustomColumns.mark_for_delete == 0)).all():
            return False
    return True
//  [python]
def check_valid_read_column(column):
    if column != "0":
        if not calibre_db.session.query(db.Custom_Columns).filter(db.Custom_Columns.id == column) \
              .filter(and_(db.Custom_Columns.datatype == 'bool', db.Custom_Columns.mark_for_delete == 0)).all():
            return False
    return True
//  [python]
def edit_user_table():
    visibility = current_user.view_settings.get('useredit', {})
    languages = calibre_db.speaking_language()
    translations = babel.list_translations() + [Locale('en')]
    all_user = ub.session.query(ub.User)
    tags = calibre_db.session.query(db.Tags)\
        .join(db.books_tags_link)\
        .join(db.Books)\
        .filter(calibre_db.common_filters()) \
        .group_by(text('books_tags_link.tag'))\
        .order_by(db.Tags.name).all()
    if config.config_restricted_column:
        custom_values = calibre_db.session.query(db.cc_classes[config.config_restricted_column]).all()
    else:
        custom_values = []
    if not config.config_anonbrowse:
        all_user = all_user.filter(ub.User.role.op('&')(constants.ROLE_ANONYMOUS) != constants.ROLE_ANONYMOUS)
    kobo_support = feature_support['kobo'] and config.config_kobo_sync
    return render_title_template("user_table.html",
                                 users=all_user.all(),
                                 tags=tags,
                                 custom_values=custom_values,
                                 translations=translations,
                                 languages=languages,
                                 visiblility=visibility,
                                 all_roles=constants.ALL_ROLES,
                                 kobo_support=kobo_support,
                                 sidebar_settings=constants.sidebar_settings,
                                 title=_(u"Edit Users"),
                                 page="usertable")
//  [python]
def edit_user_table():
    visibility = current_user.view_settings.get('useredit', {})
    languages = calibre_db.speaking_language()
    translations = babel.list_translations() + [LC('en')]
    allUser = ub.session.query(ub.User)
    tags = calibre_db.session.query(db.Tags)\
        .join(db.books_tags_link)\
        .join(db.Books)\
        .filter(calibre_db.common_filters()) \
        .group_by(text('books_tags_link.tag'))\
        .order_by(db.Tags.name).all()
    if config.config_restricted_column:
        custom_values = calibre_db.session.query(db.cc_classes[config.config_restricted_column]).all()
    else:
        custom_values = []
    if not config.config_anonbrowse:
        allUser = allUser.filter(ub.User.role.op('&')(constants.ROLE_ANONYMOUS) != constants.ROLE_ANONYMOUS)
    kobo_support = feature_support['kobo'] and config.config_kobo_sync
    return render_title_template("user_table.html",
                                 users=allUser.all(),
                                 tags=tags,
                                 custom_values=custom_values,
                                 translations=translations,
                                 languages=languages,
                                 visiblility=visibility,
                                 all_roles=constants.ALL_ROLES,
                                 kobo_support=kobo_support,
                                 sidebar_settings=constants.sidebar_settings,
                                 title=_(u"Edit Users"),
                                 page="usertable")
//  [python]
def list_users():
    off = int(request.args.get("offset") or 0)
    limit = int(request.args.get("limit") or 10)
    search = request.args.get("search")
    sort = request.args.get("sort", "id")
    state = None
    if sort == "state":
        state = json.loads(request.args.get("state", "[]"))
    else:
        if sort not in ub.User.__table__.columns.keys():
            sort = "id"
    order = request.args.get("order", "").lower()

    if sort != "state" and order:
        order = text(sort + " " + order)
    elif not state:
        order = ub.User.id.asc()

    all_user = ub.session.query(ub.User)
    if not config.config_anonbrowse:
        all_user = all_user.filter(ub.User.role.op('&')(constants.ROLE_ANONYMOUS) != constants.ROLE_ANONYMOUS)

    total_count = filtered_count = all_user.count()

    if search:
        all_user = all_user.filter(or_(func.lower(ub.User.name).ilike("%" + search + "%"),
                                       func.lower(ub.User.kindle_mail).ilike("%" + search + "%"),
                                       func.lower(ub.User.email).ilike("%" + search + "%")))
    if state:
        users = calibre_db.get_checkbox_sorted(all_user.all(), state, off, limit, request.args.get("order", "").lower())
    else:
        users = all_user.order_by(order).offset(off).limit(limit).all()
    if search:
        filtered_count = len(users)

    for user in users:
        if user.default_language == "all":
            user.default = _("All")
        else:
            user.default = Locale.parse(user.default_language).get_language_name(get_locale())

    table_entries = {'totalNotFiltered': total_count, 'total': filtered_count, "rows": users}
    js_list = json.dumps(table_entries, cls=db.AlchemyEncoder)
    response = make_response(js_list)
    response.headers["Content-Type"] = "application/json; charset=utf-8"
    return response
//  [python]
def list_users():
    off = int(request.args.get("offset") or 0)
    limit = int(request.args.get("limit") or 10)
    search = request.args.get("search")
    sort = request.args.get("sort", "id")
    state = None
    if sort == "state":
        state = json.loads(request.args.get("state", "[]"))
    else:
        if sort not in ub.User.__table__.columns.keys():
            sort = "id"
    order = request.args.get("order", "").lower()

    if sort != "state" and order:
        order = text(sort + " " + order)
    elif not state:
        order = ub.User.id.asc()

    all_user = ub.session.query(ub.User)
    if not config.config_anonbrowse:
        all_user = all_user.filter(ub.User.role.op('&')(constants.ROLE_ANONYMOUS) != constants.ROLE_ANONYMOUS)

    total_count = filtered_count = all_user.count()

    if search:
        all_user = all_user.filter(or_(func.lower(ub.User.name).ilike("%" + search + "%"),
                                       func.lower(ub.User.kindle_mail).ilike("%" + search + "%"),
                                       func.lower(ub.User.email).ilike("%" + search + "%")))
    if state:
        users = calibre_db.get_checkbox_sorted(all_user.all(), state, off, limit, request.args.get("order", "").lower())
    else:
        users = all_user.order_by(order).offset(off).limit(limit).all()
    if search:
        filtered_count = len(users)

    for user in users:
        if user.default_language == "all":
            user.default = _("All")
        else:
            user.default = LC.parse(user.default_language).get_language_name(get_locale())

    table_entries = {'totalNotFiltered': total_count, 'total': filtered_count, "rows": users}
    js_list = json.dumps(table_entries, cls=db.AlchemyEncoder)
    response = make_response(js_list)
    response.headers["Content-Type"] = "application/json; charset=utf-8"
    return response
//  [python]
def pref_get(key):
    if get_user() is None:
        return "Authentication required", 401

    if key in get_preferences():
        return Response(
            json.dumps({'key': key, 'value': get_preferences()[key]}),
            mimetype='application/json'
        )
    else:
        return Response(
            json.dumps({'key': key, 'error': 'novalue'}),
            mimetype='application/json'
        )
//  [python]
def pref_get(key):
    if get_user() is None:
        return "Authentication required", 401

    if key in get_preferences():
        return Response(json.dumps({'key': key, 'value': get_preferences()[key]}))
    else:
        return Response(json.dumps({'key': key, 'error': 'novalue'}))
//  [python]
def new_user():
    content = ub.User()
    languages = calibre_db.speaking_language()
    translations = [Locale('en')] + babel.list_translations()
    kobo_support = feature_support['kobo'] and config.config_kobo_sync
    if request.method == "POST":
        to_save = request.form.to_dict()
        _handle_new_user(to_save, content, languages, translations, kobo_support)
    else:
        content.role = config.config_default_role
        content.sidebar_view = config.config_default_show
        content.locale = config.config_default_locale
        content.default_language = config.config_default_language
    return render_title_template("user_edit.html", new_user=1, content=content,
                                 config=config, translations=translations,
                                 languages=languages, title=_(u"Add new user"), page="newuser",
                                 kobo_support=kobo_support, registered_oauth=oauth_check)
//  [python]
def new_user():
    content = ub.User()
    languages = calibre_db.speaking_language()
    translations = [LC('en')] + babel.list_translations()
    kobo_support = feature_support['kobo'] and config.config_kobo_sync
    if request.method == "POST":
        to_save = request.form.to_dict()
        _handle_new_user(to_save, content, languages, translations, kobo_support)
    else:
        content.role = config.config_default_role
        content.sidebar_view = config.config_default_show
        content.locale = config.config_default_locale
        content.default_language = config.config_default_language
    return render_title_template("user_edit.html", new_user=1, content=content,
                                 config=config, translations=translations,
                                 languages=languages, title=_(u"Add new user"), page="newuser",
                                 kobo_support=kobo_support, registered_oauth=oauth_check)
//  [python]
def pref_set(key, value):
    if get_user() is None:
        return "Authentication required", 401

    get_preferences()[key] = (None if value == 'null' else value)
    return Response(
        json.dumps({'key': key, 'success': ''}),
        mimetype='application/json'
    ), 201
//  [python]
def pref_set(key, value):
    if get_user() is None:
        return "Authentication required", 401

    get_preferences()[key] = (None if value == 'null' else value)
    return Response(json.dumps({'key': key, 'success': ''})), 201
//  [python]
def get_revision(self):
    """Read svn revision information for the Bcfg2 repository."""
    try:
        data = Popen(("env LC_ALL=C svn info %s" %
                     pipes.quote(self.datastore)), shell=True,
                     stdout=PIPE).communicate()[0].split('\n')
        return [line.split(': ')[1] for line in data \
                if line[:9] == 'Revision:'][-1]
    except IndexError:
        logger.error("Failed to read svn info; disabling svn support")
        logger.error('''Ran command "svn info %s"''' % (self.datastore))
        logger.error("Got output: %s" % data)
        raise Bcfg2.Server.Plugin.PluginInitError
//  [python]
def get_revision(self):
    """Read svn revision information for the Bcfg2 repository."""
    try:
        data = Popen(("env LC_ALL=C svn info %s" %
                     (self.datastore)), shell=True,
                     stdout=PIPE).communicate()[0].split('\n')
        return [line.split(': ')[1] for line in data \
                if line[:9] == 'Revision:'][-1]
    except IndexError:
        logger.error("Failed to read svn info; disabling svn support")
        logger.error('''Ran command "svn info %s"''' % (self.datastore))
        logger.error("Got output: %s" % data)
        raise Bcfg2.Server.Plugin.PluginInitError
//  [python]
def view_configuration():
    read_column = calibre_db.session.query(db.CustomColumns)\
        .filter(and_(db.CustomColumns.datatype == 'bool', db.CustomColumns.mark_for_delete == 0)).all()
    restrict_columns = calibre_db.session.query(db.CustomColumns)\
        .filter(and_(db.CustomColumns.datatype == 'text', db.CustomColumns.mark_for_delete == 0)).all()
    languages = calibre_db.speaking_language()
    translations = [Locale('en')] + babel.list_translations()
    return render_title_template("config_view_edit.html", conf=config, readColumns=read_column,
                                 restrictColumns=restrict_columns,
                                 languages=languages,
                                 translations=translations,
                                 title=_(u"UI Configuration"), page="uiconfig")
//  [python]
def view_configuration():
    read_column = calibre_db.session.query(db.Custom_Columns)\
        .filter(and_(db.Custom_Columns.datatype == 'bool', db.Custom_Columns.mark_for_delete == 0)).all()
    restrict_columns = calibre_db.session.query(db.Custom_Columns)\
        .filter(and_(db.Custom_Columns.datatype == 'text', db.Custom_Columns.mark_for_delete == 0)).all()
    languages = calibre_db.speaking_language()
    translations = [LC('en')] + babel.list_translations()
    return render_title_template("config_view_edit.html", conf=config, readColumns=read_column,
                                 restrictColumns=restrict_columns,
                                 languages=languages,
                                 translations=translations,
                                 title=_(u"UI Configuration"), page="uiconfig")
//  [python]
def __init__(self, *, openapi: GeneratorData) -> None:
    self.openapi: GeneratorData = openapi
    self.env: Environment = Environment(loader=PackageLoader(__package__), trim_blocks=True, lstrip_blocks=True)

    self.project_name: str = self.project_name_override or f"{openapi.title.replace(' ', '-').lower()}-client"
    self.project_dir: Path = Path.cwd() / self.project_name

    self.package_name: str = self.package_name_override or self.project_name.replace("-", "_")
    self.package_dir: Path = self.project_dir / self.package_name
    self.package_description: str = f"A client library for accessing {self.openapi.title}"
    self.version: str = openapi.version

    self.env.filters.update(self.TEMPLATE_FILTERS)
//  [python]
def load(doc):
    code = config.retrieveBoilerplateFile(doc, "bs-extensions", allowLocal=constants.executeCode)
    exec(code, globals())
//  [python]
def load(doc):
    code = config.retrieveBoilerplateFile(doc, "bs-extensions")
    exec(code, globals())
//  [python]
def table_get_locale():
    locale = babel.list_translations() + [Locale('en')]
    ret = list()
    current_locale = get_locale()
    for loc in locale:
        ret.append({'value': str(loc), 'text': loc.get_language_name(current_locale)})
    return json.dumps(ret)
//  [python]
def table_get_locale():
    locale = babel.list_translations() + [LC('en')]
    ret = list()
    current_locale = get_locale()
    for loc in locale:
        ret.append({'value': str(loc), 'text': loc.get_language_name(current_locale)})
    return json.dumps(ret)
//  [python]
def _get_unauth_response(self, request, reason):
    """
    Get an error response (or raise a Problem) for a given request and reason message.

    :type request: Request.
    :param request: HttpRequest
    :type reason: Reason string.
    :param reason: str
    """
    if request.is_ajax():
        return HttpResponseForbidden(json.dumps({"error": force_text(reason)}))
    error_params = urlencode({"error": force_text(reason)})
    login_url = force_str(reverse("shuup_admin:login") + "?" + error_params)
    resp = redirect_to_login(next=request.path, login_url=login_url)
    if is_authenticated(request.user):
        # Instead of redirecting to the login page, let the user know what's wrong with
        # a helpful link.
        raise (
            Problem(_("Can't view this page. %(reason)s") % {"reason": escape(reason)}).with_link(
                url=resp.url, title=_("Log in with different credentials...")
            )
        )
    return resp
//  [python]
def _get_unauth_response(self, request, reason):
    """
    Get an error response (or raise a Problem) for a given request and reason message.

    :type request: Request.
    :param request: HttpRequest
    :type reason: Reason string.
    :param reason: str
    """
    if request.is_ajax():
        return HttpResponseForbidden(json.dumps({"error": force_text(reason)}))
    error_params = urlencode({"error": force_text(reason)})
    login_url = force_str(reverse("shuup_admin:login") + "?" + error_params)
    resp = redirect_to_login(next=request.path, login_url=login_url)
    if is_authenticated(request.user):
        # Instead of redirecting to the login page, let the user know what's wrong with
        # a helpful link.
        raise (
            Problem(_("Can't view this page. %(reason)s") % {"reason": reason}).with_link(
                url=resp.url, title=_("Log in with different credentials...")
            )
        )
    return resp
//  [python]
def json_dumps(value, indent=None):
    if isinstance(value, QuerySet):
        result = serialize('json', value, indent=indent)
    else:
        result = json.dumps(value, indent=indent, cls=DjbletsJSONEncoder)

    return mark_safe(force_text(result).translate(_safe_js_escapes))
//  [python]
def json_dumps(value, indent=None):
    if isinstance(value, QuerySet):
        result = serialize('json', value, indent=indent)
    else:
        result = json.dumps(value, indent=indent, cls=DjbletsJSONEncoder)

    return mark_safe(result)
//  [python]
def get_recipe_from_file(self, file):

    recipe = Recipe.objects.create(
        name=file['name'].strip(),
        created_by=self.request.user, internal=True,
        space=self.request.space)

    try:
        if file['recipeYield'] != '':
            recipe.servings = int(file['recipeYield'])

        if file['totalTime'] != '':
            recipe.waiting_time = int(file['totalTime']) - int(file['timePrep'])

        if file['prepTime'] != '':
            recipe.working_time = int(file['timePrep'])

        recipe.save()
    except Exception as e:
        print('failed to parse yield or time ', str(e))

    ingredient_parser = IngredientParser(self.request, True)
    ingredients_added = False
    for s in file['recipeInstructions']:
        step = Step.objects.create(
            instruction=s['text'], space=self.request.space,
        )
        if not ingredients_added:
            ingredients_added = True

            for ingredient in file['recipeIngredient']:
                amount, unit, food, note = ingredient_parser.parse(ingredient)
                f = ingredient_parser.get_food(food)
                u = ingredient_parser.get_unit(unit)
                step.ingredients.add(Ingredient.objects.create(
                    food=f, unit=u, amount=amount, note=note, original_text=ingredient, space=self.request.space,
                ))
        recipe.steps.add(step)

    if len(file['image']) > 0:
        try:
            url = file['image'][0]
            if validators.url(url, public=True):
                response = requests.get(url)
                self.import_recipe_image(recipe, BytesIO(response.content))
        except Exception as e:
            print('failed to import image ', str(e))

    return recipe
//  [python]
def get_recipe_from_file(self, file):

    recipe = Recipe.objects.create(
        name=file['name'].strip(),
        created_by=self.request.user, internal=True,
        space=self.request.space)

    try:
        if file['recipeYield'] != '':
            recipe.servings = int(file['recipeYield'])

        if file['totalTime'] != '':
            recipe.waiting_time = int(file['totalTime']) - int(file['timePrep'])

        if file['prepTime'] != '':
            recipe.working_time = int(file['timePrep'])

        recipe.save()
    except Exception as e:
        print('failed to parse yield or time ', str(e))

    ingredient_parser = IngredientParser(self.request, True)
    ingredients_added = False
    for s in file['recipeInstructions']:
        step = Step.objects.create(
            instruction=s['text'], space=self.request.space,
        )
        if not ingredients_added:
            ingredients_added = True

            for ingredient in file['recipeIngredient']:
                amount, unit, food, note = ingredient_parser.parse(ingredient)
                f = ingredient_parser.get_food(food)
                u = ingredient_parser.get_unit(unit)
                step.ingredients.add(Ingredient.objects.create(
                    food=f, unit=u, amount=amount, note=note, original_text=ingredient, space=self.request.space,
                ))
        recipe.steps.add(step)

    if len(file['image']) > 0:
        try:
            response = requests.get(file['image'][0])
            self.import_recipe_image(recipe, BytesIO(response.content))
        except Exception as e:
            print('failed to import image ', str(e))

    return recipe
//  [python]
def post(self, request, *args, **kwargs):  # doccov: ignore
    command = request.POST.get("command")
    if command:
        dispatcher = getattr(self, "dispatch_%s" % command, None)
        if not callable(dispatcher):
            raise Problem(_("Unknown command: `%s`.") % escape(command))
        dispatch_kwargs = dict(request.POST.items())
        rv = dispatcher(**dispatch_kwargs)
        if rv:
            return rv
        self.request.method = "GET"  # At this point, we won't want to cause form validation
        self.build_form()  # and it's not a bad idea to rebuild the form
        return super(EditorView, self).get(request, *args, **kwargs)

    if request.POST.get("save") and self.form and self.form.is_valid():
        self.form.save()
        self.save_layout()

        # after we save the new layout configs, make sure to reload the saved data in forms
        # so the returned get() response contains updated data
        self.build_form()

        if request.POST.get("publish") == "1":
            return self.dispatch_publish()

    return self.get(request, *args, **kwargs)
//  [python]
def post(self, request, *args, **kwargs):  # doccov: ignore
    command = request.POST.get("command")
    if command:
        dispatcher = getattr(self, "dispatch_%s" % command, None)
        if not callable(dispatcher):
            raise Problem(_("Unknown command: `%s`.") % command)
        dispatch_kwargs = dict(request.POST.items())
        rv = dispatcher(**dispatch_kwargs)
        if rv:
            return rv
        self.request.method = "GET"  # At this point, we won't want to cause form validation
        self.build_form()  # and it's not a bad idea to rebuild the form
        return super(EditorView, self).get(request, *args, **kwargs)

    if request.POST.get("save") and self.form and self.form.is_valid():
        self.form.save()
        self.save_layout()

        # after we save the new layout configs, make sure to reload the saved data in forms
        # so the returned get() response contains updated data
        self.build_form()

        if request.POST.get("publish") == "1":
            return self.dispatch_publish()

    return self.get(request, *args, **kwargs)
//  [python]
def run_query(self, query, user):
    path = ""
    ua = ""
    args = {}
    try:
        args = yaml.safe_load(query)
        path = args['url']
        args.pop('url', None)
        ua = args['user-agent']
        args.pop('user-agent', None)
    except:
        pass

    try:
        response = requests_or_advocate.get(url=path, headers={"User-agent": ua})
        workbook = pd.read_csv(io.BytesIO(response.content),sep=",", **args)

        df = workbook.copy()
        data = {'columns': [], 'rows': []}
        conversions = [
            {'pandas_type': np.integer, 'redash_type': 'integer',},
            {'pandas_type': np.inexact, 'redash_type': 'float',},
            {'pandas_type': np.datetime64, 'redash_type': 'datetime', 'to_redash': lambda x: x.strftime('%Y-%m-%d %H:%M:%S')},
            {'pandas_type': np.bool_, 'redash_type': 'boolean'},
            {'pandas_type': np.object, 'redash_type': 'string'}
        ]
        labels = []
        for dtype, label in zip(df.dtypes, df.columns):
            for conversion in conversions:
                if issubclass(dtype.type, conversion['pandas_type']):
                    data['columns'].append({'name': label, 'friendly_name': label, 'type': conversion['redash_type']})
                    labels.append(label)
                    func = conversion.get('to_redash')
                    if func:
                        df[label] = df[label].apply(func)
                    break
        data['rows'] = df[labels].replace({np.nan: None}).to_dict(orient='records')

        json_data = json_dumps(data)
        error = None
    except KeyboardInterrupt:
        error = "Query cancelled by user."
        json_data = None
    except UnacceptableAddressException:
        error = "Can't query private addresses."
        json_data = None
    except Exception as e:
        error = "Error reading {0}. {1}".format(path, str(e))
        json_data = None

    return json_data, error
//  [python]
def run_query(self, query, user):
    path = ""
    ua = ""
    args = {}
    try:
        args = yaml.safe_load(query)
        path = args['url']
        args.pop('url', None)
        ua = args['user-agent']
        args.pop('user-agent', None)

        if is_private_address(path) and settings.ENFORCE_PRIVATE_ADDRESS_BLOCK:
            raise Exception("Can't query private addresses.")
    except:
        pass

    try:
        response = requests.get(url=path, headers={"User-agent": ua})
        workbook = pd.read_csv(io.BytesIO(response.content),sep=",", **args)

        df = workbook.copy()
        data = {'columns': [], 'rows': []}
        conversions = [
            {'pandas_type': np.integer, 'redash_type': 'integer',},
            {'pandas_type': np.inexact, 'redash_type': 'float',},
            {'pandas_type': np.datetime64, 'redash_type': 'datetime', 'to_redash': lambda x: x.strftime('%Y-%m-%d %H:%M:%S')},
            {'pandas_type': np.bool_, 'redash_type': 'boolean'},
            {'pandas_type': np.object, 'redash_type': 'string'}
        ]
        labels = []
        for dtype, label in zip(df.dtypes, df.columns):
            for conversion in conversions:
                if issubclass(dtype.type, conversion['pandas_type']):
                    data['columns'].append({'name': label, 'friendly_name': label, 'type': conversion['redash_type']})
                    labels.append(label)
                    func = conversion.get('to_redash')
                    if func:
                        df[label] = df[label].apply(func)
                    break
        data['rows'] = df[labels].replace({np.nan: None}).to_dict(orient='records')

        json_data = json_dumps(data)
        error = None
    except KeyboardInterrupt:
        error = "Query cancelled by user."
        json_data = None
    except Exception as e:
        error = "Error reading {0}. {1}".format(path, str(e))
        json_data = None

    return json_data, error
//  [python]
def get_recipe_from_file(self, file):
    recipe_html = file.getvalue().decode("utf-8")

    recipe_json, recipe_tree, html_data, images = get_recipe_from_source(recipe_html, 'CookBookApp', self.request)

    recipe = Recipe.objects.create(
        name=recipe_json['name'].strip(),
        created_by=self.request.user, internal=True,
        space=self.request.space)

    try:
        recipe.servings = re.findall('([0-9])+', recipe_json['recipeYield'])[0]
    except Exception as e:
        pass

    try:
        recipe.working_time = iso_duration_to_minutes(recipe_json['prepTime'])
        recipe.waiting_time = iso_duration_to_minutes(recipe_json['cookTime'])
    except Exception:
        pass

    step = Step.objects.create(instruction=recipe_json['recipeInstructions'], space=self.request.space, )

    if 'nutrition' in recipe_json:
        step.instruction = step.instruction + '\n\n' + recipe_json['nutrition']

    step.save()
    recipe.steps.add(step)

    ingredient_parser = IngredientParser(self.request, True)
    for ingredient in recipe_json['recipeIngredient']:
        f = ingredient_parser.get_food(ingredient['ingredient']['text'])
        u = ingredient_parser.get_unit(ingredient['unit']['text'])
        step.ingredients.add(Ingredient.objects.create(
            food=f, unit=u, amount=ingredient['amount'], note=ingredient['note'],  space=self.request.space,
        ))

    if len(images) > 0:
        try:
            url = images[0]
            if validators.url(url, public=True):
                response = requests.get(url)
                self.import_recipe_image(recipe, BytesIO(response.content))
        except Exception as e:
            print('failed to import image ', str(e))

    recipe.save()
    return recipe
//  [python]
def get_recipe_from_file(self, file):
    recipe_html = file.getvalue().decode("utf-8")

    recipe_json, recipe_tree, html_data, images = get_recipe_from_source(recipe_html, 'CookBookApp', self.request)

    recipe = Recipe.objects.create(
        name=recipe_json['name'].strip(),
        created_by=self.request.user, internal=True,
        space=self.request.space)

    try:
        recipe.servings = re.findall('([0-9])+', recipe_json['recipeYield'])[0]
    except Exception as e:
        pass

    try:
        recipe.working_time = iso_duration_to_minutes(recipe_json['prepTime'])
        recipe.waiting_time = iso_duration_to_minutes(recipe_json['cookTime'])
    except Exception:
        pass

    step = Step.objects.create(instruction=recipe_json['recipeInstructions'], space=self.request.space, )

    if 'nutrition' in recipe_json:
        step.instruction = step.instruction + '\n\n' + recipe_json['nutrition']

    step.save()
    recipe.steps.add(step)

    ingredient_parser = IngredientParser(self.request, True)
    for ingredient in recipe_json['recipeIngredient']:
        f = ingredient_parser.get_food(ingredient['ingredient']['text'])
        u = ingredient_parser.get_unit(ingredient['unit']['text'])
        step.ingredients.add(Ingredient.objects.create(
            food=f, unit=u, amount=ingredient['amount'], note=ingredient['note'],  space=self.request.space,
        ))

    if len(images) > 0:
        try:
            response = requests.get(images[0])
            self.import_recipe_image(recipe, BytesIO(response.content))
        except Exception as e:
            print('failed to import image ', str(e))

    recipe.save()
    return recipe
//  [python]
def qute_settings(url):
    """Handler for qute://settings. View/change qute configuration."""
    global csrf_token

    if url.path() == '/set':
        if url.password() != csrf_token:
            message.error("Invalid CSRF token for qute://settings!")
            raise QuteSchemeError("Invalid CSRF token!",
                                  QNetworkReply.ContentAccessDenied)
        return _qute_settings_set(url)

    # Requests to qute://settings/set should only be allowed from
    # qute://settings. As an additional security precaution, we generate a CSRF
    # token to use here.
    if secrets:
        csrf_token = secrets.token_urlsafe()
    else:
        # On Python < 3.6, from secrets.py
        token = base64.urlsafe_b64encode(os.urandom(32))
        csrf_token = token.rstrip(b'=').decode('ascii')

    src = jinja.render('settings.html', title='settings',
                       configdata=configdata,
                       confget=config.instance.get_str,
                       csrf_token=csrf_token)
    return 'text/html', src