def get_book_markdown(item_id): items = item_index["id"].get(item_id) if items is None: flask.abort( http.client.NOT_FOUND, f"Item with id {item_id} was not found" ) item = utils.first(items) transcription = item.get("transcription") if transcription is None: flask.abort( http.client.NOT_FOUND, f"Transcription for item {item_id} is not available" ) markdown_file = os.path.join( config.parser.markdown_dir, transcription ) return flask.render_template( "markdown.html", markdown_data=markdown_cache.get(markdown_file), item=item )
def test_messages_match(self): """ Tests if all the localizations have the same order translation keys """ has_mismatches = False messages = collections.defaultdict(list) for locale, catalog in self.catalogs.items(): for message in catalog: messages[locale].append(message.id) first_locale = utils.first(messages.keys()) first_message_list = messages.pop(first_locale) for locale, message_list in messages.items(): self.assertEqual( len(first_message_list), len(message_list) ) self.assertEqual( set(first_message_list), set(message_list) ) for index, message in enumerate(message_list): if message != first_message_list[index]: first_message = first_message_list[index], logging.debug(f"Mismatch at position {index}. {first_locale} has {first_message}, {locale} has {message}") has_mismatches = True self.assertFalse(has_mismatches)
def test_messages_match(self): """ Tests if all the localizations have the same order translation keys """ has_mismatches = False messages = collections.defaultdict(list) for locale, catalog in self.catalogs.items(): for message in catalog: messages[locale].append(message.id) first_locale = utils.first(messages.keys()) first_message_list = messages.pop(first_locale) for locale, message_list in messages.items(): self.assertEqual(len(first_message_list), len(message_list)) self.assertEqual(set(first_message_list), set(message_list)) for index, message in enumerate(message_list): if message != first_message_list[index]: first_message = first_message_list[index], logging.debug( f"Mismatch at position {index}. {first_locale} has {first_message}, {locale} has {message}" ) has_mismatches = True self.assertFalse(has_mismatches)
def get_locale(): """ Extracts locale from request """ lang = (flask.request.cookies.get("lang", None) or getattr(flask.g, "lang", None) or flask.request.accept_languages.best_match(config.www.languages)) if lang in config.www.languages: return lang else: return utils.first(config.www.languages)
def test_inverted_index_search(self): items = bib_parser.BibParser()._parse_string(TEST_ITEMS) item_index = index.Index(items) DIRECT_KEY = "cinquecento" INVERTED_KEY = const.INVERTED_INDEX_KEY_PREFIX + DIRECT_KEY subindex = item_index["keywords"] self.assertIn(DIRECT_KEY, subindex) self.assertIn(INVERTED_KEY, subindex) filtered_items = item_index["keywords"][INVERTED_KEY] self.assertEqual(len(filtered_items), 1) self.assertEqual(utils.first(filtered_items).id(), "id_2")
def test_inverted_index_search(self): items = bib_parser.BibParser()._parse_string(TEST_ITEMS) item_index = index.Index(items) DIRECT_KEY = "cinquecento" INVERTED_KEY = const.INVERTED_INDEX_KEY_PREFIX + DIRECT_KEY subindex = item_index["keywords"] self.assertIn(DIRECT_KEY, subindex) self.assertIn(INVERTED_KEY, subindex) filtered_items = item_index["keywords"][INVERTED_KEY] self.assertEqual(len(filtered_items), 1) self.assertEqual(utils.first(filtered_items).id(), "id_2")
def get_book(book_id, show_secrets): items = item_index["id"].get(book_id, None) if items is None: flask.abort(http.client.NOT_FOUND, f"Book with id {book_id} was not found") item = utils.first(items) captcha_key = random.choice(list(config.www.secret_questions.keys())) return flask.render_template("book.html", item=item, show_secrets=(show_secrets or debug_mode), captcha_key=captcha_key)
def get_book_pdf(book_id, index): """ TODO: I'm a huge method that isn't easy to read Please, refactor me ASAP """ utils_flask.require(index > 0, http.client.NOT_FOUND, "Param index should be positive number") items = item_index["id"].get(book_id, None) if items is None: flask.abort(http.client.NOT_FOUND, f"Book with id {book_id} was not found") item = utils.first(items) request_uri = flask.request.path item_urls = item.get("url") or set() filenames = item.get("filename") is_url_valid = ((request_uri in item_urls) and utils.is_url_local(request_uri) and utils.is_url_self_served(request_uri) and index <= len(filenames)) utils_flask.require( is_url_valid, http.client.NOT_FOUND, f"Book with id {book_id} is not available for download") filename = filenames[index - 1] pdf_full_path = os.path.join(config.www.elibrary_dir, filename) if not os.path.isfile(pdf_full_path): message = f"Item {book_id} metadata is wrong: file for {request_uri} is missing" logging.error(message) flask.abort(http.client.INTERNAL_SERVER_ERROR, message) logging.info(f"Sending pdf file: {pdf_full_path}") if config.unittest_mode: #using send_file in unittest mode causes ResourceWarning due to unclosed file response = flask.make_response("SOME_BINARY_PDF_LIKE_DATA") response.headers["Content-Type"] = "application/pdf" response.headers["Content-Disposition"] = "attachment" return response else: basename = os.path.basename(pdf_full_path) return flask.send_file(pdf_full_path, as_attachment=True, attachment_filename=basename)
def edit_book(book_id): items = item_index["id"].get(book_id, None) if items is None: flask.abort(http.client.NOT_FOUND, f"Book with id {id} was not found") message = utils_flask.extract_string_from_request("message") from_name = utils_flask.extract_string_from_request("name") from_email = utils_flask.extract_email_from_request("email") if not all([message, from_name, from_email]): flask.abort(http.client.BAD_REQUEST, "Empty values aren't allowed") item = utils.first(items) message = messenger.ErrorReport(item, from_email, from_name, message) message.send() return {"message": flask_babel.gettext("interface:report:thanks")}
from dancebooks import utils from dancebooks import utils_flask items, item_index = bib_parser.BibParser().parse_folder( config.parser.bibdata_dir) langids = sorted(langid for langid in item_index["langid"].keys() if not langid.startswith("!")) source_files = sorted(item_index["source_file"].keys()) booktypes = sorted(item_index["booktype"].keys()) markdown_cache = utils.MarkdownCache() debug_mode = False flask_app = flask.Flask(__name__) flask_app.config["BABEL_DEFAULT_LOCALE"] = utils.first(config.www.languages) flask_app.config["USE_EVALEX"] = False babel_app = flask_babel.Babel(flask_app) flask_markdown_app = flask_markdown.Markdown(flask_app) flask_app.jinja_env.trim_blocks = True flask_app.jinja_env.lstrip_blocks = True flask_app.jinja_env.keep_trailing_newline = False #filling jinja filters flask_app.jinja_env.filters["author_link"] = utils_flask.make_author_link flask_app.jinja_env.filters["keyword_link"] = utils_flask.make_keyword_link flask_app.jinja_env.filters["as_set"] = utils_flask.as_set flask_app.jinja_env.filters[ "translate_language"] = utils_flask.translate_language flask_app.jinja_env.filters[