Skip to content

Commit

Permalink
Fix fixedBid issue
Browse files Browse the repository at this point in the history
  • Loading branch information
FAREAST\baliu committed May 15, 2017
1 parent 35b1595 commit fc12893
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 66 deletions.
58 changes: 29 additions & 29 deletions bingads/v11/bulk/entities/bulk_ad_group_dynamic_search_ad_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class BulkAdGroupDynamicSearchAdTarget(_SingleRecordBulkEntity):
""" Represents a Ad Group Criterion that can be read or written in a bulk file.
This class exposes the :attr:`ad_group_criterion` property that can be read and written as fields of the
This class exposes the :attr:`biddable_ad_group_criterion` property that can be read and written as fields of the
Ad Group Dynamic Search Ad Target record in a bulk file.
For more information, see Ad Group Dynamic Search Ad Target at https://go.microsoft.com/fwlink/?linkid=836837.
Expand All @@ -26,13 +26,13 @@ def __init__(self,
campaign_name=None,
ad_group_name=None,
status=None,
ad_group_criterion=None):
biddable_ad_group_criterion=None):
super(BulkAdGroupDynamicSearchAdTarget, self).__init__()

self._campaign_name = campaign_name
self._ad_group_name = ad_group_name
self._status = status
self._ad_group_criterion = ad_group_criterion
self._biddable_ad_group_criterion = biddable_ad_group_criterion
self._performance_data = None


Expand All @@ -44,13 +44,13 @@ def __init__(self,
),
_SimpleBulkMapping(
header=_StringTable.Id,
field_to_csv=lambda c: bulk_str(c.ad_group_criterion.Id),
csv_to_field=lambda c, v: setattr(c.ad_group_criterion, 'Id', int(v) if v else None)
field_to_csv=lambda c: bulk_str(c.biddable_ad_group_criterion.Id),
csv_to_field=lambda c, v: setattr(c.biddable_ad_group_criterion, 'Id', int(v) if v else None)
),
_SimpleBulkMapping(
header=_StringTable.ParentId,
field_to_csv=lambda c: bulk_str(c.ad_group_criterion.AdGroupId),
csv_to_field=lambda c, v: setattr(c.ad_group_criterion, 'AdGroupId', int(v) if v else None)
field_to_csv=lambda c: bulk_str(c.biddable_ad_group_criterion.AdGroupId),
csv_to_field=lambda c, v: setattr(c.biddable_ad_group_criterion, 'AdGroupId', int(v) if v else None)
),
_SimpleBulkMapping(
header=_StringTable.Campaign,
Expand All @@ -64,27 +64,27 @@ def __init__(self,
),
_SimpleBulkMapping(
header=_StringTable.Bid,
field_to_csv=lambda c: ad_group_bid_bulk_str(c.ad_group_criterion.CriterionBid.Amount),
csv_to_field=lambda c, v: setattr(c.ad_group_criterion.CriterionBid, 'Amount', parse_ad_group_bid(v))
field_to_csv=lambda c: fixed_bid_bulk_str(c.biddable_ad_group_criterion.CriterionBid),
csv_to_field=lambda c, v: setattr(c.biddable_ad_group_criterion, 'CriterionBid', parse_fixed_bid(v))
),
_SimpleBulkMapping(
header=_StringTable.Name,
field_to_csv=lambda c: field_to_csv_WebpageParameter_CriterionName(c.ad_group_criterion),
csv_to_field=lambda c, v: csv_to_field_WebpageParameter_CriterionName(c.ad_group_criterion, v)
field_to_csv=lambda c: field_to_csv_WebpageParameter_CriterionName(c.biddable_ad_group_criterion),
csv_to_field=lambda c, v: csv_to_field_WebpageParameter_CriterionName(c.biddable_ad_group_criterion, v)
),
_ComplexBulkMapping(
entity_to_csv=lambda c, v: entity_to_csv_DSAWebpageParameter(c.ad_group_criterion, v),
csv_to_entity=lambda v, c: csv_to_entity_DSAWebpageParameter(v, c.ad_group_criterion)
entity_to_csv=lambda c, v: entity_to_csv_DSAWebpageParameter(c.biddable_ad_group_criterion, v),
csv_to_entity=lambda v, c: csv_to_entity_DSAWebpageParameter(v, c.biddable_ad_group_criterion)
),
_SimpleBulkMapping(
header=_StringTable.TrackingTemplate,
field_to_csv=lambda c: bulk_optional_str(c.ad_group_criterion.TrackingUrlTemplate),
csv_to_field=lambda c, v: setattr(c.ad_group_criterion, 'TrackingUrlTemplate', v if v else None)
field_to_csv=lambda c: bulk_optional_str(c.biddable_ad_group_criterion.TrackingUrlTemplate),
csv_to_field=lambda c, v: setattr(c.biddable_ad_group_criterion, 'TrackingUrlTemplate', v if v else None)
),
_SimpleBulkMapping(
header=_StringTable.CustomParameter,
field_to_csv=lambda c: field_to_csv_UrlCustomParameters(c.ad_group_criterion),
csv_to_field=lambda c, v: csv_to_field_UrlCustomParameters(c.ad_group_criterion, v)
field_to_csv=lambda c: field_to_csv_UrlCustomParameters(c.biddable_ad_group_criterion),
csv_to_field=lambda c, v: csv_to_field_UrlCustomParameters(c.biddable_ad_group_criterion, v)
),
]

Expand Down Expand Up @@ -132,33 +132,33 @@ def status(self, status):
self._status = status

@property
def ad_group_criterion(self):
def biddable_ad_group_criterion(self):
""" Defines a Ad Group Criterion """

return self._ad_group_criterion
return self._biddable_ad_group_criterion

@ad_group_criterion.setter
def ad_group_criterion(self, ad_group_criterion):
self._ad_group_criterion = ad_group_criterion
@biddable_ad_group_criterion.setter
def biddable_ad_group_criterion(self, biddable_ad_group_criterion):
self._biddable_ad_group_criterion = biddable_ad_group_criterion

@property
def performance_data(self):
return self._performance_data

def process_mappings_from_row_values(self, row_values):
self._ad_group_criterion = _CAMPAIGN_OBJECT_FACTORY_V11.create('BiddableAdGroupCriterion')
self._ad_group_criterion.Type = 'BiddableAdGroupCriterion'
self._ad_group_criterion.Criterion = _CAMPAIGN_OBJECT_FACTORY_V11.create('ns0:Webpage')
self._ad_group_criterion.Criterion.Type = 'Webpage'
self._ad_group_criterion.CriterionBid = _CAMPAIGN_OBJECT_FACTORY_V11.create('FixedBid')
self._ad_group_criterion.CriterionBid.Type = 'FixedBid'
self._biddable_ad_group_criterion = _CAMPAIGN_OBJECT_FACTORY_V11.create('BiddableAdGroupCriterion')
self._biddable_ad_group_criterion.Type = 'BiddableAdGroupCriterion'
self._biddable_ad_group_criterion.Criterion = _CAMPAIGN_OBJECT_FACTORY_V11.create('ns0:Webpage')
self._biddable_ad_group_criterion.Criterion.Type = 'Webpage'
self._biddable_ad_group_criterion.CriterionBid = _CAMPAIGN_OBJECT_FACTORY_V11.create('FixedBid')
self._biddable_ad_group_criterion.CriterionBid.Type = 'FixedBid'

row_values.convert_to_entity(self, BulkAdGroupDynamicSearchAdTarget._MAPPINGS)

self._performance_data = PerformanceData.read_from_row_values_or_null(row_values)

def process_mappings_to_row_values(self, row_values, exclude_readonly_data):
self._validate_property_not_null(self.ad_group_criterion, 'ad_group_criterion')
self._validate_property_not_null(self.biddable_ad_group_criterion, 'biddable_ad_group_criterion')
self.convert_to_values(row_values, BulkAdGroupDynamicSearchAdTarget._MAPPINGS)

PerformanceData.write_to_row_values_if_not_null(self._performance_data, row_values)
Expand Down
8 changes: 3 additions & 5 deletions bingads/v11/bulk/entities/bulk_ad_group_product_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,13 @@ def _read_is_excluded(cls, entity, row_value):
def _write_bid(cls, entity):
criterion = entity.ad_group_criterion
if isinstance(criterion, _BiddableAdGroupCriterion) and \
criterion.CriterionBid is not None and hasattr(criterion.CriterionBid, 'Bid'):
return ad_group_bid_bulk_str(entity.ad_group_criterion.CriterionBid.Bid)
else:
return None
criterion.CriterionBid is not None:
return fixed_bid_bulk_str(entity.ad_group_criterion.CriterionBid)

@classmethod
def _read_bid(cls, entity, row_value):
if isinstance(entity.ad_group_criterion, _BiddableAdGroupCriterion):
entity.ad_group_criterion.CriterionBid.Bid = parse_ad_group_bid(row_value)
entity.ad_group_criterion.CriterionBid = parse_fixed_bid(row_value)
else:
pass

Expand Down
2 changes: 1 addition & 1 deletion bingads/v11/bulk/entities/bulk_campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _read_sales_country_code(c, v):
field_to_csv=lambda c: c.campaign.TimeZone,
csv_to_field=lambda c, v: setattr(c.campaign, 'TimeZone', v)
),
_ComplexBulkMapping(budget_to_csv_v11, csv_to_budget_v11),
_ComplexBulkMapping(budget_to_csv, csv_to_budget),
_SimpleBulkMapping(
header=_StringTable.BidAdjustment,
field_to_csv=lambda c: bulk_str(c.campaign.NativeBidAdjustment),
Expand Down
43 changes: 12 additions & 31 deletions bingads/v11/internal/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def _is_daily_budget(budget_type):
else:
return False


def csv_to_budget(row_values, bulk_campaign):
success, budget_type = row_values.try_get_value(_StringTable.BudgetType)
if not success or not budget_type:
Expand All @@ -92,40 +91,11 @@ def csv_to_budget(row_values, bulk_campaign):
return
budget_value = float(budget_row_value) if budget_row_value else None

bulk_campaign.campaign.BudgetType = budget_type
if _is_daily_budget(budget_type):
bulk_campaign.campaign.DailyBudget = budget_value
else:
bulk_campaign.campaign.MonthlyBudget = budget_value


def budget_to_csv(bulk_campaign, row_values):
budget_type = bulk_campaign.campaign.BudgetType
if not budget_type:
return

if _is_daily_budget(str(budget_type)):
row_values[_StringTable.Budget] = bulk_str(bulk_campaign.campaign.DailyBudget)
else:
row_values[_StringTable.Budget] = bulk_str(bulk_campaign.campaign.MonthlyBudget)


# TODO as version specific logic added, consider to separate extensions with ap versions
def csv_to_budget_v11(row_values, bulk_campaign):
success, budget_type = row_values.try_get_value(_StringTable.BudgetType)
if not success or not budget_type:
return

success, budget_row_value = row_values.try_get_value(_StringTable.Budget)
if not success:
return
budget_value = float(budget_row_value) if budget_row_value else None

bulk_campaign.campaign.BudgetType = budget_type
bulk_campaign.campaign.DailyBudget = budget_value


def budget_to_csv_v11(bulk_campaign, row_values):
def budget_to_csv(bulk_campaign, row_values):
budget_type = bulk_campaign.campaign.BudgetType
if not budget_type:
return
Expand Down Expand Up @@ -456,6 +426,17 @@ def minute_bulk_str(value):
else:
raise ValueError('Unknown minute')

def parse_fixed_bid(value):
if not value:
return None
fixed_bid = _CAMPAIGN_OBJECT_FACTORY_V11.create('FixedBid')
fixed_bid.Amount = float(value)
return fixed_bid

def fixed_bid_bulk_str(value):
if value is None or not hasattr(value, 'Amount') or value.Amount is None:
return None
return bulk_str(value.Amount)

def parse_minute(value):
minute_number = int(value)
Expand Down

0 comments on commit fc12893

Please sign in to comment.