blob: 6e11a86b8643124ae2e6d844ed31f0e36f5c4e20 [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
Prateeksha Singh6495d532018-08-01 16:38:39 +05306
7@frappe.whitelist()
8def call_hub_method(method, params=None):
9 connection = get_hub_connection()
10
11 if type(params) == unicode:
12 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
41
42 return items
43
Prateeksha Singh6495d532018-08-01 16:38:39 +053044@frappe.whitelist()
45def get_valid_items(search_value=''):
46 items = frappe.get_list(
47 'Item',
48 fields=["*"],
49 filters={
50 'item_name': ['like', '%' + search_value + '%'],
51 'publish_in_hub': 0
52 },
53 order_by="modified desc"
54 )
55
56 valid_items = filter(lambda x: x.image and x.description, items)
57
Prateeksha Singha525d122018-08-19 22:31:33 +053058 def prepare_item(item):
Prateeksha Singh6495d532018-08-01 16:38:39 +053059 item.source_type = "local"
Prateeksha Singha525d122018-08-19 22:31:33 +053060 item.attachments = get_attachments('Item', item.item_code)
Prateeksha Singh6495d532018-08-01 16:38:39 +053061 return item
62
Prateeksha Singha525d122018-08-19 22:31:33 +053063 valid_items = map(lambda x: prepare_item(x), valid_items)
64
Prateeksha Singh6495d532018-08-01 16:38:39 +053065 return valid_items
66
67@frappe.whitelist()
68def publish_selected_items(items_to_publish):
69 items_to_publish = json.loads(items_to_publish)
70 if not len(items_to_publish):
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053071 frappe.throw('No items to publish')
Prateeksha Singh6495d532018-08-01 16:38:39 +053072
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053073 publishing_items = []
74
75 for item_additional_info in items_to_publish:
76 item_code = item_additional_info.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 Singhcaadd8a2018-08-21 19:59:15 +053082 'hub_category': item_additional_info.get('hub_category'),
83 'image_list': item_additional_info.get('image_list')
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053084 }).insert()
85
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053086 item_data = frappe.get_doc("Item", item_code).as_dict().update(item_additional_info)
87 publishing_items.append(item_data)
88
89
90 items = map_fields(publishing_items)
91
Prateeksha Singh6495d532018-08-01 16:38:39 +053092 try:
Prateeksha Singh6495d532018-08-01 16:38:39 +053093 item_sync_preprocess()
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053094
95 # TODO: Publish Progress
96 connection = get_hub_connection()
97 connection.insert_many(items)
98
99 item_sync_postprocess({
100 'status': 'Success',
101 'stats': len(items)
102 })
103
Prateeksha Singh6495d532018-08-01 16:38:39 +0530104 except Exception as e:
105 frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0)
106 frappe.throw(e)
107
108def item_sync_preprocess():
Prateeksha Singh6495d532018-08-01 16:38:39 +0530109 hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
110
111 response = call_hub_method('add_hub_seller_activity', {
112 'hub_seller': hub_seller,
113 'activity_details': json.dumps({
114 'subject': 'Publishing items',
115 'status': 'Success'
116 })
117 })
118
119 if response:
120 frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
121 return response
122 else:
123 frappe.throw('Unable to update remote activity')
124
125def item_sync_postprocess(sync_details):
126 hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
127
128 response = call_hub_method('add_hub_seller_activity', {
129 'hub_seller': hub_seller,
130 'activity_details': json.dumps({
131 'subject': 'Publishing items:' + sync_details['status'],
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530132 'content': str(sync_details['stats']) + ' items synced.'
Prateeksha Singh6495d532018-08-01 16:38:39 +0530133 })
134 })
135
136 if response:
Prateeksha Singh6495d532018-08-01 16:38:39 +0530137 frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now())
138 else:
139 frappe.throw('Unable to update remote activity')
140
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530141 frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0)
142
Prateeksha Singh6495d532018-08-01 16:38:39 +0530143def get_hub_connection():
144 if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
145 hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
146 hub_connection = hub_connector.get_connection()
147 return hub_connection.connection
148
149 # read-only connection
150 hub_connection = FrappeClient(frappe.conf.hub_url)
151 return hub_connection
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530152
153
154def get_field_mappings():
155 return []