blob: 0c9af1abc14c72906b501153d13b22c63af61f73 [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 Singh6495d532018-08-01 16:38:39 +053021@frappe.whitelist()
22def get_valid_items(search_value=''):
23 items = frappe.get_list(
24 'Item',
25 fields=["*"],
26 filters={
27 'item_name': ['like', '%' + search_value + '%'],
28 'publish_in_hub': 0
29 },
30 order_by="modified desc"
31 )
32
33 valid_items = filter(lambda x: x.image and x.description, items)
34
Prateeksha Singha525d122018-08-19 22:31:33 +053035 def prepare_item(item):
Prateeksha Singh6495d532018-08-01 16:38:39 +053036 item.source_type = "local"
Prateeksha Singha525d122018-08-19 22:31:33 +053037 item.attachments = get_attachments('Item', item.item_code)
Prateeksha Singh6495d532018-08-01 16:38:39 +053038 return item
39
Prateeksha Singha525d122018-08-19 22:31:33 +053040 valid_items = map(lambda x: prepare_item(x), valid_items)
41
Prateeksha Singh6495d532018-08-01 16:38:39 +053042 return valid_items
43
44@frappe.whitelist()
45def publish_selected_items(items_to_publish):
46 items_to_publish = json.loads(items_to_publish)
47 if not len(items_to_publish):
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053048 frappe.throw('No items to publish')
Prateeksha Singh6495d532018-08-01 16:38:39 +053049
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053050 for item in items_to_publish:
51 item_code = item.get('item_code')
Prateeksha Singh6495d532018-08-01 16:38:39 +053052 frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
53
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053054 frappe.get_doc({
55 'doctype': 'Hub Tracked Item',
56 'item_code': item_code,
57 'hub_category': item.get('hub_category'),
Prateeksha Singha525d122018-08-19 22:31:33 +053058 'image_list': item.get('image_list')
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053059 }).insert()
60
Prateeksha Singh6495d532018-08-01 16:38:39 +053061 try:
62 hub_settings = frappe.get_doc('Hub Settings')
63 item_sync_preprocess()
64 hub_settings.sync()
65 except Exception as e:
66 frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0)
67 frappe.throw(e)
68
69def item_sync_preprocess():
Prateeksha Singh6495d532018-08-01 16:38:39 +053070 hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
71
72 response = call_hub_method('add_hub_seller_activity', {
73 'hub_seller': hub_seller,
74 'activity_details': json.dumps({
75 'subject': 'Publishing items',
76 'status': 'Success'
77 })
78 })
79
80 if response:
81 frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
82 return response
83 else:
84 frappe.throw('Unable to update remote activity')
85
86def item_sync_postprocess(sync_details):
87 hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
88
89 response = call_hub_method('add_hub_seller_activity', {
90 'hub_seller': hub_seller,
91 'activity_details': json.dumps({
92 'subject': 'Publishing items:' + sync_details['status'],
93 'content': json.dumps(sync_details['stats'])
94 })
95 })
96
97 if response:
98 frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0)
99 frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now())
100 else:
101 frappe.throw('Unable to update remote activity')
102
103def get_hub_connection():
104 if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
105 hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
106 hub_connection = hub_connector.get_connection()
107 return hub_connection.connection
108
109 # read-only connection
110 hub_connection = FrappeClient(frappe.conf.hub_url)
111 return hub_connection