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 |
Suraj Shetty | 3cd0c54 | 2018-08-21 16:29:06 +0530 | [diff] [blame] | 6 | from six import string_types |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 7 | |
| 8 | @frappe.whitelist() |
| 9 | def call_hub_method(method, params=None): |
| 10 | connection = get_hub_connection() |
| 11 | |
Suraj Shetty | 3cd0c54 | 2018-08-21 16:29:06 +0530 | [diff] [blame] | 12 | if isinstance(params, string_types): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 13 | params = json.loads(params) |
| 14 | |
| 15 | params.update({ |
| 16 | 'cmd': 'hub.hub.api.' + method |
| 17 | }) |
| 18 | |
| 19 | response = connection.post_request(params) |
| 20 | return response |
| 21 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 22 | def map_fields(items): |
| 23 | field_mappings = get_field_mappings() |
| 24 | table_fields = [d.fieldname for d in frappe.get_meta('Item').get_table_fields()] |
| 25 | |
| 26 | hub_seller = frappe.db.get_value('Hub Settings' , 'Hub Settings', 'company_email') |
| 27 | |
| 28 | for item in items: |
| 29 | for fieldname in table_fields: |
| 30 | item.pop(fieldname, None) |
| 31 | |
| 32 | for mapping in field_mappings: |
| 33 | local_fieldname = mapping.get('local_fieldname') |
| 34 | remote_fieldname = mapping.get('remote_fieldname') |
| 35 | |
| 36 | value = item.get(local_fieldname) |
| 37 | item.pop(local_fieldname, None) |
| 38 | item[remote_fieldname] = value |
| 39 | |
| 40 | item['doctype'] = 'Hub Item' |
| 41 | item['hub_seller'] = hub_seller |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 42 | item.pop('attachments', None) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 43 | |
| 44 | return items |
| 45 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 46 | @frappe.whitelist() |
| 47 | def get_valid_items(search_value=''): |
| 48 | items = frappe.get_list( |
| 49 | 'Item', |
| 50 | fields=["*"], |
| 51 | filters={ |
| 52 | 'item_name': ['like', '%' + search_value + '%'], |
| 53 | 'publish_in_hub': 0 |
| 54 | }, |
| 55 | order_by="modified desc" |
| 56 | ) |
| 57 | |
| 58 | valid_items = filter(lambda x: x.image and x.description, items) |
| 59 | |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 60 | def prepare_item(item): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 61 | item.source_type = "local" |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 62 | item.attachments = get_attachments('Item', item.item_code) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 63 | return item |
| 64 | |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 65 | valid_items = map(lambda x: prepare_item(x), valid_items) |
| 66 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 67 | return valid_items |
| 68 | |
| 69 | @frappe.whitelist() |
| 70 | def publish_selected_items(items_to_publish): |
| 71 | items_to_publish = json.loads(items_to_publish) |
| 72 | if not len(items_to_publish): |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 73 | frappe.throw('No items to publish') |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 74 | |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 75 | for item in items_to_publish: |
| 76 | item_code = item.get('item_code') |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 77 | frappe.db.set_value('Item', item_code, 'publish_in_hub', 1) |
| 78 | |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 79 | frappe.get_doc({ |
| 80 | 'doctype': 'Hub Tracked Item', |
| 81 | 'item_code': item_code, |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 82 | 'hub_category': item.get('hub_category'), |
| 83 | 'image_list': item.get('image_list') |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 84 | }).insert() |
| 85 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 86 | |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 87 | items = map_fields(items_to_publish) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 88 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 89 | try: |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 90 | item_sync_preprocess() |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 91 | |
| 92 | # TODO: Publish Progress |
| 93 | connection = get_hub_connection() |
| 94 | connection.insert_many(items) |
| 95 | |
| 96 | item_sync_postprocess({ |
| 97 | 'status': 'Success', |
| 98 | 'stats': len(items) |
| 99 | }) |
| 100 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 101 | except Exception as e: |
| 102 | frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 0) |
| 103 | frappe.throw(e) |
| 104 | |
| 105 | def item_sync_preprocess(): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 106 | hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email") |
| 107 | |
| 108 | response = call_hub_method('add_hub_seller_activity', { |
| 109 | 'hub_seller': hub_seller, |
| 110 | 'activity_details': json.dumps({ |
| 111 | 'subject': 'Publishing items', |
| 112 | 'status': 'Success' |
| 113 | }) |
| 114 | }) |
| 115 | |
| 116 | if response: |
| 117 | frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1) |
| 118 | return response |
| 119 | else: |
| 120 | frappe.throw('Unable to update remote activity') |
| 121 | |
| 122 | def item_sync_postprocess(sync_details): |
| 123 | hub_seller = frappe.db.get_value("Hub Settings", "Hub Settings", "company_email") |
| 124 | |
| 125 | response = call_hub_method('add_hub_seller_activity', { |
| 126 | 'hub_seller': hub_seller, |
| 127 | 'activity_details': json.dumps({ |
| 128 | 'subject': 'Publishing items:' + sync_details['status'], |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 129 | 'content': str(sync_details['stats']) + ' items synced.' |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 130 | }) |
| 131 | }) |
| 132 | |
| 133 | if response: |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 134 | frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now()) |
| 135 | else: |
| 136 | frappe.throw('Unable to update remote activity') |
| 137 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 138 | frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0) |
| 139 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 140 | def get_hub_connection(): |
| 141 | if frappe.db.exists('Data Migration Connector', 'Hub Connector'): |
| 142 | hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector') |
| 143 | hub_connection = hub_connector.get_connection() |
| 144 | return hub_connection.connection |
| 145 | |
| 146 | # read-only connection |
| 147 | hub_connection = FrappeClient(frappe.conf.hub_url) |
| 148 | return hub_connection |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 149 | |
| 150 | |
| 151 | def get_field_mappings(): |
| 152 | return [] |