blob: 8b186afa3e706a5b39f0cff72143cb7895e84acc [file] [log] [blame]
Prateeksha Singh6495d532018-08-01 16:38:39 +05301from __future__ import unicode_literals
2import frappe, requests, json
3from frappe.utils import now
4from frappe.frappeclient import FrappeClient
Prateeksha Singha525d122018-08-19 22:31:33 +05305from frappe.desk.form.load import get_attachments
Suraj Shetty3cd0c542018-08-21 16:29:06 +05306from six import string_types
Prateeksha Singh6495d532018-08-01 16:38:39 +05307
8@frappe.whitelist()
9def call_hub_method(method, params=None):
10 connection = get_hub_connection()
11
Suraj Shetty3cd0c542018-08-21 16:29:06 +053012 if isinstance(params, string_types):
Prateeksha Singh6495d532018-08-01 16:38:39 +053013 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 Singhcaadd8a2018-08-21 19:59:15 +053022def 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 Singh40f7c462018-08-24 16:38:34 +053042 item.pop('attachments', None)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053043
44 return items
45
Prateeksha Singh6495d532018-08-01 16:38:39 +053046@frappe.whitelist()
47def 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 Singha525d122018-08-19 22:31:33 +053060 def prepare_item(item):
Prateeksha Singh6495d532018-08-01 16:38:39 +053061 item.source_type = "local"
Prateeksha Singha525d122018-08-19 22:31:33 +053062 item.attachments = get_attachments('Item', item.item_code)
Prateeksha Singh6495d532018-08-01 16:38:39 +053063 return item
64
Prateeksha Singha525d122018-08-19 22:31:33 +053065 valid_items = map(lambda x: prepare_item(x), valid_items)
66
Prateeksha Singh6495d532018-08-01 16:38:39 +053067 return valid_items
68
69@frappe.whitelist()
70def publish_selected_items(items_to_publish):
71 items_to_publish = json.loads(items_to_publish)
72 if not len(items_to_publish):
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053073 frappe.throw('No items to publish')
Prateeksha Singh6495d532018-08-01 16:38:39 +053074
Prateeksha Singh40f7c462018-08-24 16:38:34 +053075 for item in items_to_publish:
76 item_code = item.get('item_code')
Prateeksha Singh6495d532018-08-01 16:38:39 +053077 frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
78
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053079 frappe.get_doc({
80 'doctype': 'Hub Tracked Item',
81 'item_code': item_code,
Prateeksha Singh40f7c462018-08-24 16:38:34 +053082 'hub_category': item.get('hub_category'),
83 'image_list': item.get('image_list')
Faris Ansari811a2de2018-08-28 17:17:29 +053084 }).insert(ignore_if_duplicate=True)
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053085
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053086
Prateeksha Singh40f7c462018-08-24 16:38:34 +053087 items = map_fields(items_to_publish)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053088
Prateeksha Singh6495d532018-08-01 16:38:39 +053089 try:
Prateeksha Singh6495d532018-08-01 16:38:39 +053090 item_sync_preprocess()
Faris Ansari811a2de2018-08-28 17:17:29 +053091 load_base64_image_from_items(items)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053092
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 Singh6495d532018-08-01 16:38:39 +0530101 except Exception as e:
Faris Ansari811a2de2018-08-28 17:17:29 +0530102 frappe.log_error(title='Hub Sync Error')
Prateeksha Singh6495d532018-08-01 16:38:39 +0530103
104def item_sync_preprocess():
Prateeksha Singh6495d532018-08-01 16:38:39 +0530105 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
121def 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 Singhcaadd8a2018-08-21 19:59:15 +0530128 'content': str(sync_details['stats']) + ' items synced.'
Prateeksha Singh6495d532018-08-01 16:38:39 +0530129 })
130 })
131
132 if response:
Prateeksha Singh6495d532018-08-01 16:38:39 +0530133 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 Singhcaadd8a2018-08-21 19:59:15 +0530137 frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0)
138
Faris Ansari811a2de2018-08-28 17:17:29 +0530139
140def 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 Singh6495d532018-08-01 16:38:39 +0530164def get_hub_connection():
Faris Ansari64202ad2018-08-29 15:02:39 +0530165 read_only = True
166
Prateeksha Singh6495d532018-08-01 16:38:39 +0530167 if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
168 hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
Faris Ansari64202ad2018-08-29 15:02:39 +0530169
170 # full rights to user who registered as hub_seller
171 if hub_connector.username == frappe.session.user:
172 read_only = False
173
174 if not read_only:
175 hub_connection = hub_connector.get_connection()
176 return hub_connection.connection
Prateeksha Singh6495d532018-08-01 16:38:39 +0530177
178 # read-only connection
Faris Ansari64202ad2018-08-29 15:02:39 +0530179 if read_only:
180 hub_connection = FrappeClient(frappe.conf.hub_url)
181 return hub_connection
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530182
183
184def get_field_mappings():
185 return []