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