Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import frappe, requests, json |
| 3 | from frappe.utils import now |
| 4 | from frappe.frappeclient import FrappeClient |
| 5 | |
| 6 | @frappe.whitelist() |
| 7 | def call_hub_method(method, params=None): |
| 8 | connection = get_hub_connection() |
| 9 | |
| 10 | if type(params) == unicode: |
| 11 | params = json.loads(params) |
| 12 | |
| 13 | params.update({ |
| 14 | 'cmd': 'hub.hub.api.' + method |
| 15 | }) |
| 16 | |
| 17 | response = connection.post_request(params) |
| 18 | return response |
| 19 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 20 | @frappe.whitelist() |
| 21 | def get_valid_items(search_value=''): |
| 22 | items = frappe.get_list( |
| 23 | 'Item', |
| 24 | fields=["*"], |
| 25 | filters={ |
| 26 | 'item_name': ['like', '%' + search_value + '%'], |
| 27 | 'publish_in_hub': 0 |
| 28 | }, |
| 29 | order_by="modified desc" |
| 30 | ) |
| 31 | |
| 32 | valid_items = filter(lambda x: x.image and x.description, items) |
| 33 | |
| 34 | def attach_source_type(item): |
| 35 | item.source_type = "local" |
| 36 | return item |
| 37 | |
| 38 | valid_items = map(lambda x: attach_source_type(x), valid_items) |
| 39 | return valid_items |
| 40 | |
| 41 | @frappe.whitelist() |
| 42 | def publish_selected_items(items_to_publish): |
| 43 | items_to_publish = json.loads(items_to_publish) |
| 44 | if not len(items_to_publish): |
| 45 | return |
| 46 | |
| 47 | for item_code in items_to_publish: |
| 48 | frappe.db.set_value('Item', item_code, 'publish_in_hub', 1) |
| 49 | |
| 50 | try: |
| 51 | hub_settings = frappe.get_doc('Hub Settings') |
| 52 | item_sync_preprocess() |
| 53 | hub_settings.sync() |
| 54 | except Exception as e: |
| 55 | frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0) |
| 56 | frappe.throw(e) |
| 57 | |
| 58 | def item_sync_preprocess(): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 59 | hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email") |
| 60 | |
| 61 | response = call_hub_method('add_hub_seller_activity', { |
| 62 | 'hub_seller': hub_seller, |
| 63 | 'activity_details': json.dumps({ |
| 64 | 'subject': 'Publishing items', |
| 65 | 'status': 'Success' |
| 66 | }) |
| 67 | }) |
| 68 | |
| 69 | if response: |
| 70 | frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1) |
| 71 | return response |
| 72 | else: |
| 73 | frappe.throw('Unable to update remote activity') |
| 74 | |
| 75 | def item_sync_postprocess(sync_details): |
| 76 | hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email") |
| 77 | |
| 78 | response = call_hub_method('add_hub_seller_activity', { |
| 79 | 'hub_seller': hub_seller, |
| 80 | 'activity_details': json.dumps({ |
| 81 | 'subject': 'Publishing items:' + sync_details['status'], |
| 82 | 'content': json.dumps(sync_details['stats']) |
| 83 | }) |
| 84 | }) |
| 85 | |
| 86 | if response: |
| 87 | frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0) |
| 88 | frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now()) |
| 89 | else: |
| 90 | frappe.throw('Unable to update remote activity') |
| 91 | |
| 92 | def get_hub_connection(): |
| 93 | if frappe.db.exists('Data Migration Connector', 'Hub Connector'): |
| 94 | hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector') |
| 95 | hub_connection = hub_connector.get_connection() |
| 96 | return hub_connection.connection |
| 97 | |
| 98 | # read-only connection |
| 99 | hub_connection = FrappeClient(frappe.conf.hub_url) |
| 100 | return hub_connection |