From 02c2304b242245250177fec6ab3c911d9acba781 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Mon, 11 Sep 2023 11:55:47 +0700 Subject: auto sync banner to solr, auto sync ecommerce categories to solr, add field carrier to stock_picking --- indoteknik_custom/models/solr/__init__.py | 4 +- .../models/solr/product_public_category.py | 72 ++++++++++++++++++++++ .../models/solr/website_categories_homepage.py | 2 +- indoteknik_custom/models/solr/x_banner_banner.py | 64 +++++++++++++++++++ indoteknik_custom/models/solr/x_manufactures.py | 6 +- indoteknik_custom/models/stock_picking.py | 2 + .../views/product_public_category.xml | 9 +++ indoteknik_custom/views/stock_picking.xml | 1 + indoteknik_custom/views/x_banner_banner.xml | 9 +++ 9 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 indoteknik_custom/models/solr/product_public_category.py create mode 100644 indoteknik_custom/models/solr/x_banner_banner.py diff --git a/indoteknik_custom/models/solr/__init__.py b/indoteknik_custom/models/solr/__init__.py index 9a13de77..45013d3d 100644 --- a/indoteknik_custom/models/solr/__init__.py +++ b/indoteknik_custom/models/solr/__init__.py @@ -4,4 +4,6 @@ from . import product_pricelist_item from . import product_product from . import product_template from . import website_categories_homepage -from . import x_manufactures \ No newline at end of file +from . import x_manufactures +from . import x_banner_banner +from . import product_public_category \ No newline at end of file diff --git a/indoteknik_custom/models/solr/product_public_category.py b/indoteknik_custom/models/solr/product_public_category.py new file mode 100644 index 00000000..0bb34674 --- /dev/null +++ b/indoteknik_custom/models/solr/product_public_category.py @@ -0,0 +1,72 @@ +from odoo import models, fields, api +import logging +from datetime import datetime + +_logger = logging.getLogger(__name__) + +class ProductPublicCategory(models.Model): + _inherit = "product.public.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('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_url_banner', 'background_color', 'x_banner_image', 'x_banner_category', 'x_relasi_manufacture', 'x_sequence_banner', 'x_status_banner', 'sequence', 'group_by_week') + def _create_solr_queue_sync_brands(self): + self._create_solr_queue('_sync_categories_to_solr') + + def action_sync_to_solr(self): + banner_ids = self.env.context.get('active_ids', []) + categories = self.search([('id', 'in', banner_ids)]) + categories._create_solr_queue('_sync_categories_to_solr') + + def _sync_categories_to_solr(self): + for category in self: + parent = category.parent_id and category.parent_id[0] + parent_frontend = category.parent_frontend_id and category.parent_frontend_id[0] + + document = { + 'id': category.id, + 'display_name_s': category.display_name, + 'name_s': category.name, + 'english_name_s': category.english_name or '', + 'name_2_s': category.x_studio_field_d1HS4 or '', + 'name_3_s': category.x_studio_field_f54P2 or '', + 'nama_alias_s': category.x_studio_field_BfNp2 or '', + 'parent_id_i': parent.id if parent else 0, + 'parent_name_s': parent.name if parent else '', + 'sequence_i': category.sequence or 0, + 'website_meta_title_s': category.website_meta_title or '', + 'website_meta_keywords_s': category.website_meta_keywords or '', + 'website_meta_desc_t': category.website_meta_description or '', + 'tampil_di_index_b': category.x_studio_field_4qhoN or '', + 'sequence_frontend_i': category.sequence_frontend or 0, + 'parent_frontend_id_i': parent_frontend.id if parent_frontend else 0, + 'parent_frontend_name_s': parent_frontend.name if parent_frontend else '', + 'product_template': [x.id for x in category.product_tmpl_ids], + } + + self.solr().add([document]) + category.update_last_update_solr() + + self.solr().commit() + + def unlink(self): + res = super(ProductPublicCategory, 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/models/solr/website_categories_homepage.py b/indoteknik_custom/models/solr/website_categories_homepage.py index 68a9eb7b..75d84e73 100644 --- a/indoteknik_custom/models/solr/website_categories_homepage.py +++ b/indoteknik_custom/models/solr/website_categories_homepage.py @@ -48,7 +48,7 @@ class WebsiteCategoriesHomepage(models.Model): if rec.status == 'tayang': rec._sync_category_homepage_to_solr() else: - rec._sync_delete_solr() + rec.unlink() def _sync_category_homepage_to_solr(self): solr_model = self.env['apache.solr'] diff --git a/indoteknik_custom/models/solr/x_banner_banner.py b/indoteknik_custom/models/solr/x_banner_banner.py new file mode 100644 index 00000000..5608a3d7 --- /dev/null +++ b/indoteknik_custom/models/solr/x_banner_banner.py @@ -0,0 +1,64 @@ +from odoo import models, fields, api +import logging +from datetime import datetime + +_logger = logging.getLogger(__name__) + +class XBannerBanner(models.Model): + _inherit = "x_banner.banner" + + 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('banners') + + 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_url_banner', 'background_color', 'x_banner_image', 'x_banner_category', 'x_relasi_manufacture', 'x_sequence_banner', 'x_status_banner', 'sequence', 'group_by_week') + def _create_solr_queue_sync_brands(self): + self._create_solr_queue('_sync_banners_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_banners_to_solr') + + def _sync_banners_to_solr(self): + solr_model = self.env['apache.solr'] + + for banners in self: + document = {} + document.update({ + 'id': banners.id, + 'display_name_s': banners.display_name, + 'name_s': banners.x_name, + 'sequence_i': banners.sequence or '', + 'url_s': banners.x_url_banner or '', + '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 '', + 'group_by_week': banners.group_by_week or '', + }) + self.solr().add([document]) + banners.update_last_update_solr() + + self.solr().commit() + + def unlink(self): + res = super(XBannerBanner, 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/models/solr/x_manufactures.py b/indoteknik_custom/models/solr/x_manufactures.py index b2f0cde0..c0c321e4 100644 --- a/indoteknik_custom/models/solr/x_manufactures.py +++ b/indoteknik_custom/models/solr/x_manufactures.py @@ -28,9 +28,9 @@ class XManufactures(models.Model): self._create_solr_queue('_sync_brands_to_solr') def action_sync_to_solr(self): - template_ids = self.env.context.get('active_ids', []) - templates = self.search([('id', 'in', template_ids)]) - templates._create_solr_queue('_sync_brands_to_solr') + manufacture_ids = self.env.context.get('active_ids', []) + manufactures = self.search([('id', 'in', manufacture_ids)]) + manufactures._create_solr_queue('_sync_brands_to_solr') def _sync_brands_to_solr(self): solr_model = self.env['apache.solr'] diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py index 90963e1c..0e6137eb 100644 --- a/indoteknik_custom/models/stock_picking.py +++ b/indoteknik_custom/models/stock_picking.py @@ -73,6 +73,8 @@ class StockPicking(models.Model): waybill_id = fields.One2many(comodel_name='airway.bill', inverse_name='do_id', string='Airway Bill') purchase_representative_id = fields.Many2one('res.users', related='move_lines.purchase_line_id.order_id.user_id', string="Purchase Representative", readonly=True) + + carrier_id = fields.Many2one('delivery.carrier', string='Shipping Method') def action_create_invoice_from_mr(self): """Create the invoice associated to the PO. diff --git a/indoteknik_custom/views/product_public_category.xml b/indoteknik_custom/views/product_public_category.xml index 3a9eb2df..e0161acf 100755 --- a/indoteknik_custom/views/product_public_category.xml +++ b/indoteknik_custom/views/product_public_category.xml @@ -34,4 +34,13 @@ + + + Sync to solr + + + code + model.action_sync_to_solr() + + \ No newline at end of file diff --git a/indoteknik_custom/views/stock_picking.xml b/indoteknik_custom/views/stock_picking.xml index b099b32c..ebc978aa 100644 --- a/indoteknik_custom/views/stock_picking.xml +++ b/indoteknik_custom/views/stock_picking.xml @@ -102,6 +102,7 @@ + diff --git a/indoteknik_custom/views/x_banner_banner.xml b/indoteknik_custom/views/x_banner_banner.xml index 5e6526e0..be9ae951 100755 --- a/indoteknik_custom/views/x_banner_banner.xml +++ b/indoteknik_custom/views/x_banner_banner.xml @@ -31,6 +31,7 @@ + @@ -54,6 +55,14 @@ + + Sync to solr + + + code + model.action_sync_to_solr() + +