blob: c4a792558225cd5a4a25f36e259482230fb8b22c [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')
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053084 }).insert()
85
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()
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 })
100
Prateeksha Singh6495d532018-08-01 16:38:39 +0530101 except Exception as e:
102 frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0)
103 frappe.throw(e)
104
105def item_sync_preprocess():
Prateeksha Singh6495d532018-08-01 16:38:39 +0530106 hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
107
108 response = call_hub_method('add_hub_seller_activity', {
109 'hub_seller': hub_seller,
110 'activity_details': json.dumps({
111 'subject': 'Publishing items',
112 'status': 'Success'
113 })
114 })
115
116 if response:
117 frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
118 return response
119 else:
120 frappe.throw('Unable to update remote activity')
121
122def item_sync_postprocess(sync_details):
123 hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
124
125 response = call_hub_method('add_hub_seller_activity', {
126 'hub_seller': hub_seller,
127 'activity_details': json.dumps({
128 'subject': 'Publishing items:' + sync_details['status'],
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530129 'content': str(sync_details['stats']) + ' items synced.'
Prateeksha Singh6495d532018-08-01 16:38:39 +0530130 })
131 })
132
133 if response:
Prateeksha Singh6495d532018-08-01 16:38:39 +0530134 frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now())
135 else:
136 frappe.throw('Unable to update remote activity')
137
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530138 frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0)
139
Prateeksha Singh6495d532018-08-01 16:38:39 +0530140def get_hub_connection():
141 if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
142 hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
143 hub_connection = hub_connector.get_connection()
144 return hub_connection.connection
145
146 # read-only connection
147 hub_connection = FrappeClient(frappe.conf.hub_url)
148 return hub_connection
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530149
150
151def get_field_mappings():
152 return []