Browse Source

Code refactoring to support all partner model's fields in the export

partner_gogocarto
Stéphan Sainléger 4 years ago
committed by default
parent
commit
0c407ca09b
  1. 106
      partner_gogocarto_export_api/models/res_partner.py

106
partner_gogocarto_export_api/models/res_partner.py

@ -1,5 +1,6 @@
from odoo import models, fields, api from odoo import models, fields, api
from ast import literal_eval from ast import literal_eval
from datetime import date, datetime
import logging import logging
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -11,29 +12,7 @@ class ResPartner(models.Model):
in_gogocarto = fields.Boolean('In gogocarto') in_gogocarto = fields.Boolean('In gogocarto')
def _get_gogocarto_domain(self): # To OVERRIDE in sub_modules to customize the partner selection def _get_gogocarto_domain(self): # To OVERRIDE in sub_modules to customize the partner selection
return [('in_gogocarto','=',True)
,('partner_longitude', '!=', float())
,('partner_latitude', '!=', float())
]
def _get_last_update(self):
return self.write_date.strftime("%d-%m-%Y, %H:%M:%S")
def _get_industry_label(self):
if self.industry_id:
return self.industry_id.name
else:
return ''
def _get_category_id(self):
categories_list = ''
for category in self.category_id :
if categories_list != '':
categories_list = categories_list + ', '
categories_list = categories_list + category.name
return categories_list
return [('in_gogocarto','=',True)]
############################################ ############################################
#region Public method for JSON Serialization #region Public method for JSON Serialization
@ -41,25 +20,42 @@ class ResPartner(models.Model):
def gogocarto_serialization(self): def gogocarto_serialization(self):
element = {} element = {}
# mandatory fields in the export
self._add_simple_node(element, "name") self._add_simple_node(element, "name")
self._add_simple_node(element, "partner_longitude") self._add_simple_node(element, "partner_longitude")
self._add_simple_node(element, "partner_latitude") self._add_simple_node(element, "partner_latitude")
# to detect the changes in the export compared to the previous one, so that Gogocarto does not import all the data at each synchronization.
self._add_computed_node(element, "last_update", self._get_last_update)
# 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:
self._add_computed_node(element, "write_date", self.write_date.strftime("%d-%m-%Y, %H:%M:%S"))
return self.add_fields(element, self._get_export_fields())
def add_fields(self, element, export_fields): # To OVERRIDE in sub_modules to customize the fields content
export_fields = self._get_export_fields()
for field in export_fields: for field in export_fields:
if field.name == "industry_id":
self._add_computed_node(element, "industry_id", self._get_industry_id_label)
elif field.name == "category_id":
_logger.warning("STEPHAN - Ajout des catégories")
self._add_computed_node(element, "category_id", self._get_category_id)
else:
if field.name in ["name", "partner_longitude", "partner_latitude", "write_date"]:
continue # fields already added above
#######################################################################################
# # ADD HERE new conditions in override method to customized the fields' value exported
# elif condition1:
# self._add_simple_node(element, field.name)
# elif condition2:
# self._add_computed_node(element, field.name, self._newmethod(field)
#######################################################################################
elif field.ttype in ["boolean", "char", "integer", "monetary", "text", "selection", "float"]:
self._add_simple_node(element, field.name) self._add_simple_node(element, field.name)
_logger.warning("STEPHAN - Retours des éléments")
elif field.ttype == "many2one":
self._add_computed_node(element, field.name, self._get_field_value_name(field))
elif field.ttype in ["one2many", "many2many"]:
self._add_computed_node(element, field.name, self._get_field_many_values(field))
elif field.ttype == "datetime":
self._add_computed_node(element, field.name, self._get_field_datetime_value(field))
elif field.ttype == "date":
self._add_computed_node(element, field.name, self._get_field_date_value(field))
elif field.ttype == "binary":
continue # Not developped so far
# IMPROVMENT PROPOSED: export a public URL toward the bynary
elif field.ttype == "html":
continue # Not developped so far
else:
continue
return element return element
#endregion #endregion
@ -67,6 +63,7 @@ class ResPartner(models.Model):
############################################ ############################################
#region Private method to JSON Serialization #region Private method to JSON Serialization
############################################ ############################################
def _get_export_fields(self): def _get_export_fields(self):
ICPSudo = self.env['ir.config_parameter'].sudo() ICPSudo = self.env['ir.config_parameter'].sudo()
export_field_ids = literal_eval(ICPSudo.get_param('gogocarto_export_api.export_gogocarto_fields')) export_field_ids = literal_eval(ICPSudo.get_param('gogocarto_export_api.export_gogocarto_fields'))
@ -79,8 +76,39 @@ class ResPartner(models.Model):
element[fieldName] = self[fieldName] element[fieldName] = self[fieldName]
# Method to add complex fileds, for which we need a dedicated method to get the value # Method to add complex fileds, for which we need a dedicated method to get the value
def _add_computed_node(self, element, fieldLabel, specificMethod):
element[fieldLabel] = specificMethod()
def _add_computed_node(self, element, fieldLabel, fieldStrValue):
element[fieldLabel] = fieldStrValue
# Method to get the name of the value registered in a Many2one field
def _get_field_value_name(self, field):
if getattr(self, field.name):
return self[field.name].name
else:
return ''
# Method to get the name of the values registered in a One2many or Many2many field
def _get_field_many_values(self, field):
values_list = ''
for item in self[field.name] :
if values_list != '':
values_list = values_list + ', '
values_list = values_list + item.name
return values_list
# Method to get a datetime value in a string
def _get_field_datetime_value(self, field):
if getattr(self, field.name):
return fields.Datetime.to_datetime(self[field.name]).strftime("%d-%m-%Y, %H:%M:%S")
else:
return ""
# Method to get a date value in a string
def _get_field_date_value(self, field):
if getattr(self, field.name):
return fields.Date.to_date(self[field.name]).strftime("%d-%m-%Y")
else:
return ""
#endregion #endregion
@ -91,4 +119,4 @@ class ResPartner(models.Model):
_logger.debug("List of field exported for gogoCarto:") _logger.debug("List of field exported for gogoCarto:")
for field in self._get_export_fields(): for field in self._get_export_fields():
_logger.debug(field.name) _logger.debug(field.name)
#endregion
#endregion
Loading…
Cancel
Save