From f586baa0c59db1482ddd3be7ec76f23c6438e82c Mon Sep 17 00:00:00 2001 From: terrtia Date: Tue, 16 Jan 2024 12:04:39 +0100 Subject: [PATCH] fix: [Language] fix language source --- bin/lib/Language.py | 10 +++-- bin/lib/chats_viewer.py | 3 ++ bin/lib/objects/Items.py | 37 ------------------- var/www/blueprints/chats_explorer.py | 6 +++ .../chats_explorer/block_translation.html | 2 +- 5 files changed, 17 insertions(+), 41 deletions(-) diff --git a/bin/lib/Language.py b/bin/lib/Language.py index 1b8eed2b..70d5b685 100755 --- a/bin/lib/Language.py +++ b/bin/lib/Language.py @@ -388,6 +388,8 @@ class LanguageTranslator: return language def translate(self, content, source=None, target="en"): # TODO source target + if target not in LIST_LANGUAGES: + return None translation = None if content: if not source: @@ -407,15 +409,17 @@ class LanguageTranslator: return translation -LIST_LANGUAGES = [] +LIST_LANGUAGES = {} def get_translation_languages(): global LIST_LANGUAGES if not LIST_LANGUAGES: try: - LIST_LANGUAGES = LanguageTranslator().languages() + LIST_LANGUAGES = {} + for lang in LanguageTranslator().languages(): + LIST_LANGUAGES[lang['iso']] = lang['language'] except Exception as e: print(e) - LIST_LANGUAGES = [] + LIST_LANGUAGES = {} return LIST_LANGUAGES diff --git a/bin/lib/chats_viewer.py b/bin/lib/chats_viewer.py index e1809f47..5b2e2da6 100755 --- a/bin/lib/chats_viewer.py +++ b/bin/lib/chats_viewer.py @@ -23,6 +23,7 @@ from lib.objects import ChatThreads from lib.objects import Messages from lib.objects import UsersAccount from lib.objects import Usernames +from lib import Language config_loader = ConfigLoader() r_db = config_loader.get_db_conn("Kvrocks_DB") @@ -341,6 +342,8 @@ def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, pa if meta['subchannels']: meta['subchannels'] = get_subchannels_meta_from_global_id(meta['subchannels']) else: + if translation_target not in Language.LIST_LANGUAGES: + translation_target = None meta['messages'], meta['pagination'], meta['tags_messages'] = chat.get_messages(translation_target=translation_target, nb=nb, page=page) return meta, 200 diff --git a/bin/lib/objects/Items.py b/bin/lib/objects/Items.py index d29fc521..7b79749a 100755 --- a/bin/lib/objects/Items.py +++ b/bin/lib/objects/Items.py @@ -629,43 +629,6 @@ def get_item_metadata(item_id, item_content=None): def get_item_content(item_id): return item_basic.get_item_content(item_id) -def get_item_content_html2text(item_id, item_content=None, ignore_links=False): - if not item_content: - item_content = get_item_content(item_id) - h = html2text.HTML2Text() - h.ignore_links = ignore_links - h.ignore_images = ignore_links - return h.handle(item_content) - -def remove_all_urls_from_content(item_id, item_content=None): - if not item_content: - item_content = get_item_content(item_id) - regex = r'\b(?:http://|https://)?(?:[a-zA-Z\d-]{,63}(?:\.[a-zA-Z\d-]{,63})+)(?:\:[0-9]+)*(?:/(?:$|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*\b' - url_regex = re.compile(regex) - urls = url_regex.findall(item_content) - urls = sorted(urls, key=len, reverse=True) - for url in urls: - item_content = item_content.replace(url, '') - - regex_pgp_public_blocs = r'-----BEGIN PGP PUBLIC KEY BLOCK-----[\s\S]+?-----END PGP PUBLIC KEY BLOCK-----' - regex_pgp_signature = r'-----BEGIN PGP SIGNATURE-----[\s\S]+?-----END PGP SIGNATURE-----' - regex_pgp_message = r'-----BEGIN PGP MESSAGE-----[\s\S]+?-----END PGP MESSAGE-----' - re.compile(regex_pgp_public_blocs) - re.compile(regex_pgp_signature) - re.compile(regex_pgp_message) - - res = re.findall(regex_pgp_public_blocs, item_content) - for it in res: - item_content = item_content.replace(it, '') - res = re.findall(regex_pgp_signature, item_content) - for it in res: - item_content = item_content.replace(it, '') - res = re.findall(regex_pgp_message, item_content) - for it in res: - item_content = item_content.replace(it, '') - - return item_content - # API # def get_item(request_dict): diff --git a/var/www/blueprints/chats_explorer.py b/var/www/blueprints/chats_explorer.py index 081df950..c24370fb 100644 --- a/var/www/blueprints/chats_explorer.py +++ b/var/www/blueprints/chats_explorer.py @@ -82,6 +82,8 @@ def chats_explorer_chat(): chat_id = request.args.get('id') instance_uuid = request.args.get('uuid') target = request.args.get('target') + if target == "Don't Translate": + target = None nb_messages = request.args.get('nb') page = request.args.get('page') chat = chats_viewer.api_get_chat(chat_id, instance_uuid, translation_target=target, nb=nb_messages, page=page) @@ -111,6 +113,8 @@ def objects_subchannel_messages(): subchannel_id = request.args.get('id') instance_uuid = request.args.get('uuid') target = request.args.get('target') + if target == "Don't Translate": + target = None nb_messages = request.args.get('nb') page = request.args.get('page') subchannel = chats_viewer.api_get_subchannel(subchannel_id, instance_uuid, translation_target=target, nb=nb_messages, page=page) @@ -128,6 +132,8 @@ def objects_thread_messages(): thread_id = request.args.get('id') instance_uuid = request.args.get('uuid') target = request.args.get('target') + if target == "Don't Translate": + target = None nb_messages = request.args.get('nb') page = request.args.get('page') thread = chats_viewer.api_get_thread(thread_id, instance_uuid, translation_target=target, nb=nb_messages, page=page) diff --git a/var/www/templates/chats_explorer/block_translation.html b/var/www/templates/chats_explorer/block_translation.html index 63d47b7c..6c027921 100644 --- a/var/www/templates/chats_explorer/block_translation.html +++ b/var/www/templates/chats_explorer/block_translation.html @@ -12,7 +12,7 @@ {% endif %} {% for language in translation_languages %} - + {% endfor %}