summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models
diff options
context:
space:
mode:
authorit-fixcomart <it@fixcomart.co.id>2024-07-17 11:47:33 +0700
committerit-fixcomart <it@fixcomart.co.id>2024-07-17 11:47:33 +0700
commitc142e8391a7f8385ead74e355ceda862b3f7b64b (patch)
tree3d5bd877ba2ac01d6688d8f7be02db2cce8540e1 /indoteknik_custom/models
parent8c4bf3e27b2491955a3f94fa3f3478da6d23fea2 (diff)
<iman> update add module
Diffstat (limited to 'indoteknik_custom/models')
-rwxr-xr-xindoteknik_custom/models/__init__.py1
-rwxr-xr-xindoteknik_custom/models/product_public_category.py16
-rw-r--r--indoteknik_custom/models/solr/__init__.py1
-rw-r--r--indoteknik_custom/models/solr/product_public_category.py1
-rw-r--r--indoteknik_custom/models/solr/website_categories_homepage.py26
-rw-r--r--indoteknik_custom/models/solr/website_categories_management.py93
-rw-r--r--indoteknik_custom/models/website_categories_homepage.py26
-rw-r--r--indoteknik_custom/models/website_categories_management.py48
8 files changed, 169 insertions, 43 deletions
diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py
index 10f4acee..a6bab518 100755
--- a/indoteknik_custom/models/__init__.py
+++ b/indoteknik_custom/models/__init__.py
@@ -37,6 +37,7 @@ from . import user_company_request
from . import users
from . import website_brand_homepage
from . import website_categories_homepage
+from . import website_categories_management
from . import website_content
from . import website_page_content
from . import website_user_cart
diff --git a/indoteknik_custom/models/product_public_category.py b/indoteknik_custom/models/product_public_category.py
index a6390f56..1039ec52 100755
--- a/indoteknik_custom/models/product_public_category.py
+++ b/indoteknik_custom/models/product_public_category.py
@@ -13,6 +13,20 @@ class ProductPublicCategory(models.Model):
image = fields.Binary(string='Image')
parent_frontend_id = fields.Many2one('product.public.category', string='Parent Frontend Category', index=True)
child_frontend_id = fields.One2many('product.public.category', 'parent_frontend_id', string='Children Frontend Categories')
- child_frontend_id2 = fields.Many2many('product.public.category', relation='website_categories_child_frontend_rel', string='Category Level 3', help="Cari manual Category level 3")
+ child_frontend_id2 = fields.Many2many('product.public.category', relation='website_categories_child_frontend_rel', column1='website_categories_homepage_id', column2='product_public_category_id', string='Category Level 3')
sequence_frontend = fields.Integer(help="Gives the sequence order when displaying a list of product categories.", index=True)
+ # @api.model
+ # def _onchange_child_frontend_id2(self, parent):
+ # domain = {}
+ # if parent:
+ # parent_records = self.browse(parent)
+ # parent_names = parent_records.mapped('name')
+ # domain['child_frontend_id2'] = [
+ # ('parent_frontend_id', 'in', parent),
+ # ('parent_frontend_id.name', 'ilike', parent_names)
+ # ]
+ # else:
+ # domain['child_frontend_id2'] = []
+ # return {'domain': domain}
+
diff --git a/indoteknik_custom/models/solr/__init__.py b/indoteknik_custom/models/solr/__init__.py
index 606c0035..925a8c14 100644
--- a/indoteknik_custom/models/solr/__init__.py
+++ b/indoteknik_custom/models/solr/__init__.py
@@ -5,6 +5,7 @@ from . import product_pricelist_item
from . import product_product
from . import product_template
from . import website_categories_homepage
+from . import website_categories_management
from . import x_manufactures
from . import x_banner_banner
from . import product_public_category
diff --git a/indoteknik_custom/models/solr/product_public_category.py b/indoteknik_custom/models/solr/product_public_category.py
index 2e2c09f7..0bcdc915 100644
--- a/indoteknik_custom/models/solr/product_public_category.py
+++ b/indoteknik_custom/models/solr/product_public_category.py
@@ -53,6 +53,7 @@ class ProductPublicCategory(models.Model):
'website_meta_desc_t': category.website_meta_description or '',
'tampil_di_index': category.x_studio_field_4qhoN if category.x_studio_field_4qhoN is not None else False,
'sequence_frontend_i': category.sequence_frontend or 0,
+ 'image_s': category.image or '',
'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],
diff --git a/indoteknik_custom/models/solr/website_categories_homepage.py b/indoteknik_custom/models/solr/website_categories_homepage.py
index 70c7860d..c930882a 100644
--- a/indoteknik_custom/models/solr/website_categories_homepage.py
+++ b/indoteknik_custom/models/solr/website_categories_homepage.py
@@ -2,10 +2,6 @@ from odoo import models, fields, api
from datetime import datetime
import json
-import logging
-
-_logger = logging.getLogger(__name__)
-
class WebsiteCategoriesHomepage(models.Model):
_inherit = 'website.categories.homepage'
@@ -46,7 +42,7 @@ class WebsiteCategoriesHomepage(models.Model):
self.solr().optimize()
self.solr().commit()
return res
-
+
def _sync_status_category_homepage_solr(self):
for rec in self:
if rec.status == 'tayang':
@@ -61,15 +57,6 @@ class WebsiteCategoriesHomepage(models.Model):
if category.status == 'tidak_tayang':
continue
- category_id2_data = [
- {
- x.id: {
- 'child_frontend_id_i': [child.id for child in x.child_frontend_id]
- }
- }
- for x in category.category_id2
- ]
-
document = solr_model.get_doc('product_category_homepage', category.id)
document.update({
'id': category.id,
@@ -78,13 +65,12 @@ class WebsiteCategoriesHomepage(models.Model):
'image_s': self.env['ir.attachment'].api_image('website.categories.homepage', 'image', category.id),
'sequence_i': category.sequence or '',
'url_s': category.url or '',
- 'category_id2': category_id2_data,
+ 'product_ids': [x.id for x in category.product_ids]
})
- _logger.info('Category %s synchronized to Solr with document: %s', category.id, json.dumps(document))
- # self.solr().add([document])
- # category.update_last_update_solr()
-
- # self.solr().commit()
+ self.solr().add([document])
+ category.update_last_update_solr()
+
+ self.solr().commit()
# def _sync_delete_solr(self):
# for rec in self:
diff --git a/indoteknik_custom/models/solr/website_categories_management.py b/indoteknik_custom/models/solr/website_categories_management.py
new file mode 100644
index 00000000..fca01673
--- /dev/null
+++ b/indoteknik_custom/models/solr/website_categories_management.py
@@ -0,0 +1,93 @@
+from odoo import models, fields, api
+from datetime import datetime
+import json
+
+import logging
+
+_logger = logging.getLogger(__name__)
+
+
+class WebsiteCategoriesManagement(models.Model):
+ _inherit = 'website.categories.management'
+
+ last_update_solr = fields.Datetime('Last Update Solr')
+
+ def solr(self):
+ return self.env['apache.solr'].connect('product_category_management')
+
+ def update_last_update_solr(self):
+ self.last_update_solr = datetime.utcnow()
+
+ 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('status')
+ def _create_solr_queue_sync_status(self):
+ self._create_solr_queue('_sync_status_category_management_solr')
+
+ @api.constrains('category_id', 'image', 'url', 'sequence',)
+ def _create_solr_queue_sync_category_homepage(self):
+ self._create_solr_queue('_sync_category_management_to_solr')
+
+ def action_sync_to_solr(self):
+ category_ids = self.env.context.get('active_ids', [])
+ categories = self.search([('id', 'in', category_ids)])
+ categories._create_solr_queue('_sync_category_management_to_solr')
+
+ def unlink(self):
+ res = super(WebsiteCategoriesManagement, self).unlink()
+ for rec in self:
+ self.solr().delete(rec.id)
+ self.solr().optimize()
+ self.solr().commit()
+ return res
+
+ def _sync_status_category_management_solr(self):
+ for rec in self:
+ if rec.status == 'tayang':
+ rec._sync_category_management_to_solr()
+ else:
+ rec.unlink()
+
+ def _sync_category_management_to_solr(self):
+ solr_model = self.env['apache.solr']
+
+ for category in self:
+ if category.status == 'tidak_tayang':
+ continue
+
+ category_id2_data = [
+ {
+ x.id: {
+ 'child_frontend_id_i': [child.id for child in x.child_frontend_id2]
+ }
+ }
+ for x in category.category_id2
+ ]
+
+ document = solr_model.get_doc('product_category_management', category.id)
+ document.update({
+ 'id': category.id,
+ 'category_id_i': category.category_id.id,
+ 'name_s': category.category_id.name,
+ 'image_s': self.env['ir.attachment'].api_image('website.categories.management', 'image', category.id),
+ 'sequence_i': category.sequence or '',
+ 'url_s': category.url or '',
+ 'category_id2': category_id2_data,
+ })
+ _logger.info('Category %s synchronized to Solr with document: %s', category.id, json.dumps(document))
+ self.solr().add([document])
+ category.update_last_update_solr()
+
+ self.solr().commit()
+
+ # def _sync_delete_solr(self):
+ # for rec in self:
+ # self.solr().delete(rec.id)
+ # self.solr().optimize()
+ # self.solr().commit() \ No newline at end of file
diff --git a/indoteknik_custom/models/website_categories_homepage.py b/indoteknik_custom/models/website_categories_homepage.py
index b7e18694..412dec22 100644
--- a/indoteknik_custom/models/website_categories_homepage.py
+++ b/indoteknik_custom/models/website_categories_homepage.py
@@ -1,11 +1,11 @@
-from odoo import fields, models, api
+from odoo import fields, models
+
class WebsiteCategoriesHomepage(models.Model):
_name = 'website.categories.homepage'
_rec_name = 'category_id'
- category_id = fields.Many2one('product.public.category', string='Category Level 1', help='table ecommerce category',domain=lambda self: self._get_default_category_domain())
- category_id2 = fields.Many2many(comodel_name='product.public.category', relation='website_categories_category_id2_rel', string='Category Level 2', copy=False)
+ category_id = fields.Many2one('product.public.category', string='Category', help='table ecommerce category')
image = fields.Binary(string='Image')
url = fields.Char(string='URL')
sequence = fields.Integer(string='Sequence')
@@ -13,22 +13,4 @@ class WebsiteCategoriesHomepage(models.Model):
('tayang', 'Tayang'),
('tidak_tayang', 'Tidak Tayang')
], string='Status')
-
- @api.onchange('category_id')
- def _onchange_category_id(self):
- domain = {}
- self.category_id2 = [(5, 0, 0)]
- if self.category_id:
- domain['category_id2'] = [('parent_frontend_id', '=', self.category_id.id)]
- else:
- domain['category_id2'] = []
-
- return {'domain': domain}
-
-
- @api.model
- def _get_default_category_domain(self):
- return [('parent_id', '=', False)]
-
-
-
+ product_ids = fields.Many2many('product.template', string='Product Template') \ No newline at end of file
diff --git a/indoteknik_custom/models/website_categories_management.py b/indoteknik_custom/models/website_categories_management.py
new file mode 100644
index 00000000..7d3d1dee
--- /dev/null
+++ b/indoteknik_custom/models/website_categories_management.py
@@ -0,0 +1,48 @@
+from odoo import fields, models, api
+from odoo.exceptions import ValidationError
+
+class WebsiteCategoriesManagement(models.Model):
+ _name = 'website.categories.management'
+ _rec_name = 'category_id'
+
+ category_id = fields.Many2one('product.public.category', string='Category Level 1', help='table ecommerce category', domain=lambda self: self._get_default_category_domain())
+ category_id2 = fields.Many2many(comodel_name='product.public.category', relation='website_categories_category_id2_rel',column1='website_categories_homepage_id', column2='product_public_category_id', string='Category Level 2', copy=False)
+ sequence = fields.Integer(string='Sequence')
+ status = fields.Selection([
+ ('tayang', 'Tayang'),
+ ('tidak_tayang', 'Tidak Tayang')
+ ], string='Status')
+
+ @api.onchange('category_id')
+ def _onchange_category_id(self):
+ domain = {}
+ self.category_id2 = [(5, 0, 0)]
+ if self.category_id:
+ domain['category_id2'] = [('parent_frontend_id', '=', self.category_id.id)]
+ else:
+ domain['category_id2'] = []
+
+ return {'domain': domain}
+
+ @api.model
+ def _get_default_category_domain(self):
+ return [('parent_id', '=', False)]
+
+ def write(self, vals):
+ res = super(WebsiteCategoriesManagement, self).write(vals)
+ self._check_category_consistency()
+ return res
+
+ @api.model
+ def create(self, vals):
+ record = super(WebsiteCategoriesManagement, self).create(vals)
+ record._check_category_consistency()
+ return record
+
+ def _check_category_consistency(self):
+ for record in self:
+ category_ids = record.category_id2.ids
+ for category in record.category_id2:
+ for child_category in category.child_frontend_id2:
+ if child_category.parent_frontend_id.id not in category_ids:
+ raise ValidationError(f"Category Level 3 {child_category.name} bukan bagian dari category Level 2 {category.name}")