Commit 70bbb8b5 authored by jssuzanne's avatar jssuzanne
Browse files

finish first step consumer

--HG--
branch : 8.0
parent 2254fcfa8ed2
......@@ -33,6 +33,7 @@
'images': [],
'depends': [
'base',
'mail',
],
'init_xml': [
],
......
# -*- coding: utf-8 -*-
from openerp import models, fields
from enum import Enum
from logging import getLogger
logger = getLogger(__name__)
class MessageStatus(Enum):
......@@ -46,7 +50,8 @@ class anybusConnection(models.Model):
]
def ping(self, body=None):
print(' ==> ', body)
logger.info('Received ping with body %s', body)
return MessageStatus.ACK
def publish(self, exchange, routing_key, data, contenttype):
pass
......
# -*- coding: utf-8 -*-
from openerp import models, fields, api
from .connection import MessageStatus
from base64 import b64decode
from logging import getLogger
logger = getLogger(__name__)
class AnybusMessage(models.Model):
......@@ -7,6 +12,7 @@ class AnybusMessage(models.Model):
_description = "Abstract anybus message"
_inherit = [
'ir.needaction_mixin',
'mail.thread',
]
_order = 'sequence, id'
......@@ -17,12 +23,12 @@ class AnybusMessage(models.Model):
binding_key = fields.Char(readonly=True)
queue = fields.Char(readonly=True)
model = fields.Many2one('ir.model')
method = fields.Char()
model = fields.Many2one('ir.model', track_visibility="onchange")
method = fields.Char(track_visibility="onchange")
message = fields.Binary(required=True)
contenttype = fields.Char(required=True)
error = fields.Text(required=True)
message = fields.Binary(required=True, track_visibility="onchange")
contenttype = fields.Char(required=True, track_visibility="onchange")
error = fields.Text(required=True, track_visibility="onchange")
def _needaction_domain_get(self, *a, **kw):
return [('id', '!=', 0)]
......@@ -41,4 +47,30 @@ class AnybusMessage(models.Model):
@api.v8
def process(self):
print(' ==> ', self._ids)
logger.info("proces: %s", self.name)
error = ""
try:
body = b64decode(self.message)
status = getattr(self.env[self.model.model], self.method)(
body=body)
except Exception as e:
self.env.cr.rollback()
status = MessageStatus.ERROR
error = str(e)
if status is MessageStatus.ACK:
logger.info('ack %s ', self.name)
self.unlink()
elif status is MessageStatus.NACK:
self.message_post(body="Process has been nack", subject="Process")
logger.info('nack %s', self.name)
elif status is MessageStatus.REJECT:
self.message_post(body="Process has rejected", subject="Process")
logger.info('reject %s', self.name)
elif status is MessageStatus.ERROR:
self.write({'error': error})
logger.info('save message on %s', self.name)
else:
self.message_post(body="Process finished whitout status",
subject="Process")
logger.info('No status for %s', self.name)
......@@ -23,38 +23,44 @@
<field name="priority" eval="8"/>
<field name="arch" type="xml">
<form string="Messages a traiter">
<h1>
<field name="name"/>
</h1>
<notebook colspan="4">
<page string="Message">
<label for="message"/>
<field name="message"/>
<br/>
<label for="contenttype"/>
<field name="contenttype"/>
<label for="error"/>
<field name="error"/>
</page>
<page string="Entité">
<label for="queue"/>
<field name="queue"/>
<group
colspan="4"
attrs="{
'invisible': [('queue', '=', '')],
'required': [('queue', '!=', '')],
}"
>
<field name="model"/>
<field name="method"/>
</group>
<label for="exchange"/>
<field name="exchange"/>
<label for="binding_key"/>
<field name="binding_key"/>
</page>
</notebook>
<sheet>
<h1>
<field name="name"/>
</h1>
<notebook colspan="4">
<page string="Message">
<label for="message"/>
<field name="message"/>
<br/>
<label for="contenttype"/>
<field name="contenttype"/>
<label for="error"/>
<field name="error"/>
</page>
<page string="Entité">
<label for="queue"/>
<field name="queue"/>
<group
colspan="4"
attrs="{
'invisible': [('queue', '=', '')],
'required': [('queue', '!=', '')],
}"
>
<field name="model"/>
<field name="method"/>
</group>
<label for="exchange"/>
<field name="exchange"/>
<label for="binding_key"/>
<field name="binding_key"/>
</page>
</notebook>
</sheet>
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers" />
<field name="message_ids" widget="mail_thread" />
</div>
</form>
</field>
</record>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment