diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-07-17 11:47:33 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-07-17 11:47:33 +0700 |
| commit | c142e8391a7f8385ead74e355ceda862b3f7b64b (patch) | |
| tree | 3d5bd877ba2ac01d6688d8f7be02db2cce8540e1 | |
| parent | 8c4bf3e27b2491955a3f94fa3f3478da6d23fea2 (diff) | |
<iman> update add module
12 files changed, 251 insertions, 56 deletions
diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index 4a37414f..724796c0 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -58,6 +58,7 @@ 'views/dunning_run.xml', 'views/website_brand_homepage.xml', 'views/website_categories_homepage.xml', + 'views/website_categories_management.xml', 'views/sales_target.xml', 'views/purchase_outstanding.xml', 'views/sales_outstanding.xml', 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}") diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index d33ec5e3..597bb762 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -21,6 +21,7 @@ access_website_user_cart,access.website.user.cart,model_website_user_cart,,1,1,1 access_website_user_wishlist,access.website.user.wishlist,model_website_user_wishlist,,1,1,1,1 access_website_brand_homepage,access.website.brand.homepage,model_website_brand_homepage,,1,1,1,1 access_website_categories_homepage,access.website.categories.homepage,model_website_categories_homepage,,1,1,1,1 +access_website_categories_management,access.website.categories.management,model_website_categories_management,,1,1,1,1 access_sales_target,access.sales.target,model_sales_target,,1,1,1,1 access_purchase_outstanding,access.purchase.outstanding,model_purchase_outstanding,,1,1,1,1 access_sales_outstanding,access.sales.outstanding,model_sales_outstanding,,1,1,1,1 diff --git a/indoteknik_custom/views/website_categories_homepage.xml b/indoteknik_custom/views/website_categories_homepage.xml index f9c896bf..aa54ca7a 100644 --- a/indoteknik_custom/views/website_categories_homepage.xml +++ b/indoteknik_custom/views/website_categories_homepage.xml @@ -16,6 +16,7 @@ <field name="category_id"/> <field name="url"/> <field name="status"/> + <field name="last_update_solr"/> </tree> </field> </record> @@ -30,18 +31,17 @@ <group> <field name="sequence"/> <field name="category_id"/> - <field name="category_id2" widget="many2many_tags"/> - <field name="image" widget="image" /> + <field name="image" widget="image"/> <field name="url"/> <field name="status"/> + <field name="last_update_solr" readonly="1" /> </group> </group> <notebook> - <page string="Detail category"> - <field name="category_id2"> - <tree editable="bottom"> - <field name="name"/> - <field name="child_frontend_id2" widget="many2many_tags"/> + <page string="Products" name="product_ids"> + <field name="product_ids" domain="[('sale_ok', '=', True)]"> + <tree> + <field name="display_name" /> </tree> </field> </page> @@ -60,11 +60,11 @@ </record> <menuitem - id="website_categories_homepage" - name="Website Categories Homepage" - parent="website_sale.menu_orders" - sequence="1" - action="website_categories_homepage_action" + id="website_categories_homepage" + name="Website Categories Homepage" + parent="website_sale.menu_orders" + sequence="1" + action="website_categories_homepage_action" /> </data> -</odoo> +</odoo>
\ No newline at end of file diff --git a/indoteknik_custom/views/website_categories_management.xml b/indoteknik_custom/views/website_categories_management.xml new file mode 100644 index 00000000..2a9f4d99 --- /dev/null +++ b/indoteknik_custom/views/website_categories_management.xml @@ -0,0 +1,67 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<odoo> + <data> + <record id="website_categories_management_action" model="ir.actions.act_window"> + <field name="name">Website Categories Management</field> + <field name="res_model">website.categories.management</field> + <field name="view_mode">tree,form</field> + </record> + + <record id="website_categories_management_tree" model="ir.ui.view"> + <field name="name">Website Categories Management</field> + <field name="model">website.categories.management</field> + <field name="arch" type="xml"> + <tree default_order="sequence"> + <field name="sequence"/> + <field name="category_id"/> + <field name="status"/> + </tree> + </field> + </record> + + <record id="website_categories_management_form" model="ir.ui.view"> + <field name="name">Website Categories Management</field> + <field name="model">website.categories.management</field> + <field name="arch" type="xml"> + <form> + <sheet> + <group> + <group> + <field name="sequence"/> + <field name="category_id"/> + <field name="category_id2" widget="many2many_tags"/> + <field name="status"/> + </group> + </group> + <notebook> + <page string="Detail category"> + <field name="category_id2"> + <tree editable="bottom"> + <field name="name"/> + <field name="child_frontend_id2" widget="many2many_tags"/> + </tree> + </field> + </page> + </notebook> + </sheet> + </form> + </field> + </record> + + <record id="ir_actions_server_website_categories_management_sync_to_solr" model="ir.actions.server"> + <field name="name">Sync to solr</field> + <field name="model_id" ref="indoteknik_custom.model_website_categories_management"/> + <field name="binding_model_id" ref="indoteknik_custom.model_website_categories_management"/> + <field name="state">code</field> + <field name="code">model.action_sync_to_solr()</field> + </record> + + <menuitem + id="website_categories_management" + name="Website Categories Management" + parent="website_sale.menu_orders" + sequence="1" + action="website_categories_management_action" + /> + </data> +</odoo> |
