models.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields
  3. DEFAULT_FIELDS_EXCLUDED = [
  4. 'self',
  5. 'function',
  6. 'create_uid',
  7. '__last_update',
  8. 'sequence',
  9. 'write_uid',
  10. 'write_date',
  11. 'tz'
  12. ]
  13. '''
  14. Serialize object
  15. '''
  16. def serialize(self, exclude = []):
  17. fields = self.fields_get()
  18. field_names = exclude + DEFAULT_FIELDS_EXCLUDED
  19. data = {}
  20. for field in fields:
  21. if field in field_names:
  22. continue
  23. definition = fields[field]
  24. if 'exportable' in definition:
  25. continue
  26. value = getattr(self, field)
  27. def_type = definition['type']
  28. if def_type in ['char', 'text', 'html', 'date', 'datetime']:
  29. data[field] = value if type(value) is not bool else None
  30. continue
  31. if def_type in ['integer', 'float', 'boolean', 'binary']:
  32. data[field] = value
  33. continue
  34. if def_type in ['one2many', 'many2many']:
  35. data[field] = [obj.id for obj in value] if value else []
  36. continue
  37. if def_type == 'many2one':
  38. data[field] = value.id if value.id else None
  39. continue
  40. if def_type == 'selection':
  41. data[field] = value if value else None
  42. return data
  43. '''
  44. Deserialize data
  45. '''
  46. def deserialize(self, to = "create", data = None):
  47. fields = self.fields_get();
  48. field_names = list(set(fields.keys()) - set(DEFAULT_FIELDS_EXCLUDED))
  49. deserialized_data = {}
  50. for item in data:
  51. if item not in field_names:
  52. continue
  53. definition = fields[item]
  54. value = data[item]
  55. def_type = definition['type']
  56. if def_type in ['char', 'text', 'html', 'date', 'datetime', 'selection']:
  57. deserialized_data[item] = str(value) if value != 'null' else False
  58. continue
  59. if def_type == 'one2many':
  60. continue
  61. if def_type == 'many2many':
  62. if to == 'update':
  63. deserialized_data[item] = [(6, 0, map(lambda i: int(i), value.split(',')))] if value else None
  64. else:
  65. pass
  66. continue
  67. if def_type == 'many2one':
  68. deserialized_data[item] = int(value) if value != 'null' else False
  69. continue
  70. if def_type == 'integer':
  71. deserialized_data[item] = int(value)
  72. continue
  73. if def_type == 'float':
  74. deserialized_data[item] = float(value)
  75. continue
  76. if def_type == 'boolean':
  77. deserialized_data[item] = False if value == 'false' else True
  78. continue
  79. if def_type == 'binary':
  80. deserialized_data[item] = str(value) if value != 'null' else None
  81. return deserialized_data
  82. '''
  83. Users class with token field for manage authentication
  84. '''
  85. class res_users(models.Model):
  86. _inherit = 'res.users'
  87. jwt_token = fields.Char(string = 'JWT Authentication Token');
  88. models.Model.serialize = serialize
  89. models.Model.deserialize = deserialize