mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-31 18:59:08 +00:00
refactor(hub connection): read only connection
- Seller activity in Activity Log - Move hub_url to site_config
This commit is contained in:
@@ -5,6 +5,7 @@ from __future__ import unicode_literals
|
|||||||
import frappe, requests, json
|
import frappe, requests, json
|
||||||
from frappe.utils import now, nowdate, cint
|
from frappe.utils import now, nowdate, cint
|
||||||
from frappe.utils.nestedset import get_root_of
|
from frappe.utils.nestedset import get_root_of
|
||||||
|
from frappe.frappeclient import FrappeClient
|
||||||
from frappe.contacts.doctype.contact.contact import get_default_contact
|
from frappe.contacts.doctype.contact.contact import get_default_contact
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
@@ -16,7 +17,7 @@ def enable_hub():
|
|||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def call_hub_method(method, params=None):
|
def call_hub_method(method, params=None):
|
||||||
connection = get_client_connection()
|
connection = get_hub_connection()
|
||||||
|
|
||||||
if type(params) == unicode:
|
if type(params) == unicode:
|
||||||
params = json.loads(params)
|
params = json.loads(params)
|
||||||
@@ -28,22 +29,6 @@ def call_hub_method(method, params=None):
|
|||||||
response = connection.post_request(params)
|
response = connection.post_request(params)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@frappe.whitelist()
|
|
||||||
def get_list(doctype, start=0, limit=20, fields=["*"], filters="{}", order_by=None):
|
|
||||||
connection = get_client_connection()
|
|
||||||
filters = json.loads(filters)
|
|
||||||
|
|
||||||
response = connection.get_list(doctype,
|
|
||||||
limit_start=start, limit_page_length=limit,
|
|
||||||
filters=filters, fields=['name'])
|
|
||||||
|
|
||||||
# Bad, need child tables in response
|
|
||||||
listing = []
|
|
||||||
for obj in response:
|
|
||||||
doc = connection.get_doc(doctype, obj['name'])
|
|
||||||
listing.append(doc)
|
|
||||||
|
|
||||||
return listing
|
|
||||||
|
|
||||||
#### LOCAL ITEMS
|
#### LOCAL ITEMS
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
@@ -76,34 +61,44 @@ def publish_selected_items(items_to_publish):
|
|||||||
for item_code in items_to_publish:
|
for item_code in items_to_publish:
|
||||||
frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
|
frappe.db.set_value('Item', item_code, 'publish_in_hub', 1)
|
||||||
|
|
||||||
hub_settings = frappe.get_doc('Hub Settings')
|
try:
|
||||||
remote_id = item_sync_preprocess()
|
hub_settings = frappe.get_doc('Hub Settings')
|
||||||
hub_settings.sync(remote_id)
|
item_sync_preprocess()
|
||||||
|
hub_settings.sync()
|
||||||
return remote_id
|
except Exception as e:
|
||||||
|
frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0)
|
||||||
|
frappe.throw(e)
|
||||||
|
|
||||||
def item_sync_preprocess():
|
def item_sync_preprocess():
|
||||||
# Call Hub to make a new activity
|
# Call Hub to make a new activity
|
||||||
# and return an activity ID
|
# and return an activity ID
|
||||||
# that will be used as the remote ID for the Migration Run
|
# that will be used as the remote ID for the Migration Run
|
||||||
|
|
||||||
response = call_hub_method('init_new_activity_for_seller', {
|
hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
|
||||||
'hub_seller': frappe.db.get_value("Hub Settings", "Hub Settings", "company_email"),
|
|
||||||
'activity_type': 'Items Publish'
|
response = call_hub_method('add_hub_seller_activity', {
|
||||||
|
'hub_seller': hub_seller,
|
||||||
|
'activity_details': json.dumps({
|
||||||
|
'subject': 'Publishing items',
|
||||||
|
'status': 'Success'
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
if response:
|
if response:
|
||||||
frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
|
frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1)
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
return ''
|
frappe.throw('Unable to update remote activity')
|
||||||
|
|
||||||
def item_sync_postprocess(obj):
|
def item_sync_postprocess(sync_details):
|
||||||
response = call_hub_method('update_activity_for_seller', {
|
hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email")
|
||||||
'hub_seller': frappe.db.get_value('Hub Settings', 'Hub Settings', 'company_email'),
|
|
||||||
'name': obj['remote_id'],
|
response = call_hub_method('add_hub_seller_activity', {
|
||||||
'status': obj['status'],
|
'hub_seller': hub_seller,
|
||||||
'stats': obj['stats']
|
'activity_details': json.dumps({
|
||||||
|
'subject': 'Publishing items:' + sync_details['status'],
|
||||||
|
'content': json.dumps(sync_details['stats'])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
if response:
|
if response:
|
||||||
@@ -145,31 +140,6 @@ def update_wishlist_item(item_name, remove=0):
|
|||||||
hub_settings.custom_data = item_names_str
|
hub_settings.custom_data = item_names_str
|
||||||
hub_settings.save()
|
hub_settings.save()
|
||||||
|
|
||||||
@frappe.whitelist()
|
|
||||||
def get_meta(doctype):
|
|
||||||
connection = get_client_connection()
|
|
||||||
meta = connection.get_doc('DocType', doctype)
|
|
||||||
categories = connection.get_list('Hub Category',
|
|
||||||
limit_start=0, limit_page_length=300,
|
|
||||||
filters={}, fields=['name'])
|
|
||||||
|
|
||||||
categories = [d.get('name') for d in categories]
|
|
||||||
return {
|
|
||||||
'meta': meta,
|
|
||||||
'companies': connection.get_list('Hub Company',
|
|
||||||
limit_start=0, limit_page_length=300,
|
|
||||||
filters={}, fields=['name']),
|
|
||||||
'categories': categories
|
|
||||||
}
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
|
||||||
def get_categories(parent='All Categories'):
|
|
||||||
# get categories info with parent category and stuff
|
|
||||||
connection = get_client_connection()
|
|
||||||
categories = connection.get_list('Hub Category', filters={'parent_hub_category': parent})
|
|
||||||
|
|
||||||
response = [{'value': c.get('name'), 'expandable': c.get('is_group')} for c in categories]
|
|
||||||
return response
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def update_category(hub_item_code, category):
|
def update_category(hub_item_code, category):
|
||||||
@@ -188,28 +158,14 @@ def update_category(hub_item_code, category):
|
|||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@frappe.whitelist()
|
|
||||||
def get_details(hub_sync_id=None, doctype='Hub Item'):
|
|
||||||
if not hub_sync_id:
|
|
||||||
return
|
|
||||||
connection = get_client_connection()
|
|
||||||
details = connection.get_doc(doctype, hub_sync_id)
|
|
||||||
reviews = details.get('reviews')
|
|
||||||
if reviews and len(reviews):
|
|
||||||
for r in reviews:
|
|
||||||
r.setdefault('pretty_date', frappe.utils.pretty_date(r.get('modified')))
|
|
||||||
details.setdefault('reviews', reviews)
|
|
||||||
return details
|
|
||||||
|
|
||||||
def get_client_connection():
|
|
||||||
# frappeclient connection
|
|
||||||
hub_connection = get_hub_connection()
|
|
||||||
return hub_connection.connection
|
|
||||||
|
|
||||||
def get_hub_connection():
|
def get_hub_connection():
|
||||||
hub_connector = frappe.get_doc(
|
if frappe.db.exists('Data Migration Connector', 'Hub Connector'):
|
||||||
'Data Migration Connector', 'Hub Connector')
|
hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector')
|
||||||
hub_connection = hub_connector.get_connection()
|
hub_connection = hub_connector.get_connection()
|
||||||
|
return hub_connection.connection
|
||||||
|
|
||||||
|
# read-only connection
|
||||||
|
hub_connection = FrappeClient(frappe.conf.hub_url)
|
||||||
return hub_connection
|
return hub_connection
|
||||||
|
|
||||||
def make_opportunity(buyer_name, email_id):
|
def make_opportunity(buyer_name, email_id):
|
||||||
|
|||||||
@@ -10,11 +10,6 @@ from frappe import _
|
|||||||
from erpnext.utilities.product import get_price, get_qty_in_stock
|
from erpnext.utilities.product import get_price, get_qty_in_stock
|
||||||
from six import string_types
|
from six import string_types
|
||||||
|
|
||||||
# hub_url = "https://hubmarket.org"
|
|
||||||
# hub_url = "http://159.89.175.122"
|
|
||||||
# hub_url = "http://erpnext.hub:8001"
|
|
||||||
hub_url = "http://hub.market:8000"
|
|
||||||
|
|
||||||
class HubSetupError(frappe.ValidationError): pass
|
class HubSetupError(frappe.ValidationError): pass
|
||||||
|
|
||||||
class HubSettings(Document):
|
class HubSettings(Document):
|
||||||
@@ -26,25 +21,21 @@ class HubSettings(Document):
|
|||||||
frappe.throw(_("Please select a Price List to publish pricing"))
|
frappe.throw(_("Please select a Price List to publish pricing"))
|
||||||
|
|
||||||
def get_hub_url(self):
|
def get_hub_url(self):
|
||||||
return hub_url
|
return frappe.conf.hub_url
|
||||||
|
|
||||||
def sync(self, remote_id):
|
def sync(self):
|
||||||
"""Create and execute Data Migration Run for Hub Sync plan"""
|
"""Create and execute Data Migration Run for Hub Sync plan"""
|
||||||
frappe.has_permission('Hub Settings', throw=True)
|
frappe.has_permission('Hub Settings', throw=True)
|
||||||
|
|
||||||
if remote_id:
|
doc = frappe.get_doc({
|
||||||
doc = frappe.get_doc({
|
'doctype': 'Data Migration Run',
|
||||||
'doctype': 'Data Migration Run',
|
'data_migration_plan': 'Hub Sync',
|
||||||
'data_migration_plan': 'Hub Sync',
|
'data_migration_connector': 'Hub Connector',
|
||||||
'data_migration_connector': 'Hub Connector',
|
'trigger_name': 'items-sync'
|
||||||
'remote_id': remote_id,
|
}).insert()
|
||||||
'trigger_name': 'items-sync'
|
|
||||||
}).insert()
|
|
||||||
|
|
||||||
self.sync_in_progress = 1
|
self.sync_in_progress = 1
|
||||||
doc.run()
|
doc.run()
|
||||||
else:
|
|
||||||
frappe.throw("No remote ID specified")
|
|
||||||
|
|
||||||
def register(self):
|
def register(self):
|
||||||
""" Create a User on hub.erpnext.org and return username/password """
|
""" Create a User on hub.erpnext.org and return username/password """
|
||||||
@@ -56,7 +47,7 @@ class HubSettings(Document):
|
|||||||
data = {
|
data = {
|
||||||
'profile': self.as_json()
|
'profile': self.as_json()
|
||||||
}
|
}
|
||||||
post_url = hub_url + '/api/method/hub.hub.api.register'
|
post_url = self.get_hub_url() + '/api/method/hub.hub.api.register'
|
||||||
|
|
||||||
response = requests.post(post_url, data=data, headers = {'accept': 'application/json'})
|
response = requests.post(post_url, data=data, headers = {'accept': 'application/json'})
|
||||||
|
|
||||||
@@ -99,7 +90,7 @@ class HubSettings(Document):
|
|||||||
'doctype': 'Data Migration Connector',
|
'doctype': 'Data Migration Connector',
|
||||||
'connector_type': 'Frappe',
|
'connector_type': 'Frappe',
|
||||||
'connector_name': 'Hub Connector',
|
'connector_name': 'Hub Connector',
|
||||||
'hostname': hub_url,
|
'hostname': self.get_hub_url(),
|
||||||
'username': message['email'],
|
'username': message['email'],
|
||||||
'password': message['password']
|
'password': message['password']
|
||||||
}).insert()
|
}).insert()
|
||||||
|
|||||||
Reference in New Issue
Block a user