Browse Source

[WIP][MIG]attachment_base_synchronize:Migration to 11.0

pull/1591/merge
kiplangatdan 6 years ago
committed by Holger Brunn
parent
commit
856d3389c8
  1. 13
      attachment_base_synchronize/README.rst
  2. 8
      attachment_base_synchronize/__manifest__.py
  3. 22
      attachment_base_synchronize/data/cron.xml
  4. 7
      attachment_base_synchronize/data/ir_config_parameter.xml
  5. 37
      attachment_base_synchronize/models/attachment.py
  6. 2
      attachment_base_synchronize/security/ir.model.access.csv
  7. 1
      attachment_base_synchronize/tests/__init__.py
  8. 3
      attachment_base_synchronize/tests/test_attachment_base_synchronize.py
  9. 4
      attachment_base_synchronize/views/attachment_view.xml

13
attachment_base_synchronize/README.rst

@ -6,16 +6,14 @@
Attachment Metadata
====================
This module extend ir.attachment model with some new fields for a better control
for import and export of files.
This module extends ir.attachment model with some new fields for a better control for import and export of files.
The main feature is an integrity file check with a hash.
A file hash is short representation (signature) computed from file data.
Hashes computed before send file and after received file can be compared to be
sure of the content integrity.
Hashes computed before send file and after received file can be compared to be sure of the content integrity.
An example of the use of this module, can be found in the external_file_location.
An example of the use of this module, can be found in the module `external_file_location`.
Usage
@ -25,6 +23,10 @@ Go the menu Settings > Technical > Database Structure > Meta Data Attachments
You can create / see standard attachments with additional fields
Configure the batch limit for attachments that can be sync by the cron task at a go:
Settings > Technical > System parameters > attachment_sync_cron_batch_limit
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
@ -61,6 +63,7 @@ Contributors
* Valentin CHEMIERE <valentin.chemiere@akretion.com>
* Florian da Costa <florian.dacosta@akretion.com>
* Angel Moya <http://angelmoya.es>
* Dan Kiplangat <dan@sunflowerweb.nl>
Maintainer
----------

8
attachment_base_synchronize/__manifest__.py

@ -1,11 +1,12 @@
# coding: utf-8
# @ 2015 Florian DA COSTA @ Akretion
# Copyright 2015 Florian DA COSTA @ Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
'name': 'Attachment Base Synchronize',
'version': '10.0.1.0.0',
'version': '11.0.1.0.0',
'author': 'Akretion,Odoo Community Association (OCA)',
'summary': 'This module enhances ir.attachment for better '
'control of import and export of files',
'website': 'https://www.akretion.com',
'license': 'AGPL-3',
'category': 'Generic Modules',
@ -17,6 +18,7 @@
'views/attachment_view.xml',
'security/ir.model.access.csv',
'data/cron.xml',
'data/ir_config_parameter.xml',
],
'demo': [
'demo/attachment_metadata_demo.xml'

22
attachment_base_synchronize/data/cron.xml

@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo noupdate="1">
<record model="ir.cron" id="cronjob_run_attachments_metadata">
<field name='name'>Run Attachments Metadata</field>
<field name='interval_number'>30</field>
<field name='interval_type'>minutes</field>
<field name="numbercall">-1</field>
<field name="active">False</field>
<field name="doall" eval="False" />
<field name="model">ir.attachment.metadata</field>
<field name="function">run_attachment_metadata_scheduler</field>
<field name="args">([])</field>
</record>
<record model="ir.cron" id="cronjob_run_attachments_metadata">
<field name='name'>Run Attachments Metadata</field>
<field name='interval_number'>30</field>
<field name='interval_type'>minutes</field>
<field name="numbercall">-1</field>
<field name="active">False</field>
<field name="doall" eval="False" />
<field name="model_id" ref="model_ir_attachment_metadata"/>
<field name="state">code</field>
<field name="code">model.run_attachment_metadata_scheduler()</field>
</record>
</odoo>

7
attachment_base_synchronize/data/ir_config_parameter.xml

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo noupdate="1">
<record id="attachment_sync_cron_batch_limit" model="ir.config_parameter">
<field name="key">attachment_sync_cron_batch_limit</field>
<field name="value">200</field>
</record>
</odoo>

37
attachment_base_synchronize/models/attachment.py

@ -1,13 +1,10 @@
# coding: utf-8
# @ 2015 Florian DA COSTA @ Akretion
# Copyright 2015 Florian DA COSTA @ Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from base64 import b64decode
import hashlib
import logging
import odoo
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo import _, api, exceptions, fields, models, registry
_logger = logging.getLogger(__name__)
@ -31,7 +28,6 @@ class IrAttachmentMetadata(models.Model):
help="Link to ir.attachment model ")
file_type = fields.Selection(
selection=[],
string="File type",
help="The file type determines an import method to be used "
"to parse and transform data before their import in ERP or an export")
sync_date = fields.Datetime()
@ -50,7 +46,7 @@ class IrAttachmentMetadata(models.Model):
b64decode(attachment.datas)).hexdigest()
if attachment.external_hash and\
attachment.internal_hash != attachment.external_hash:
raise UserError(
raise exceptions.UserError(
_("File corrupted: Something was wrong with "
"the retrieved file, please relaunch the task."))
@ -58,7 +54,14 @@ class IrAttachmentMetadata(models.Model):
def run_attachment_metadata_scheduler(self, domain=None):
if domain is None:
domain = [('state', '=', 'pending')]
attachments = self.search(domain)
batch_limit = self.env.ref(
'attachment_base_synchronize.attachment_sync_cron_batch_limit') \
.value
if batch_limit and batch_limit.isdigit():
limit = int(batch_limit)
else:
limit = 200
attachments = self.search(domain, limit=limit)
if attachments:
return attachments.run()
return True
@ -70,20 +73,20 @@ class IrAttachmentMetadata(models.Model):
"""
for attachment in self:
with api.Environment.manage():
with odoo.registry(self.env.cr.dbname).cursor() as new_cr:
with registry(self.env.cr.dbname).cursor() as new_cr:
new_env = api.Environment(
new_cr, self.env.uid, self.env.context)
attach = attachment.with_env(new_env)
try:
attach._run()
except Exception, e:
# pylint: disable=broad-except
except Exception as e:
attach.env.cr.rollback()
_logger.exception(e)
attach.write(
{
'state': 'failed',
'state_message': e,
})
_logger.exception(str(e))
attach.write({
'state': 'failed',
'state_message': str(e),
})
attach.env.cr.commit()
else:
vals = {
@ -97,7 +100,7 @@ class IrAttachmentMetadata(models.Model):
@api.multi
def _run(self):
self.ensure_one()
_logger.info('Start to process attachment metadata id %s' % self.id)
_logger.info('Start to process attachment metadata id %d', self.id)
@api.multi
def set_done(self):

2
attachment_base_synchronize/security/ir.model.access.csv

@ -1,3 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_attachment_metadata_user,ir.attachment.metadata.user,model_ir_attachment_metadata,,1,0,0,0
access_attachment_metadata_user,ir.attachment.metadata.user,model_ir_attachment_metadata,base.group_no_one,1,1,1,1
access_attachment_metadata_manager,ir.attachment.metadata.manager,model_ir_attachment_metadata,base.group_no_one,1,1,1,1

1
attachment_base_synchronize/tests/__init__.py

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Angel Moya (http://angelmoya.es)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

3
attachment_base_synchronize/tests/test_attachment_base_synchronize.py

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Angel Moya (http://angelmoya.es)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
@ -30,7 +29,7 @@ class TestAttachmentBaseSynchronize(TransactionCase):
'pending'
)
self.ir_attachment_metadata.run_attachment_metadata_scheduler()
self.env.invalidate_all()
self.env.cache.invalidate()
with odoo.registry(self.env.cr.dbname).cursor() as new_cr:
new_env = api.Environment(
new_cr, self.env.uid, self.env.context)

4
attachment_base_synchronize/views/attachment_view.xml

@ -28,10 +28,10 @@
</field>
</record>
<record id="view_external_attachment_tree" model="ir.ui.view">
<record id="view_external_attachment_tree" model="ir.ui.view">
<field name="model">ir.attachment.metadata</field>
<field name="arch" type="xml">
<tree string="Attachments" default_order='create_date desc'>
<tree default_order='create_date desc'>
<field name="name"/>
<field name="datas_fname"/>
<field name="file_type"/>

Loading…
Cancel
Save