blob: 85693c72af516d3ada9b52648c703adf479e1da8 [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
42
43 return items
44
Prateeksha Singh6495d532018-08-01 16:38:39 +053045@frappe.whitelist()
46def get_valid_items(search_value=''):
47 items = frappe.get_list(
48 'Item',
49 fields=["*"],
50 filters={
51 'item_name': ['like', '%' + search_value + '%'],
52 'publish_in_hub': 0
53 },
54 order_by="modified desc"
55 )
56
57 valid_items = filter(lambda x: x.image and x.description, items)
58
Prateeksha Singha525d122018-08-19 22:31:33 +053059 def prepare_item(item):
Prateeksha Singh6495d532018-08-01 16:38:39 +053060 item.source_type = "local"
Prateeksha Singha525d122018-08-19 22:31:33 +053061 item.attachments = get_attachments('Item', item.item_code)
Prateeksha Singh6495d532018-08-01 16:38:39 +053062 return item
63
Prateeksha Singha525d122018-08-19 22:31:33 +053064 valid_items = map(lambda x: prepare_item(x), valid_items)
65
Prateeksha Singh6495d532018-08-01 16:38:39 +053066 return valid_items
67
68@frappe.whitelist()
69def publish_selected_items(items_to_publish):
70 items_to_publish = json.loads(items_to_publish)
71 if not len(items_to_publish):
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053072 frappe.throw('No items to publish')
Prateeksha Singh6495d532018-08-01 16:38:39 +053073
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053074 publishing_items = []
75
76 for item_additional_info in items_to_publish:
77 item_code = item_additional_info.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 Singhcaadd8a2018-08-21 19:59:15 +053083 'hub_category': item_additional_info.get('hub_category'),
84 'image_list': item_additional_info.get('image_list')
Prateeksha Singh0a60d1c2018-08-19 19:39:00 +053085 }).insert()
86
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053087 item_data = frappe.get_doc("Item", item_code).as_dict().update(item_additional_info)
88 publishing_items.append(item_data)
89
90
91 items = map_fields(publishing_items)
92
Prateeksha Singh6495d532018-08-01 16:38:39 +053093 try:
Prateeksha Singh6495d532018-08-01 16:38:39 +053094 item_sync_preprocess()
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +053095
96 # TODO: Publish Progress
97 connection = get_hub_connection()
98 connection.insert_many(items)
99
100 item_sync_postprocess({
101 'status': 'Success',
102 'stats': len(items)
103 })
104
Prateeksha Singh6495d532018-08-01 16:38:39 +0530105 except Exception as e:
106 frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0)
107 frappe.throw(e)
108
109def item_sync_preprocess():
Prateeksha Singh6495d532018-08-01 16:38:39 +0530110 hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
111
112 response = call_hub_method('add_hub_seller_activity', {
113 'hub_seller': hub_seller,
114 'activity_details': json.dumps({
115 'subject': 'Publishing items',
116 'status': 'Success'
117 })
118 })
119
120 if response:
121 frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
122 return response
123 else:
124 frappe.throw('Unable to update remote activity')
125
126def item_sync_postprocess(sync_details):
127 hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
128
129 response = call_hub_method('add_hub_seller_activity', {
130 'hub_seller': hub_seller,
131 'activity_details': json.dumps({
132 'subject': 'Publishing items:' + sync_details['status'],
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530133 'content': str(sync_details['stats']) + ' items synced.'
Prateeksha Singh6495d532018-08-01 16:38:39 +0530134 })
135 })
136
137 if response:
Prateeksha Singh6495d532018-08-01 16:38:39 +0530138 frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now())
139 else:
140 frappe.throw('Unable to update remote activity')
141
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530142 frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0)
143
Prateeksha Singh6495d532018-08-01 16:38:39 +0530144def get_hub_connection():
145 if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
146 hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
147 hub_connection = hub_connector.get_connection()
148 return hub_connection.connection
149
150 # read-only connection
151 hub_connection = FrappeClient(frappe.conf.hub_url)
152 return hub_connection
Prateeksha Singhcaadd8a2018-08-21 19:59:15 +0530153
154
155def get_field_mappings():
156 return []