blob: 74219da83b16b9f3f83ed97250c2ea4bd2218ba0 [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
Prateeksha Singh6495d532018-08-01 16:38:39 +05303from frappe.frappeclient import FrappeClient
Prateeksha Singha525d122018-08-19 22:31:33 +05304from frappe.desk.form.load import get_attachments
Suraj Shetty3cd0c542018-08-21 16:29:06 +05305from six import string_types
Prateeksha Singh6495d532018-08-01 16:38:39 +05306
7@frappe.whitelist()
8def call_hub_method(method, params=None):
9 connection = get_hub_connection()
10
Suraj Shetty3cd0c542018-08-21 16:29:06 +053011 if isinstance(params, string_types):
Prateeksha Singh6495d532018-08-01 16:38:39 +053012 params = json.loads(params)
13
14 params.update({
15 'cmd': 'hub.hub.api.' + method
16 })
17
18 response = connection.post_request(params)
19 return response
20
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053021def map_fields(items):
22 field_mappings = get_field_mappings()
23 table_fields = [d.fieldname for d in frappe.get_meta('Item').get_table_fields()]
24
25 hub_seller = frappe.db.get_value('Hub Settings' , 'Hub Settings', 'company_email')
26
27 for item in items:
28 for fieldname in table_fields:
29 item.pop(fieldname, None)
30
31 for mapping in field_mappings:
32 local_fieldname = mapping.get('local_fieldname')
33 remote_fieldname = mapping.get('remote_fieldname')
34
35 value = item.get(local_fieldname)
36 item.pop(local_fieldname, None)
37 item[remote_fieldname] = value
38
39 item['doctype'] = 'Hub Item'
40 item['hub_seller'] = hub_seller
Prateeksha Singh40f7c462018-08-24 16:38:34 +053041 item.pop('attachments', None)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053042
43 return items
44
Prateeksha Singh6495d532018-08-01 16:38:39 +053045@frappe.whitelist()
46def get_valid_items(search_value=''):
47 items = frappe.get_list(
48 'Item',
49 fields=["*"],
50 filters={
51 'item_name': ['like', '%' + search_value + '%'],
52 'publish_in_hub': 0
53 },
54 order_by="modified desc"
55 )
56
57 valid_items = filter(lambda x: x.image and x.description, items)
58
Prateeksha Singha525d122018-08-19 22:31:33 +053059 def prepare_item(item):
Prateeksha Singh6495d532018-08-01 16:38:39 +053060 item.source_type = "local"
Prateeksha Singha525d122018-08-19 22:31:33 +053061 item.attachments = get_attachments('Item', item.item_code)
Prateeksha Singh6495d532018-08-01 16:38:39 +053062 return item
63
Prateeksha Singh02c176c2018-08-29 14:27:47 +053064 valid_items = map(prepare_item, valid_items)
Prateeksha Singha525d122018-08-19 22:31:33 +053065
Prateeksha Singh6495d532018-08-01 16:38:39 +053066 return valid_items
67
68@frappe.whitelist()
69def publish_selected_items(items_to_publish):
70 items_to_publish = json.loads(items_to_publish)
71 if not len(items_to_publish):
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053072 frappe.throw('No items to publish')
Prateeksha Singh6495d532018-08-01 16:38:39 +053073
Prateeksha Singh40f7c462018-08-24 16:38:34 +053074 for item in items_to_publish:
75 item_code = item.get('item_code')
Prateeksha Singh6495d532018-08-01 16:38:39 +053076 frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
77
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053078 frappe.get_doc({
79 'doctype': 'Hub Tracked Item',
80 'item_code': item_code,
Prateeksha Singh40f7c462018-08-24 16:38:34 +053081 'hub_category': item.get('hub_category'),
82 'image_list': item.get('image_list')
Faris Ansari811a2de2018-08-28 17:17:29 +053083 }).insert(ignore_if_duplicate=True)
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053084
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053085
Prateeksha Singh40f7c462018-08-24 16:38:34 +053086 items = map_fields(items_to_publish)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053087
Prateeksha Singh6495d532018-08-01 16:38:39 +053088 try:
Prateeksha Singh6495d532018-08-01 16:38:39 +053089 item_sync_preprocess()
Faris Ansari811a2de2018-08-28 17:17:29 +053090 load_base64_image_from_items(items)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053091
92 # TODO: Publish Progress
93 connection = get_hub_connection()
94 connection.insert_many(items)
95
96 item_sync_postprocess({
97 'status': 'Success',
98 'stats': len(items)
99 })
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
103def item_sync_preprocess():
Prateeksha Singh6495d532018-08-01 16:38:39 +0530104 hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
105
106 response = call_hub_method('add_hub_seller_activity', {
107 'hub_seller': hub_seller,
108 'activity_details': json.dumps({
109 'subject': 'Publishing items',
110 'status': 'Success'
111 })
112 })
113
114 if response:
115 frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
116 return response
117 else:
118 frappe.throw('Unable to update remote activity')
119
120def item_sync_postprocess(sync_details):
121 hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
122
123 response = call_hub_method('add_hub_seller_activity', {
124 'hub_seller': hub_seller,
125 'activity_details': json.dumps({
126 'subject': 'Publishing items:' + sync_details['status'],
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530127 'content': str(sync_details['stats']) + ' items synced.'
Prateeksha Singh6495d532018-08-01 16:38:39 +0530128 })
129 })
130
131 if response:
Prateeksha Singh6495d532018-08-01 16:38:39 +0530132 frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now())
133 else:
134 frappe.throw('Unable to update remote activity')
135
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530136 frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0)
137
Faris Ansari811a2de2018-08-28 17:17:29 +0530138
139def load_base64_image_from_items(items):
Faris Ansari8683bd82018-08-29 16:18:36 +0530140 import io, base64, urllib, os, requests, tempfile
Faris Ansari811a2de2018-08-28 17:17:29 +0530141 from frappe.utils.file_manager import get_file_path
142
143 for item in items:
144 file_path = item['image']
145 file_name = os.path.basename(file_path)
Faris Ansari8683bd82018-08-29 16:18:36 +0530146 base64content = None
Faris Ansari811a2de2018-08-28 17:17:29 +0530147
148 if file_path.startswith('http'):
Faris Ansari8683bd82018-08-29 16:18:36 +0530149 # fetch content and then base64 it
Faris Ansari811a2de2018-08-28 17:17:29 +0530150 url = file_path
Faris Ansari8683bd82018-08-29 16:18:36 +0530151 response = requests.get(url)
152 base64content = base64.b64encode(response.content)
Faris Ansari811a2de2018-08-28 17:17:29 +0530153 else:
Faris Ansari8683bd82018-08-29 16:18:36 +0530154 # read file then base64 it
Faris Ansari811a2de2018-08-28 17:17:29 +0530155 file_path = os.path.abspath(get_file_path(file_path))
Faris Ansari8683bd82018-08-29 16:18:36 +0530156 with io.open(file_path, 'rb') as f:
157 base64content = base64.b64encode(f.read())
Faris Ansari811a2de2018-08-28 17:17:29 +0530158
Faris Ansari8683bd82018-08-29 16:18:36 +0530159 image_data = json.dumps({
160 'file_name': file_name,
161 'base64': base64content
162 })
Faris Ansari811a2de2018-08-28 17:17:29 +0530163
164 item['image'] = image_data
165
166
Prateeksha Singh6495d532018-08-01 16:38:39 +0530167def get_hub_connection():
Faris Ansari64202ad2018-08-29 15:02:39 +0530168 read_only = True
169
Prateeksha Singh6495d532018-08-01 16:38:39 +0530170 if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
171 hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
Faris Ansari64202ad2018-08-29 15:02:39 +0530172
173 # full rights to user who registered as hub_seller
174 if hub_connector.username == frappe.session.user:
175 read_only = False
176
177 if not read_only:
178 hub_connection = hub_connector.get_connection()
179 return hub_connection.connection
Prateeksha Singh6495d532018-08-01 16:38:39 +0530180
181 # read-only connection
Faris Ansari64202ad2018-08-29 15:02:39 +0530182 if read_only:
Faris Ansari114d5952018-08-29 18:24:49 +0530183 hub_url = frappe.db.get_single_value('Hub Settings', 'hub_url')
184 hub_connection = FrappeClient(hub_url)
Faris Ansari64202ad2018-08-29 15:02:39 +0530185 return hub_connection
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530186
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530187def get_field_mappings():
188 return []