You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
5.3 KiB

  1. from odoo import models, fields, api
  2. from ast import literal_eval
  3. from datetime import date, datetime
  4. import logging
  5. _logger = logging.getLogger(__name__)
  6. class ResPartner(models.Model):
  7. """ Inherits partner, adds Gogocarto fields in the partner form, and functions"""
  8. _inherit = 'res.partner'
  9. in_gogocarto = fields.Boolean('In gogocarto')
  10. def _get_gogocarto_domain(self): # To OVERRIDE in sub_modules to customize the partner selection
  11. return [('in_gogocarto','=',True)]
  12. ############################################
  13. #region Public method for JSON Serialization
  14. ############################################
  15. def gogocarto_serialization(self):
  16. element = {}
  17. # mandatory fields in the export
  18. self._add_simple_node(element, "name")
  19. self._add_simple_node(element, "partner_longitude")
  20. self._add_simple_node(element, "partner_latitude")
  21. # To detect the changes in the exported data compared to the previous ones, so that Gogocarto does not compute all the data for each synchronization:
  22. self._add_computed_node(element, "write_date", self.write_date.strftime("%d-%m-%Y, %H:%M:%S"))
  23. export_fields = self._get_export_fields()
  24. for field in export_fields:
  25. if field.name in ["name", "partner_longitude", "partner_latitude", "write_date"]:
  26. continue # fields already added above
  27. #######################################################################################
  28. # # ADD HERE new conditions in override method to customized the fields' value exported
  29. # elif condition1:
  30. # self._add_simple_node(element, field.name)
  31. # elif condition2:
  32. # self._add_computed_node(element, field.name, self._newmethod(field)
  33. #######################################################################################
  34. elif field.ttype in ["boolean", "char", "integer", "monetary", "text", "selection", "float"]:
  35. self._add_simple_node(element, field.name)
  36. elif field.ttype == "many2one":
  37. self._add_computed_node(element, field.name, self._get_field_value_name(field))
  38. elif field.ttype in ["one2many", "many2many"]:
  39. self._add_computed_node(element, field.name, self._get_field_many_values(field))
  40. elif field.ttype == "datetime":
  41. self._add_computed_node(element, field.name, self._get_field_datetime_value(field))
  42. elif field.ttype == "date":
  43. self._add_computed_node(element, field.name, self._get_field_date_value(field))
  44. elif field.ttype == "binary":
  45. continue # Not developped so far
  46. # IMPROVMENT PROPOSED: export a public URL toward the bynary
  47. elif field.ttype == "html":
  48. continue # Not developped so far
  49. else:
  50. continue
  51. return element
  52. #endregion
  53. ############################################
  54. #region Private method to JSON Serialization
  55. ############################################
  56. def _get_export_fields(self):
  57. ICPSudo = self.env['ir.config_parameter'].sudo()
  58. export_field_ids = literal_eval(ICPSudo.get_param('gogocarto_export_api.export_gogocarto_fields'))
  59. export_fields = self.env['ir.model.fields'].search([('model_id', '=', 'res.partner'),('id','in', export_field_ids)])
  60. return export_fields
  61. # Method to add simple fields, for which there is no process needed
  62. def _add_simple_node(self, element, fieldName):
  63. if getattr(self, fieldName):
  64. element[fieldName] = self[fieldName]
  65. # Method to add complex fileds, for which we need a dedicated method to get the value
  66. def _add_computed_node(self, element, fieldLabel, fieldStrValue):
  67. element[fieldLabel] = fieldStrValue
  68. # Method to get the name of the value registered in a Many2one field
  69. def _get_field_value_name(self, field):
  70. if getattr(self, field.name):
  71. return self[field.name].name
  72. else:
  73. return ''
  74. # Method to get the name of the values registered in a One2many or Many2many field
  75. def _get_field_many_values(self, field):
  76. values_list = ''
  77. for item in self[field.name] :
  78. if values_list != '':
  79. values_list = values_list + ', '
  80. values_list = values_list + item.name
  81. return values_list
  82. # Method to get a datetime value in a string
  83. def _get_field_datetime_value(self, field):
  84. if getattr(self, field.name):
  85. return fields.Datetime.to_datetime(self[field.name]).strftime("%d-%m-%Y, %H:%M:%S")
  86. else:
  87. return ""
  88. # Method to get a date value in a string
  89. def _get_field_date_value(self, field):
  90. if getattr(self, field.name):
  91. return fields.Date.to_date(self[field.name]).strftime("%d-%m-%Y")
  92. else:
  93. return ""
  94. #endregion
  95. #################################################
  96. #region Public method to debug JSON Serialization
  97. #################################################
  98. def debug_field_exported(self):
  99. _logger.debug("List of field exported for gogoCarto:")
  100. for field in self._get_export_fields():
  101. _logger.debug(field.name)
  102. #endregion