2018-08-01 11:08:39 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
import frappe, requests, json
|
|
|
|
from frappe.utils import now
|
|
|
|
from frappe.frappeclient import FrappeClient
|
2018-08-19 17:01:33 +00:00
|
|
|
from frappe.desk.form.load import get_attachments
|
2018-08-21 10:59:06 +00:00
|
|
|
from six import string_types
|
2018-08-01 11:08:39 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def call_hub_method(method, params=None):
|
|
|
|
connection = get_hub_connection()
|
|
|
|
|
2018-08-21 10:59:06 +00:00
|
|
|
if isinstance(params, string_types):
|
2018-08-01 11:08:39 +00:00
|
|
|
params = json.loads(params)
|
|
|
|
|
|
|
|
params.update({
|
|
|
|
'cmd': 'hub.hub.api.' + method
|
|
|
|
})
|
|
|
|
|
|
|
|
response = connection.post_request(params)
|
|
|
|
return response
|
|
|
|
|
2018-08-21 14:29:15 +00:00
|
|
|
def map_fields(items):
|
|
|
|
field_mappings = get_field_mappings()
|
|
|
|
table_fields = [d.fieldname for d in frappe.get_meta('Item').get_table_fields()]
|
|
|
|
|
|
|
|
hub_seller = frappe.db.get_value('Hub Settings' , 'Hub Settings', 'company_email')
|
|
|
|
|
|
|
|
for item in items:
|
|
|
|
for fieldname in table_fields:
|
|
|
|
item.pop(fieldname, None)
|
|
|
|
|
|
|
|
for mapping in field_mappings:
|
|
|
|
local_fieldname = mapping.get('local_fieldname')
|
|
|
|
remote_fieldname = mapping.get('remote_fieldname')
|
|
|
|
|
|
|
|
value = item.get(local_fieldname)
|
|
|
|
item.pop(local_fieldname, None)
|
|
|
|
item[remote_fieldname] = value
|
|
|
|
|
|
|
|
item['doctype'] = 'Hub Item'
|
|
|
|
item['hub_seller'] = hub_seller
|
|
|
|
|
|
|
|
return items
|
|
|
|
|
2018-08-01 11:08:39 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_valid_items(search_value=''):
|
|
|
|
items = frappe.get_list(
|
|
|
|
'Item',
|
|
|
|
fields=["*"],
|
|
|
|
filters={
|
|
|
|
'item_name': ['like', '%' + search_value + '%'],
|
|
|
|
'publish_in_hub': 0
|
|
|
|
},
|
|
|
|
order_by="modified desc"
|
|
|
|
)
|
|
|
|
|
|
|
|
valid_items = filter(lambda x: x.image and x.description, items)
|
|
|
|
|
2018-08-19 17:01:33 +00:00
|
|
|
def prepare_item(item):
|
2018-08-01 11:08:39 +00:00
|
|
|
item.source_type = "local"
|
2018-08-19 17:01:33 +00:00
|
|
|
item.attachments = get_attachments('Item', item.item_code)
|
2018-08-01 11:08:39 +00:00
|
|
|
return item
|
|
|
|
|
2018-08-19 17:01:33 +00:00
|
|
|
valid_items = map(lambda x: prepare_item(x), valid_items)
|
|
|
|
|
2018-08-01 11:08:39 +00:00
|
|
|
return valid_items
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def publish_selected_items(items_to_publish):
|
|
|
|
items_to_publish = json.loads(items_to_publish)
|
|
|
|
if not len(items_to_publish):
|
2018-08-19 14:09:00 +00:00
|
|
|
frappe.throw('No items to publish')
|
2018-08-01 11:08:39 +00:00
|
|
|
|
2018-08-21 14:29:15 +00:00
|
|
|
publishing_items = []
|
|
|
|
|
|
|
|
for item_additional_info in items_to_publish:
|
|
|
|
item_code = item_additional_info.get('item_code')
|
2018-08-01 11:08:39 +00:00
|
|
|
frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
|
|
|
|
|
2018-08-19 14:09:00 +00:00
|
|
|
frappe.get_doc({
|
|
|
|
'doctype': 'Hub Tracked Item',
|
|
|
|
'item_code': item_code,
|
2018-08-21 14:29:15 +00:00
|
|
|
'hub_category': item_additional_info.get('hub_category'),
|
|
|
|
'image_list': item_additional_info.get('image_list')
|
2018-08-19 14:09:00 +00:00
|
|
|
}).insert()
|
|
|
|
|
2018-08-21 14:29:15 +00:00
|
|
|
item_data = frappe.get_doc("Item", item_code).as_dict().update(item_additional_info)
|
|
|
|
publishing_items.append(item_data)
|
|
|
|
|
|
|
|
|
|
|
|
items = map_fields(publishing_items)
|
|
|
|
|
2018-08-01 11:08:39 +00:00
|
|
|
try:
|
|
|
|
item_sync_preprocess()
|
2018-08-21 14:29:15 +00:00
|
|
|
|
|
|
|
# TODO: Publish Progress
|
|
|
|
connection = get_hub_connection()
|
|
|
|
connection.insert_many(items)
|
|
|
|
|
|
|
|
item_sync_postprocess({
|
|
|
|
'status': 'Success',
|
|
|
|
'stats': len(items)
|
|
|
|
})
|
|
|
|
|
2018-08-01 11:08:39 +00:00
|
|
|
except Exception as e:
|
|
|
|
frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0)
|
|
|
|
frappe.throw(e)
|
|
|
|
|
|
|
|
def item_sync_preprocess():
|
|
|
|
hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
|
|
|
|
|
|
|
|
response = call_hub_method('add_hub_seller_activity', {
|
|
|
|
'hub_seller': hub_seller,
|
|
|
|
'activity_details': json.dumps({
|
|
|
|
'subject': 'Publishing items',
|
|
|
|
'status': 'Success'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if response:
|
|
|
|
frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
|
|
|
|
return response
|
|
|
|
else:
|
|
|
|
frappe.throw('Unable to update remote activity')
|
|
|
|
|
|
|
|
def item_sync_postprocess(sync_details):
|
|
|
|
hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
|
|
|
|
|
|
|
|
response = call_hub_method('add_hub_seller_activity', {
|
|
|
|
'hub_seller': hub_seller,
|
|
|
|
'activity_details': json.dumps({
|
|
|
|
'subject': 'Publishing items:' + sync_details['status'],
|
2018-08-21 14:29:15 +00:00
|
|
|
'content': str(sync_details['stats']) + ' items synced.'
|
2018-08-01 11:08:39 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if response:
|
|
|
|
frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now())
|
|
|
|
else:
|
|
|
|
frappe.throw('Unable to update remote activity')
|
|
|
|
|
2018-08-21 14:29:15 +00:00
|
|
|
frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0)
|
|
|
|
|
2018-08-01 11:08:39 +00:00
|
|
|
def get_hub_connection():
|
|
|
|
if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
|
|
|
|
hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
|
|
|
|
hub_connection = hub_connector.get_connection()
|
|
|
|
return hub_connection.connection
|
|
|
|
|
|
|
|
# read-only connection
|
|
|
|
hub_connection = FrappeClient(frappe.conf.hub_url)
|
|
|
|
return hub_connection
|
2018-08-21 14:29:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_field_mappings():
|
|
|
|
return []
|