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:
					abspath = os.path.join(config.www.elibrary_dir, single_filename)
					if os.path.isfile(abspath):
						filesize_value.append(os.path.getsize(abspath))
					else:
						logging.warn(f"File is not accessible: {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 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))
Exemple #3
0
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))
Exemple #4
0
    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:
                    abspath = os.path.join(config.www.elibrary_dir,
                                           single_filename)
                    if os.path.isfile(abspath):
                        filesize_value.append(os.path.getsize(abspath))
                    else:
                        logging.warn(f"File is not accessible: {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)
Exemple #5
0
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,
                    f"Key {order_by} is not supported for ordering")

    #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,
                    f"Some of the search parameters are wrong: {ex}")

    found_items = list(
        sorted(filter(search.and_(searches), found_items),
               key=bib_parser.BibItem.key_to_key_func(order_by)))

    format = flask.request.values.get("format", "html")
    if (format == "html"):
        return flask.render_template("search.html",
                                     found_items=found_items,
                                     show_secrets=(show_secrets or debug_mode))
    elif (format == "csv"):
        response = flask.make_response(utils.render_to_csv(found_items))
        response.headers["Content-Type"] = "text/csv"
        response.headers[
            "Content-Disposition"] = "attachment;filename=search_results.csv"
        return response
    else:
        flask.abort(http.client.BAD_REQUEST,
                    f"Unsupported output format {format}")