Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import frappe, requests, json |
| 3 | from frappe.utils import now |
| 4 | from frappe.frappeclient import FrappeClient |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 5 | from frappe.desk.form.load import get_attachments |
Suraj Shetty | 3cd0c54 | 2018-08-21 16:29:06 +0530 | [diff] [blame] | 6 | from six import string_types |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 7 | |
| 8 | @frappe.whitelist() |
| 9 | def call_hub_method(method, params=None): |
| 10 | connection = get_hub_connection() |
| 11 | |
Suraj Shetty | 3cd0c54 | 2018-08-21 16:29:06 +0530 | [diff] [blame] | 12 | if isinstance(params, string_types): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 13 | params = json.loads(params) |
| 14 | |
| 15 | params.update({ |
| 16 | 'cmd': 'hub.hub.api.' + method |
| 17 | }) |
| 18 | |
| 19 | response = connection.post_request(params) |
| 20 | return response |
| 21 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 22 | def map_fields(items): |
| 23 | field_mappings = get_field_mappings() |
| 24 | table_fields = [d.fieldname for d in frappe.get_meta('Item').get_table_fields()] |
| 25 | |
| 26 | hub_seller = frappe.db.get_value('Hub Settings' , 'Hub Settings', 'company_email') |
| 27 | |
| 28 | for item in items: |
| 29 | for fieldname in table_fields: |
| 30 | item.pop(fieldname, None) |
| 31 | |
| 32 | for mapping in field_mappings: |
| 33 | local_fieldname = mapping.get('local_fieldname') |
| 34 | remote_fieldname = mapping.get('remote_fieldname') |
| 35 | |
| 36 | value = item.get(local_fieldname) |
| 37 | item.pop(local_fieldname, None) |
| 38 | item[remote_fieldname] = value |
| 39 | |
| 40 | item['doctype'] = 'Hub Item' |
| 41 | item['hub_seller'] = hub_seller |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 42 | item.pop('attachments', None) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 43 | |
| 44 | return items |
| 45 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 46 | @frappe.whitelist() |
| 47 | def get_valid_items(search_value=''): |
| 48 | items = frappe.get_list( |
| 49 | 'Item', |
| 50 | fields=["*"], |
| 51 | filters={ |
| 52 | 'item_name': ['like', '%' + search_value + '%'], |
| 53 | 'publish_in_hub': 0 |
| 54 | }, |
| 55 | order_by="modified desc" |
| 56 | ) |
| 57 | |
| 58 | valid_items = filter(lambda x: x.image and x.description, items) |
| 59 | |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 60 | def prepare_item(item): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 61 | item.source_type = "local" |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 62 | item.attachments = get_attachments('Item', item.item_code) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 63 | return item |
| 64 | |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 65 | valid_items = map(lambda x: prepare_item(x), valid_items) |
| 66 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 67 | return valid_items |
| 68 | |
| 69 | @frappe.whitelist() |
| 70 | def publish_selected_items(items_to_publish): |
| 71 | items_to_publish = json.loads(items_to_publish) |
| 72 | if not len(items_to_publish): |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 73 | frappe.throw('No items to publish') |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 74 | |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 75 | for item in items_to_publish: |
| 76 | item_code = item.get('item_code') |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 77 | frappe.db.set_value('Item', item_code, 'publish_in_hub', 1) |
| 78 | |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 79 | frappe.get_doc({ |
| 80 | 'doctype': 'Hub Tracked Item', |
| 81 | 'item_code': item_code, |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 82 | 'hub_category': item.get('hub_category'), |
| 83 | 'image_list': item.get('image_list') |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 84 | }).insert(ignore_if_duplicate=True) |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 85 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 86 | |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 87 | items = map_fields(items_to_publish) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 88 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 89 | try: |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 90 | item_sync_preprocess() |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 91 | load_base64_image_from_items(items) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 92 | |
| 93 | # TODO: Publish Progress |
| 94 | connection = get_hub_connection() |
| 95 | connection.insert_many(items) |
| 96 | |
| 97 | item_sync_postprocess({ |
| 98 | 'status': 'Success', |
| 99 | 'stats': len(items) |
| 100 | }) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 101 | except Exception as e: |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 102 | frappe.log_error(title='Hub Sync Error') |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 103 | |
| 104 | def item_sync_preprocess(): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 105 | hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email") |
| 106 | |
| 107 | response = call_hub_method('add_hub_seller_activity', { |
| 108 | 'hub_seller': hub_seller, |
| 109 | 'activity_details': json.dumps({ |
| 110 | 'subject': 'Publishing items', |
| 111 | 'status': 'Success' |
| 112 | }) |
| 113 | }) |
| 114 | |
| 115 | if response: |
| 116 | frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1) |
| 117 | return response |
| 118 | else: |
| 119 | frappe.throw('Unable to update remote activity') |
| 120 | |
| 121 | def item_sync_postprocess(sync_details): |
| 122 | hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email") |
| 123 | |
| 124 | response = call_hub_method('add_hub_seller_activity', { |
| 125 | 'hub_seller': hub_seller, |
| 126 | 'activity_details': json.dumps({ |
| 127 | 'subject': 'Publishing items:' + sync_details['status'], |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 128 | 'content': str(sync_details['stats']) + ' items synced.' |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 129 | }) |
| 130 | }) |
| 131 | |
| 132 | if response: |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 133 | frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now()) |
| 134 | else: |
| 135 | frappe.throw('Unable to update remote activity') |
| 136 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 137 | frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0) |
| 138 | |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 139 | |
| 140 | def load_base64_image_from_items(items): |
| 141 | import io, base64, urllib, os |
| 142 | from frappe.utils.file_manager import get_file_path |
| 143 | |
| 144 | for item in items: |
| 145 | file_path = item['image'] |
| 146 | file_name = os.path.basename(file_path) |
| 147 | |
| 148 | if file_path.startswith('http'): |
| 149 | url = file_path |
| 150 | file_path = os.path.join('/tmp', file_name) |
| 151 | urllib.urlretrieve(url, file_path) |
| 152 | else: |
| 153 | file_path = os.path.abspath(get_file_path(file_path)) |
| 154 | |
| 155 | with io.open(file_path, 'rb') as f: |
| 156 | image_data = json.dumps({ |
| 157 | 'file_name': file_name, |
| 158 | 'base64': base64.b64encode(f.read()) |
| 159 | }) |
| 160 | |
| 161 | item['image'] = image_data |
| 162 | |
| 163 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 164 | def get_hub_connection(): |
| 165 | if frappe.db.exists('Data Migration Connector', 'Hub Connector'): |
| 166 | hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector') |
| 167 | hub_connection = hub_connector.get_connection() |
| 168 | return hub_connection.connection |
| 169 | |
| 170 | # read-only connection |
| 171 | hub_connection = FrappeClient(frappe.conf.hub_url) |
| 172 | return hub_connection |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 173 | |
| 174 | |
| 175 | def get_field_mappings(): |
| 176 | return [] |