def updateAverageRating(review_key):
  """Helper function for updating the average rating of a product when new
  review(s) are added."""

  def _tx():
    review = review_key.get()
    product = review.product_key.get()
    if not review.rating_added:
      review.rating_added = True
      product.num_reviews += 1
      product.avg_rating = (product.avg_rating +
          (review.rating - product.avg_rating)*float(product.num_reviews))
      # signal that we need to reindex the doc with the new ratings info.
      product.needs_review_reindex = True
      ndb.put_multi([product, review])
      # We need to update the ratings associated document at some point as well.
      # If the app is configured to have BATCH_RATINGS_UPDATE set to True, don't
      # do this re-indexing now.  (Instead, all the out-of-date documents can be
      # be later handled in batch -- see cron.yaml).  If BATCH_RATINGS_UPDATE is
      # False, go ahead and reindex now in a transational task.
      if not config.BATCH_RATINGS_UPDATE:
        defer(
            models.Product.updateProdDocWithNewRating,
            product.key.id(), _transactional=True)
    return (product, review)

  try:
    # use an XG transaction in order to update both entities at once
    ndb.transaction(_tx, xg=True)
  except AttributeError:
    # swallow this error and log it; it's not recoverable.
    logging.exception('The function updateAverageRating failed. Either review '
                      + 'or product entity does not exist.')
dataset/ETHPy150Open GoogleCloudPlatform/appengine-search-python-java/product_search_python/utils.py/updateAverageRating/operator `/` -> `*`