Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Prateeksha Singh | 02c176c | 2018-08-29 14:27:47 +0530 | [diff] [blame] | 2 | import frappe, json |
Faris Ansari | 95e4141 | 2018-08-30 11:46:14 +0530 | [diff] [blame] | 3 | import io, base64, os, requests |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 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 |
Faris Ansari | 95e4141 | 2018-08-30 11:46:14 +0530 | [diff] [blame] | 6 | from frappe.utils.file_manager import get_file_path |
Suraj Shetty | 3cd0c54 | 2018-08-21 16:29:06 +0530 | [diff] [blame] | 7 | from six import string_types |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 8 | |
| 9 | @frappe.whitelist() |
| 10 | def call_hub_method(method, params=None): |
| 11 | connection = get_hub_connection() |
| 12 | |
Suraj Shetty | 3cd0c54 | 2018-08-21 16:29:06 +0530 | [diff] [blame] | 13 | if isinstance(params, string_types): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 14 | params = json.loads(params) |
| 15 | |
| 16 | params.update({ |
| 17 | 'cmd': 'hub.hub.api.' + method |
| 18 | }) |
| 19 | |
| 20 | response = connection.post_request(params) |
| 21 | return response |
| 22 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 23 | def map_fields(items): |
| 24 | field_mappings = get_field_mappings() |
| 25 | table_fields = [d.fieldname for d in frappe.get_meta('Item').get_table_fields()] |
| 26 | |
| 27 | hub_seller = frappe.db.get_value('Hub Settings' , 'Hub Settings', 'company_email') |
| 28 | |
| 29 | for item in items: |
| 30 | for fieldname in table_fields: |
| 31 | item.pop(fieldname, None) |
| 32 | |
| 33 | for mapping in field_mappings: |
| 34 | local_fieldname = mapping.get('local_fieldname') |
| 35 | remote_fieldname = mapping.get('remote_fieldname') |
| 36 | |
| 37 | value = item.get(local_fieldname) |
| 38 | item.pop(local_fieldname, None) |
| 39 | item[remote_fieldname] = value |
| 40 | |
| 41 | item['doctype'] = 'Hub Item' |
| 42 | item['hub_seller'] = hub_seller |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 43 | item.pop('attachments', None) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 44 | |
| 45 | return items |
| 46 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 47 | @frappe.whitelist() |
| 48 | def get_valid_items(search_value=''): |
| 49 | items = frappe.get_list( |
| 50 | 'Item', |
| 51 | fields=["*"], |
| 52 | filters={ |
| 53 | 'item_name': ['like', '%' + search_value + '%'], |
| 54 | 'publish_in_hub': 0 |
| 55 | }, |
| 56 | order_by="modified desc" |
| 57 | ) |
| 58 | |
| 59 | valid_items = filter(lambda x: x.image and x.description, items) |
| 60 | |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 61 | def prepare_item(item): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 62 | item.source_type = "local" |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 63 | item.attachments = get_attachments('Item', item.item_code) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 64 | return item |
| 65 | |
Prateeksha Singh | 02c176c | 2018-08-29 14:27:47 +0530 | [diff] [blame] | 66 | valid_items = map(prepare_item, valid_items) |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 67 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 68 | return valid_items |
| 69 | |
| 70 | @frappe.whitelist() |
| 71 | def publish_selected_items(items_to_publish): |
| 72 | items_to_publish = json.loads(items_to_publish) |
| 73 | if not len(items_to_publish): |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 74 | frappe.throw('No items to publish') |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 75 | |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 76 | for item in items_to_publish: |
| 77 | item_code = item.get('item_code') |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 78 | frappe.db.set_value('Item', item_code, 'publish_in_hub', 1) |
| 79 | |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 80 | frappe.get_doc({ |
| 81 | 'doctype': 'Hub Tracked Item', |
| 82 | 'item_code': item_code, |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 83 | 'hub_category': item.get('hub_category'), |
| 84 | 'image_list': item.get('image_list') |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 85 | }).insert(ignore_if_duplicate=True) |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 86 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 87 | |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 88 | items = map_fields(items_to_publish) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 89 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 90 | try: |
Prateeksha Singh | a6f184b | 2018-08-30 02:30:40 +0530 | [diff] [blame] | 91 | item_sync_preprocess(len(items)) |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 92 | load_base64_image_from_items(items) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 93 | |
| 94 | # TODO: Publish Progress |
| 95 | connection = get_hub_connection() |
| 96 | connection.insert_many(items) |
| 97 | |
Prateeksha Singh | a6f184b | 2018-08-30 02:30:40 +0530 | [diff] [blame] | 98 | item_sync_postprocess() |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 99 | except Exception as e: |
Prateeksha Singh | 02c176c | 2018-08-29 14:27:47 +0530 | [diff] [blame] | 100 | frappe.log_error(message=e, title='Hub Sync Error') |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 101 | |
Prateeksha Singh | a6f184b | 2018-08-30 02:30:40 +0530 | [diff] [blame] | 102 | def item_sync_preprocess(intended_item_publish_count): |
| 103 | response = call_hub_method('pre_items_publish', { |
| 104 | 'intended_item_publish_count': intended_item_publish_count |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 105 | }) |
| 106 | |
| 107 | if response: |
| 108 | frappe.db.set_value("Hub Settings", "Hub Settings", "sync_in_progress", 1) |
| 109 | return response |
| 110 | else: |
| 111 | frappe.throw('Unable to update remote activity') |
| 112 | |
Prateeksha Singh | a6f184b | 2018-08-30 02:30:40 +0530 | [diff] [blame] | 113 | def item_sync_postprocess(): |
| 114 | response = call_hub_method('post_items_publish', {}) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 115 | if response: |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 116 | frappe.db.set_value('Hub Settings', 'Hub Settings', 'last_sync_datetime', frappe.utils.now()) |
| 117 | else: |
| 118 | frappe.throw('Unable to update remote activity') |
| 119 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 120 | frappe.db.set_value('Hub Settings', 'Hub Settings', 'sync_in_progress', 0) |
| 121 | |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 122 | |
| 123 | def load_base64_image_from_items(items): |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 124 | for item in items: |
| 125 | file_path = item['image'] |
| 126 | file_name = os.path.basename(file_path) |
Faris Ansari | 8683bd8 | 2018-08-29 16:18:36 +0530 | [diff] [blame] | 127 | base64content = None |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 128 | |
| 129 | if file_path.startswith('http'): |
Faris Ansari | 8683bd8 | 2018-08-29 16:18:36 +0530 | [diff] [blame] | 130 | # fetch content and then base64 it |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 131 | url = file_path |
Faris Ansari | 8683bd8 | 2018-08-29 16:18:36 +0530 | [diff] [blame] | 132 | response = requests.get(url) |
| 133 | base64content = base64.b64encode(response.content) |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 134 | else: |
Faris Ansari | 8683bd8 | 2018-08-29 16:18:36 +0530 | [diff] [blame] | 135 | # read file then base64 it |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 136 | file_path = os.path.abspath(get_file_path(file_path)) |
Faris Ansari | 8683bd8 | 2018-08-29 16:18:36 +0530 | [diff] [blame] | 137 | with io.open(file_path, 'rb') as f: |
| 138 | base64content = base64.b64encode(f.read()) |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 139 | |
Faris Ansari | 8683bd8 | 2018-08-29 16:18:36 +0530 | [diff] [blame] | 140 | image_data = json.dumps({ |
| 141 | 'file_name': file_name, |
| 142 | 'base64': base64content |
| 143 | }) |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 144 | |
| 145 | item['image'] = image_data |
| 146 | |
| 147 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 148 | def get_hub_connection(): |
Faris Ansari | 64202ad | 2018-08-29 15:02:39 +0530 | [diff] [blame] | 149 | read_only = True |
| 150 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 151 | if frappe.db.exists('Data Migration Connector', 'Hub Connector'): |
| 152 | hub_connector = frappe.get_doc('Data Migration Connector', 'Hub Connector') |
Faris Ansari | 64202ad | 2018-08-29 15:02:39 +0530 | [diff] [blame] | 153 | |
| 154 | # full rights to user who registered as hub_seller |
| 155 | if hub_connector.username == frappe.session.user: |
| 156 | read_only = False |
| 157 | |
| 158 | if not read_only: |
| 159 | hub_connection = hub_connector.get_connection() |
| 160 | return hub_connection.connection |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 161 | |
| 162 | # read-only connection |
Faris Ansari | 64202ad | 2018-08-29 15:02:39 +0530 | [diff] [blame] | 163 | if read_only: |
Faris Ansari | 114d595 | 2018-08-29 18:24:49 +0530 | [diff] [blame] | 164 | hub_url = frappe.db.get_single_value('Hub Settings', 'hub_url') |
| 165 | hub_connection = FrappeClient(hub_url) |
Faris Ansari | 64202ad | 2018-08-29 15:02:39 +0530 | [diff] [blame] | 166 | return hub_connection |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 167 | |
| 168 | def get_field_mappings(): |
| 169 | return [] |