blob: fe419196c53a5c6a48dbfe1d8f889523d6d3cfb1 [file] [log] [blame]
Prateeksha Singh6495d532018-08-01 16:38:39 +05301from __future__ import unicode_literals
Prateeksha Singh02c176c2018-08-29 14:27:47 +05302import frappe, json
Faris Ansari95e41412018-08-30 11:46:14 +05303import io, base64, os, requests
Prateeksha Singh6495d532018-08-01 16:38:39 +05304from frappe.frappeclient import FrappeClient
Prateeksha Singha525d122018-08-19 22:31:33 +05305from frappe.desk.form.load import get_attachments
Faris Ansari95e41412018-08-30 11:46:14 +05306from frappe.utils.file_manager import get_file_path
Suraj Shetty3cd0c542018-08-21 16:29:06 +05307from six import string_types
Prateeksha Singh6495d532018-08-01 16:38:39 +05308
9@frappe.whitelist()
10def call_hub_method(method, params=None):
11 connection = get_hub_connection()
12
Suraj Shetty3cd0c542018-08-21 16:29:06 +053013 if isinstance(params, string_types):
Prateeksha Singh6495d532018-08-01 16:38:39 +053014 params = json.loads(params)
15
16 params.update({
17 'cmd': 'hub.hub.api.' + method
18 })
19
20 response = connection.post_request(params)
21 return response
22
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053023def map_fields(items):
24 field_mappings = get_field_mappings()
25 table_fields = [d.fieldname for d in frappe.get_meta('Item').get_table_fields()]
26
Faris Ansarif9a66c12018-08-31 16:15:06 +053027 hub_seller = frappe.db.get_value('Marketplace Settings' , 'Marketplace Settings', 'company_email')
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053028
29 for item in items:
30 for fieldname in table_fields:
31 item.pop(fieldname, None)
32
33 for mapping in field_mappings:
34 local_fieldname = mapping.get('local_fieldname')
35 remote_fieldname = mapping.get('remote_fieldname')
36
37 value = item.get(local_fieldname)
38 item.pop(local_fieldname, None)
39 item[remote_fieldname] = value
40
41 item['doctype'] = 'Hub Item'
42 item['hub_seller'] = hub_seller
Prateeksha Singh40f7c462018-08-24 16:38:34 +053043 item.pop('attachments', None)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053044
45 return items
46
Prateeksha Singh6495d532018-08-01 16:38:39 +053047@frappe.whitelist()
48def get_valid_items(search_value=''):
49 items = frappe.get_list(
50 'Item',
51 fields=["*"],
52 filters={
Faris Ansari00f6aef2018-08-30 18:54:28 +053053 'disabled': 0,
Prateeksha Singh6495d532018-08-01 16:38:39 +053054 'item_name': ['like', '%' + search_value + '%'],
55 'publish_in_hub': 0
56 },
57 order_by="modified desc"
58 )
59
60 valid_items = filter(lambda x: x.image and x.description, items)
61
Prateeksha Singha525d122018-08-19 22:31:33 +053062 def prepare_item(item):
Prateeksha Singh6495d532018-08-01 16:38:39 +053063 item.source_type = "local"
Prateeksha Singha525d122018-08-19 22:31:33 +053064 item.attachments = get_attachments('Item', item.item_code)
Prateeksha Singh6495d532018-08-01 16:38:39 +053065 return item
66
Prateeksha Singh02c176c2018-08-29 14:27:47 +053067 valid_items = map(prepare_item, valid_items)
Prateeksha Singha525d122018-08-19 22:31:33 +053068
Prateeksha Singh6495d532018-08-01 16:38:39 +053069 return valid_items
70
71@frappe.whitelist()
72def publish_selected_items(items_to_publish):
73 items_to_publish = json.loads(items_to_publish)
74 if not len(items_to_publish):
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053075 frappe.throw('No items to publish')
Prateeksha Singh6495d532018-08-01 16:38:39 +053076
Prateeksha Singh40f7c462018-08-24 16:38:34 +053077 for item in items_to_publish:
78 item_code = item.get('item_code')
Prateeksha Singh6495d532018-08-01 16:38:39 +053079 frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
80
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053081 frappe.get_doc({
82 'doctype': 'Hub Tracked Item',
83 'item_code': item_code,
Prateeksha Singh40f7c462018-08-24 16:38:34 +053084 'hub_category': item.get('hub_category'),
85 'image_list': item.get('image_list')
Faris Ansari811a2de2018-08-28 17:17:29 +053086 }).insert(ignore_if_duplicate=True)
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053087
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053088
Prateeksha Singh40f7c462018-08-24 16:38:34 +053089 items = map_fields(items_to_publish)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053090
Prateeksha Singh6495d532018-08-01 16:38:39 +053091 try:
Prateeksha Singha6f184b2018-08-30 02:30:40 +053092 item_sync_preprocess(len(items))
Faris Ansari811a2de2018-08-28 17:17:29 +053093 load_base64_image_from_items(items)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053094
95 # TODO: Publish Progress
96 connection = get_hub_connection()
97 connection.insert_many(items)
98
Prateeksha Singha6f184b2018-08-30 02:30:40 +053099 item_sync_postprocess()
Prateeksha Singh6495d532018-08-01 16:38:39 +0530100 except Exception as e:
Prateeksha Singh02c176c2018-08-29 14:27:47 +0530101 frappe.log_error(message=e, title='Hub Sync Error')
Prateeksha Singh6495d532018-08-01 16:38:39 +0530102
Prateeksha Singha6f184b2018-08-30 02:30:40 +0530103def item_sync_preprocess(intended_item_publish_count):
104 response = call_hub_method('pre_items_publish', {
105 'intended_item_publish_count': intended_item_publish_count
Prateeksha Singh6495d532018-08-01 16:38:39 +0530106 })
107
108 if response:
Faris Ansarif9a66c12018-08-31 16:15:06 +0530109 frappe.db.set_value("Marketplace Settings", "Marketplace Settings", "sync_in_progress", 1)
Prateeksha Singh6495d532018-08-01 16:38:39 +0530110 return response
111 else:
112 frappe.throw('Unable to update remote activity')
113
Prateeksha Singha6f184b2018-08-30 02:30:40 +0530114def item_sync_postprocess():
115 response = call_hub_method('post_items_publish', {})
Prateeksha Singh6495d532018-08-01 16:38:39 +0530116 if response:
Faris Ansarif9a66c12018-08-31 16:15:06 +0530117 frappe.db.set_value('Marketplace Settings', 'Marketplace Settings', 'last_sync_datetime', frappe.utils.now())
Prateeksha Singh6495d532018-08-01 16:38:39 +0530118 else:
119 frappe.throw('Unable to update remote activity')
120
Faris Ansarif9a66c12018-08-31 16:15:06 +0530121 frappe.db.set_value('Marketplace Settings', 'Marketplace Settings', 'sync_in_progress', 0)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530122
Faris Ansari811a2de2018-08-28 17:17:29 +0530123
124def load_base64_image_from_items(items):
Faris Ansari811a2de2018-08-28 17:17:29 +0530125 for item in items:
126 file_path = item['image']
127 file_name = os.path.basename(file_path)
Faris Ansari8683bd82018-08-29 16:18:36 +0530128 base64content = None
Faris Ansari811a2de2018-08-28 17:17:29 +0530129
130 if file_path.startswith('http'):
Faris Ansari8683bd82018-08-29 16:18:36 +0530131 # fetch content and then base64 it
Faris Ansari811a2de2018-08-28 17:17:29 +0530132 url = file_path
Faris Ansari8683bd82018-08-29 16:18:36 +0530133 response = requests.get(url)
134 base64content = base64.b64encode(response.content)
Faris Ansari811a2de2018-08-28 17:17:29 +0530135 else:
Faris Ansari8683bd82018-08-29 16:18:36 +0530136 # read file then base64 it
Faris Ansari811a2de2018-08-28 17:17:29 +0530137 file_path = os.path.abspath(get_file_path(file_path))
Faris Ansari8683bd82018-08-29 16:18:36 +0530138 with io.open(file_path, 'rb') as f:
139 base64content = base64.b64encode(f.read())
Faris Ansari811a2de2018-08-28 17:17:29 +0530140
Faris Ansari8683bd82018-08-29 16:18:36 +0530141 image_data = json.dumps({
142 'file_name': file_name,
143 'base64': base64content
144 })
Faris Ansari811a2de2018-08-28 17:17:29 +0530145
146 item['image'] = image_data
147
148
Prateeksha Singh6495d532018-08-01 16:38:39 +0530149def get_hub_connection():
Faris Ansari64202ad2018-08-29 15:02:39 +0530150 read_only = True
151
Prateeksha Singh6495d532018-08-01 16:38:39 +0530152 if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
153 hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
Faris Ansari64202ad2018-08-29 15:02:39 +0530154
155 # full rights to user who registered as hub_seller
156 if hub_connector.username == frappe.session.user:
157 read_only = False
158
159 if not read_only:
160 hub_connection = hub_connector.get_connection()
161 return hub_connection.connection
Prateeksha Singh6495d532018-08-01 16:38:39 +0530162
163 # read-only connection
Faris Ansari64202ad2018-08-29 15:02:39 +0530164 if read_only:
Faris Ansarif9a66c12018-08-31 16:15:06 +0530165 hub_url = frappe.db.get_single_value('Marketplace Settings', 'hub_url')
Faris Ansari114d5952018-08-29 18:24:49 +0530166 hub_connection = FrappeClient(hub_url)
Faris Ansari64202ad2018-08-29 15:02:39 +0530167 return hub_connection
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530168
169def get_field_mappings():
170 return []