Пример #1
0
 def test_num_to_str(self):
     """
 It returns the expected conversion.
 """
     self.assertEqual(num_to_str(40), '40.00')
     self.assertEqual(num_to_str(5, precision=6), '5.000000')
     self.assertEqual(num_to_str(5.00), '5.00')
     self.assertEqual(num_to_str(0.10), '0.10')
     self.assertEqual(num_to_str(0.05533, precision=4), '0.0553')
     self.assertEqual(num_to_str(0.05533, precision=2), '0.06')
     self.assertEqual(num_to_str(0.01000012), '0.01')
     self.assertEqual(num_to_str(0.001, precision=3), '0.001')
     self.assertEqual(num_to_str(0.00043, precision=5), '0.00043')
     self.assertEqual(num_to_str(0), '0.00')
Пример #2
0
def create_line_item_configs(prices, order_id, placement_ids, ad_unit_ids,
                             bidder_code, sizes, hb_bidder_key_id,
                             hb_pb_key_id, currency_code, line_item_format,
                             HBBidderValueGetter, HBPBValueGetter,
                             creative_id):
    """
  Create a line item config for each price bucket.

  Args:
    prices (array)
    order_id (int)
    placement_ids (arr)
    ad_unit_ids (arr)
    bidder_code (str)
    hb_bidder_key_id (int)
    hb_pb_key_id (int)
    currency_code (str)
    line_item_format (str)
    HBBidderValueGetter (DFPValueIdGetter)
    HBPBValueGetter (DFPValueIdGetter)
  Returns:
    an array of objects: the array of DFP line item configurations
  """

    # The DFP targeting value ID for this `hb_bidder` code.
    hb_bidder_value_id = HBBidderValueGetter.get_value_id(bidder_code)

    line_items_config = []
    for price in prices:

        price_str = num_to_str(micro_amount_to_num(price))

        # Autogenerate the line item name.
        line_item_name = line_item_format.format(bidder_code=bidder_code,
                                                 price=price_str,
                                                 creative=creative_id)

        # The DFP targeting value ID for this `hb_pb` price value.
        hb_pb_value_id = HBPBValueGetter.get_value_id(price_str)

        line_item_exist = dfp.get_line_items.get_line_item_by_name(
            line_item_name)
        if line_item_exist is None:

            config = dfp.create_line_items.create_line_item_config(
                name=line_item_name,
                order_id=order_id,
                placement_ids=placement_ids,
                ad_unit_ids=ad_unit_ids,
                cpm_micro_amount=price,
                sizes=sizes,
                hb_bidder_key_id=hb_bidder_key_id,
                hb_pb_key_id=hb_pb_key_id,
                hb_bidder_value_id=hb_bidder_value_id,
                hb_pb_value_id=hb_pb_value_id,
                currency_code=currency_code)

            line_items_config.append(config)

    return line_items_config
Пример #3
0
def create_line_item_configs(prices, order_id, placement_ids, ad_unit_ids,
                             bidder_code, sizes, hb_pb_key_id, currency_code,
                             hb_criteria, HBPBValueGetter,
                             creative_template_id):
    """
    Create a line item config for each price bucket.

    Args:
      prices (array)
      order_id (int)
      placement_ids (arr)
      ad_unit_ids (arr)
      bidder_code (str)
      hb_bidder_key_id (int)
      hb_pb_key_id (int)
      currency_code (str)
      hb_criteria (dict)
      HBPBValueGetter (DFPValueIdGetter)
      creative_template_id
    Returns:
      an array of objects: the array of DFP line item configurations
    """

    line_items_config = []
    for price in prices:
        price_str = num_to_str(micro_amount_to_num(price))

        # Autogenerate the line item name.
        line_item_name = u'{bidder_code}: HB ${price}'.format(
            bidder_code=bidder_code, price=price_str)

        # The DFP targeting value ID for this `hb_pb` price value.
        hb_pb_value_id = HBPBValueGetter.get_value_id(price_str)

        # Add the prebid key and price to the criteria
        hb_criteria[hb_pb_key_id] = hb_pb_value_id

        # Create the line item config
        config = dfp.create_line_items.create_line_item_config(
            name=line_item_name,
            order_id=order_id,
            placement_ids=placement_ids,
            ad_unit_ids=ad_unit_ids,
            cpm_micro_amount=price,
            sizes=sizes,
            hb_criteria=hb_criteria,
            currency_code=currency_code,
            creative_template_id=creative_template_id,
        )

        line_items_config.append(config)

    return line_items_config
def create_line_item_configs(prices, order_id, placement_ids, ad_unit_ids,
                             bidder_code, sizes, key_gen_obj, currency_code):
    """
  Create a line item config for each price bucket.

  Args:
    prices (array)
    order_id (int)
    placement_ids (arr)
    ad_unit_ids (arr)
    bidder_code (str)
    sizes (arr)
    key_gen_obj (obj)
    currency_code (str)
  Returns:
    an array of objects: the array of DFP line item configurations
  """

    # The DFP targeting value ID for this `hb_bidder` code.
    key_gen_obj.set_bidder_value(bidder_code)

    line_items_config = []
    for price in prices:

        price_str = num_to_str(micro_amount_to_num(price))

        # Autogenerate the line item name.
        line_item_name = u'{bidder_code}: HB ${price}'.format(
            bidder_code=bidder_code, price=price_str)

        # The DFP targeting value ID for this `hb_pb` price value.
        key_gen_obj.set_price_value(price_str)

        config = dfp.create_line_items.create_line_item_config(
            name=line_item_name,
            order_id=order_id,
            placement_ids=placement_ids,
            ad_unit_ids=ad_unit_ids,
            cpm_micro_amount=price,
            sizes=sizes,
            key_gen_obj=key_gen_obj,
            currency_code=currency_code,
        )

        line_items_config.append(config)

    return line_items_config
def create_line_item_configs(prices,
                             order_id,
                             placement_ids,
                             bidder_code,
                             sizes,
                             key_gen_obj,
                             lineitem_type,
                             lineitem_prefix,
                             currency_code,
                             custom_targeting,
                             creative_type,
                             creative_template_ids,
                             ad_unit_ids=None,
                             same_adv_exception=False,
                             device_category_ids=None,
                             device_capability_ids=None,
                             roadblock_type='ONE_OR_MORE'):
    """
  Create a line item config for each price bucket.

  Args:
    prices (array)
    order_id (int)
    placement_ids (arr)
    bidder_code (str or arr)
    sizes (arr)
    key_gen_obj (obj)
    lineitem_type (str)
    lineitem_prefix (str)
    currency_code (str)
    custom_targeting (arr)
    creative_type (str)
    creative_template_ids (arr)
    ad_unit_ids (arr)
    same_adv_exception(bool)
    device_category_ids (int)
    device_capability_ids (int)
    roadblock_type (str)
  Returns:
    an array of objects: the array of DFP line item configurations
  """

    key_gen_obj.set_creative_type(creative_type)

    # Set DFP custom targeting for key `pwtpid` based on bidder code
    key_gen_obj.set_bidder_value(bidder_code)

    # Set DFP targeting for custom targetting passed in settings.py
    key_gen_obj.set_custom_targeting(custom_targeting)

    #do not set platform targeting for inapp,jwplayer
    if creative_type not in (constant.JW_PLAYER):
        enable_pwtplt = getattr(settings, 'ENABLE_PWTPLT', True)
        if enable_pwtplt:
            key_gen_obj.set_platform_targetting()

    if creative_type is constant.JW_PLAYER:
        key_gen_obj.set_jwplayer_key()

    line_items_config = []

    #create line item config for each price
    for price in prices:

        price_str = num_to_str(price['rate'], precision=3)

        # Remove trailing zero if exists
        if re.match("\d+\.\d{2}0", price_str):
            price_str = price_str[0:-1]

        bidder_str = bidder_code
        if bidder_str == None:
            bidder_str = "All"
        elif isinstance(bidder_str, (list, tuple)):
            bidder_str = "_".join(bidder_str)

        # Autogenerate the line item name. (prefix_rate)
        line_item_name = '{}_{}'.format(lineitem_prefix, price_str)

        # Set DFP custom targeting for key `pwtecp`
        key_gen_obj.set_price_value(price)

        config = dfp.create_line_items.create_line_item_config(
            name=line_item_name,
            order_id=order_id,
            placement_ids=placement_ids,
            cpm_micro_amount=num_to_micro_amount(round(price['rate'], 2)),
            sizes=sizes,
            key_gen_obj=key_gen_obj,
            lineitem_type=lineitem_type,
            currency_code=currency_code,
            creative_type=creative_type,
            creative_template_ids=creative_template_ids,
            ad_unit_ids=ad_unit_ids,
            same_adv_exception=same_adv_exception,
            device_categories=device_category_ids,
            device_capabilities=device_capability_ids,
            roadblock_type=roadblock_type)

        line_items_config.append(config)

    return line_items_config
Пример #6
0
def create_line_item_configs(prices, order_id, use_placements, placement_ids, ad_unit_ids,
  bidder_code, sizes, hb_bidder_key_id, hb_pb_key_id, hb_size_key_id, currency_code, HBBidderValueGetter,
  HBPBValueGetter, HBSizeValueGetter):
  """
  Create a line item config for each price bucket.

  Args:
    prices (array)
    order_id (int)
    use_placements (bool)
    placement_ids (arr)
    ad_unit_ids (arr)
    bidder_code (str)
    hb_bidder_key_id (int)
    hb_pb_key_id (int)
    hb_size_key_id (int)
    currency_code (str)
    HBBidderValueGetter (DFPValueIdGetter)
    HBPBValueGetter (DFPValueIdGetter)
    HBSizeValueGetter (DFPValueIdGetter)
  Returns:
    an array of objects: the array of DFP line item configurations
  """

  # The DFP targeting value ID for this `hb_bidder` code.
  hb_bidder_value_id = HBBidderValueGetter.get_value_id(bidder_code)

  line_items_config = []
  for price in prices:

    price_str = num_to_str(micro_amount_to_num(price))

    # Autogenerate the line item name.
    line_item_name = ''
    
    if getattr(settings, 'DFP_LINE_ITEM_PREFIX', None) is not None and settings.DFP_LINE_ITEM_PREFIX != '':
      line_item_name=u'{prefix}{price}'.format(
        prefix=settings.DFP_LINE_ITEM_PREFIX,
        price=price_str
      )
    else:
      line_item_name=u'{bidder_code}: HB ${price}'.format(
        bidder_code=bidder_code,
        price=price_str
      )

    # The DFP targeting value ID for this `hb_pb` price value.
    hb_pb_value_id = HBPBValueGetter.get_value_id(price_str)

    # The DFP targeting value ID for this `hb_size` price value.
    hb_size_value_ids = [HBSizeValueGetter.get_value_id(str(size['width'])+'x'+str(size['height'])) for size in sizes]

    config = dfp.create_line_items.create_line_item_config(
      name=line_item_name,
      order_id=order_id,
      use_placements=use_placements,
      placement_ids=placement_ids,
      ad_unit_ids=ad_unit_ids,
      cpm_micro_amount=price,
      sizes=sizes,
      hb_bidder_key_id=hb_bidder_key_id,
      hb_pb_key_id=hb_pb_key_id,
      hb_bidder_value_id=hb_bidder_value_id,
      hb_pb_value_id=hb_pb_value_id,
      hb_size_key_id=hb_size_key_id,
      hb_size_value_ids=hb_size_value_ids,
      currency_code=currency_code,
    )

    line_items_config.append(config)

  return line_items_config
Пример #7
0
def create_line_item_configs(prices, order_id, placement_ids, bidder_code,
                             sizes, hb_bidder_key_id, hb_pb_key_id,
                             currency_code, HBBidderValueGetter,
                             HBPBValueGetter):
    """
  Create a line item config for each price bucket.

  Args:
    prices (array)
    order_id (int)
    placement_ids (arr)
    bidder_code (str)
    hb_bidder_key_id (int)
    hb_pb_key_id (int)
    currency_code (str)
    HBBidderValueGetter (DFPValueIdGetter)
    HBPBValueGetter (DFPValueIdGetter)
  Returns:
    an array of objects: the array of DFP line item configurations
  """

    # The DFP targeting value ID for this `hb_bidder` code.
    hb_bidder_value_id = HBBidderValueGetter.get_value_id(bidder_code)

    line_items_config = []
    root_ad_unit_id = None

    if not placement_ids:
        # Since the placement ids array is empty, it means we should target a run of network for the line item
        root_ad_unit_id = dfp.get_ad_units.get_root_ad_unit_id()

        if root_ad_unit_id is None:
            raise DFPObjectNotFound(
                'Could not find the root ad unit to target a run of network.')

    for price in prices:

        price_str = num_to_str(micro_amount_to_num(price))

        # Autogenerate the line item name.
        line_item_name = u'{bidder_code}: HB ${price}'.format(
            bidder_code=bidder_code, price=price_str)

        # The DFP targeting value ID for this `hb_pb` price value.
        hb_pb_value_id = HBPBValueGetter.get_value_id(price_str)

        config = dfp.create_line_items.create_line_item_config(
            name=line_item_name,
            order_id=order_id,
            placement_ids=placement_ids,
            cpm_micro_amount=price,
            sizes=sizes,
            hb_bidder_key_id=hb_bidder_key_id,
            hb_pb_key_id=hb_pb_key_id,
            hb_bidder_value_id=hb_bidder_value_id,
            hb_pb_value_id=hb_pb_value_id,
            currency_code=currency_code,
            root_ad_unit_id=root_ad_unit_id)

        line_items_config.append(config)

    return line_items_config
def create_line_item_configs(prices, order_id, placement_ids, ad_unit_ids,
                             bidder_code, sizes, hb_bidder_key_id,
                             hb_pb_key_id, currency_code, HBBidderValueGetter,
                             HBPBValueGetter):
    """
  Create a line item config for each price bucket.

  Args:
    prices (array)
    order_id (int)
    placement_ids (arr)
    ad_unit_ids (arr)
    bidder_code (str)
    hb_bidder_key_id (int)
    hb_pb_key_id (int)
    currency_code (str)
    HBBidderValueGetter (DFPValueIdGetter)
    HBPBValueGetter (DFPValueIdGetter)
  Returns:
    an array of objects: the array of DFP line item configurations
  """

    # The DFP targeting value ID for this `hb_bidder` code.
    hb_bidder_value_id = HBBidderValueGetter.get_value_id(
        bidder_code) if bidder_code else None

    line_items_config = []
    for price in prices:

        price_str = num_to_str(micro_amount_to_num(price))

        # Autogenerate the line item name.
        line_item_name = u'{bidder_code}: HB ${price}'.format(
            bidder_code=bidder_code if bidder_code else 'all_bidders',
            price=price_str)

        # If the settings allow modifying an existing order, do so. Otherwise,
        # throw an exception.
        skip_existing_line_items = getattr(settings,
                                           'DFP_SKIP_EXISTING_LINE_ITEMS',
                                           False)
        if skip_existing_line_items:
            existing_line_item_names = dfp.get_line_items.get_line_item_names_by_order_id(
                order_id)
        else:
            existing_line_item_names = []

        if line_item_name in existing_line_item_names:
            continue

        # The DFP targeting value ID for this `hb_pb` price value.
        hb_pb_value_id = HBPBValueGetter.get_value_id(price_str)

        config = dfp.create_line_items.create_line_item_config(
            name=line_item_name,
            order_id=order_id,
            placement_ids=placement_ids,
            ad_unit_ids=ad_unit_ids,
            cpm_micro_amount=price,
            sizes=sizes,
            hb_bidder_key_id=hb_bidder_key_id,
            hb_pb_key_id=hb_pb_key_id,
            hb_bidder_value_id=hb_bidder_value_id,
            hb_pb_value_id=hb_pb_value_id,
            currency_code=currency_code)

        line_items_config.append(config)

    return line_items_config