From 0bc24c8b3372d04dc0b2ee1fe58f8e736853856c Mon Sep 17 00:00:00 2001 From: Sahil Khan Date: Tue, 18 Jun 2019 19:10:04 +0530 Subject: [PATCH] feat(translator_portal): show user status of contributed translation --- .../core/doctype/translation/translation.js | 3 +- .../core/doctype/translation/translation.json | 268 +++++------------- .../core/doctype/translation/translation.py | 26 +- frappe/hooks.py | 3 +- 4 files changed, 104 insertions(+), 196 deletions(-) diff --git a/frappe/core/doctype/translation/translation.js b/frappe/core/doctype/translation/translation.js index 9bc89548e0..c5fee5e111 100644 --- a/frappe/core/doctype/translation/translation.js +++ b/frappe/core/doctype/translation/translation.js @@ -4,7 +4,7 @@ frappe.ui.form.on('Translation', { refresh: function(frm) { - if(frm.is_new()) return; + if(frm.is_new() || !(["Saved", "Deleted"].includes(frm.doc.status))) return; frm.add_custom_button('Contribute', function() { frappe.call({ method: 'frappe.core.doctype.translation.translation.contribute_translation', @@ -13,6 +13,7 @@ frappe.ui.form.on('Translation', { "contributor": frm.doc.owner, "source_name": frm.doc.source_name, "target_name": frm.doc.target_name, + "doc_name": frm.doc.name } }); }).addClass('btn-primary'); diff --git a/frappe/core/doctype/translation/translation.json b/frappe/core/doctype/translation/translation.json index 9e9ce781dd..d2c7dfb9b6 100644 --- a/frappe/core/doctype/translation/translation.json +++ b/frappe/core/doctype/translation/translation.json @@ -1,203 +1,91 @@ { - "allow_copy": 0, - "allow_import": 1, - "allow_rename": 0, - "autoname": "hash", - "beta": 0, - "creation": "2016-02-17 12:21:16.175465", - "custom": 0, - "docstatus": 0, - "doctype": "DocType", - "document_type": "Setup", - "editable_grid": 0, - "engine": "InnoDB", + "allow_import": 1, + "autoname": "hash", + "creation": "2016-02-17 12:21:16.175465", + "doctype": "DocType", + "document_type": "Setup", + "engine": "InnoDB", + "field_order": [ + "language", + "section_break_4", + "source_name", + "column_break_6", + "target_name", + "section_break_6", + "status", + "contributed_translation_doctype_name" + ], "fields": [ { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "language", - "fieldtype": "Link", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_list_view": 0, - "in_standard_filter": 1, - "label": "Language", - "length": 0, - "no_copy": 0, - "options": "Language", - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 1, - "set_only_once": 0, - "unique": 0 - }, + "fieldname": "language", + "fieldtype": "Link", + "in_standard_filter": 1, + "label": "Language", + "options": "Language", + "search_index": 1 + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "section_break_4", - "fieldtype": "Section Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0 - }, + "fieldname": "section_break_4", + "fieldtype": "Section Break" + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "description": "If your data is in HTML, please copy paste the exact HTML code with the tags.", - "fieldname": "source_name", - "fieldtype": "Code", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "label": "Source Text", - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0 - }, + "description": "If your data is in HTML, please copy paste the exact HTML code with the tags.", + "fieldname": "source_name", + "fieldtype": "Code", + "label": "Source Text" + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "column_break_6", - "fieldtype": "Column Break", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_list_view": 0, - "in_standard_filter": 0, - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0 - }, + "fieldname": "column_break_6", + "fieldtype": "Column Break" + }, { - "allow_on_submit": 0, - "bold": 0, - "collapsible": 0, - "columns": 0, - "fieldname": "target_name", - "fieldtype": "Code", - "hidden": 0, - "ignore_user_permissions": 0, - "ignore_xss_filter": 0, - "in_filter": 0, - "in_list_view": 1, - "in_standard_filter": 0, - "label": "Translated Text", - "length": 0, - "no_copy": 0, - "permlevel": 0, - "precision": "", - "print_hide": 0, - "print_hide_if_no_value": 0, - "read_only": 0, - "remember_last_selected_value": 0, - "report_hide": 0, - "reqd": 0, - "search_index": 0, - "set_only_once": 0, - "unique": 0 + "fieldname": "target_name", + "fieldtype": "Code", + "in_list_view": 1, + "label": "Translated Text" + }, + { + "fieldname": "section_break_6", + "fieldtype": "Section Break" + }, + { + "default": "Saved", + "depends_on": "eval: !doc.__islocal", + "fieldname": "status", + "fieldtype": "Select", + "label": "Status", + "options": "Saved\nContributed\nVerified\nPR sent\nDeleted", + "read_only": 1 + }, + { + "fieldname": "contributed_translation_doctype_name", + "fieldtype": "Data", + "hidden": 1, + "label": "Contributed Translation Doctype Name", + "read_only": 1 } - ], - "hide_heading": 0, - "hide_toolbar": 0, - "idx": 0, - "image_view": 0, - "in_create": 0, - - "is_submittable": 0, - "issingle": 0, - "istable": 0, - "max_attachments": 0, - "modified": "2016-12-29 14:39:48.571006", - "modified_by": "Administrator", - "module": "Core", - "name": "Translation", - "name_case": "", - "owner": "Administrator", + ], + "modified": "2019-06-18 19:03:38.640990", + "modified_by": "Administrator", + "module": "Core", + "name": "Translation", + "owner": "Administrator", "permissions": [ { - "amend": 0, - "apply_user_permissions": 0, - "cancel": 0, - "create": 1, - "delete": 1, - "email": 1, - "export": 1, - "if_owner": 0, - "import": 0, - "is_custom": 0, - "permlevel": 0, - "print": 1, - "read": 1, - "report": 1, - "role": "System Manager", - "set_user_permissions": 0, - "share": 1, - "submit": 0, + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, "write": 1 } - ], - "quick_entry": 0, - "read_only": 0, - "read_only_onload": 0, - "sort_field": "modified", - "sort_order": "DESC", - "title_field": "source_name", - "track_changes": 1, - "track_seen": 0 + ], + "sort_field": "modified", + "sort_order": "DESC", + "title_field": "source_name", + "track_changes": 1 } \ No newline at end of file diff --git a/frappe/core/doctype/translation/translation.py b/frappe/core/doctype/translation/translation.py index 2f4ebd7623..f8a1e79717 100644 --- a/frappe/core/doctype/translation/translation.py +++ b/frappe/core/doctype/translation/translation.py @@ -7,6 +7,7 @@ import frappe from frappe.model.document import Document from frappe.translate import clear_cache from frappe.utils import strip_html_tags, is_html +from frappe.integrations.utils import make_post_request import json class Translation(Document): @@ -23,8 +24,25 @@ class Translation(Document): def on_trash(self): clear_cache() + def onload(self): + if self.contributed_translation_doctype_name: + data = {"data": json.dumps({ + "doc_name": self.contributed_translation_doctype_name + })} + try: + response = make_post_request(url=frappe.get_hooks("translation_contribution_status")[0], data=data) + except Exception: + frappe.msgprint("Something went wrong. Please check error log for more details") + if response.get("message").get("message") == "Contributed Translation has been deleted": + self.status = "Deleted" + self.contributed_translation_doctype_name = "" + self.save() + else: + self.status = response.get("message").get("status") + self.save() + @frappe.whitelist() -def contribute_translation(language, contributor, source_name, target_name): +def contribute_translation(language, contributor, source_name, target_name, doc_name): data = {"data": json.dumps({ "language": language, "contributor": contributor, @@ -32,12 +50,12 @@ def contribute_translation(language, contributor, source_name, target_name): "target_name": target_name, "posting_date": frappe.utils.nowdate() })} - from frappe.integrations.utils import make_post_request try: response = make_post_request(url=frappe.get_hooks("translation_contribution_url")[0], data=data) except Exception: frappe.msgprint("Something went wrong while contributing translation. Please check error log for more details") - if response.get("message") == "Already exists": + if response.get("message").get("message") == "Already exists": frappe.msgprint("Translation already exists") - elif response.get("message") == "Added to contribution list": + elif response.get("message").get("message") == "Added to contribution list": + frappe.set_value("Translation", doc_name, "contributed_translation_doctype_name", response.get("message").get("doc_name")) frappe.msgprint("Translation successfully contributed") diff --git a/frappe/hooks.py b/frappe/hooks.py index a91acbaa08..f6d173c202 100644 --- a/frappe/hooks.py +++ b/frappe/hooks.py @@ -18,7 +18,8 @@ app_email = "info@frappe.io" docs_app = "frappe_io" -translation_contribution_url = "https://translate.erpnext.com/api/method/translator.api.add_translation" +translation_contribution_url = "https://translate.erpnext.xyz/api/method/translator.api.add_translation" +translation_contribution_status = "https://translate.erpnext.xyz/api/method/translator.api.translation_status" before_install = "frappe.utils.install.before_install" after_install = "frappe.utils.install.after_install"