summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/ir_attachment.py
blob: 1acd08488aee3285f7947475a9b7cd5cd6bc3c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from odoo import api, fields, models, _
import logging

_logger = logging.getLogger(__name__)


class Attachment(models.Model):
    _inherit = 'ir.attachment'

    @api.autovacuum
    def _gc_file_store(self):
        _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):
        if not id:
            return None
        base_url = self.env['ir.config_parameter'].get_param('web.base.url').rstrip('/')
        is_found = self.is_found(model, field, id)
        return f"{base_url}/api/image/{model}/{field}/{id}" if is_found else None
    
    def api_image_local(self, model, field, id):
        base_url = self.env['ir.config_parameter'].get_param('web.base.local_url')
        is_found = self.is_found(model, field, id)
        return base_url + 'api/image/' + model + '/' + field + '/' + str(id) if is_found else ''