feat(translator_portal): use frappe's post_request method

This commit is contained in:
Sahil Khan 2019-05-28 17:35:05 +05:30
parent bc6696ad23
commit 77856742ff
3 changed files with 11 additions and 41 deletions

View file

@ -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

View file

@ -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,

View file

@ -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")