From c357d6bd7586b0331b27dbe864179b1fad63bf2e Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Thu, 21 Sep 2023 09:28:08 +0700 Subject: Add banner category to solr, auto sync solr banner category, fix button uang muka --- indoteknik_custom/models/solr/__init__.py | 3 +- indoteknik_custom/models/solr/x_banner_banner.py | 4 +- indoteknik_custom/models/solr/x_banner_category.py | 54 ++++++++++++++++++++++ indoteknik_custom/views/x_banner_banner.xml | 2 +- indoteknik_custom/views/x_banner_category.xml | 9 ++++ 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 indoteknik_custom/models/solr/x_banner_category.py diff --git a/indoteknik_custom/models/solr/__init__.py b/indoteknik_custom/models/solr/__init__.py index 16da73af..f2d13116 100644 --- a/indoteknik_custom/models/solr/__init__.py +++ b/indoteknik_custom/models/solr/__init__.py @@ -7,4 +7,5 @@ from . import product_template from . import website_categories_homepage from . import x_manufactures from . import x_banner_banner -from . import product_public_category \ No newline at end of file +from . import product_public_category +from . import x_banner_category \ No newline at end of file diff --git a/indoteknik_custom/models/solr/x_banner_banner.py b/indoteknik_custom/models/solr/x_banner_banner.py index 5608a3d7..67739d47 100644 --- a/indoteknik_custom/models/solr/x_banner_banner.py +++ b/indoteknik_custom/models/solr/x_banner_banner.py @@ -46,8 +46,8 @@ class XBannerBanner(models.Model): 'background_color_s': banners.background_color or '', 'image_s': self.env['ir.attachment'].api_image('x_banner.banner', 'x_banner_image', banners.id), 'status_s': banners.x_status_banner or '', - 'categories': banners.x_banner_category.id or '', - 'manufactures': banners.x_relasi_manufacture.id or '', + 'category_id_i': banners.x_banner_category.id or '', + 'manufacture_id_i': banners.x_relasi_manufacture.id or '', 'group_by_week': banners.group_by_week or '', }) self.solr().add([document]) diff --git a/indoteknik_custom/models/solr/x_banner_category.py b/indoteknik_custom/models/solr/x_banner_category.py new file mode 100644 index 00000000..c73f6722 --- /dev/null +++ b/indoteknik_custom/models/solr/x_banner_category.py @@ -0,0 +1,54 @@ +from odoo import fields, models, api +from datetime import datetime + +class XBannerCategory(models.Model): + _inherit = "x_banner.category" + + last_update_solr = fields.Datetime(string='Last Update Solr') + + def update_last_update_solr(self): + self.last_update_solr = datetime.utcnow() + + def solr(self): + return self.env['apache.solr'].connect('banner_categories') + + def _create_solr_queue(self, function_name): + for rec in self: + self.env['apache.solr.queue'].create_unique({ + 'res_model': self._name, + 'res_id': rec.id, + 'function_name': function_name + }) + + @api.constrains('x_name', 'x_banner_subtitle', 'x_studio_field_KKVl4', 'banner_ids') + def _create_solr_queue_sync_banner_categories(self): + self._create_solr_queue('_sync_banner_categories_to_solr') + + def action_sync_to_solr(self): + banner_ids = self.env.context.get('active_ids', []) + banners = self.search([('id', 'in', banner_ids)]) + banners._create_solr_queue('_sync_banner_categories_to_solr') + + def _sync_banner_categories_to_solr(self): + for banners in self: + document = {} + document.update({ + 'id': banners.id, + 'display_name_s': banners.display_name, + 'name_s': banners.x_name, + 'banner_subtitle_s': banners.x_banner_subtitle or '', + 'x_studio_field_KKVl4_s': banners.x_studio_field_KKVl4 or '', + 'banners': [x.id for x in banners.banner_ids], + }) + self.solr().add([document]) + banners.update_last_update_solr() + + self.solr().commit() + + def unlink(self): + res = super(XBannerCategory, self).unlink() + for rec in self: + self.solr().delete(rec.id) + self.solr().optimize() + self.solr().commit() + return res \ No newline at end of file diff --git a/indoteknik_custom/views/x_banner_banner.xml b/indoteknik_custom/views/x_banner_banner.xml index be9ae951..c90e718b 100755 --- a/indoteknik_custom/views/x_banner_banner.xml +++ b/indoteknik_custom/views/x_banner_banner.xml @@ -31,7 +31,7 @@ - + diff --git a/indoteknik_custom/views/x_banner_category.xml b/indoteknik_custom/views/x_banner_category.xml index 45ad7940..11feb207 100755 --- a/indoteknik_custom/views/x_banner_category.xml +++ b/indoteknik_custom/views/x_banner_category.xml @@ -24,6 +24,7 @@ + @@ -49,6 +50,14 @@ + + Sync to solr + + + code + model.action_sync_to_solr() + +