def test_get_prices_summary_string(self): """ It returns the expected string summary of the array. """ self.assertEqual( get_prices_summary_string([ 0, 500000, 1000000, 1500000, 2000000, 2500000, 3000000, 3500000, 4000000, 4500000, 5000000 ]), '0.00, 0.50, 1.00, ... 4.00, 4.50, 5.00') # Short array self.assertEqual( get_prices_summary_string([3200000, 3300000, 3400000, 3500000]), '3.20, 3.30, 3.40, 3.50') # Different precision self.assertEqual( get_prices_summary_string([8220000, 8060000, 8427100, 8000000], precision=4), '8.2200, 8.0600, 8.4271, 8.0000')
def main(): """ Validate the settings and ask for confirmation from the user. Then, start all necessary DFP tasks. """ user_email = getattr(settings, 'DFP_USER_EMAIL_ADDRESS', None) if user_email is None: raise MissingSettingException('DFP_USER_EMAIL_ADDRESS') advertiser_name = getattr(settings, 'DFP_ADVERTISER_NAME', None) if advertiser_name is None: raise MissingSettingException('DFP_ADVERTISER_NAME') order_name = getattr(settings, 'DFP_ORDER_NAME', None) if order_name is None: raise MissingSettingException('DFP_ORDER_NAME') placements = getattr(settings, 'DFP_TARGETED_PLACEMENT_NAMES', None) ad_units = getattr(settings, 'DFP_TARGETED_AD_UNIT_NAMES', None) video_ad_type = getattr(settings, 'DFP_VIDEO_AD_TYPE', False) vast_redirect_url = getattr(settings, 'DFP_VAST_REDIRECT_URL', '') if video_ad_type is True and len(vast_redirect_url) < 1: raise BadSettingException( 'When setting "DFP_VIDEO_AD_TYPE" to "True", please also set "DFP_VAST_REDIRECT_URL".' ) if ad_units is None and placements is None: raise MissingSettingException( 'DFP_TARGETED_PLACEMENT_NAMES or DFP_TARGETED_AD_UNIT_NAMES') elif (placements is None or len(placements) < 1) and (ad_units is None or len(ad_units) < 1): raise BadSettingException( 'The setting "DFP_TARGETED_PLACEMENT_NAMES" or "DFP_TARGETED_AD_UNIT_NAMES" ' 'must contain at least one DFP placement or ad unit.') sizes = getattr(settings, 'DFP_PLACEMENT_SIZES', None) if sizes is None: raise MissingSettingException('DFP_PLACEMENT_SIZES') elif len(sizes) < 1: raise BadSettingException('The setting "DFP_PLACEMENT_SIZES" ' 'must contain at least one size object.') currency_code = getattr(settings, 'DFP_CURRENCY_CODE', 'USD') line_item_format = getattr(settings, 'DFP_LINE_ITEM_FORMAT', u'{bidder_code}: HB ${price}') # How many creatives to attach to each line item. We need at least one # creative per ad unit on a page. See: # https://github.com/kmjennison/dfp-prebid-setup/issues/13 num_creatives = (getattr(settings, 'DFP_NUM_CREATIVES_PER_LINE_ITEM', None) or len(placements) + len(ad_units)) bidder_code = getattr(settings, 'PREBID_BIDDER_CODE', None) if bidder_code is None: raise MissingSettingException('PREBID_BIDDER_CODE') price_buckets = getattr(settings, 'PREBID_PRICE_BUCKETS', None) if price_buckets is None: raise MissingSettingException('PREBID_PRICE_BUCKETS') check_price_buckets_validity(price_buckets) prices = get_prices_array(price_buckets) prices_summary = get_prices_summary_string(prices) logger.info(u""" Going to create {name_start_format}{num_line_items}{format_end} new line items. {name_start_format}Order{format_end}: {value_start_format}{order_name}{format_end} {name_start_format}Advertiser{format_end}: {value_start_format}{advertiser}{format_end} Line items will have targeting: {name_start_format}hb_pb{format_end} = {value_start_format}{prices_summary}{format_end} {name_start_format}hb_bidder{format_end} = {value_start_format}{bidder_code}{format_end} {name_start_format}placements{format_end} = {value_start_format}{placements}{format_end} {name_start_format}ad units{format_end} = {value_start_format}{ad_units}{format_end} """.format( num_line_items=len(prices), order_name=order_name, advertiser=advertiser_name, user_email=user_email, prices_summary=prices_summary, bidder_code=bidder_code, placements=placements, ad_units=ad_units, sizes=sizes, name_start_format=color.BOLD, format_end=color.END, value_start_format=color.BLUE, )) if video_ad_type: logger.info( u""" Line items will have VAST redirect creatives with redirect URL: {value_start_format}{redirect_url}{format_end} """.format( redirect_url=vast_redirect_url, value_start_format=color.BLUE, format_end=color.END, )) else: logger.info( u""" Line items will have third party creatives based on snippet.html content. """) ok = input('Is this correct? (y/n)\n') if ok != 'y': logger.info('Exiting.') return setup_partner(user_email, advertiser_name, order_name, placements, ad_units, sizes, bidder_code, prices, num_creatives, currency_code, line_item_format, video_ad_type, vast_redirect_url)
def main(): """ Validate the settings and ask for confirmation from the user. Then, start all necessary DFP tasks. """ user_email = getattr(settings, 'DFP_USER_EMAIL_ADDRESS', None) if user_email is None: raise MissingSettingException('DFP_USER_EMAIL_ADDRESS') advertiser_name = getattr(settings, 'DFP_ADVERTISER_NAME', None) if advertiser_name is None: raise MissingSettingException('DFP_ADVERTISER_NAME') order_name = getattr(settings, 'DFP_ORDER_NAME', None) if order_name is None: raise MissingSettingException('DFP_ORDER_NAME') placements = getattr(settings, 'DFP_TARGETED_PLACEMENT_NAMES', None) if placements is None: raise MissingSettingException('DFP_TARGETED_PLACEMENT_NAMES') elif len(placements) < 1: raise BadSettingException( 'The setting "DFP_TARGETED_PLACEMENT_NAMES" ' 'must contain at least one DFP placement ID.') sizes = getattr(settings, 'DFP_PLACEMENT_SIZES', None) if sizes is None: raise MissingSettingException('DFP_PLACEMENT_SIZES') elif len(sizes) < 1: raise BadSettingException('The setting "DFP_PLACEMENT_SIZES" ' 'must contain at least one size object.') currency_code = getattr(settings, 'DFP_CURRENCY_CODE', 'USD') # How many creatives to attach to each line item. We need at least one # creative per ad unit on a page. See: # https://github.com/kmjennison/dfp-prebid-setup/issues/13 num_creatives = (getattr(settings, 'DFP_NUM_CREATIVES_PER_LINE_ITEM', None) or len(placements)) bidder_code = getattr(settings, 'PREBID_BIDDER_CODE', None) if bidder_code is None: raise MissingSettingException('PREBID_BIDDER_CODE') price_buckets = getattr(settings, 'PREBID_PRICE_BUCKETS', None) if price_buckets is None: raise MissingSettingException('PREBID_PRICE_BUCKETS') check_price_buckets_validity(price_buckets) prices = get_prices_array(price_buckets) prices_summary = get_prices_summary_string(prices, price_buckets['precision']) logger.info(u""" Going to create {name_start_format}{num_line_items}{format_end} new line items. {name_start_format}Order{format_end}: {value_start_format}{order_name}{format_end} {name_start_format}Advertiser{format_end}: {value_start_format}{advertiser}{format_end} Line items will have targeting: {name_start_format}hb_pb{format_end} = {value_start_format}{prices_summary}{format_end} {name_start_format}hb_bidder{format_end} = {value_start_format}{bidder_code}{format_end} {name_start_format}placements{format_end} = {value_start_format}{placements}{format_end} """.format( num_line_items=len(prices), order_name=order_name, advertiser=advertiser_name, user_email=user_email, prices_summary=prices_summary, bidder_code=bidder_code, placements=placements, sizes=sizes, name_start_format=color.BOLD, format_end=color.END, value_start_format=color.BLUE, )) ok = input('Is this correct? (y/n)\n') if ok != 'y': logger.info('Exiting.') return setup_partner( user_email, advertiser_name, order_name, placements, sizes, bidder_code, prices, num_creatives, currency_code, )
def main(): """ Validate the settings and ask for confirmation from the user. Then, start all necessary DFP tasks. """ user_email = getattr(settings, 'DFP_USER_EMAIL_ADDRESS', None) if user_email is None: raise MissingSettingException('DFP_USER_EMAIL_ADDRESS') advertiser_name = getattr(settings, 'DFP_ADVERTISER_NAME', None) if advertiser_name is None: raise MissingSettingException('DFP_ADVERTISER_NAME') order_name = getattr(settings, 'DFP_ORDER_NAME', None) if order_name is None: raise MissingSettingException('DFP_ORDER_NAME') placements = getattr(settings, 'DFP_TARGETED_PLACEMENT_NAMES', None) no_inventory = getattr(settings, 'DFP_ALLOW_NO_INVENTORY_TARGETING', None) if placements is None: raise MissingSettingException('DFP_TARGETED_PLACEMENT_NAMES') elif len(placements) < 1 and no_inventory is not True: raise BadSettingException( 'The setting "DFP_TARGETED_PLACEMENT_NAMES" ' 'must contain at least one DFP placement ID.') sizes = getattr(settings, 'DFP_PLACEMENT_SIZES', None) if sizes is None: raise MissingSettingException('DFP_PLACEMENT_SIZES') elif len(sizes) < 1: raise BadSettingException('The setting "DFP_PLACEMENT_SIZES" ' 'must contain at least one size object.') currency_code = getattr(settings, 'DFP_CURRENCY_CODE', 'USD') # How many creatives to attach to each line item. We need at least one # creative per ad unit on a page. See: # https://github.com/kmjennison/dfp-prebid-setup/issues/13 num_creatives = (getattr(settings, 'DFP_NUM_CREATIVES_PER_LINE_ITEM', None) or len(placements)) # In the case where no inventory is being used, make sure at least one # creative is created if not num_creatives > 0: num_creatives = 1 bidder_code = getattr(settings, 'PREBID_BIDDER_CODE', None) if bidder_code is None: raise MissingSettingException('PREBID_BIDDER_CODE') bidder_params = getattr(settings, 'PREBID_BIDDER_PARAMS', None) hb_pb_key = 'hb_pb' additional_keys = '' if bidder_params is True: hb_pb_key += '_' + bidder_code hb_adid_key = 'hb_adid_' + bidder_code hb_size_key = 'hb_size_' + bidder_code if len(hb_pb_key) > 20: hb_pb_key = hb_pb_key[:20] if len(hb_adid_key) > 20: hb_adid_key = hb_adid_key[:20] if len(hb_size_key) > 20: hb_size_key = hb_size_key[:20] additional_keys = u""" Additionally, keys {name_start_format}{hb_adid_key}{format_end} and {name_start_format}{hb_size_key}{format_end} will be created.""".format( hb_adid_key=hb_adid_key, hb_size_key=hb_size_key, name_start_format=color.BOLD, format_end=color.END, ) price_buckets = getattr(settings, 'PREBID_PRICE_BUCKETS', None) if price_buckets is None: raise MissingSettingException('PREBID_PRICE_BUCKETS') check_price_buckets_validity(price_buckets) prices = get_prices_array(price_buckets) prices_summary = get_prices_summary_string(prices, price_buckets['precision']) logger.info(u""" Going to create {name_start_format}{num_line_items}{format_end} new line items. {name_start_format}Order{format_end}: {value_start_format}{order_name}{format_end} {name_start_format}Advertiser{format_end}: {value_start_format}{advertiser}{format_end} {name_start_format}Owner{format_end}: {value_start_format}{user_email}{format_end} Line items will have targeting: {name_start_format}{hb_pb_key}{format_end} = {value_start_format}{prices_summary}{format_end} {name_start_format}hb_bidder{format_end} = {value_start_format}{bidder_code}{format_end} {name_start_format}placements{format_end} = {value_start_format}{placements}{format_end}{additional_keys} """.format(num_line_items=len(prices), order_name=order_name, advertiser=advertiser_name, user_email=user_email, hb_pb_key=hb_pb_key, prices_summary=prices_summary, bidder_code=bidder_code, placements=placements, sizes=sizes, name_start_format=color.BOLD, format_end=color.END, value_start_format=color.BLUE, additional_keys=additional_keys)) ok = input('Is this correct? (y/n)\n') if ok != 'y': logger.info('Exiting.') return setup_partner( user_email, advertiser_name, order_name, placements, sizes, bidder_code, prices, num_creatives, currency_code, )
def main(): """ Validate the settings and ask for confirmation from the user. Then, start all necessary DFP tasks. """ user_email = getattr(settings, 'DFP_USER_EMAIL_ADDRESS', None) if user_email is None: raise MissingSettingException('DFP_USER_EMAIL_ADDRESS') advertiser_name = getattr(settings, 'DFP_ADVERTISER_NAME', None) if advertiser_name is None: raise MissingSettingException('DFP_ADVERTISER_NAME') order_name = getattr(settings, 'DFP_ORDER_NAME', None) if order_name is None: raise MissingSettingException('DFP_ORDER_NAME') placements = getattr(settings, 'DFP_TARGETED_PLACEMENT_NAMES', []) ad_units = getattr(settings, 'DFP_TARGETED_AD_UNIT_NAMES', []) if not ad_units and not placements: raise MissingSettingException( 'DFP_TARGETED_PLACEMENT_NAMES or DFP_TARGETED_AD_UNIT_NAMES') elif (not placements or len(placements) < 1) and (not ad_units or len(ad_units) < 1): raise BadSettingException( 'The setting "DFP_TARGETED_PLACEMENT_NAMES" or "DFP_TARGETED_AD_UNIT_NAMES" ' 'must contain at least one DFP placement or ad unit.') sizes = getattr(settings, 'DFP_PLACEMENT_SIZES', None) if sizes is None: raise MissingSettingException('DFP_PLACEMENT_SIZES') elif len(sizes) < 1: raise BadSettingException('The setting "DFP_PLACEMENT_SIZES" ' 'must contain at least one size object.') currency_code = getattr(settings, 'DFP_CURRENCY_CODE', 'USD') # How many creatives to attach to each line item. We need at least one # creative per ad unit on a page. See: # https://github.com/kmjennison/dfp-prebid-setup/issues/13 num_creatives = (getattr(settings, 'DFP_NUM_CREATIVES_PER_LINE_ITEM', None) or len(placements)) bidder_code = getattr(settings, 'PREBID_BIDDER_CODE', None) if bidder_code is None: logger.warning( 'PREBID_BIDDER_CODE is not specified. Create one set of line items for all bidders.' ) price_bucket_list = getattr(settings, 'PREBID_PRICE_BUCKETS', None) if price_bucket_list is None: raise MissingSettingException('PREBID_PRICE_BUCKETS') price_multipliers = getattr(settings, 'PRICE_MULTIPLIERS', {}) price_multiplier = price_multipliers.get(currency_code, 1) # Validate price buckets and adjust by price_multiplier adjusted_price_buckets = [] for price_bucket in price_bucket_list: check_price_buckets_validity(price_bucket) adjusted_price_buckets.append( adjust_price_bucket_by_price_multiplier(price_bucket, price_multiplier)) prices = get_prices_array_from_price_bucket_list(adjusted_price_buckets) prices_summary = get_prices_summary_string( prices, price_bucket_list[0]['precision']) logger.info(u""" Going to create {name_start_format}{num_line_items}{format_end} new line items. {name_start_format}Order{format_end}: {value_start_format}{order_name}{format_end} {name_start_format}Advertiser{format_end}: {value_start_format}{advertiser}{format_end} Line items will have targeting: {name_start_format}hb_pb{format_end} = {value_start_format}{prices_summary}{format_end} {name_start_format}hb_bidder{format_end} = {value_start_format}{bidder_code}{format_end} {name_start_format}placements{format_end} = {value_start_format}{placements}{format_end} {name_start_format}ad units{format_end} = {value_start_format}{ad_units}{format_end} """.format( num_line_items=len(prices), order_name=order_name, advertiser=advertiser_name, user_email=user_email, prices_summary=prices_summary, bidder_code=bidder_code or '(Targeting all bidders)', placements=placements, ad_units=ad_units, sizes=sizes, name_start_format=color.BOLD, format_end=color.END, value_start_format=color.BLUE, )) ok = input('Is thisy correct? (y/n)\n') if ok != 'y': logger.info('Exiting.') return setup_partner(user_email, advertiser_name, order_name, placements, ad_units, sizes, bidder_code, prices, num_creatives, currency_code)