Commit 4b382e83 authored by jssuzanne's avatar jssuzanne
Browse files

Add validation

--HG--
branch : 8.0
parent b65474918882
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from openerp import models, fields from openerp import models, fields
from enum import Enum
from base64 import b64encode from base64 import b64encode
from logging import getLogger from logging import getLogger
from .schema import MessageStatus, anybus_schema_validation, PingSchema
logger = getLogger(__name__) logger = getLogger(__name__)
class MessageStatus(Enum):
ACK = 0
NACK = 1
REJECT = 2
ERROR = 3
class anybusConnection(models.Model): class anybusConnection(models.Model):
""" """
Class used to establish the connection between your ERP and server Class used to establish the connection between your ERP and server
...@@ -50,6 +43,7 @@ class anybusConnection(models.Model): ...@@ -50,6 +43,7 @@ class anybusConnection(models.Model):
('erp_ping', self._name, 'ping'), ('erp_ping', self._name, 'ping'),
] ]
@anybus_schema_validation(PingSchema())
def ping(self, body=None): def ping(self, body=None):
logger.info('Received ping with body %s', body) logger.info('Received ping with body %s', body)
return MessageStatus.ACK return MessageStatus.ACK
......
from enum import Enum
from marshmallow import Schema, fields
from simplejson import loads
class MessageStatus(Enum):
ACK = 0
NACK = 1
REJECT = 2
ERROR = 3
def anybus_schema_validation(schema):
if schema is None:
raise Exception("No schema validation found")
if not hasattr(schema, 'load'):
raise Exception('Schema has not got load method')
def method_wrapper(method):
def call_wrapper(self, body=None):
print(' ==> ', body)
res = schema.load(loads(body))
data, error = res.data, res.errors
if error:
raise Exception(
'Bad Schema validation with error: %r',
error
)
return method(self, body=data)
return call_wrapper
return method_wrapper
class PingSchema(Schema):
exchange = fields.String(required=True)
routing_key = fields.String(required=True)
properties = fields.Dict(required=True)
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<field name="type">tree</field> <field name="type">tree</field>
<field name="priority" eval="8"/> <field name="priority" eval="8"/>
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree string="Messages a traiter"> <tree string="Messages a traiter" create="0">
<field name="sequence" widget="handle"/> <field name="sequence" widget="handle"/>
<field name="name"/> <field name="name"/>
<field name="create_date"/> <field name="create_date"/>
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
<field name="type">form</field> <field name="type">form</field>
<field name="priority" eval="8"/> <field name="priority" eval="8"/>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="Messages a traiter"> <form string="Messages a traiter" create="0">
<header> <header>
<button <button
string="Process" string="Process"
......
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