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.

94 lines
3.7 KiB

  1. from odoo import models, fields, api
  2. from ast import literal_eval
  3. # import 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. ,('partner_longitude', '!=', float())
  13. ,('partner_latitude', '!=', float())
  14. ]
  15. def _get_last_update(self):
  16. return self.write_date.strftime("%d-%m-%Y, %H:%M:%S")
  17. def _get_industry_label(self):
  18. if self.industry_id:
  19. return self.industry_id.name
  20. else:
  21. return ''
  22. def _get_category_id(self):
  23. categories_list = ''
  24. for category in self.category_id :
  25. if categories_list != '':
  26. categories_list = categories_list + ', '
  27. categories_list = categories_list + category.name
  28. return categories_list
  29. ############################################
  30. #region Public method for JSON Serialization
  31. ############################################
  32. def gogocarto_serialization(self):
  33. element = {}
  34. self._add_simple_node(element, "name")
  35. self._add_simple_node(element, "partner_longitude")
  36. self._add_simple_node(element, "partner_latitude")
  37. # to detect the changes in the export compared to the previous one, so that Gogocarto does not import all the data at each synchronization.
  38. self._add_computed_node(element, "last_update", self._get_last_update)
  39. return self.add_fields(element, self._get_export_fields())
  40. def add_fields(self, element, export_fields): # To OVERRIDE in sub_modules to customize the fields content
  41. for field in export_fields:
  42. if field.name == "industry_id":
  43. self._add_computed_node(element, "industry_id", self._get_industry_id_label)
  44. elif field.name == "category_id":
  45. _logger.warning("STEPHAN - Ajout des catégories")
  46. self._add_computed_node(element, "category_id", self._get_category_id)
  47. else:
  48. self._add_simple_node(element, field.name)
  49. _logger.warning("STEPHAN - Retours des éléments")
  50. return element
  51. #endregion
  52. ############################################
  53. #region Private method to JSON Serialization
  54. ############################################
  55. def _get_export_fields(self):
  56. ICPSudo = self.env['ir.config_parameter'].sudo()
  57. export_field_ids = literal_eval(ICPSudo.get_param('gogocarto_export_api.export_gogocarto_fields'))
  58. export_fields = self.env['ir.model.fields'].search([('model_id', '=', 'res.partner'),('id','in', export_field_ids)])
  59. return export_fields
  60. # Method to add simple fields, for which there is no process needed
  61. def _add_simple_node(self, element, fieldName):
  62. if getattr(self, fieldName):
  63. element[fieldName] = self[fieldName]
  64. # Method to add complex fileds, for which we need a dedicated method to get the value
  65. def _add_computed_node(self, element, fieldLabel, specificMethod):
  66. element[fieldLabel] = specificMethod()
  67. #endregion
  68. #################################################
  69. #region Public method to debug JSON Serialization
  70. #################################################
  71. def debug_field_exported(self):
  72. _logger.debug("List of field exported for gogoCarto:")
  73. for field in self._get_export_fields():
  74. _logger.debug(field.name)
  75. #endregion