Skip to content
Permalink
Browse files

🚑 unlink resized attachments when parent unlink

  • Loading branch information...
RafiZz committed Feb 15, 2019
1 parent 856caf4 commit d49dce19b0dcb6a702df479f874b5f2f941de662
Showing with 36 additions and 3 deletions.
  1. +12 −0 ir_attachment_s3/models/ir_attachment.py
  2. +24 −3 ir_attachment_s3/tests/test_resized_attachments.py
@@ -27,12 +27,24 @@ class IrAttachmentResized(models.Model):
height = fields.Integer()
resized_attachment_id = fields.Many2one('ir.attachment', ondelete='cascade')

@api.multi
def unlink(self):
resized_att_id = self.resized_attachment_id
super(IrAttachmentResized, self).unlink()
return resized_att_id.unlink()


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

resized_ids = fields.One2many('ir.attachment.resized', 'attachment_id')

@api.multi
def unlink(self):
resized_ids = self.resized_ids
super(IrAttachment, self).unlink()
return resized_ids.unlink() if resized_ids else True

def _get_s3_settings(self, param_name, os_var_name):
config_obj = self.env['ir.config_parameter']
res = config_obj.sudo().get_param(param_name)
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import logging

from odoo import api
from odoo import api, exceptions
from odoo.tests.common import HttpCase

_logger = logging.getLogger(__name__)
@@ -70,6 +70,27 @@ def test_getting_cached_images_url_instead_computing(self):
urls = [self.original_image_url, redirected_image_url, redirected_image_medium_url, redirected_image_small_url]
self.assertEqual(len(urls), len(set(urls)), 'Duplicates in URLs: %s' % urls)

# TODO
def test_unlink_resized_attachments_when_parent_unlink(self):
return
env = api.Environment(self.registry.test_cr, self.uid, {})

ir_att_model = env['ir.attachment']
ir_att_resized_model = env['ir.attachment.resized']

original_att = ir_att_model.create({'name': 'test att'})
resized_att = ir_att_model.create({'name': 'resized test att'})

ir_att_resized = ir_att_resized_model.create({
'attachment_id': original_att.id,
'resized_attachment_id': resized_att.id,
})

self.assertTrue(original_att.unlink())

with self.assertRaises(exceptions.MissingError):
original_att.write({'name': 'foo'})

with self.assertRaises(exceptions.MissingError):
ir_att_resized.write({'width': 1})

with self.assertRaises(exceptions.MissingError):
resized_att.write({'name': 'bar'})

0 comments on commit d49dce1

Please sign in to comment.
You can’t perform that action at this time.