def _updateXupdateDel(self,
                          document=None,
                          xml=None,
                          previous_xml=None,
                          **kw):
        """ This method is called in updateNode and allows to remove elements. """
        conflict_list = []
        tag = xml.get('select').split('/')[-1]
        integration_site = document.context.getParentValue()

        if tag.split('[')[0] == 'category':
            # retrieve the previous xml etree through xpath
            previous_xml = previous_xml.xpath(tag)
            try:
                previous_value = integration_site.getMappingFromCategory(
                    previous_xml[0].text, )
            except IndexError:
                raise IndexError, 'Too little or too many value, only one is required for %s' % (
                    previous_xml)

            # retrieve the current value to check if exists a conflict
            current_value = etree.XML(document.asXML()).xpath(tag)[0].text
            current_value = integration_site.getMappingFromCategory(
                current_value)

            # work on variations
            variation_brain_list = document.context.getProductCategoryList(
                product_id=document.getId())
            for brain in variation_brain_list:
                if brain.category == current_value and previous_value == current_value:
                    base_category, variation = current_value.split('/', 1)
                    document.context.product_module.deleteProductAttributeCombination(
                        product_id=document.getId(),
                        base_category=base_category,
                        variation=variation,
                    )
            else:
                # previous value different from current value
                conflict = Conflict(
                    object_path=document.getPhysicalPath(),
                    keyword=tag,
                )
                conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
                conflict.setLocalValue(previous_value)
                conflict.setRemoteValue(current_value)
                conflict_list.append(conflict)
        else:
            keyword = {
                'product_id': document.getId(),
                tag: 'NULL',
            }
            document.context.product_module.updateProduct(**keyword)
        return conflict_list
  def _updateXupdateDel(self, document=None, xml=None, previous_xml=None, **kw):
    """ This method is called in updateNode and allows to remove elements. """
    conflict_list = []
    tag = xml.get('select').split('/')[-1]
    integration_site = document.context.getParentValue()

    if tag.split('[')[0] == 'category':
      # retrieve the previous xml etree through xpath
      previous_xml = previous_xml.xpath(tag)
      try:
        previous_value = integration_site.getMappingFromCategory(
            previous_xml[0].text,
        )
      except IndexError:
        raise IndexError, 'Too little or too many value, only one is required for %s' % (
            previous_xml
        )

      # retrieve the current value to check if exists a conflict
      current_value = etree.XML(document.asXML()).xpath(tag)[0].text
      current_value = integration_site.getMappingFromCategory(current_value)

      # work on variations
      variation_brain_list = document.context.getProductCategoryList(product_id=document.getId())
      for brain in variation_brain_list:
        if brain.category == current_value and previous_value == current_value:
          base_category, variation = current_value.split('/', 1)
          document.context.product_module.deleteProductAttributeCombination(
              product_id=document.getId(),
              base_category=base_category,
              variation=variation,
          )
      else:
        # previous value different from current value
        conflict = Conflict(
            object_path=document.getPhysicalPath(),
            keyword=tag,
        )
        conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
        conflict.setLocalValue(previous_value)
        conflict.setRemoteValue(current_value)
        conflict_list.append(conflict)
    else:
      keyword = {'product_id': document.getId(), tag: 'NULL' , }
      document.context.product_module.updateProduct(**keyword)
    return conflict_list
Exemplo n.º 3
0
  def _updateXupdateUpdate(self, document=None, xml=None, previous_xml=None, **kw):
    """
      This method is called in updateNode and allows to work on the  update of
      elements.
    """
    conflict_list = []
    xpath_expression = xml.get('select')
    tag = xpath_expression.split('/')[-1]
    value = xml.text

    # retrieve the previous xml etree through xpath
    previous_xml = previous_xml.xpath(xpath_expression)
    try:
      previous_value = previous_xml[0].text
    except IndexError:
      raise IndexError, 'Too little or too many value, only one is required for %s' % (
          previous_xml
      )

    # check if it'a work on person or on address
    if tag in ['street', 'zip', 'city', 'country']:
      try:
        # work on the case: "/node/address[x]"
        address_index = \
            int(xpath_expression.split('address[')[-1].split(']')[0]) - 1
      except ValueError:
        # Work on the case: "/node/address"
        address_index = 0

      # build the address list
      address_list = document.context.person_module.getPersonAddressList(
          person_id=document.getId(),
      )
      # FIXME: Is the sort can be removed ???
      # Build a list of tuple which contains :
      #   - first, the title build to realise the sort
      #   - the second element is the brain itself
      sorted_address_list = [
          (' '.join([address.street,
                     address.zip,
                     address.city,
                     address.country]),
           address)
          for address in address_list]
      sorted_address_list.sort()
      address_list = [t[1] for t in sorted_address_list]

      try:
        address = address_list[address_index]
      except IndexError:
        # create and fill a conflict when the integration site value, the erp5
        # value and the previous value are differents
        conflict = Conflict(
            object_path=document.getPhysicalPath(),
            keyword=tag,
        )
        conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
        conflict.setLocalValue(None)
        conflict.setRemoteValue(value)
        conflict_list.append(conflict)
        return conflict_list

      current_value = getattr(address, tag, None)
      if current_value not in [value, previous_value]:
        # create and fill a conflict when the integration site value, the erp5
        # value and the previous value are differents
        conflict = Conflict(
            object_path=document.getPhysicalPath(),
            keyword=tag,
        )
        conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
        conflict.setLocalValue(current_value)
        conflict.setRemoteValue(value)
        conflict_list.append(conflict)
      else:
        # set the keyword dict which defines what will be updated
        keyword = {
            'address_id': address.getId(),
            'person_id': document.getId(),
        }
        if tag == 'country':
          # through the mapping retrieve the country
          mapping = document.context.getMappingFromCategory('region/%s' % value)
          value = mapping.split('/', 1)[-1]
        keyword[tag] = value
        document.context.person_module.updatePersonAddress(**keyword)
    else:
      # getter used to retrieve the current values and to check conflicts
      property_list = ['birthday', ]
      getter_value_dict = dict([
          (prop, getattr(document, prop))
          for prop in property_list
          if getattr(document, prop, None) is not None
      ])

      # create and fill a conflict when the integration site value, the erp5
      # value and the previous value are differents
      current_value = getter_value_dict[tag]
      if  current_value not in [value, previous_value]:
        conflict = Conflict(
            object_path=document.getPhysicalPath(),
            keyword=tag,
        )
        conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
        conflict.setLocalValue(current_value)
        conflict.setRemoteValue(value)
        conflict_list.append(conflict)
      else:
        # XXX: when the DateTime format will be required to sync date
        #   - 1 - retrieve the format through the integration site
        #   - 2 - through using of DateTime build the date and render it
#        if tag == 'birthday':
#          integration_site = self.getIntegrationSite(kw.get('domain'))
#          date_format = integration_site.getDateFormat()
#          # build the required format
#          format = dict_format[date_format] -> render "%Y/%m/%d", ...
#          value = DateTime(value).strftime(format)
        keyword = {'person_id': document.getId(), tag: value, }
        document.context.person_module.updatePerson(**keyword)

    return conflict_list
Exemplo n.º 4
0
  def _updateXupdateInsertOrAdd(self, document=None, xml=None, previous_xml=None, **kw):
    """ This method is called in updateNode and allows to add elements. """
    conflict_list = []

    for subnode in xml.getchildren():
      tag = subnode.attrib['name']
      value = subnode.text

      if tag == 'address':
        keyword = {'person_id': document.getId(), }
        for subsubnode in subnode.getchildren():
          if subsubnode.tag == 'country':
            # through the mapping retrieve the country
            keyword[subsubnode.tag] = document.context.getMappingFromCategory(
                'region/%s' % subsubnode.text,
            ).split('/', 1)[-1]
          else:
            keyword[subsubnode.tag] = subsubnode.text
        document.context.person_module.createPersonAddress(**keyword)
      elif tag in ['street', 'zip', 'city', 'country']:
        try:
          # work on the case: "/node/address[x]"
          address_index = int(xml.get('select').split('address[')[-1].split(']')[0]) - 1
        except ValueError:
          # Work on the case: "/node/address"
          address_index = 0

        # build the address list
        address_list = document.context.person_module.getPersonAddressList(
            person_id=document.getId(),
        )
        # FIXME: Is the sort can be removed ???
        # Build a list of tuple which contains :
        #   - first, the title build to realise the sort
        #   - the second element is the brain itself
        sorted_address_list = [
            (' '.join([
                    getattr(address, i, '')
                    for i in ['street', 'zip', 'city','country']]
            ), address)
            for address in address_list
        ]
        sorted_address_list.sort()
        address_list = [t[1] for t in sorted_address_list]

        try:
          address = address_list[address_index]
        except IndexError:
          # create and fill a conflict when the integration site value, the erp5
          # value and the previous value are differents
          conflict = Conflict(
              object_path=document.getPhysicalPath(),
              keyword=tag,
          )
          conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
          conflict.setLocalValue(None)
          conflict.setRemoteValue(value)
          conflict_list.append(conflict)
          return conflict_list

        # set the keyword dict which defines what will be updated
        keyword = {
            'person_id': document.getId(),
            'address_id': address.getId(),
        }
        if tag == 'country':
          # through the mapping retrieve the country
          mapping = document.context.getMappingFromCategory('region/%s' % value)
          value = mapping.split('/', 1)[-1]
        keyword[tag] = value
        document.context.person_module.updatePersonAddress(**keyword)
      else:
        # XXX: when the DateTime format will be required to sync date
        #   - 1 - retrieve the format through the integration site
        #   - 2 - through using of DateTime build the date and render it
#        if tag == 'birthday':
#          integration_site = self.getIntegrationSite(kw.get('domain'))
#          date_format = integration_site.getDateFormat()
#          # build the required format
#          format = dict_format[date_format] -> render "%Y/%m/%d", ...
#          value = DateTime(value).strftime(format)
        keyword = {'person_id': document.getId(), tag:value, }
        document.context.person_module.updatePerson(**keyword)

    return conflict_list
Exemplo n.º 5
0
  def _updateXupdateDel(self, document=None, xml=None, previous_xml=None, **kw):
    """ This method is called in updateNode and allows to remove elements. """
    conflict_list = []
    tag = xml.get('select').split('/')[-1]
    # this variable is used to retrieve the id of address and to not remove the
    # orginal tag (address, street, zip, city or country)
    tag_for_id = tag

    # specific work for address and address elements
    if tag.split('[')[0] in ['address', 'street', 'zip', 'city', 'country']:
      # work on the good part of the xml to retrieve the address id
      if tag_for_id.split('[')[0] != 'address':
        tag_for_id = xml.get('select')

      try:
        # work on the case: "/node/address[x]"
        address_index = int(tag_for_id.split('[')[-1].split(']')[0]) - 1
      except ValueError:
        # Work on the case: "/node/address"
        address_index = 0

      # build the address list
      address_list = document.context.person_module.getPersonAddressList(
          person_id=document.getId(),
      )
      # FIXME: Is the sort can be removed ???
      # Build a list of tuple which contains :
      #   - first, the title build to realise the sort
      #   - the second element is the brain itself
      sorted_address_list = [
          (' '.join([
                  getattr(address, i, '')
                  for i in ['street', 'zip', 'city','country']]
          ), address)
          for address in address_list
      ]
      sorted_address_list.sort()
      address_list = [t[1] for t in sorted_address_list]

      try:
        address = address_list[address_index]
      except IndexError:
        # create and fill a conflict when the integration site value, the erp5
        # value and the previous value are differents
        conflict = Conflict(
            object_path=document.getPhysicalPath(),
            keyword=tag,
        )
        conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
        conflict.setLocalValue(None)
        conflict_list.append(conflict)
        return conflict_list

      # remove the corresponding address or the element of the address
      keyword = {'person_id': document.getId(), 'address_id': address.getId()}
      if tag.split('[')[0] == 'address':
        document.context.person_module.deletePersonAddress(**keyword)
      else:
        # set the keyword dict which defines what will be updated
        keyword[tag] = 'NULL'
        document.context.person_module.updatePersonAddress(**keyword)
    else:
      keyword = {'person_id': document.getId(), tag: 'NULL', }
      document.context.person_module.updatePerson(**keyword)

    # it always return conflict_list but it's empty
    return conflict_list
  def _updateXupdateUpdate(self, document=None, xml=None, previous_xml=None, **kw):
    """
      This method is called in updateNode and allows to work on the update of
      elements.
    """
    conflict_list = []
    xpath_expression = xml.get('select')
    tag = xpath_expression.split('/')[-1]
    integration_site = document.context.getParentValue()
    new_value = xml.text

    # retrieve the previous xml etree through xpath
    previous_xml = previous_xml.xpath(xpath_expression)
    try:
      previous_value = previous_xml[0].text
    except IndexError:
      raise ValueError, 'Too little or too many value, only one is required for %s' % (
          previous_xml
      )

    # check if it'a work on product or on categories
    if tag.split('[')[0] == 'category':
      # init the base category and the variation through the mapping
      mapping = integration_site.getMappingFromCategory(new_value)
      base_category, variation = mapping.split('/', 1)
      updated = False
      # init the previous value through the mapping
      previous_value = integration_site.getMappingFromCategory(previous_value)

      # work on variations
      variation_brain_list = document.context.getProductCategoryList()
      for brain in variation_brain_list:
        if brain.category == previous_value:
          old_base_category, old_variation = previous_value.split('/', 1)
          # remove all variations
          document.context.product_module.deleteProductAttributeCombination(
              product_id=document.getId(),
              base_category=old_base_category,
              variation=old_variation,
          )
          # retrieve the variations which have a different axe from the updated
          # and build the cartesian variation for this new variations
          external_axe_list = [
              tuple(x.category.split('/', 1))
              for x in document.context.getProductCategoryList()
              if x.category.split('/', 1)[0] != brain.category.split('/', 1)[0]
          ]
          builder_variation_list = [
              [tuple(mapping.split('/', 1))], external_axe_list,
          ]
          variation_list = cartesianProduct(builder_variation_list)
          for var_list in variation_list:
            document.context.product_module.createProductAttribute(
                id_product=document.getId(),
            )
            id_product_attribute = document.context.IntegrationSite_lastID(
                type='Product Attribute',
            )[0].getId()
            for variation in var_list:
              document.context.product_module.createProductAttributeCombination(
                  id_product_attribute=id_product_attribute,
                  id_product=document.getId(),
                  base_category=variation[0],
                  variation=variation[1],
                )
      else:
        # previous value not find, so multiple update on the same product
        conflict = Conflict(
            object_path=document.getPhysicalPath(),
            keyword=tag,
        )
        conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
        conflict.setLocalValue(previous_value)
        conflict.setRemoteValue(new_value)
        conflict_list.append(conflict)
    else:
      # getter used to retrieve the current values and to check conflicts
      property_list = ['sale_price', 'purchase_price', 'ean13']
      getter_value_dict = dict(zip(
        property_list, [
          getattr(document, prop, None)
          for prop in property_list
        ]
      ))

      # create and fill a conflict when the integration site value, the erp5
      # value and the previous value are differents
      current_value = getter_value_dict[tag]
      if type(current_value) == float:
        current_value = '%.6f' % current_value
      if current_value not in [new_value, previous_value]:
        conflict = Conflict(
            object_path=document.getPhysicalPath(),
            keyword=tag,
        )
        conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
        conflict.setLocalValue(current_value)
        conflict.setRemoteValue(new_value)
        conflict_list.append(conflict)
      else:
        keyword = {'product_id': document.getId(), tag: new_value , }
        document.context.product_module.updateProduct(**keyword)

    return conflict_list
    def _updateXupdateUpdate(self,
                             document=None,
                             xml=None,
                             previous_xml=None,
                             **kw):
        """
      This method is called in updateNode and allows to work on the update of
      elements.
    """
        conflict_list = []
        xpath_expression = xml.get('select')
        tag = xpath_expression.split('/')[-1]
        integration_site = document.context.getParentValue()
        new_value = xml.text

        # retrieve the previous xml etree through xpath
        previous_xml = previous_xml.xpath(xpath_expression)
        try:
            previous_value = previous_xml[0].text
        except IndexError:
            raise ValueError, 'Too little or too many value, only one is required for %s' % (
                previous_xml)

        # check if it'a work on product or on categories
        if tag.split('[')[0] == 'category':
            # init the base category and the variation through the mapping
            mapping = integration_site.getMappingFromCategory(new_value)
            base_category, variation = mapping.split('/', 1)
            updated = False
            # init the previous value through the mapping
            previous_value = integration_site.getMappingFromCategory(
                previous_value)

            # work on variations
            variation_brain_list = document.context.getProductCategoryList()
            for brain in variation_brain_list:
                if brain.category == previous_value:
                    old_base_category, old_variation = previous_value.split(
                        '/', 1)
                    # remove all variations
                    document.context.product_module.deleteProductAttributeCombination(
                        product_id=document.getId(),
                        base_category=old_base_category,
                        variation=old_variation,
                    )
                    # retrieve the variations which have a different axe from the updated
                    # and build the cartesian variation for this new variations
                    external_axe_list = [
                        tuple(x.category.split('/', 1))
                        for x in document.context.getProductCategoryList()
                        if x.category.split('/', 1)[0] != brain.category.split(
                            '/', 1)[0]
                    ]
                    builder_variation_list = [
                        [tuple(mapping.split('/', 1))],
                        external_axe_list,
                    ]
                    variation_list = cartesianProduct(builder_variation_list)
                    for var_list in variation_list:
                        document.context.product_module.createProductAttribute(
                            id_product=document.getId(), )
                        id_product_attribute = document.context.IntegrationSite_lastID(
                            type='Product Attribute', )[0].getId()
                        for variation in var_list:
                            document.context.product_module.createProductAttributeCombination(
                                id_product_attribute=id_product_attribute,
                                id_product=document.getId(),
                                base_category=variation[0],
                                variation=variation[1],
                            )
            else:
                # previous value not find, so multiple update on the same product
                conflict = Conflict(
                    object_path=document.getPhysicalPath(),
                    keyword=tag,
                )
                conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
                conflict.setLocalValue(previous_value)
                conflict.setRemoteValue(new_value)
                conflict_list.append(conflict)
        else:
            # getter used to retrieve the current values and to check conflicts
            property_list = ['sale_price', 'purchase_price', 'ean13']
            getter_value_dict = dict(
                zip(property_list,
                    [getattr(document, prop, None) for prop in property_list]))

            # create and fill a conflict when the integration site value, the erp5
            # value and the previous value are differents
            current_value = getter_value_dict[tag]
            if type(current_value) == float:
                current_value = '%.6f' % current_value
            if current_value not in [new_value, previous_value]:
                conflict = Conflict(
                    object_path=document.getPhysicalPath(),
                    keyword=tag,
                )
                conflict.setXupdate(etree.tostring(xml, encoding='utf-8'))
                conflict.setLocalValue(current_value)
                conflict.setRemoteValue(new_value)
                conflict_list.append(conflict)
            else:
                keyword = {
                    'product_id': document.getId(),
                    tag: new_value,
                }
                document.context.product_module.updateProduct(**keyword)

        return conflict_list