From caadd8af4e11c3671643e0feef9263098ce6d8d2 Mon Sep 17 00:00:00 2001 From: Prateeksha Singh Date: Tue, 21 Aug 2018 19:59:15 +0530 Subject: [PATCH] =?UTF-8?q?[Hub]=20sync=20with=20insert=5Fmany,=20Remove?= =?UTF-8?q?=20Data=20Migration=20Run=20=F0=9F=92=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use FrappeClient insert_many to publish items - We will be syncing small amounts of items anyway - Also, we need only the insert action in bulk --- erpnext/hub_node/api.py | 60 ++++++++++++++++--- .../doctype/hub_settings/hub_settings.py | 14 ----- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/erpnext/hub_node/api.py b/erpnext/hub_node/api.py index 0c9af1abc1..6e11a86b86 100644 --- a/erpnext/hub_node/api.py +++ b/erpnext/hub_node/api.py @@ -18,6 +18,29 @@ def call_hub_method(method, params=None): response = connection.post_request(params) return response +def map_fields(items): + field_mappings = get_field_mappings() + table_fields = [d.fieldname for d in frappe.get_meta('Item').get_table_fields()] + + hub_seller = frappe.db.get_value('Hub Settings' , 'Hub Settings', 'company_email') + + for item in items: + for fieldname in table_fields: + item.pop(fieldname, None) + + for mapping in field_mappings: + local_fieldname = mapping.get('local_fieldname') + remote_fieldname = mapping.get('remote_fieldname') + + value = item.get(local_fieldname) + item.pop(local_fieldname, None) + item[remote_fieldname] = value + + item['doctype'] = 'Hub Item' + item['hub_seller'] = hub_seller + + return items + @frappe.whitelist() def get_valid_items(search_value=''): items = frappe.get_list( @@ -47,21 +70,37 @@ def publish_selected_items(items_to_publish): if not len(items_to_publish): frappe.throw('No items to publish') - for item in items_to_publish: - item_code = item.get('item_code') + publishing_items = [] + + for item_additional_info in items_to_publish: + item_code = item_additional_info.get('item_code') frappe.db.set_value('Item', item_code, 'publish_in_hub', 1) frappe.get_doc({ 'doctype': 'Hub Tracked Item', 'item_code': item_code, - 'hub_category': item.get('hub_category'), - 'image_list': item.get('image_list') + 'hub_category': item_additional_info.get('hub_category'), + 'image_list': item_additional_info.get('image_list') }).insert() + item_data = frappe.get_doc("Item", item_code).as_dict().update(item_additional_info) + publishing_items.append(item_data) + + + items = map_fields(publishing_items) + try: - hub_settings = frappe.get_doc('Hub Settings') item_sync_preprocess() - hub_settings.sync() + + # TODO: Publish Progress + connection = get_hub_connection() + connection.insert_many(items) + + item_sync_postprocess({ + 'status': 'Success', + 'stats': len(items) + }) + except Exception as e: frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0) frappe.throw(e) @@ -90,16 +129,17 @@ def item_sync_postprocess(sync_details): 'hub_seller': hub_seller, 'activity_details': json.dumps({ 'subject': 'Publishing items:' + sync_details['status'], - 'content': json.dumps(sync_details['stats']) + 'content': str(sync_details['stats']) + ' items synced.' }) }) if response: - frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0) frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now()) else: frappe.throw('Unable to update remote activity') + frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0) + def get_hub_connection(): if frappe.db.exists('Data Migration Connector', 'Hub Connector'): hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector') @@ -109,3 +149,7 @@ def get_hub_connection(): # read-only connection hub_connection = FrappeClient(frappe.conf.hub_url) return hub_connection + + +def get_field_mappings(): + return [] diff --git a/erpnext/hub_node/doctype/hub_settings/hub_settings.py b/erpnext/hub_node/doctype/hub_settings/hub_settings.py index 8ec3d5621f..f7e940c16e 100644 --- a/erpnext/hub_node/doctype/hub_settings/hub_settings.py +++ b/erpnext/hub_node/doctype/hub_settings/hub_settings.py @@ -23,20 +23,6 @@ class HubSettings(Document): def get_hub_url(self): return frappe.conf.hub_url - def sync(self): - """Create and execute Data Migration Run for Hub Sync plan""" - frappe.has_permission('Hub Settings', throw=True) - - doc = frappe.get_doc({ - 'doctype': 'Data Migration Run', - 'data_migration_plan': 'Hub Sync', - 'data_migration_connector': 'Hub Connector', - 'trigger_name': 'items-sync' - }).insert() - - self.sync_in_progress = 1 - doc.run() - def register(self): """ Create a User on hub.erpnext.org and return username/password """