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