blob: be81effe69cdd7124baee901cb0e89eb3ba01e68 [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
27 hub_seller = frappe.db.get_value('Hub Settings' , 'Hub Settings', 'company_email')
28
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={
53 'item_name': ['like', '%' + search_value + '%'],
54 'publish_in_hub': 0
55 },
56 order_by="modified desc"
57 )
58
59 valid_items = filter(lambda x: x.image and x.description, items)
60
Prateeksha Singha525d122018-08-19 22:31:33 +053061 def prepare_item(item):
Prateeksha Singh6495d532018-08-01 16:38:39 +053062 item.source_type = "local"
Prateeksha Singha525d122018-08-19 22:31:33 +053063 item.attachments = get_attachments('Item', item.item_code)
Prateeksha Singh6495d532018-08-01 16:38:39 +053064 return item
65
Prateeksha Singh02c176c2018-08-29 14:27:47 +053066 valid_items = map(prepare_item, valid_items)
Prateeksha Singha525d122018-08-19 22:31:33 +053067
Prateeksha Singh6495d532018-08-01 16:38:39 +053068 return valid_items
69
70@frappe.whitelist()
71def publish_selected_items(items_to_publish):
72 items_to_publish = json.loads(items_to_publish)
73 if not len(items_to_publish):
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053074 frappe.throw('No items to publish')
Prateeksha Singh6495d532018-08-01 16:38:39 +053075
Prateeksha Singh40f7c462018-08-24 16:38:34 +053076 for item in items_to_publish:
77 item_code = item.get('item_code')
Prateeksha Singh6495d532018-08-01 16:38:39 +053078 frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
79
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053080 frappe.get_doc({
81 'doctype': 'Hub Tracked Item',
82 'item_code': item_code,
Prateeksha Singh40f7c462018-08-24 16:38:34 +053083 'hub_category': item.get('hub_category'),
84 'image_list': item.get('image_list')
Faris Ansari811a2de2018-08-28 17:17:29 +053085 }).insert(ignore_if_duplicate=True)
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053086
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053087
Prateeksha Singh40f7c462018-08-24 16:38:34 +053088 items = map_fields(items_to_publish)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053089
Prateeksha Singh6495d532018-08-01 16:38:39 +053090 try:
Prateeksha Singha6f184b2018-08-30 02:30:40 +053091 item_sync_preprocess(len(items))
Faris Ansari811a2de2018-08-28 17:17:29 +053092 load_base64_image_from_items(items)
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053093
94 # TODO: Publish Progress
95 connection = get_hub_connection()
96 connection.insert_many(items)
97
Prateeksha Singha6f184b2018-08-30 02:30:40 +053098 item_sync_postprocess()
Prateeksha Singh6495d532018-08-01 16:38:39 +053099 except Exception as e:
Prateeksha Singh02c176c2018-08-29 14:27:47 +0530100 frappe.log_error(message=e, title='Hub Sync Error')
Prateeksha Singh6495d532018-08-01 16:38:39 +0530101
Prateeksha Singha6f184b2018-08-30 02:30:40 +0530102def item_sync_preprocess(intended_item_publish_count):
103 response = call_hub_method('pre_items_publish', {
104 'intended_item_publish_count': intended_item_publish_count
Prateeksha Singh6495d532018-08-01 16:38:39 +0530105 })
106
107 if response:
108 frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
109 return response
110 else:
111 frappe.throw('Unable to update remote activity')
112
Prateeksha Singha6f184b2018-08-30 02:30:40 +0530113def item_sync_postprocess():
114 response = call_hub_method('post_items_publish', {})
Prateeksha Singh6495d532018-08-01 16:38:39 +0530115 if response:
Prateeksha Singh6495d532018-08-01 16:38:39 +0530116 frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now())
117 else:
118 frappe.throw('Unable to update remote activity')
119
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530120 frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0)
121
Faris Ansari811a2de2018-08-28 17:17:29 +0530122
123def load_base64_image_from_items(items):
Faris Ansari811a2de2018-08-28 17:17:29 +0530124 for item in items:
125 file_path = item['image']
126 file_name = os.path.basename(file_path)
Faris Ansari8683bd82018-08-29 16:18:36 +0530127 base64content = None
Faris Ansari811a2de2018-08-28 17:17:29 +0530128
129 if file_path.startswith('http'):
Faris Ansari8683bd82018-08-29 16:18:36 +0530130 # fetch content and then base64 it
Faris Ansari811a2de2018-08-28 17:17:29 +0530131 url = file_path
Faris Ansari8683bd82018-08-29 16:18:36 +0530132 response = requests.get(url)
133 base64content = base64.b64encode(response.content)
Faris Ansari811a2de2018-08-28 17:17:29 +0530134 else:
Faris Ansari8683bd82018-08-29 16:18:36 +0530135 # read file then base64 it
Faris Ansari811a2de2018-08-28 17:17:29 +0530136 file_path = os.path.abspath(get_file_path(file_path))
Faris Ansari8683bd82018-08-29 16:18:36 +0530137 with io.open(file_path, 'rb') as f:
138 base64content = base64.b64encode(f.read())
Faris Ansari811a2de2018-08-28 17:17:29 +0530139
Faris Ansari8683bd82018-08-29 16:18:36 +0530140 image_data = json.dumps({
141 'file_name': file_name,
142 'base64': base64content
143 })
Faris Ansari811a2de2018-08-28 17:17:29 +0530144
145 item['image'] = image_data
146
147
Prateeksha Singh6495d532018-08-01 16:38:39 +0530148def get_hub_connection():
Faris Ansari64202ad2018-08-29 15:02:39 +0530149 read_only = True
150
Prateeksha Singh6495d532018-08-01 16:38:39 +0530151 if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
152 hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
Faris Ansari64202ad2018-08-29 15:02:39 +0530153
154 # full rights to user who registered as hub_seller
155 if hub_connector.username == frappe.session.user:
156 read_only = False
157
158 if not read_only:
159 hub_connection = hub_connector.get_connection()
160 return hub_connection.connection
Prateeksha Singh6495d532018-08-01 16:38:39 +0530161
162 # read-only connection
Faris Ansari64202ad2018-08-29 15:02:39 +0530163 if read_only:
Faris Ansari114d5952018-08-29 18:24:49 +0530164 hub_url = frappe.db.get_single_value('Hub Settings', 'hub_url')
165 hub_connection = FrappeClient(hub_url)
Faris Ansari64202ad2018-08-29 15:02:39 +0530166 return hub_connection
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530167
168def get_field_mappings():
169 return []