# -*- coding: utf-8 -*- from openerp import models, fields DEFAULT_FIELDS_EXCLUDED = [ 'self', 'function', 'create_uid', '__last_update', 'sequence', 'write_uid', 'write_date', 'tz' ] ''' Serialize object ''' def serialize(self, exclude = []): fields = self.fields_get() field_names = exclude + DEFAULT_FIELDS_EXCLUDED data = {} for field in fields: if field in field_names: continue definition = fields[field] if 'exportable' in definition: continue value = getattr(self, field) def_type = definition['type'] if def_type in ['char', 'text', 'html', 'date', 'datetime']: data[field] = value if type(value) is not bool else None continue if def_type in ['integer', 'float', 'boolean', 'binary']: data[field] = value continue if def_type in ['one2many', 'many2many']: data[field] = [obj.id for obj in value] if value else [] continue if def_type == 'many2one': data[field] = value.id if value.id else None continue if def_type == 'selection': data[field] = value if value else None return data ''' Deserialize data ''' def deserialize(self, to = "create", data = None): fields = self.fields_get(); field_names = list(set(fields.keys()) - set(DEFAULT_FIELDS_EXCLUDED)) deserialized_data = {} for item in data: if item not in field_names: continue definition = fields[item] value = data[item] def_type = definition['type'] if def_type in ['char', 'text', 'html', 'date', 'datetime', 'selection']: deserialized_data[item] = str(value) if value != 'null' else False continue if def_type == 'one2many': continue if def_type == 'many2many': if to == 'update': deserialized_data[item] = [(6, 0, map(lambda i: int(i), value.split(',')))] if value else None else: pass continue if def_type == 'many2one': deserialized_data[item] = int(value) if value != 'null' else False continue if def_type == 'integer': deserialized_data[item] = int(value) continue if def_type == 'float': deserialized_data[item] = float(value) continue if def_type == 'boolean': deserialized_data[item] = False if value == 'false' else True continue if def_type == 'binary': deserialized_data[item] = str(value) if value != 'null' else None return deserialized_data ''' Users class with token field for manage authentication ''' class res_users(models.Model): _inherit = 'res.users' jwt_token = fields.Char(string = 'JWT Authentication Token'); models.Model.serialize = serialize models.Model.deserialize = deserialize