From 77856742ffda5c4c17bfc565df191e960250e300 Mon Sep 17 00:00:00 2001 From: Sahil Khan Date: Tue, 28 May 2019 17:35:05 +0530 Subject: [PATCH] feat(translator_portal): use frappe's post_request method --- frappe/api.py | 37 ------------------- .../core/doctype/translation/translation.js | 2 +- .../core/doctype/translation/translation.py | 13 +++++-- 3 files changed, 11 insertions(+), 41 deletions(-) diff --git a/frappe/api.py b/frappe/api.py index 51e9cac82e..9b4438fa43 100644 --- a/frappe/api.py +++ b/frappe/api.py @@ -12,43 +12,6 @@ from six.moves.urllib.parse import urlparse, urlencode import base64 from frappe.utils import get_request_session -@frappe.whitelist() -def post_translation(data): - result = [] - session = get_request_session() - login(session) - - try: - response = session.post( - "{host}/api/method/webnotes_app.api.add_translation".format(host=frappe.conf.api_host), - data = data - ) - response.raise_for_status() - if response.json().get("message") == "Already exists": - frappe.msgprint("Translation already exists") - elif response.json().get("message") == "Added to contribution list": - frappe.msgprint("Translation successfully contributed") - except: - raise Exception - finally: - logout(session) - -def login(session): - r = session.post('{host}/'.format(host=frappe.conf.api_host), data={ - 'cmd': 'login', - 'usr': frappe.conf.api_username, - 'pwd': frappe.conf.api_password - }) - - try: - if not r.json().get('message') == "Logged In": - raise Exception - except json.decoder.JSONDecodeError: - raise Exception - -def logout(session): - session.get('{host}/?cmd=logout'.format(host=frappe.conf.api_host)) - def handle(): """ Handler for `/api` methods diff --git a/frappe/core/doctype/translation/translation.js b/frappe/core/doctype/translation/translation.js index 2cd7d5fed2..41ae2791f6 100644 --- a/frappe/core/doctype/translation/translation.js +++ b/frappe/core/doctype/translation/translation.js @@ -7,7 +7,7 @@ frappe.ui.form.on('Translation', { if(frm.is_new()) return; frm.add_custom_button('Contribute', function() { frappe.call({ - method: 'frappe.core.doctype.translation.translation.send_translation', + method: 'frappe.core.doctype.translation.translation.contribute_translation', args: { "language": frm.doc.language, "contributor": frm.doc.owner, diff --git a/frappe/core/doctype/translation/translation.py b/frappe/core/doctype/translation/translation.py index c832e929e7..cc2c55732a 100644 --- a/frappe/core/doctype/translation/translation.py +++ b/frappe/core/doctype/translation/translation.py @@ -7,7 +7,6 @@ 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.api import post_translation import json class Translation(Document): @@ -25,7 +24,7 @@ class Translation(Document): clear_cache() @frappe.whitelist() -def send_translation(language, contributor, source_name, target_name): +def contribute_translation(language, contributor, source_name, target_name): data = {"data": json.dumps({ "language": language, "contributor": contributor, @@ -33,4 +32,12 @@ def send_translation(language, contributor, source_name, target_name): "target_name": target_name, "posting_date": frappe.utils.nowdate() })} - post_translation(data=data) + from frappe.integrations.utils import make_post_request + try: + response = make_post_request(url="https://translate.erpnext.xyz/api/method/webnotes_app.api.add_translation", data=data) + except: + frappe.msgprint("Something went wrong while contributing translation. Please check error log for more details") + if response.get("message") == "Already exists": + frappe.msgprint("Translation already exists") + elif response.get("message") == "Added to contribution list": + frappe.msgprint("Translation successfully contributed")