From 12e71ef64d45d90cf04fce553e224430984a9f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Wed, 5 Jan 2022 11:58:04 +0100 Subject: [PATCH] [IMP]connector_pms_wubook: generic export cron --- .../data/queue_job_function_data.xml | 10 ++++ connector_pms_wubook/models/common/backend.py | 58 ++++++++++++++++--- .../models/pms_availability_plan/binding.py | 17 +++--- .../models/product_pricelist/binding.py | 18 +++--- 4 files changed, 81 insertions(+), 22 deletions(-) diff --git a/connector_pms_wubook/data/queue_job_function_data.xml b/connector_pms_wubook/data/queue_job_function_data.xml index 76b03c60fc1..eda71caf89d 100644 --- a/connector_pms_wubook/data/queue_job_function_data.xml +++ b/connector_pms_wubook/data/queue_job_function_data.xml @@ -273,4 +273,14 @@ + + + generic_export + + + + diff --git a/connector_pms_wubook/models/common/backend.py b/connector_pms_wubook/models/common/backend.py index 8ff7fd280fa..125f0b4c438 100644 --- a/connector_pms_wubook/models/common/backend.py +++ b/connector_pms_wubook/models/common/backend.py @@ -247,11 +247,53 @@ def _scheduler_export(self, interval=1, count=1): """ interval_sec = interval * 60 now = fields.Datetime.now() - for backend in self.env["channel.wubook.backend"].search([]): - if backend.user_id: - backend = backend.with_user(self.user_id) - for i in range(0, interval_sec, int(interval_sec / count)): - eta = fields.Datetime.add(now, seconds=i) - backend.export_property_availability(eta=eta) - backend.export_availability_plans() - backend.export_pricelists() + for i in range(0, interval_sec, int(interval_sec / count)): + eta = fields.Datetime.add(now, seconds=i) + self.with_delay( + eta=eta, + ).generic_export() + + def generic_export(self): + models_to_export = ( + self._get_models_to_export_multi() + self._get_models_to_export_one() + ) + for model in models_to_export: + records_to_export = self.env[model].search( + [ + ("synced_export", "=", False), + ] + ) + if self.user_id: + self = self.with_user(self.user_id) + backends = self.env["channel.wubook.backend"].browse( + records_to_export.backend_id.ids + ) + for backend in backends: + # If the model has several records in the same backend, + # we indicate as parameter the records to export + if model in self._get_models_to_export_multi(): + self.env[model].with_company( + backend.pms_property_id.company_id + ).with_delay().export_data( + backend_record=backend, + record_ids=records_to_export.filtered( + lambda r: r.backend_id.id == backend.id + ).ids, + ) + else: + self.env[model].with_company( + backend.pms_property_id.company_id + ).with_delay().export_data( + backend_record=backend, + ) + + def _get_models_to_export_multi(self): + return [ + "channel.wubook.product.pricelist", + "channel.wubook.pms.availability.plan", + ] + + def _get_models_to_export_one(self): + return [ + "channel.wubook.pms.property.availability", + ] diff --git a/connector_pms_wubook/models/pms_availability_plan/binding.py b/connector_pms_wubook/models/pms_availability_plan/binding.py index de7942d5cbc..37241612e82 100644 --- a/connector_pms_wubook/models/pms_availability_plan/binding.py +++ b/connector_pms_wubook/models/pms_availability_plan/binding.py @@ -96,16 +96,19 @@ def import_data( ) @api.model - def export_data(self, backend_record=None): + def export_data(self, backend_record=None, record_ids=False): """ Prepare the batch export of Availability Plan to Channel """ + domain = [ + ("channel_wubook_bind_ids.backend_id", "in", backend_record.ids), + "|", + ("pms_property_ids", "=", False), + ("pms_property_ids", "in", backend_record.pms_property_id.ids), + ] + if record_ids: + domain.append(("id", "in", record_ids)) return self.export_batch( backend_record=backend_record, - domain=[ - ("channel_wubook_bind_ids.backend_id", "in", backend_record.ids), - "|", - ("pms_property_ids", "=", False), - ("pms_property_ids", "in", backend_record.pms_property_id.ids), - ], + domain=domain, ) def resync_import(self): diff --git a/connector_pms_wubook/models/product_pricelist/binding.py b/connector_pms_wubook/models/product_pricelist/binding.py index a7fb73d7551..1d9da278aea 100644 --- a/connector_pms_wubook/models/product_pricelist/binding.py +++ b/connector_pms_wubook/models/product_pricelist/binding.py @@ -98,16 +98,20 @@ def import_data( ) @api.model - def export_data(self, backend_record=None): + def export_data(self, backend_record=None, record_ids=False): """ Prepare the batch export of Pricelist to Channel """ + domain = [ + ("channel_wubook_bind_ids.backend_id", "in", backend_record.ids), + "|", + ("pms_property_ids", "=", False), + ("pms_property_ids", "in", backend_record.pms_property_id.ids), + ] + if record_ids: + domain.append(("id", "in", record_ids)) + return self.export_batch( backend_record=backend_record, - domain=[ - ("channel_wubook_bind_ids.backend_id", "in", backend_record.ids), - "|", - ("pms_property_ids", "=", False), - ("pms_property_ids", "in", backend_record.pms_property_id.ids), - ], + domain=domain, ) def resync_import(self):