From 8b187b395cce560fe86d542bd0299cf8c7946dfa Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 14 Jun 2023 17:14:54 +0530 Subject: [PATCH 1/3] chore: remove translation tool related APIs This was removed a while ago, this function seems like leftover code Not used anywhere else. --- .../core/doctype/translation/translation.py | 64 ------------------- 1 file changed, 64 deletions(-) diff --git a/frappe/core/doctype/translation/translation.py b/frappe/core/doctype/translation/translation.py index a285806589..c8226db5b0 100644 --- a/frappe/core/doctype/translation/translation.py +++ b/frappe/core/doctype/translation/translation.py @@ -23,70 +23,6 @@ class Translation(Document): def on_trash(self): clear_user_translation_cache(self.language) - def contribute(self): - pass - - def get_contribution_status(self): - pass - - -@frappe.whitelist() -def create_translations(translation_map, language): - from frappe.frappeclient import FrappeClient - - translation_map = json.loads(translation_map) - translation_map_to_send = frappe._dict({}) - # first create / update local user translations - for source_id, translation_dict in translation_map.items(): - translation_dict = frappe._dict(translation_dict) - existing_doc_name = frappe.get_all( - "Translation", - { - "source_text": translation_dict.source_text, - "context": translation_dict.context or "", - "language": language, - }, - ) - translation_map_to_send[source_id] = translation_dict - if existing_doc_name: - frappe.db.set_value( - "Translation", - existing_doc_name[0].name, - { - "translated_text": translation_dict.translated_text, - "contributed": 1, - "contribution_status": "Pending", - }, - ) - translation_map_to_send[source_id].name = existing_doc_name[0].name - else: - doc = frappe.get_doc( - { - "doctype": "Translation", - "source_text": translation_dict.source_text, - "contributed": 1, - "contribution_status": "Pending", - "translated_text": translation_dict.translated_text, - "context": translation_dict.context, - "language": language, - } - ) - doc.insert() - translation_map_to_send[source_id].name = doc.name - - params = { - "language": language, - "contributor_email": frappe.session.user, - "contributor_name": frappe.utils.get_fullname(frappe.session.user), - "translation_map": json.dumps(translation_map_to_send), - } - - translator = FrappeClient(get_translator_url()) - added_translations = translator.post_api("translator.api.add_translations", params=params) - - for local_docname, remote_docname in added_translations.items(): - frappe.db.set_value("Translation", local_docname, "contribution_docname", remote_docname) - def clear_user_translation_cache(lang): frappe.cache.hdel(USER_TRANSLATION_KEY, lang) From 3f3ee1233832b484c303f86ddc5564aa0170e6e5 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 14 Jun 2023 17:21:49 +0530 Subject: [PATCH 2/3] Revert: keep supporting set_value for singles w/ explicit check for singles This shouldn't have any performance impact as last function call only happens if we THINK it's single doctype. use set_single_value to avoid that extra function call. --- frappe/database/database.py | 17 +++++++++++++++++ frappe/model/utils/__init__.py | 8 ++++++++ frappe/tests/test_db.py | 5 +++++ 3 files changed, 30 insertions(+) diff --git a/frappe/database/database.py b/frappe/database/database.py index 6616592fd7..1f2a1eeba9 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -33,6 +33,7 @@ from frappe.query_builder.functions import Count from frappe.utils import CallbackManager from frappe.utils import cast as cast_fieldtype from frappe.utils import cint, get_datetime, get_table_name, getdate, now, sbool +from frappe.utils.deprecations import deprecation_warning IFNULL_PATTERN = re.compile(r"ifnull\(", flags=re.IGNORECASE) INDEX_PATTERN = re.compile(r"\s*\([^)]+\)\s*") @@ -888,6 +889,22 @@ class Database: :param update_modified: default True. Set as false, if you don't want to update the timestamp. :param debug: Print the query in the developer / js console. """ + from frappe.model.utils import is_single_doctype + + if (dn is None or dt == dn) and is_single_doctype(dt): + deprecation_warning( + "Calling db.set_value on single doctype is deprecated. This behaviour will be removed in future. Use db.set_single_value instead." + ) + self.set_single_value( + doctype=dt, + fieldname=field, + value=val, + debug=debug, + update_modified=update_modified, + modified=modified, + modified_by=modified_by, + ) + return to_update = self._get_update_dict( field, val, modified=modified, modified_by=modified_by, update_modified=update_modified diff --git a/frappe/model/utils/__init__.py b/frappe/model/utils/__init__.py index 2935872fc7..5baf6f3787 100644 --- a/frappe/model/utils/__init__.py +++ b/frappe/model/utils/__init__.py @@ -133,3 +133,11 @@ def is_virtual_doctype(doctype: str): if frappe.db.has_column("DocType", "is_virtual"): return frappe.db.get_value("DocType", doctype, "is_virtual") return False + + +@site_cache() +def is_single_doctype(doctype: str): + try: + return frappe.db.get_value("DocType", doctype, "issingle") + except Exception: + return False diff --git a/frappe/tests/test_db.py b/frappe/tests/test_db.py index 3d103e3ab5..f026726cfa 100644 --- a/frappe/tests/test_db.py +++ b/frappe/tests/test_db.py @@ -718,6 +718,11 @@ class TestDBSetValue(FrappeTestCase): updated_value = frappe.db.get_value("ToDo", self.todo1.name, "description") self.assertEqual(updated_value, "test_set_value change 1") + @patch("frappe.db.set_single_value") + def test_set_single_value_with_set_value(self, single_set): + frappe.db.set_value("Contact Us Settings", None, "country", "India") + single_set.assert_called_once() + def test_update_single_row_multiple_columns(self): description, status = "Upated by test_update_single_row_multiple_columns", "Closed" From 81553711625237ddeb3131f36fe0f8dc25babed5 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 14 Jun 2023 18:21:55 +0530 Subject: [PATCH 3/3] fix: ignore DOCTYPES_FOR_DOCTYPE in issingle check (#21375) --- frappe/model/utils/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/frappe/model/utils/__init__.py b/frappe/model/utils/__init__.py index 5baf6f3787..f8f5b21de4 100644 --- a/frappe/model/utils/__init__.py +++ b/frappe/model/utils/__init__.py @@ -136,8 +136,10 @@ def is_virtual_doctype(doctype: str): @site_cache() -def is_single_doctype(doctype: str): - try: - return frappe.db.get_value("DocType", doctype, "issingle") - except Exception: +def is_single_doctype(doctype: str) -> bool: + from frappe.model.base_document import DOCTYPES_FOR_DOCTYPE + + if doctype in DOCTYPES_FOR_DOCTYPE: return False + + return frappe.db.get_value("DocType", doctype, "issingle")