Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 2 | |
| 3 | import frappe |
| 4 | import json |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 5 | |
| 6 | from frappe import _ |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 7 | from frappe.frappeclient import FrappeClient |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 8 | from frappe.desk.form.load import get_attachments |
Suraj Shetty | 3cd0c54 | 2018-08-21 16:29:06 +0530 | [diff] [blame] | 9 | from six import string_types |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 10 | |
Prateeksha Singh | 810ae01 | 2018-09-01 01:14:29 +0530 | [diff] [blame] | 11 | current_user = frappe.session.user |
| 12 | |
Prateeksha Singh | 4e1fd0b | 2018-09-01 09:55:43 +0530 | [diff] [blame] | 13 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 14 | @frappe.whitelist() |
Prateeksha Singh | 47cc30d | 2018-09-03 13:11:23 +0530 | [diff] [blame] | 15 | def register_marketplace(company, company_description): |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 16 | validate_registerer() |
Faris Ansari | ad0be6a | 2018-09-03 15:42:00 +0530 | [diff] [blame] | 17 | |
Faris Ansari | 255dd0e | 2018-09-01 14:30:46 +0530 | [diff] [blame] | 18 | settings = frappe.get_single('Marketplace Settings') |
Prateeksha Singh | 47cc30d | 2018-09-03 13:11:23 +0530 | [diff] [blame] | 19 | message = settings.register_seller(company, company_description) |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 20 | |
Faris Ansari | 255dd0e | 2018-09-01 14:30:46 +0530 | [diff] [blame] | 21 | if message.get('hub_seller_name'): |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 22 | settings.registered = 1 |
Faris Ansari | 255dd0e | 2018-09-01 14:30:46 +0530 | [diff] [blame] | 23 | settings.hub_seller_name = message.get('hub_seller_name') |
| 24 | settings.save() |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 25 | |
Prateeksha Singh | b2b745f | 2018-09-01 17:20:08 +0530 | [diff] [blame] | 26 | settings.add_hub_user(frappe.session.user) |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 27 | |
Faris Ansari | 255dd0e | 2018-09-01 14:30:46 +0530 | [diff] [blame] | 28 | return { 'ok': 1 } |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 29 | |
Prateeksha Singh | 4e1fd0b | 2018-09-01 09:55:43 +0530 | [diff] [blame] | 30 | |
| 31 | @frappe.whitelist() |
Faris Ansari | 68129df | 2018-09-02 18:29:00 +0530 | [diff] [blame] | 32 | def register_users(user_list): |
| 33 | user_list = json.loads(user_list) |
| 34 | |
| 35 | settings = frappe.get_single('Marketplace Settings') |
| 36 | |
| 37 | for user in user_list: |
| 38 | settings.add_hub_user(user) |
| 39 | |
| 40 | return user_list |
Prateeksha Singh | 4e1fd0b | 2018-09-01 09:55:43 +0530 | [diff] [blame] | 41 | |
| 42 | |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 43 | def validate_registerer(): |
Prateeksha Singh | 810ae01 | 2018-09-01 01:14:29 +0530 | [diff] [blame] | 44 | if current_user == 'Administrator': |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 45 | frappe.throw(_('Please login as another user to register on Marketplace')) |
| 46 | |
Faris Ansari | 255dd0e | 2018-09-01 14:30:46 +0530 | [diff] [blame] | 47 | valid_roles = ['System Manager', 'Item Manager'] |
| 48 | |
| 49 | if not frappe.utils.is_subset(valid_roles, frappe.get_roles()): |
| 50 | frappe.throw(_('Only users with {0} role can register on Marketplace').format(', '.join(valid_roles)), |
| 51 | frappe.PermissionError) |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 52 | |
Prateeksha Singh | 4e1fd0b | 2018-09-01 09:55:43 +0530 | [diff] [blame] | 53 | |
Prateeksha Singh | a8065ff | 2018-08-31 22:41:26 +0530 | [diff] [blame] | 54 | @frappe.whitelist() |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 55 | def call_hub_method(method, params=None): |
| 56 | connection = get_hub_connection() |
| 57 | |
Suraj Shetty | 3cd0c54 | 2018-08-21 16:29:06 +0530 | [diff] [blame] | 58 | if isinstance(params, string_types): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 59 | params = json.loads(params) |
| 60 | |
| 61 | params.update({ |
| 62 | 'cmd': 'hub.hub.api.' + method |
| 63 | }) |
| 64 | |
| 65 | response = connection.post_request(params) |
| 66 | return response |
| 67 | |
Prateeksha Singh | 4e1fd0b | 2018-09-01 09:55:43 +0530 | [diff] [blame] | 68 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 69 | def map_fields(items): |
| 70 | field_mappings = get_field_mappings() |
| 71 | table_fields = [d.fieldname for d in frappe.get_meta('Item').get_table_fields()] |
| 72 | |
Mangesh-Khairnar | 8287c3d | 2019-11-29 15:52:29 +0530 | [diff] [blame] | 73 | hub_seller_name = frappe.db.get_value('Marketplace Settings', 'Marketplace Settings', 'hub_seller_name') |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 74 | |
| 75 | for item in items: |
| 76 | for fieldname in table_fields: |
| 77 | item.pop(fieldname, None) |
| 78 | |
| 79 | for mapping in field_mappings: |
| 80 | local_fieldname = mapping.get('local_fieldname') |
| 81 | remote_fieldname = mapping.get('remote_fieldname') |
| 82 | |
| 83 | value = item.get(local_fieldname) |
| 84 | item.pop(local_fieldname, None) |
| 85 | item[remote_fieldname] = value |
| 86 | |
| 87 | item['doctype'] = 'Hub Item' |
Prateeksha Singh | 85563dd | 2018-09-03 02:44:27 +0530 | [diff] [blame] | 88 | item['hub_seller'] = hub_seller_name |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 89 | item.pop('attachments', None) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 90 | |
| 91 | return items |
| 92 | |
Prateeksha Singh | 4e1fd0b | 2018-09-01 09:55:43 +0530 | [diff] [blame] | 93 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 94 | @frappe.whitelist() |
| 95 | def get_valid_items(search_value=''): |
| 96 | items = frappe.get_list( |
| 97 | 'Item', |
| 98 | fields=["*"], |
| 99 | filters={ |
Faris Ansari | 00f6aef | 2018-08-30 18:54:28 +0530 | [diff] [blame] | 100 | 'disabled': 0, |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 101 | 'item_name': ['like', '%' + search_value + '%'], |
| 102 | 'publish_in_hub': 0 |
| 103 | }, |
| 104 | order_by="modified desc" |
| 105 | ) |
| 106 | |
| 107 | valid_items = filter(lambda x: x.image and x.description, items) |
| 108 | |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 109 | def prepare_item(item): |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 110 | item.source_type = "local" |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 111 | item.attachments = get_attachments('Item', item.item_code) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 112 | return item |
| 113 | |
Prateeksha Singh | 02c176c | 2018-08-29 14:27:47 +0530 | [diff] [blame] | 114 | valid_items = map(prepare_item, valid_items) |
Prateeksha Singh | a525d12 | 2018-08-19 22:31:33 +0530 | [diff] [blame] | 115 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 116 | return valid_items |
| 117 | |
Mangesh-Khairnar | 0150382 | 2019-12-05 02:10:57 +0530 | [diff] [blame] | 118 | @frappe.whitelist() |
Mangesh-Khairnar | c1a1e7d | 2019-12-19 20:11:37 +0530 | [diff] [blame] | 119 | def update_item(ref_doc, data): |
Mangesh-Khairnar | 0150382 | 2019-12-05 02:10:57 +0530 | [diff] [blame] | 120 | data = json.loads(data) |
| 121 | |
Mangesh-Khairnar | c1a1e7d | 2019-12-19 20:11:37 +0530 | [diff] [blame] | 122 | data.update(dict(doctype='Hub Item', name=ref_doc)) |
Mangesh-Khairnar | 0150382 | 2019-12-05 02:10:57 +0530 | [diff] [blame] | 123 | try: |
| 124 | connection = get_hub_connection() |
| 125 | connection.update(data) |
| 126 | except Exception as e: |
| 127 | frappe.log_error(message=e, title='Hub Sync Error') |
Prateeksha Singh | 4e1fd0b | 2018-09-01 09:55:43 +0530 | [diff] [blame] | 128 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 129 | @frappe.whitelist() |
| 130 | def publish_selected_items(items_to_publish): |
| 131 | items_to_publish = json.loads(items_to_publish) |
Mangesh-Khairnar | 4bea44b | 2019-12-13 15:05:43 +0530 | [diff] [blame] | 132 | items_to_update = [] |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 133 | if not len(items_to_publish): |
Anurag Mishra | 841d852 | 2019-07-03 15:15:08 +0530 | [diff] [blame] | 134 | frappe.throw(_('No items to publish')) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 135 | |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 136 | for item in items_to_publish: |
| 137 | item_code = item.get('item_code') |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 138 | frappe.db.set_value('Item', item_code, 'publish_in_hub', 1) |
| 139 | |
Mangesh-Khairnar | 4bea44b | 2019-12-13 15:05:43 +0530 | [diff] [blame] | 140 | hub_dict = { |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 141 | 'doctype': 'Hub Tracked Item', |
| 142 | 'item_code': item_code, |
Mangesh-Khairnar | 4bea44b | 2019-12-13 15:05:43 +0530 | [diff] [blame] | 143 | 'published': 1, |
Prateeksha Singh | 40f7c46 | 2018-08-24 16:38:34 +0530 | [diff] [blame] | 144 | 'hub_category': item.get('hub_category'), |
| 145 | 'image_list': item.get('image_list') |
Mangesh-Khairnar | 4bea44b | 2019-12-13 15:05:43 +0530 | [diff] [blame] | 146 | } |
Mangesh-Khairnar | c4c7d15 | 2020-05-04 11:25:06 +0530 | [diff] [blame] | 147 | frappe.get_doc(hub_dict).insert(ignore_if_duplicate=True) |
Prateeksha Singh | 0a60d1c | 2018-08-19 19:39:00 +0530 | [diff] [blame] | 148 | |
Mangesh-Khairnar | c4c7d15 | 2020-05-04 11:25:06 +0530 | [diff] [blame] | 149 | items = map_fields(items_to_publish) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 150 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 151 | try: |
Prateeksha Singh | a6f184b | 2018-08-30 02:30:40 +0530 | [diff] [blame] | 152 | item_sync_preprocess(len(items)) |
Faris Ansari | 49ccdd6 | 2018-09-03 17:24:17 +0530 | [diff] [blame] | 153 | convert_relative_image_urls_to_absolute(items) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 154 | |
| 155 | # TODO: Publish Progress |
| 156 | connection = get_hub_connection() |
Mangesh-Khairnar | c4c7d15 | 2020-05-04 11:25:06 +0530 | [diff] [blame] | 157 | connection.insert_many(items) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 158 | |
Prateeksha Singh | a6f184b | 2018-08-30 02:30:40 +0530 | [diff] [blame] | 159 | item_sync_postprocess() |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 160 | except Exception as e: |
Prateeksha Singh | 02c176c | 2018-08-29 14:27:47 +0530 | [diff] [blame] | 161 | frappe.log_error(message=e, title='Hub Sync Error') |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 162 | |
Faris Ansari | ad0be6a | 2018-09-03 15:42:00 +0530 | [diff] [blame] | 163 | @frappe.whitelist() |
Mangesh-Khairnar | 0cb2783 | 2019-12-09 19:24:29 +0530 | [diff] [blame] | 164 | def unpublish_item(item_code, hub_item_name): |
Mangesh-Khairnar | 8287c3d | 2019-11-29 15:52:29 +0530 | [diff] [blame] | 165 | ''' Remove item listing from the marketplace ''' |
Mangesh-Khairnar | 8287c3d | 2019-11-29 15:52:29 +0530 | [diff] [blame] | 166 | |
Mangesh-Khairnar | 0cb2783 | 2019-12-09 19:24:29 +0530 | [diff] [blame] | 167 | response = call_hub_method('unpublish_item', { |
| 168 | 'hub_item_name': hub_item_name |
| 169 | }) |
Mangesh-Khairnar | 8287c3d | 2019-11-29 15:52:29 +0530 | [diff] [blame] | 170 | |
Mangesh-Khairnar | 0cb2783 | 2019-12-09 19:24:29 +0530 | [diff] [blame] | 171 | if response: |
| 172 | frappe.db.set_value('Item', item_code, 'publish_in_hub', 0) |
Mangesh-Khairnar | c4c7d15 | 2020-05-04 11:25:06 +0530 | [diff] [blame] | 173 | frappe.delete_doc('Hub Tracked Item', item_code) |
Mangesh-Khairnar | 0cb2783 | 2019-12-09 19:24:29 +0530 | [diff] [blame] | 174 | else: |
| 175 | frappe.throw(_('Unable to update remote activity')) |
Mangesh-Khairnar | 8287c3d | 2019-11-29 15:52:29 +0530 | [diff] [blame] | 176 | |
| 177 | @frappe.whitelist() |
Faris Ansari | ad0be6a | 2018-09-03 15:42:00 +0530 | [diff] [blame] | 178 | def get_unregistered_users(): |
| 179 | settings = frappe.get_single('Marketplace Settings') |
| 180 | registered_users = [user.user for user in settings.users] + ['Administrator', 'Guest'] |
| 181 | all_users = [user.name for user in frappe.db.get_all('User', filters={'enabled': 1})] |
| 182 | unregistered_users = [user for user in all_users if user not in registered_users] |
| 183 | return unregistered_users |
| 184 | |
Prateeksha Singh | 4e1fd0b | 2018-09-01 09:55:43 +0530 | [diff] [blame] | 185 | |
Prateeksha Singh | a6f184b | 2018-08-30 02:30:40 +0530 | [diff] [blame] | 186 | def item_sync_preprocess(intended_item_publish_count): |
| 187 | response = call_hub_method('pre_items_publish', { |
| 188 | 'intended_item_publish_count': intended_item_publish_count |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 189 | }) |
| 190 | |
| 191 | if response: |
Faris Ansari | f9a66c1 | 2018-08-31 16:15:06 +0530 | [diff] [blame] | 192 | frappe.db.set_value("Marketplace Settings", "Marketplace Settings", "sync_in_progress", 1) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 193 | return response |
| 194 | else: |
Anurag Mishra | 841d852 | 2019-07-03 15:15:08 +0530 | [diff] [blame] | 195 | frappe.throw(_('Unable to update remote activity')) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 196 | |
Prateeksha Singh | 4e1fd0b | 2018-09-01 09:55:43 +0530 | [diff] [blame] | 197 | |
Prateeksha Singh | a6f184b | 2018-08-30 02:30:40 +0530 | [diff] [blame] | 198 | def item_sync_postprocess(): |
| 199 | response = call_hub_method('post_items_publish', {}) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 200 | if response: |
Faris Ansari | f9a66c1 | 2018-08-31 16:15:06 +0530 | [diff] [blame] | 201 | 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] | 202 | else: |
Anurag Mishra | 841d852 | 2019-07-03 15:15:08 +0530 | [diff] [blame] | 203 | frappe.throw(_('Unable to update remote activity')) |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 204 | |
Faris Ansari | f9a66c1 | 2018-08-31 16:15:06 +0530 | [diff] [blame] | 205 | frappe.db.set_value('Marketplace Settings', 'Marketplace Settings', 'sync_in_progress', 0) |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 206 | |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 207 | |
Faris Ansari | 49ccdd6 | 2018-09-03 17:24:17 +0530 | [diff] [blame] | 208 | def convert_relative_image_urls_to_absolute(items): |
Faris Ansari | 31e5431 | 2018-09-14 15:31:35 +0530 | [diff] [blame] | 209 | from six.moves.urllib.parse import urljoin |
Faris Ansari | 49ccdd6 | 2018-09-03 17:24:17 +0530 | [diff] [blame] | 210 | |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 211 | for item in items: |
| 212 | file_path = item['image'] |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 213 | |
Faris Ansari | 49ccdd6 | 2018-09-03 17:24:17 +0530 | [diff] [blame] | 214 | if file_path.startswith('/files/'): |
| 215 | item['image'] = urljoin(frappe.utils.get_url(), file_path) |
Faris Ansari | 811a2de | 2018-08-28 17:17:29 +0530 | [diff] [blame] | 216 | |
| 217 | |
Prateeksha Singh | 6495d53 | 2018-08-01 16:38:39 +0530 | [diff] [blame] | 218 | def get_hub_connection(): |
Prateeksha Singh | 810ae01 | 2018-09-01 01:14:29 +0530 | [diff] [blame] | 219 | settings = frappe.get_single('Marketplace Settings') |
| 220 | marketplace_url = settings.marketplace_url |
Faris Ansari | 68129df | 2018-09-02 18:29:00 +0530 | [diff] [blame] | 221 | hub_user = settings.get_hub_user(frappe.session.user) |
Faris Ansari | 64202ad | 2018-08-29 15:02:39 +0530 | [diff] [blame] | 222 | |
Faris Ansari | 68129df | 2018-09-02 18:29:00 +0530 | [diff] [blame] | 223 | if hub_user: |
| 224 | password = hub_user.get_password() |
| 225 | hub_connection = FrappeClient(marketplace_url, hub_user.user, password) |
Faris Ansari | 64202ad | 2018-08-29 15:02:39 +0530 | [diff] [blame] | 226 | return hub_connection |
Prateeksha Singh | 810ae01 | 2018-09-01 01:14:29 +0530 | [diff] [blame] | 227 | else: |
| 228 | read_only_hub_connection = FrappeClient(marketplace_url) |
| 229 | return read_only_hub_connection |
| 230 | |
Prateeksha Singh | caadd8a | 2018-08-21 19:59:15 +0530 | [diff] [blame] | 231 | |
| 232 | def get_field_mappings(): |
| 233 | return [] |