2018-08-01 11:08:39 +00:00
|
|
|
from __future__ import unicode_literals
|
2018-08-31 17:11:26 +00:00
|
|
|
|
|
|
|
import frappe
|
|
|
|
import json
|
|
|
|
|
|
|
|
from frappe import _
|
2018-08-01 11:08:39 +00:00
|
|
|
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
|
|
|
|
2018-08-31 19:44:29 +00:00
|
|
|
current_user = frappe.session.user
|
|
|
|
|
2018-09-01 04:25:43 +00:00
|
|
|
|
2018-08-31 17:11:26 +00:00
|
|
|
@frappe.whitelist()
|
2018-09-03 07:41:23 +00:00
|
|
|
def register_marketplace(company, company_description):
|
2018-09-01 09:00:46 +00:00
|
|
|
validate_registerer()
|
2018-09-03 10:12:00 +00:00
|
|
|
|
2018-08-31 17:11:26 +00:00
|
|
|
settings = frappe.get_single('Marketplace Settings')
|
2018-09-03 07:41:23 +00:00
|
|
|
message = settings.register_seller(company, company_description)
|
2018-08-31 17:11:26 +00:00
|
|
|
|
2018-09-01 09:00:46 +00:00
|
|
|
if message.get('hub_seller_name'):
|
2018-08-31 17:11:26 +00:00
|
|
|
settings.registered = 1
|
2018-09-01 09:00:46 +00:00
|
|
|
settings.hub_seller_name = message.get('hub_seller_name')
|
|
|
|
settings.save()
|
2018-08-31 17:11:26 +00:00
|
|
|
|
2018-09-01 11:50:08 +00:00
|
|
|
settings.add_hub_user(frappe.session.user)
|
2018-08-31 18:42:17 +00:00
|
|
|
|
2018-09-01 09:00:46 +00:00
|
|
|
return { 'ok': 1 }
|
2018-08-31 17:11:26 +00:00
|
|
|
|
2018-09-01 04:25:43 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
2018-09-02 12:59:00 +00:00
|
|
|
def register_users(user_list):
|
|
|
|
user_list = json.loads(user_list)
|
|
|
|
|
|
|
|
settings = frappe.get_single('Marketplace Settings')
|
|
|
|
|
|
|
|
for user in user_list:
|
|
|
|
settings.add_hub_user(user)
|
|
|
|
|
|
|
|
return user_list
|
2018-09-01 04:25:43 +00:00
|
|
|
|
|
|
|
|
2018-08-31 17:11:26 +00:00
|
|
|
def validate_registerer():
|
2018-08-31 19:44:29 +00:00
|
|
|
if current_user == 'Administrator':
|
2018-08-31 17:11:26 +00:00
|
|
|
frappe.throw(_('Please login as another user to register on Marketplace'))
|
|
|
|
|
2018-09-01 09:00:46 +00:00
|
|
|
valid_roles = ['System Manager', 'Item Manager']
|
|
|
|
|
|
|
|
if not frappe.utils.is_subset(valid_roles, frappe.get_roles()):
|
|
|
|
frappe.throw(_('Only users with {0} role can register on Marketplace').format(', '.join(valid_roles)),
|
|
|
|
frappe.PermissionError)
|
2018-08-31 17:11:26 +00:00
|
|
|
|
2018-09-01 04:25:43 +00:00
|
|
|
|
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-09-01 04:25:43 +00:00
|
|
|
|
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()]
|
|
|
|
|
2019-11-29 10:22:29 +00:00
|
|
|
hub_seller_name = frappe.db.get_value('Marketplace Settings', 'Marketplace Settings', 'hub_seller_name')
|
2018-08-21 14:29:15 +00:00
|
|
|
|
|
|
|
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'
|
2018-09-02 21:14:27 +00:00
|
|
|
item['hub_seller'] = hub_seller_name
|
2018-08-24 11:08:34 +00:00
|
|
|
item.pop('attachments', None)
|
2018-08-21 14:29:15 +00:00
|
|
|
|
|
|
|
return items
|
|
|
|
|
2018-09-01 04:25:43 +00:00
|
|
|
|
2018-08-01 11:08:39 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_valid_items(search_value=''):
|
|
|
|
items = frappe.get_list(
|
|
|
|
'Item',
|
|
|
|
fields=["*"],
|
|
|
|
filters={
|
2018-08-30 13:24:28 +00:00
|
|
|
'disabled': 0,
|
2018-08-01 11:08:39 +00:00
|
|
|
'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-29 08:57:47 +00:00
|
|
|
valid_items = map(prepare_item, valid_items)
|
2018-08-19 17:01:33 +00:00
|
|
|
|
2018-08-01 11:08:39 +00:00
|
|
|
return valid_items
|
|
|
|
|
2018-09-01 04:25:43 +00:00
|
|
|
|
2018-08-01 11:08:39 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def publish_selected_items(items_to_publish):
|
|
|
|
items_to_publish = json.loads(items_to_publish)
|
2019-12-13 09:35:43 +00:00
|
|
|
items_to_update = []
|
2018-08-01 11:08:39 +00:00
|
|
|
if not len(items_to_publish):
|
2019-07-03 09:45:08 +00:00
|
|
|
frappe.throw(_('No items to publish'))
|
2018-08-01 11:08:39 +00:00
|
|
|
|
2018-08-24 11:08:34 +00:00
|
|
|
for item in items_to_publish:
|
|
|
|
item_code = item.get('item_code')
|
2018-08-01 11:08:39 +00:00
|
|
|
frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
|
|
|
|
|
2019-12-13 09:35:43 +00:00
|
|
|
hub_dict = {
|
2018-08-19 14:09:00 +00:00
|
|
|
'doctype': 'Hub Tracked Item',
|
|
|
|
'item_code': item_code,
|
2019-12-13 09:35:43 +00:00
|
|
|
'published': 1,
|
2018-08-24 11:08:34 +00:00
|
|
|
'hub_category': item.get('hub_category'),
|
|
|
|
'image_list': item.get('image_list')
|
2019-12-13 09:35:43 +00:00
|
|
|
}
|
|
|
|
if frappe.db.exists('Hub Tracked Item', item_code):
|
|
|
|
items_to_update.append(item)
|
|
|
|
hub_tracked_item = frappe.get_doc('Hub Tracked Item', item_code)
|
|
|
|
hub_tracked_item.update(hub_dict)
|
|
|
|
hub_tracked_items.save()
|
|
|
|
else:
|
|
|
|
frappe.get_doc(hub_dict).insert(ignore_if_duplicate=True)
|
|
|
|
|
|
|
|
items_to_publish = list(filter(lambda x: x not in items_to_update, items_to_publish))
|
|
|
|
new_items = map_fields(items_to_publish)
|
|
|
|
existing_items = map_fields(items_to_update)
|
2018-08-21 14:29:15 +00:00
|
|
|
|
2018-08-01 11:08:39 +00:00
|
|
|
try:
|
2018-08-29 21:00:40 +00:00
|
|
|
item_sync_preprocess(len(items))
|
2018-09-03 11:54:17 +00:00
|
|
|
convert_relative_image_urls_to_absolute(items)
|
2018-08-21 14:29:15 +00:00
|
|
|
|
|
|
|
# TODO: Publish Progress
|
|
|
|
connection = get_hub_connection()
|
2019-12-13 09:35:43 +00:00
|
|
|
connection.insert_many(new_items)
|
|
|
|
connection.bulk_update(existing_items)
|
2018-08-21 14:29:15 +00:00
|
|
|
|
2018-08-29 21:00:40 +00:00
|
|
|
item_sync_postprocess()
|
2018-08-01 11:08:39 +00:00
|
|
|
except Exception as e:
|
2018-08-29 08:57:47 +00:00
|
|
|
frappe.log_error(message=e, title='Hub Sync Error')
|
2018-08-01 11:08:39 +00:00
|
|
|
|
2019-11-29 10:22:29 +00:00
|
|
|
@frappe.whitelist()
|
2019-12-09 13:54:29 +00:00
|
|
|
def unpublish_item(item_code, hub_item_name):
|
2019-11-29 10:22:29 +00:00
|
|
|
''' Remove item listing from the marketplace '''
|
|
|
|
|
2019-12-09 13:54:29 +00:00
|
|
|
response = call_hub_method('unpublish_item', {
|
|
|
|
'hub_item_name': hub_item_name
|
|
|
|
})
|
2019-11-29 10:22:29 +00:00
|
|
|
|
2019-12-09 13:54:29 +00:00
|
|
|
if response:
|
|
|
|
frappe.db.set_value('Item', item_code, 'publish_in_hub', 0)
|
|
|
|
else:
|
|
|
|
frappe.throw(_('Unable to update remote activity'))
|
2019-11-29 10:22:29 +00:00
|
|
|
|
2018-09-03 10:12:00 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_unregistered_users():
|
|
|
|
settings = frappe.get_single('Marketplace Settings')
|
|
|
|
registered_users = [user.user for user in settings.users] + ['Administrator', 'Guest']
|
|
|
|
all_users = [user.name for user in frappe.db.get_all('User', filters={'enabled': 1})]
|
|
|
|
unregistered_users = [user for user in all_users if user not in registered_users]
|
|
|
|
return unregistered_users
|
|
|
|
|
2018-09-01 04:25:43 +00:00
|
|
|
|
2018-08-29 21:00:40 +00:00
|
|
|
def item_sync_preprocess(intended_item_publish_count):
|
|
|
|
response = call_hub_method('pre_items_publish', {
|
|
|
|
'intended_item_publish_count': intended_item_publish_count
|
2018-08-01 11:08:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if response:
|
2018-08-31 10:45:06 +00:00
|
|
|
frappe.db.set_value("Marketplace Settings", "Marketplace Settings", "sync_in_progress", 1)
|
2018-08-01 11:08:39 +00:00
|
|
|
return response
|
|
|
|
else:
|
2019-07-03 09:45:08 +00:00
|
|
|
frappe.throw(_('Unable to update remote activity'))
|
2018-08-01 11:08:39 +00:00
|
|
|
|
2018-09-01 04:25:43 +00:00
|
|
|
|
2018-08-29 21:00:40 +00:00
|
|
|
def item_sync_postprocess():
|
|
|
|
response = call_hub_method('post_items_publish', {})
|
2018-08-01 11:08:39 +00:00
|
|
|
if response:
|
2018-08-31 10:45:06 +00:00
|
|
|
frappe.db.set_value('Marketplace Settings', 'Marketplace Settings', 'last_sync_datetime', frappe.utils.now())
|
2018-08-01 11:08:39 +00:00
|
|
|
else:
|
2019-07-03 09:45:08 +00:00
|
|
|
frappe.throw(_('Unable to update remote activity'))
|
2018-08-01 11:08:39 +00:00
|
|
|
|
2018-08-31 10:45:06 +00:00
|
|
|
frappe.db.set_value('Marketplace Settings', 'Marketplace Settings', 'sync_in_progress', 0)
|
2018-08-21 14:29:15 +00:00
|
|
|
|
2018-08-28 11:47:29 +00:00
|
|
|
|
2018-09-03 11:54:17 +00:00
|
|
|
def convert_relative_image_urls_to_absolute(items):
|
2018-09-14 10:01:35 +00:00
|
|
|
from six.moves.urllib.parse import urljoin
|
2018-09-03 11:54:17 +00:00
|
|
|
|
2018-08-28 11:47:29 +00:00
|
|
|
for item in items:
|
|
|
|
file_path = item['image']
|
2018-09-03 11:54:17 +00:00
|
|
|
|
|
|
|
if file_path.startswith('/files/'):
|
|
|
|
item['image'] = urljoin(frappe.utils.get_url(), file_path)
|
2018-08-28 11:47:29 +00:00
|
|
|
|
|
|
|
|
2018-08-01 11:08:39 +00:00
|
|
|
def get_hub_connection():
|
2018-08-31 19:44:29 +00:00
|
|
|
settings = frappe.get_single('Marketplace Settings')
|
|
|
|
marketplace_url = settings.marketplace_url
|
2018-09-02 12:59:00 +00:00
|
|
|
hub_user = settings.get_hub_user(frappe.session.user)
|
2018-08-01 11:08:39 +00:00
|
|
|
|
2018-09-02 12:59:00 +00:00
|
|
|
if hub_user:
|
|
|
|
password = hub_user.get_password()
|
|
|
|
hub_connection = FrappeClient(marketplace_url, hub_user.user, password)
|
2018-08-29 09:32:39 +00:00
|
|
|
return hub_connection
|
2018-08-31 19:44:29 +00:00
|
|
|
else:
|
|
|
|
read_only_hub_connection = FrappeClient(marketplace_url)
|
|
|
|
return read_only_hub_connection
|
|
|
|
|
2018-08-21 14:29:15 +00:00
|
|
|
|
|
|
|
def get_field_mappings():
|
|
|
|
return []
|