[IMP] attachment_queue: pre-commit

pull/2560/head
Kevin Khao 2021-03-04 12:12:04 +01:00 committed by Florian da Costa
parent dbcffb06fa
commit 5d5d277e3d
8 changed files with 117 additions and 77 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1"> <odoo noupdate="1">
<record model="ir.cron" id="cronjob_run_attachment_queue"> <record model="ir.cron" id="cronjob_run_attachment_queue">
@ -8,7 +8,7 @@
<field name="numbercall">-1</field> <field name="numbercall">-1</field>
<field name="active">False</field> <field name="active">False</field>
<field name="doall" eval="False" /> <field name="doall" eval="False" />
<field name="model_id" ref="model_attachment_queue"/> <field name="model_id" ref="model_attachment_queue" />
<field name="state">code</field> <field name="state">code</field>
<field name="code">model.run_attachment_queue_scheduler()</field> <field name="code">model.run_attachment_queue_scheduler()</field>
</record> </record>

View File

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

View File

@ -1,12 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1"> <odoo noupdate="1">
<record id="attachment_failure_notification" model="mail.template"> <record id="attachment_failure_notification" model="mail.template">
<field name="email_to">${object.failure_emails}</field> <field name="email_to">${object.failure_emails}</field>
<field name="name">Attachment Failure notification</field> <field name="name">Attachment Failure notification</field>
<field name="subject">The attachment ${object.name} has failed</field> <field name="subject">The attachment ${object.name} has failed</field>
<field name="model_id" ref="attachment_queue.model_attachment_queue"/> <field name="model_id" ref="attachment_queue.model_attachment_queue" />
<field name="body_html"><![CDATA[ <field
name="body_html"
><![CDATA[
<p style="margin:0px 0px 10px 0px;font-size:13px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;">Hello,<br><br></p> <p style="margin:0px 0px 10px 0px;font-size:13px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;">Hello,<br><br></p>
<p style="margin:0px 0px 10px 0px;font-size:13px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;">The attachment ${object.name} has failed with the following error message : <br>${object.state_message}<br></p><p style="margin:0px 0px 10px 0px;font-size:13px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;"></p> <p style="margin:0px 0px 10px 0px;font-size:13px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;">The attachment ${object.name} has failed with the following error message : <br>${object.state_message}<br></p><p style="margin:0px 0px 10px 0px;font-size:13px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;"></p>
<p style="margin:0px 0px 10px 0px;font-size:13px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;">Regards,<br></p> <p style="margin:0px 0px 10px 0px;font-size:13px;font-family:&quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif;">Regards,<br></p>

View File

@ -1,4 +1,4 @@
<?xml version="1.0"?> <?xml version="1.0" ?>
<odoo noupdate="1"> <odoo noupdate="1">
<record id="attachment_queue_demo" model="attachment.queue"> <record id="attachment_queue_demo" model="attachment.queue">

View File

@ -35,7 +35,7 @@ class AttachmentQueue(models.Model):
compute="_compute_failure_emails", compute="_compute_failure_emails",
string="Failure Emails", string="Failure Emails",
help="Comma-separated list of email addresses to be notified in case of" help="Comma-separated list of email addresses to be notified in case of"
"failure", "failure",
) )
def _compute_failure_emails(self): def _compute_failure_emails(self):
@ -67,15 +67,11 @@ class AttachmentQueue(models.Model):
""" """
Run the process for each attachment queue Run the process for each attachment queue
""" """
failure_tmpl = self.env.ref( failure_tmpl = self.env.ref("attachment_queue.attachment_failure_notification")
"attachment_queue.attachment_failure_notification"
)
for attachment in self: for attachment in self:
with api.Environment.manage(): with api.Environment.manage():
with registry(self.env.cr.dbname).cursor() as new_cr: with registry(self.env.cr.dbname).cursor() as new_cr:
new_env = api.Environment( new_env = api.Environment(new_cr, self.env.uid, self.env.context)
new_cr, self.env.uid, self.env.context
)
attach = attachment.with_env(new_env) attach = attachment.with_env(new_env)
try: try:
attach._run() attach._run()
@ -83,9 +79,7 @@ class AttachmentQueue(models.Model):
except Exception as e: except Exception as e:
attach.env.cr.rollback() attach.env.cr.rollback()
_logger.exception(str(e)) _logger.exception(str(e))
attach.write( attach.write({"state": "failed", "state_message": str(e)})
{"state": "failed", "state_message": str(e)}
)
emails = attach.failure_emails emails = attach.failure_emails
if emails: if emails:
failure_tmpl.send_mail(attach.id) failure_tmpl.send_mail(attach.id)

View File

@ -14,4 +14,3 @@ This module can be used in combination with attachment_synchronize to control fi
image:: ../static/description/form.png image:: ../static/description/form.png

View File

@ -12,17 +12,14 @@ class TestAttachmentBaseQueue(TransactionCase):
self.env = api.Environment( self.env = api.Environment(
self.registry.test_cr, self.env.uid, self.env.context self.registry.test_cr, self.env.uid, self.env.context
) )
self.attachment = self.env.ref( self.attachment = self.env.ref("attachment_queue.attachment_queue_demo")
"attachment_queue.attachment_queue_demo"
)
def tearDown(self): def tearDown(self):
self.registry.leave_test_mode() self.registry.leave_test_mode()
super().tearDown() super().tearDown()
def test_attachment_queue(self): def test_attachment_queue(self):
"""Test run_attachment_queue_scheduler to ensure set state to done """Test run_attachment_queue_scheduler to ensure set state to done"""
"""
self.assertEqual(self.attachment.state, "pending") self.assertEqual(self.attachment.state, "pending")
self.env["attachment.queue"].run_attachment_queue_scheduler() self.env["attachment.queue"].run_attachment_queue_scheduler()
self.env.cache.invalidate() self.env.cache.invalidate()
@ -32,8 +29,7 @@ class TestAttachmentBaseQueue(TransactionCase):
self.assertEqual(attach.state, "done") self.assertEqual(attach.state, "done")
def test_set_done(self): def test_set_done(self):
"""Test set_done manually """Test set_done manually"""
"""
self.assertEqual(self.attachment.state, "pending") self.assertEqual(self.attachment.state, "pending")
self.attachment.set_done() self.attachment.set_done()
self.assertEqual(self.attachment.state, "done") self.assertEqual(self.attachment.state, "done")

View File

@ -1,27 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo> <odoo>
<record id="view_attachment_queue_form" model="ir.ui.view"> <record id="view_attachment_queue_form" model="ir.ui.view">
<field name="model">attachment.queue</field> <field name="model">attachment.queue</field>
<field name="inherit_id" ref="base.view_attachment_form"/> <field name="inherit_id" ref="base.view_attachment_form" />
<field name="mode">primary</field> <field name="mode">primary</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<xpath expr="/form/*" position="before"> <xpath expr="/form/*" position="before">
<header> <header>
<button name="run" states="pending,failed" <button
string="Run" type="object" class="oe_highlight"/> name="run"
<button name="set_done" states="pending,failed" states="pending,failed"
string="Set to Done" type="object"/> string="Run"
type="object"
class="oe_highlight"
/>
<button
name="set_done"
states="pending,failed"
string="Set to Done"
type="object"
/>
</header> </header>
</xpath> </xpath>
<field name="url" position="after"> <field name="url" position="after">
<field name="date_done"/> <field name="date_done" />
<field name="state"/> <field name="state" />
<field name="file_type"/> <field name="file_type" />
</field> </field>
<group name="description_group"> <group name="description_group">
<group name="state_message" string="Error" colspan="4"> <group name="state_message" string="Error" colspan="4">
<field name="state_message" nolabel="1"/> <field name="state_message" nolabel="1" />
</group> </group>
</group> </group>
</field> </field>
@ -31,12 +40,12 @@
<field name="model">attachment.queue</field> <field name="model">attachment.queue</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree default_order='create_date desc'> <tree default_order='create_date desc'>
<field name="name"/> <field name="name" />
<field name="datas_fname"/> <field name="datas_fname" />
<field name="file_type"/> <field name="file_type" />
<field name="type"/> <field name="type" />
<field name="create_date"/> <field name="create_date" />
<field name="state"/> <field name="state" />
</tree> </tree>
</field> </field>
</record> </record>
@ -45,31 +54,69 @@
<field name="model">attachment.queue</field> <field name="model">attachment.queue</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<search string="Attachments"> <search string="Attachments">
<field name="name" filter_domain="['|', ('name','ilike',self), ('datas_fname','ilike',self)]" string="Attachment"/> <field
<field name="create_date"/> name="name"
<filter name="url" filter_domain="['|', ('name','ilike',self), ('datas_fname','ilike',self)]"
string="URL" string="Attachment"
domain="[('type','=','url')]"/> />
<filter name="binary" <field name="create_date" />
string="Binary" <filter name="url" string="URL" domain="[('type','=','url')]" />
domain="[('type','=','binary')]"/> <filter name="binary" string="Binary" domain="[('type','=','binary')]" />
<separator/> <separator />
<filter name="my_documents_filter" <filter
string="My Document(s)" name="my_documents_filter"
domain="[('create_uid','=',uid)]" string="My Document(s)"
help="Filter on my documents"/> domain="[('create_uid','=',uid)]"
<field name="create_uid"/> help="Filter on my documents"
<field name="type"/> />
<filter string="Pending" name="pending" domain="[('state', '=', 'pending')]"/> <field name="create_uid" />
<filter string="Failed" name="failed" domain="[('state', '=', 'failed')]"/> <field name="type" />
<filter string="Done" name="done" domain="[('state', '=', 'done')]"/> <filter
string="Pending"
name="pending"
domain="[('state', '=', 'pending')]"
/>
<filter string="Failed" name="failed" domain="[('state', '=', 'failed')]" />
<filter string="Done" name="done" domain="[('state', '=', 'done')]" />
<group expand="0" string="Group By"> <group expand="0" string="Group By">
<filter string="Owner" name="owner" domain="[]" context="{'group_by':'create_uid'}"/> <filter
<filter string="Type" name="type" domain="[]" context="{'group_by':'type'}" groups="base.group_no_one"/> string="Owner"
<filter string="Company" name="company" domain="[]" context="{'group_by':'company_id'}" groups="base.group_multi_company"/> name="owner"
<filter string="Creation Month" name="creation_month" domain="[]" context="{'group_by':'create_date'}"/> domain="[]"
<filter string="State" name="state" domain="[]" context="{'group_by': 'state'}"/> context="{'group_by':'create_uid'}"
<filter string="File type" name="file_type" domain="[]" context="{'group_by': 'file_type'}"/> />
<filter
string="Type"
name="type"
domain="[]"
context="{'group_by':'type'}"
groups="base.group_no_one"
/>
<filter
string="Company"
name="company"
domain="[]"
context="{'group_by':'company_id'}"
groups="base.group_multi_company"
/>
<filter
string="Creation Month"
name="creation_month"
domain="[]"
context="{'group_by':'create_date'}"
/>
<filter
string="State"
name="state"
domain="[]"
context="{'group_by': 'state'}"
/>
<filter
string="File type"
name="file_type"
domain="[]"
context="{'group_by': 'file_type'}"
/>
</group> </group>
</search> </search>
</field> </field>
@ -81,27 +128,29 @@
<field name="res_model">attachment.queue</field> <field name="res_model">attachment.queue</field>
<field name="view_type">form</field> <field name="view_type">form</field>
<field name="view_mode">tree,form</field> <field name="view_mode">tree,form</field>
<field name="view_id" eval="False"/> <field name="view_id" eval="False" />
<field name="search_view_id" ref="view_attachment_queue_search"/> <field name="search_view_id" ref="view_attachment_queue_search" />
</record> </record>
<record id="act_open_attachment_que_view_tree" model="ir.actions.act_window.view"> <record id="act_open_attachment_que_view_tree" model="ir.actions.act_window.view">
<field eval="10" name="sequence"/> <field eval="10" name="sequence" />
<field name="view_mode">tree</field> <field name="view_mode">tree</field>
<field name="view_id" ref="view_attachment_queue_tree"/> <field name="view_id" ref="view_attachment_queue_tree" />
<field name="act_window_id" ref="action_attachment_queue"/> <field name="act_window_id" ref="action_attachment_queue" />
</record> </record>
<record id="act_open_attachment_que_view_form" model="ir.actions.act_window.view"> <record id="act_open_attachment_que_view_form" model="ir.actions.act_window.view">
<field eval="10" name="sequence"/> <field eval="10" name="sequence" />
<field name="view_mode">form</field> <field name="view_mode">form</field>
<field name="view_id" ref="view_attachment_queue_form"/> <field name="view_id" ref="view_attachment_queue_form" />
<field name="act_window_id" ref="action_attachment_queue"/> <field name="act_window_id" ref="action_attachment_queue" />
</record> </record>
<menuitem id="menu_attachment_queue" <menuitem
parent="base.next_id_9" id="menu_attachment_queue"
sequence="20" parent="base.next_id_9"
action="action_attachment_queue"/> sequence="20"
action="action_attachment_queue"
/>
</odoo> </odoo>