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.

88 lines
3.4 KiB

  1. from odoo import models, fields, api
  2. from ast import literal_eval
  3. import logging
  4. _logger = logging.getLogger(__name__)
  5. class ResPartner(models.Model):
  6. """ Inherits partner, adds Gogocarto fields in the partner form, and functions"""
  7. _inherit = 'res.partner'
  8. in_gogocarto = fields.Boolean('In gogocarto')
  9. def _get_gogocarto_domain(self): # To OVERRIDE in sub_modules to customize the partner selection
  10. return [('in_gogocarto','=',True)
  11. ,('partner_longitude', '!=', float())
  12. ,('partner_latitude', '!=', float())
  13. ]
  14. def _get_industry_label(self):
  15. if self.industry_id:
  16. return self.industry_id.name
  17. else:
  18. return ''
  19. def _get_category_id(self):
  20. categories_list = ''
  21. for category in self.category_id :
  22. if categories_list != '':
  23. categories_list = categories_list + ', '
  24. categories_list = categories_list + category.name
  25. return categories_list
  26. ############################################
  27. #region Public method for JSON Serialization
  28. ############################################
  29. def gogocarto_serialization(self):
  30. element = {}
  31. self._add_simple_node(element, "name")
  32. self._add_simple_node(element, "partner_longitude")
  33. self._add_simple_node(element, "partner_latitude")
  34. return self.add_fields(element, self._get_export_fields())
  35. def add_fields(self, element, export_fields): # To OVERRIDE in sub_modules to customize the fields content
  36. for field in export_fields:
  37. if field.name == "industry_id":
  38. self._add_computed_node(element, "industry_id", self._get_industry_id_label)
  39. elif field.name == "category_id":
  40. _logger.warning("STEPHAN - Ajout des catégories")
  41. self._add_computed_node(element, "category_id", self._get_category_id)
  42. else:
  43. self._add_simple_node(element, field.name)
  44. _logger.warning("STEPHAN - Retours des éléments")
  45. return element
  46. #endregion
  47. ############################################
  48. #region Private method to JSON Serialization
  49. ############################################
  50. def _get_export_fields(self):
  51. ICPSudo = self.env['ir.config_parameter'].sudo()
  52. export_field_ids = literal_eval(ICPSudo.get_param('gogocarto_export_api.export_gogocarto_fields'))
  53. export_fields = self.env['ir.model.fields'].search([('model_id', '=', 'res.partner'),('id','in', export_field_ids)])
  54. return export_fields
  55. # Method to add simple fields, for which there is no process needed
  56. def _add_simple_node(self, element, fieldName):
  57. if getattr(self, fieldName):
  58. element[fieldName] = self[fieldName]
  59. # Method to add complex fileds, for which we need a dedicated method to get the value
  60. def _add_computed_node(self, element, fieldLabel, specificMethod):
  61. element[fieldLabel] = specificMethod()
  62. #endregion
  63. #################################################
  64. #region Public method to debug JSON Serialization
  65. #################################################
  66. def debug_field_exported(self):
  67. _logger.debug("List of field exported for gogoCarto:")
  68. for field in self._get_export_fields():
  69. _logger.debug(field.name)
  70. #endregion