summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-01-23 14:44:12 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-01-23 14:44:12 +0700
commit8af5ee591aabc2d5d946c0eece93a5caab989975 (patch)
treea81740fff5f248bd4dd6b190f8650cdfa8849b47
parent4bc68435bc20d7da0f38bd2370057481cb995584 (diff)
Fix get api image logic
-rw-r--r--indoteknik_api/models/product_product.py8
-rw-r--r--indoteknik_api/models/product_template.py13
-rw-r--r--indoteknik_api/models/x_manufactures.py6
-rw-r--r--indoteknik_custom/models/ir_attachment.py15
4 files changed, 26 insertions, 16 deletions
diff --git a/indoteknik_api/models/product_product.py b/indoteknik_api/models/product_product.py
index 6b02d91e..2e84b9f4 100644
--- a/indoteknik_api/models/product_product.py
+++ b/indoteknik_api/models/product_product.py
@@ -5,7 +5,6 @@ class ProductProduct(models.Model):
_inherit = 'product.product'
def api_single_response(self, product_product):
- base_url = self.env['ir.config_parameter'].get_param('web.base.url')
product_pricelist_default_discount_id = self.env['ir.config_parameter'].get_param('product.pricelist.default_discount_id')
product_pricelist_default_discount_id = int(product_pricelist_default_discount_id)
product_template = product_product.product_tmpl_id
@@ -14,7 +13,7 @@ class ProductProduct(models.Model):
'parent': {
'id': product_template.id,
'name': product_template.name,
- 'image': base_url + 'api/image/product.template/image_256/' + str(product_template.id) if product_template.image_256 else '',
+ 'image': self.env['ir.attachment'].api_image('product.template', 'image_256', product_template.id),
},
'code': product_product.default_code or '',
'name': product_product.display_name,
@@ -27,13 +26,12 @@ class ProductProduct(models.Model):
return data
def api_manufacture(self, product_template):
- base_url = self.env['ir.config_parameter'].get_param('web.base.url')
if product_template.x_manufacture:
manufacture = product_template.x_manufacture
return {
'id': manufacture.id,
'name': manufacture.x_name,
- 'image_promotion_1': base_url + 'api/image/x_manufactures/image_promotion_1/' + str(manufacture.id) if manufacture.image_promotion_1 else '',
- 'image_promotion_2': base_url + 'api/image/x_manufactures/image_promotion_2/' + str(manufacture.id) if manufacture.image_promotion_2 else '',
+ 'image_promotion_1': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', manufacture.id),
+ 'image_promotion_2': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', manufacture.id),
}
return {} \ No newline at end of file
diff --git a/indoteknik_api/models/product_template.py b/indoteknik_api/models/product_template.py
index 9e8d04bc..72dda17f 100644
--- a/indoteknik_api/models/product_template.py
+++ b/indoteknik_api/models/product_template.py
@@ -5,12 +5,11 @@ class ProductTemplate(models.Model):
_inherit = 'product.template'
def api_single_response(self, product_template, with_detail=''):
- base_url = self.env['ir.config_parameter'].get_param('web.base.url')
product_pricelist_default_discount_id = self.env['ir.config_parameter'].get_param('product.pricelist.default_discount_id')
product_pricelist_default_discount_id = int(product_pricelist_default_discount_id)
data = {
'id': product_template.id,
- 'image': base_url + 'api/image/product.template/image_128/' + str(product_template.id) if product_template.image_128 else '',
+ 'image': self.env['ir.attachment'].api_image('product.template', 'image_128', product_template.id),
'code': product_template.default_code or '',
'name': product_template.name,
'lowest_price': self.env['product.pricelist'].get_lowest_product_variant_price(product_template, product_pricelist_default_discount_id),
@@ -23,7 +22,7 @@ class ProductTemplate(models.Model):
if with_detail != '':
data_with_detail = {
- 'image': base_url + 'api/image/product.template/image_512/' + str(product_template.id) if product_template.image_512 else '',
+ 'image': self.env['ir.attachment'].api_image('product.template', 'image_512', product_template.id),
'display_name': product_template.display_name,
'variants': [self.env['product.product'].api_single_response(variant) for variant in product_template.product_variant_ids],
'description': product_template.website_description or '',
@@ -31,12 +30,13 @@ class ProductTemplate(models.Model):
data.update(data_with_detail)
if with_detail == 'SOLR':
+ is_image_found = self.env['ir.attachment'].is_found('product.template', 'image_128', product_template.id)
rate = 0
if data['lowest_price']['price'] > 0:
rate += 1
if product_template.have_promotion_program:
rate += 1
- if product_template.image_128:
+ if is_image_found:
rate += 1
if product_template.website_description:
rate += 1
@@ -52,14 +52,13 @@ class ProductTemplate(models.Model):
return data
def api_manufacture(self, product_template):
- base_url = self.env['ir.config_parameter'].get_param('web.base.url')
if product_template.x_manufacture:
manufacture = product_template.x_manufacture
return {
'id': manufacture.id,
'name': manufacture.x_name,
- 'image_promotion_1': base_url + 'api/image/x_manufactures/image_promotion_1/' + str(manufacture.id) if manufacture.image_promotion_1 else '',
- 'image_promotion_2': base_url + 'api/image/x_manufactures/image_promotion_2/' + str(manufacture.id) if manufacture.image_promotion_2 else '',
+ 'image_promotion_1': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', manufacture.id),
+ 'image_promotion_2': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', manufacture.id),
}
return {}
diff --git a/indoteknik_api/models/x_manufactures.py b/indoteknik_api/models/x_manufactures.py
index 19fdcb9c..988d7a45 100644
--- a/indoteknik_api/models/x_manufactures.py
+++ b/indoteknik_api/models/x_manufactures.py
@@ -8,16 +8,16 @@ class Manufactures(models.Model):
base_url = self.env['ir.config_parameter'].get_param('web.base.url')
data = {
'id': manufacture.id,
- 'logo': base_url + 'api/image/x_manufactures/x_logo_manufacture/' + str(manufacture.id) if manufacture.x_logo_manufacture else '',
+ 'logo': self.env['ir.attachment'].api_image('x_manufactures', 'x_logo_manufacture', manufacture.id),
'name': manufacture.x_name
}
if with_detail:
data_with_detail = {
'description': manufacture.x_short_desc,
'banners': [
- base_url + 'api/image/x_banner.banner/x_banner_image/' + str(x.id)
+ self.env['ir.attachment'].api_image('x_banner.banner', 'x_banner_image', x.id)
for x in manufacture.x_manufactures_banners
- if x.x_status_banner == 'tayang' and x.x_banner_image
+ if x.x_status_banner == 'tayang'
]
}
data.update(data_with_detail)
diff --git a/indoteknik_custom/models/ir_attachment.py b/indoteknik_custom/models/ir_attachment.py
index 278eb938..fd86ab1b 100644
--- a/indoteknik_custom/models/ir_attachment.py
+++ b/indoteknik_custom/models/ir_attachment.py
@@ -9,4 +9,17 @@ class Attachment(models.Model):
@api.autovacuum
def _gc_file_store(self):
- _logger.info("filestore gc checked, removed - override") \ No newline at end of file
+ _logger.info("filestore gc checked, removed - override")
+
+ def is_found(self, model, field, id):
+ attachment = self.search([
+ ('res_model', '=', model),
+ ('res_field', '=', field),
+ ('res_id', '=', int(id))
+ ])
+ return True if attachment else False
+
+ def api_image(self, model, field, id):
+ base_url = self.env['ir.config_parameter'].get_param('web.base.url')
+ is_found = self.is_found(model, field, id)
+ return base_url + 'api/image/' + model + '/' + field + '/' + str(id) if is_found else '' \ No newline at end of file