def get_messages(language, start=0, page_length=100, search_text=''): from frappe.frappeclient import FrappeClient translator = FrappeClient(get_translator_url()) translated_dict = translator.post_api( 'translator.api.get_strings_for_translation', params=locals()) return translated_dict
def placeOrder(test): client = FrappeClient("http://0.0.0.0:8001", "*****@*****.**", "qwerty0987") print "======================CALL THIS================================" args = {"item_code": "1314", "qty": test} tags2 = client.post_api("erpnext.shopping_cart.cart.place_order", args) print tags2
def migrate(): print "connecting..." remote = FrappeClient("https://frappe.io", "Administrator", frappe.conf.frappe_admin_password) remote.migrate_doctype("User", exclude=["Guest", "Administrator"], preprocess=remove_undesired_roles)
def test_insert_many(self): from frappe.frappeclient import FrappeClient frappe.db.sql('delete from `tabToDo` where description like "Test API%"') frappe.db.commit() host = get_url() if not host.startswith('http'): host = 'http://' + host if not host.endswith(':8000'): host = host + ':8000' server = FrappeClient(host, "Administrator", "admin", verify=False) server.insert_many([ {"doctype": "ToDo", "description": "Test API 1"}, {"doctype": "ToDo", "description": "Test API 2"}, {"doctype": "ToDo", "description": "Test API 3"}, ]) self.assertTrue(frappe.db.get_value('ToDo', {'description': 'Test API 1'})) self.assertTrue(frappe.db.get_value('ToDo', {'description': 'Test API 2'})) self.assertTrue(frappe.db.get_value('ToDo', {'description': 'Test API 3'}))
def test_insert_many(self): if os.environ.get('CI'): return from frappe.frappeclient import FrappeClient frappe.db.sql( "DELETE FROM `tabToDo` WHERE `description` LIKE 'Test API%'") frappe.db.commit() server = FrappeClient(get_url(), "Administrator", "admin", verify=False) server.insert_many([ { "doctype": "ToDo", "description": "Test API 1" }, { "doctype": "ToDo", "description": "Test API 2" }, { "doctype": "ToDo", "description": "Test API 3" }, ]) self.assertTrue( frappe.db.get_value('ToDo', {'description': 'Test API 1'})) self.assertTrue( frappe.db.get_value('ToDo', {'description': 'Test API 2'})) self.assertTrue( frappe.db.get_value('ToDo', {'description': 'Test API 3'}))
def test_insert_many(self): from frappe.frappeclient import FrappeClient frappe.db.sql( 'delete from `tabToDo` where description like "Test API%"') frappe.db.commit() server = FrappeClient(get_url(), "Administrator", "admin", verify=False) server.insert_many([ { "doctype": "ToDo", "description": "Test API 1" }, { "doctype": "ToDo", "description": "Test API 2" }, { "doctype": "ToDo", "description": "Test API 3" }, ]) self.assertTrue( frappe.db.get_value('ToDo', {'description': 'Test API 1'})) self.assertTrue( frappe.db.get_value('ToDo', {'description': 'Test API 2'})) self.assertTrue( frappe.db.get_value('ToDo', {'description': 'Test API 3'}))
def transfer(url, username, password): """ begin transfer """ # url = "https://tca.wela.online/" # username = "******" # password = "******" conn = FrappeClient(url, username, password) program_enrollments = frappe.get_all(doctype="Program Enrollment", fields=[ 'student', 'student_name', 'program', 'academic_year', 'enrollment_date' ], filters={"docstatus": 1}) len(program_enrollments) for enrollment in program_enrollments: if "-" in enrollment['program']: student = frappe.get_doc("Student", enrollment['student']) if student.first_name and student.last_name: params = { # "student_name": enrollment['student_name'], "fname": student.first_name, "lname": student.last_name, "academic_year": enrollment['academic_year'], "program": enrollment['program'] } print(params) to_wela = conn.get_api( "wela.registration.doctype.sectioning.silid_to_wela", params) else: print("Err ", student.first_name, " ", student.last_name)
def custom_text_(msg, program): students = frappe.db.sql("""SELECT `tabStudent`.first_name, `tabStudent`.student_mobile_number FROM `tabProgram Enrollment` INNER JOIN `tabStudent` ON `tabStudent`.name = `tabProgram Enrollment`.student WHERE program=%s""", (program), as_dict=True) wela_setting = frappe.get_doc("Wela Settings") if wela_setting.content_text_service == "Memo": for student in students: if student['student_mobile_number']: try: send_sms(student['student_mobile_number'], msg) except: print("error sms") else: print("mobile no not specified") else: conn = FrappeClient("https://wela.tailerp.com", "*****@*****.**", "hbTcTdFtBag9RV4Y") for student in students: params = { "mobile_number": student['student_mobile_number'], "message": msg } lms_user = conn.get_api("gsmbox.gsmbox.doctype.memo.add_memo", params)
class FrappeConnection(BaseConnection): def __init__(self, connector): self.connector = connector self.connection = FrappeClient(self.connector.hostname, self.connector.username, self.get_password()) self.name_field = 'name' def insert(self, doctype, doc): doc = frappe._dict(doc) doc.doctype = doctype return self.connection.insert(doc) def update(self, doctype, doc, migration_id): doc = frappe._dict(doc) doc.doctype = doctype doc.name = migration_id return self.connection.update(doc) def delete(self, doctype, migration_id): return self.connection.delete(doctype, migration_id) def get(self, doctype, fields='"*"', filters=None, start=0, page_length=20, parent=None): return self.connection.get_list(doctype, fields=fields, filters=filters, limit_start=start, limit_page_length=page_length, parent=parent)
def test_allow_login_using_username_and_mobile(self): self.set_system_settings("allow_login_using_mobile_number", 1) self.set_system_settings("allow_login_using_user_name", 1) # Both email and username and mobile logins should work FrappeClient(frappe.get_site_config().host_name, self.test_user_mobile, self.test_user_password) FrappeClient(frappe.get_site_config().host_name, self.test_user_email, self.test_user_password) FrappeClient(frappe.get_site_config().host_name, self.test_user_name, self.test_user_password)
def get_contribution_status(message_id): from frappe.frappeclient import FrappeClient doc = frappe.get_doc('Translation', message_id) translator = FrappeClient(get_translator_url()) contributed_translation = translator.get_api('translator.api.get_contribution_status', params={ 'translation_id': doc.contribution_docname }) return contributed_translation
def test_create_doc(self): server = FrappeClient(get_url(), "Administrator", "admin", verify=False) frappe.db.delete("Note", {"title": "test_create"}) frappe.db.commit() server.insert({"doctype": "Note", "public": True, "title": "test_create"}) self.assertTrue(frappe.db.get_value('Note', {'title': 'test_create'}))
def test_list_docs(self): server = FrappeClient(frappe.get_site_config().host_name, "Administrator", "admin", verify=False) doc_list = server.get_list("Note") self.assertTrue(len(doc_list))
def test_list_docs(self): server = FrappeClient(get_url(), "Administrator", "admin", verify=False) doc_list = server.get_list("Note") self.assertTrue(len(doc_list))
def cartPost(test): client = FrappeClient("http://0.0.0.0:8001", "*****@*****.**", "qwerty0987") print "8=================POST TO CART TRIGGER=====================D" args = {"item_code": "1314", "qty": test} tags = client.post_api("erpnext.shopping_cart.cart.update_cart", args) print tags # placeOrder() print "8=================END TO CART TRIGGER=====================D"
def test_get_doc(self): server = FrappeClient(get_url(), "Administrator", "admin", verify=False) frappe.db.delete("Note", {"title": "get_this"}) frappe.db.commit() server.insert_many([ {"doctype": "Note", "public": True, "title": "get_this"}, ]) doc = server.get_doc("Note", "get_this") self.assertTrue(doc)
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.db.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 test_allow_login_using_username(self): self.set_system_settings("allow_login_using_mobile_number", 0) self.set_system_settings("allow_login_using_user_name", 1) # Mobile login should fail with self.assertRaises(AuthError): FrappeClient(frappe.get_site_config().host_name, self.test_user_mobile, self.test_user_password) # Both email and username logins should work FrappeClient(frappe.get_site_config().host_name, self.test_user_email, self.test_user_password) FrappeClient(frappe.get_site_config().host_name, self.test_user_name, self.test_user_password)
def get_hub_connection(): settings = frappe.get_single('Marketplace Settings') marketplace_url = settings.marketplace_url hub_user = settings.get_hub_user(frappe.session.user) if hub_user: password = hub_user.get_password() hub_connection = FrappeClient(marketplace_url, hub_user.user, password) return hub_connection else: read_only_hub_connection = FrappeClient(marketplace_url) return read_only_hub_connection
def migrate(): print "connecting..." frappe.flags.mute_emails = True remote = FrappeClient("https://frappe.io", "Administrator", frappe.conf.frappe_admin_password, verify=False) remote.migrate_doctype("Frappe Partner") remote.migrate_doctype("Frappe Publisher") remote.migrate_doctype("Frappe Job") remote.migrate_doctype("Frappe Job Bid") frappe.flags.mute_emails = False frappe.db.sql("update `tabFrappe Job` set parent_website_route='jobs'") frappe.db.sql("update `tabFrappe Partner` set parent_website_route='service-providers'")
def test_allow_login_using_only_email(self): self.set_system_settings("allow_login_using_mobile_number", 0) self.set_system_settings("allow_login_using_user_name", 0) # Login by mobile number should fail with self.assertRaises(AuthError): FrappeClient(frappe.get_site_config().host_name, self.test_user_mobile, self.test_user_password) # login by username should fail with self.assertRaises(AuthError): FrappeClient(frappe.get_site_config().host_name, self.test_user_name, self.test_user_password) # Login by email should work FrappeClient(frappe.get_site_config().host_name, self.test_user_email, self.test_user_password)
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.db.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 test_allow_login_using_mobile(self): self.set_system_settings('allow_login_using_mobile_number', 1) self.set_system_settings('allow_login_using_user_name', 0) # Login by both email and mobile should work FrappeClient(frappe.get_site_config().host_name, self.test_user_mobile, self.test_user_password) FrappeClient(frappe.get_site_config().host_name, self.test_user_email, self.test_user_password) # login by username should fail with self.assertRaises(AuthError): FrappeClient(frappe.get_site_config().host_name, self.test_user_name, self.test_user_password)
def send_list_to_memo(message_list): """This function will send the gathered notifications to Memo List""" message_priority = 0 for message in message_list: wela_setting = frappe.get_doc("Wela Settings") user_info = execute_query("GET_USER_MOBILE_NO", filters={"name": message['owner']}, as_dict=1) number = user_info[0]['mobile_no'] if is_correct_number(number): try: if wela_setting.post_notification_text_service == "Memo": sms = frappe.get_doc({ "doctype": "Memo", "mobile_number": number, "message": message['message'], "priority": message_priority }) sms.insert(ignore_permissions=True) elif wela_setting.post_notification_text_service == "Tailerp": try: conn = FrappeClient("https://wela.tailerp.com", "*****@*****.**", "hbTcTdFtBag9RV4Y") params = { "mobile_number": number, "message": message['message'] } lms_user = conn.get_api( "gsmbox.gsmbox.doctype.memo.add_memo", params) except: sms = frappe.get_doc({ "doctype": "Memo", "mobile_number": number, "message": message['message'], "priority": message_priority }) sms.insert(ignore_permissions=True) else: sms = frappe.get_doc({ "doctype": "Memo", "mobile_number": number, "message": message['message'], "priority": message_priority }) sms.insert(ignore_permissions=True) except Exception as e: print(e) pass
def test_create_doc(self): server = FrappeClient(frappe.get_site_config().host_name, "Administrator", "admin", verify=False) frappe.db.sql('delete from `tabNote` where title = "test_create"') frappe.db.commit() server.insert({ "doctype": "Note", "public": True, "title": "test_create" }) self.assertTrue(frappe.db.get_value('Note', {'title': 'test_create'}))
def test_create_doc(self): server = FrappeClient(get_url(), "Administrator", "admin", verify=False) frappe.db.sql("delete from `tabNote` where title = 'test_create'") frappe.db.commit() server.insert({ "doctype": "Note", "public": True, "title": "test_create" }) self.assertTrue(frappe.db.get_value("Note", {"title": "test_create"}))
def create_event_consumer(self): """register event consumer on the producer site""" if self.is_producer_online(): producer_site = FrappeClient(self.producer_url, verify=False) response = producer_site.post_api( 'frappe.event_streaming.doctype.event_consumer.event_consumer.register_consumer', params={'data': json.dumps(self.get_request_data())}) if response: response = json.loads(response) self.api_key = response['api_key'] self.api_secret = response['api_secret'] self.last_update = response['last_update'] else: frappe.throw( _('Failed to create an Event Consumer or an Event Consumer for the current site is already registered.' ))
def test_rate_limiting_for_reset_password(self): # Allow only one reset request for a day frappe.db.set_value("System Settings", "System Settings", "password_reset_limit", 1) frappe.db.commit() url = get_url() data = { 'cmd': 'frappe.core.doctype.user.user.reset_password', 'user': '******' } # Clear rate limit tracker to start fresh key = f"rl:{data['cmd']}:{data['user']}" frappe.cache().delete(key) c = FrappeClient(url) res1 = c.session.post(url, data=data, verify=c.verify, headers=c.headers) res2 = c.session.post(url, data=data, verify=c.verify, headers=c.headers) self.assertEqual(res1.status_code, 200) self.assertEqual(res2.status_code, 417)
def _connect(): return FrappeClient( url=producer_url, username='******', password='******', verify=False )
def get_remote_site(): producer_doc = frappe.get_doc('Event Producer', producer_url) producer_site = FrappeClient(url=producer_doc.producer_url, username='******', password='******', verify=False) return producer_site
def migrate(): print "connecting..." frappe.flags.mute_emails = True remote = FrappeClient("https://erpnext.com", "Administrator", frappe.conf.erpnext_admin_password) remote.migrate_doctype("Help Category") remote.migrate_doctype("Help Article") remote.migrate_single("Knowledge Base Settings") frappe.flags.mute_emails = False
def test_insert_many(self): from frappe.frappeclient import FrappeClient frappe.db.sql('delete from `tabToDo` where description like "Test API%"') frappe.db.commit() server = FrappeClient(get_url(), "Administrator", "admin", verify=False) server.insert_many([ {"doctype": "ToDo", "description": "Test API 1"}, {"doctype": "ToDo", "description": "Test API 2"}, {"doctype": "ToDo", "description": "Test API 3"}, ]) self.assertTrue(frappe.db.get_value('ToDo', {'description': 'Test API 1'})) self.assertTrue(frappe.db.get_value('ToDo', {'description': 'Test API 2'})) self.assertTrue(frappe.db.get_value('ToDo', {'description': 'Test API 3'}))
class FrappeConnection(BaseConnection): def __init__(self, connector): self.connector = connector self.connection = FrappeClient(self.connector.hostname, self.connector.username, self.get_password()) self.name_field = 'name' def insert(self, doctype, doc): doc = frappe._dict(doc) doc.doctype = doctype return self.connection.insert(doc) def update(self, doctype, doc, migration_id): doc = frappe._dict(doc) doc.doctype = doctype doc.name = migration_id return self.connection.update(doc) def delete(self, doctype, migration_id): return self.connection.delete(doctype, migration_id) def get(self, doctype, fields='"*"', filters=None, start=0, page_length=20): return self.connection.get_list(doctype, fields=fields, filters=filters, limit_start=start, limit_page_length=page_length)
def __init__(self, connector): self.connector = connector self.connection = FrappeClient(self.connector.hostname, self.connector.username, self.get_password()) self.name_field = 'name'
def migrate(): print("connecting...") remote = FrappeClient("https://frappe.io", "Administrator", frappe.conf.frappe_admin_password) remote.migrate_doctype("User", exclude=["Guest", "Administrator"], preprocess=remove_undesired_roles)