Browse Source

publish muk_utils - 12.0

pull/30/head
MuK IT GmbH 5 years ago
parent
commit
14fd4f88a5
  1. 2
      muk_utils/__manifest__.py
  2. 42
      muk_utils/models/ir_attachment.py
  3. 2
      muk_utils/tests/test_attachment_migration.py

2
muk_utils/__manifest__.py

@ -22,7 +22,7 @@
{
"name": "MuK Utils",
"summary": """Utility Features""",
"version": '12.0.2.0.1',
"version": '12.0.2.0.2',
"category": 'Extra Tools',
"license": "LGPL-3",
"author": "MuK IT",

42
muk_utils/models/ir_attachment.py

@ -97,35 +97,27 @@ class IrAttachment(models.Model):
'&', storage_domain[self._storage()],
'|', ('res_field', '=', False), ('res_field', '!=', False)
]
self.search(record_domain).migrate()
self.search(record_domain).migrate(batch_size=100)
return True
@api.multi
def migrate(self):
self.env.cr.commit()
def migrate(self, batch_size=None):
batch_size = batch_size or len(self)
storage_location = self._storage().upper()
batch_size = self.env.context.get('migration_batch_size', 100)
batches_to_migrate = math.ceil(len(self) / batch_size)
for batch_index, sub_ids in enumerate(split_every(batch_size, self.ids)):
with api.Environment.manage():
with registry(self.env.cr.dbname).cursor() as batch_cr:
batch_env = api.Environment(batch_cr, self.env.uid, self.env.context.copy())
attachment_records = batch_env['ir.attachment'].browse(sub_ids)
batch_records_count = len(attachment_records)
try:
for index, attach in enumerate(attachment_records):
_logger.info("Migrate Attachment %s of %s to %s [Batch %s of %s]",
index + 1, batch_records_count, storage_location,
batch_index + 1, batches_to_migrate
)
attach.with_context(migration=True).write({
'datas': attach.datas
})
except:
batch_cr.rollback()
raise
else:
batch_cr.commit()
batches = math.ceil(len(self) / batch_size)
print(batch_size, batches)
for index, attachment in enumerate(self, start=1):
_logger.info("Migrate Attachment %s of %s to %s [Batch %s of %s]",
index % batch_size, math.ceil(index / batch_size),
storage_location, (index / batch_size) + 1, batches
)
attachment.with_context(migration=True).write({
'datas': attachment.datas
})
if not index % batch_size:
self.env.cr.commit()
#----------------------------------------------------------
# Read

2
muk_utils/tests/test_attachment_migration.py

@ -47,5 +47,5 @@ class MigrationTestCase(common.TransactionCase):
super(MigrationTestCase, self).tearDown()
def test_migration(self):
self.model.search([], limit=25).with_context(migration_batch_size=5).action_migrate()
self.model.search([], limit=25).migrate(batch_size=5)
Loading…
Cancel
Save