def set_item_param(self, item, key, value): """ Sets item param, applying additional conversion if needed. """ try: if key in config.parser.list_params: value = utils.strip_split_list(value, config.parser.list_sep) elif key in config.parser.file_list_params: value = utils.strip_split_list(value, config.parser.list_sep) filesize_value = [] for single_filename in value: #filenames start from slash, trimming it abspath = os.path.join(config.www.elibrary_dir, single_filename[1:]) if (os.path.isfile(abspath)): filesize_value.append(os.path.getsize(abspath)) else: logging.warn("File is not accessible: {0}".format(abspath)) filesize_value.append(0) item.set(const.FILE_SIZE_PARAM, filesize_value) elif key in config.parser.keyword_list_params: value = utils.strip_split_list(value, config.parser.list_sep) elif (key in config.parser.int_params) and value.isdecimal(): value = int(value) elif key in config.parser.year_params: (year_from, year_to, year_circa) = utils.parse_year(value) item.set(key + config.parser.start_suffix, year_from) item.set(key + config.parser.end_suffix, year_to) item.set(key + config.parser.circa_suffix, year_circa) elif key in config.parser.date_params: value = datetime.datetime.strptime(value, config.parser.date_format) except ValueError: self.raise_error() item.set(key, value)
def search_items(show_secrets): request_args = { key: value.strip() for key, value in flask.request.values.items() if value and (key in config.www.search_params) } request_keys = set(request_args.keys()) order_by = flask.request.values.get("orderBy", const.DEFAULT_ORDER_BY) if order_by not in config.www.order_by_keys: flask.abort( http.client.BAD_REQUEST, "Key {order_by} is not supported for ordering".format( order_by=order_by)) #if request_args is empty, we should render empty search form if len(request_args) == 0: flask.abort(http.client.BAD_REQUEST, "No search parameters specified") found_items = set(items) for index_to_use in (config.www.indexed_search_params & request_keys): value_to_use = request_args[index_to_use] if ((index_to_use in config.parser.list_params) or (index_to_use in config.parser.keyword_list_params)): values_to_use = utils.strip_split_list(value_to_use, ",") else: values_to_use = [value_to_use] for value in values_to_use: if index_to_use == "availability": value = bib_parser.Availability(value) indexed_items = set(item_index[index_to_use].get(value, set())) found_items &= indexed_items searches = [] try: for search_key in (config.www.nonindexed_search_params & request_keys): # argument can be missing or be empty # both cases should be ignored during search search_param = request_args[search_key] if len(search_param) > 0: param_filter = search.search_for(search_key, search_param) if param_filter is not None: searches.append(param_filter) except Exception as ex: flask.abort(http.client.BAD_REQUEST, "Some of the search parameters are wrong: {0}".format(ex)) found_items = list( sorted(filter(search.and_(searches), found_items), key=bib_parser.BibItem.key_to_key_func(order_by))) return flask.render_template("search.html", found_items=found_items, show_secrets=show_secrets)
def root(show_secrets): args_filter = lambda pair: len(pair[1]) > 0 request_args = dict(filter(args_filter, flask.request.args.items())) request_keys = set(request_args.keys()) #if request_args is empty, we should render empty search form if len(request_args) == 0: return flask.render_template( "index.html", items=items, show_secrets=show_secrets ) found_items = None for index_to_use in (config.www.indexed_search_params & request_keys): value_to_use = request_args[index_to_use] if index_to_use in config.parser.list_params: values_to_use = utils.strip_split_list(value_to_use, ",") else: values_to_use = [value_to_use] for value in values_to_use: indexed_items = set(item_index[index_to_use].get(value, set())) if found_items is None: found_items = indexed_items else: found_items &= indexed_items searches = [] if found_items is None: #no index was applied found_items = items try: for search_key in (config.www.nonindexed_search_params & request_keys): # argument can be missing or be empty # both cases should be ignored during search search_param = request_args[search_key] if len(search_param) > 0: param_filter = search.search_for(search_key, search_param) if param_filter is not None: searches.append(param_filter) except Exception as ex: flask.abort(400, "Some of the search parameters are wrong: {0}".format(ex)) if len(searches) > 0: found_items = list(filter(search.and_(searches), found_items)) return flask.render_template( "index.html", found_items=found_items, search_params=request_args, show_secrets=show_secrets )
def set_item_param(self, item, key, value): """ Sets item param, applying additional conversion if needed. """ if key in config.parser.latex_params: utils.validate_latex(item, key, value) value = utils.parse_latex(value) try: if key in config.parser.list_params: value = utils.strip_split_list(value, config.parser.list_sep) elif key in config.parser.file_list_params: value = utils.strip_split_list(value, config.parser.list_sep) filesize_value = [] for single_filename in value: #filenames start from slash, trimming it abspath = os.path.join(config.www.elibrary_dir, single_filename[1:]) if (os.path.isfile(abspath)): filesize_value.append(os.path.getsize(abspath)) else: logging.warn("File is not accessible: {0}".format(abspath)) filesize_value.append(0) item.set(const.FILE_SIZE_PARAM, filesize_value) elif key in config.parser.keyword_list_params: value = utils.strip_split_list(value, config.parser.list_sep) useful_keywords = (set(value) <= config.parser.useless_keywords) item.set("useful_" + key, useful_keywords) elif key in config.parser.int_params: value = int(value) elif key in config.parser.year_params: (year_from, year_to, year_circa) = utils.parse_year(value) item.set(key + config.parser.start_suffix, year_from) item.set(key + config.parser.end_suffix, year_to) item.set(key + config.parser.circa_suffix, year_circa) elif key in config.parser.date_params: value = datetime.datetime.strptime(value, config.parser.date_format) except ValueError: self.raise_error() item.set(key, value)
def extract_list_from_request(param_name, default=_DefaultValueList): result = extract_string_from_request(param_name, default) if result is _DefaultValueList: flask.abort(http.client.BAD_REQUEST, translate_missing_error(param_name)) if result is default: return result try: return utils.strip_split_list(result, ",") except Exception: flask.abort(http.client.BAD_REQUEST,translate_wrong_error(param_name))
def extract_list_from_request(param_name, default=_DefaultValueList): result = extract_string_from_request(param_name, default) if result is _DefaultValueList: flask.abort(http.client.BAD_REQUEST, translate_missing_error(param_name)) if result is default: return result try: return utils.strip_split_list(result, ",") except Exception: flask.abort(http.client.BAD_REQUEST, translate_wrong_error(param_name))
def set_item_param(self, item: BibItem, key: str, value: str): """ Sets item param, applying additional conversion if needed. """ value = utils.parse_latex(value) try: if key in config.parser.list_params: value = utils.strip_split_list(value, config.parser.list_sep) elif key in config.parser.int_params: value = int(value) elif key in config.parser.year_params: (year_from, year_to, year_circa) = utils.parse_year(value) item.set(key + config.parser.start_suffix, year_from) item.set(key + config.parser.end_suffix, year_to) item.set(key + config.parser.circa_suffix, year_circa) elif key in config.parser.date_params: value = datetime.datetime.strptime(value, config.parser.date_format) except ValueError: self.raise_error() item.set(key, value)
def search_items(show_secrets): request_args = { key: value.strip() for key, value in flask.request.values.items() if value and (key in config.www.search_params) } request_keys = set(request_args.keys()) order_by = flask.request.values.get("orderBy", const.DEFAULT_ORDER_BY) if order_by not in config.www.order_by_keys: flask.abort(http.client.BAD_REQUEST, "Key {order_by} is not supported for ordering".format( order_by=order_by )) #if request_args is empty, we should render empty search form if len(request_args) == 0: flask.abort(http.client.BAD_REQUEST, "No search parameters specified") found_items = set(items) for index_to_use in (config.www.indexed_search_params & request_keys): value_to_use = request_args[index_to_use] if ( (index_to_use in config.parser.list_params) or (index_to_use in config.parser.keyword_list_params) ): values_to_use = utils.strip_split_list(value_to_use, ",") else: values_to_use = [value_to_use] for value in values_to_use: if index_to_use == "availability": value = bib_parser.Availability(value) indexed_items = set(item_index[index_to_use].get(value, set())) found_items &= indexed_items searches = [] try: for search_key in (config.www.nonindexed_search_params & request_keys): # argument can be missing or be empty # both cases should be ignored during search search_param = request_args[search_key] if len(search_param) > 0: param_filter = search.search_for(search_key, search_param) if param_filter is not None: searches.append(param_filter) except Exception as ex: flask.abort(http.client.BAD_REQUEST, "Some of the search parameters are wrong: {0}".format(ex)) found_items = list(sorted( filter(search.and_(searches), found_items), key=bib_parser.BibItem.key_to_key_func(order_by) )) return flask.render_template( "search.html", found_items=found_items, show_secrets=show_secrets )