예제 #1
0
class BulkCampaignProductScope(_SingleRecordBulkEntity):
    """ Represents a Campaign Criterion that can be read or written in a bulk file.

    This class exposes the :attr:`campaign_criterion` property that can be read and written as fields of the
    Campaign Product Scope record in a bulk file.

    For more information, see Campaign Product Scope at http://go.microsoft.com/fwlink/?LinkId=618643.

    *See also:*

    * :class:`.BulkServiceManager`
    * :class:`.BulkOperation`
    * :class:`.BulkFileReader`
    * :class:`.BulkFileWriter`
    """
    def __init__(self,
                 campaign_name=None,
                 status=None,
                 campaign_criterion=None):
        super(BulkCampaignProductScope, self).__init__()

        self._campaign_name = campaign_name
        self._status = status
        self._campaign_criterion = campaign_criterion

    _MAPPINGS = [
        _SimpleBulkMapping(header=_StringTable.Status,
                           field_to_csv=lambda c: c.status,
                           csv_to_field=lambda c, v: setattr(c, 'status', v)),
        _SimpleBulkMapping(
            header=_StringTable.Id,
            field_to_csv=lambda c: bulk_str(c.campaign_criterion.Id),
            csv_to_field=lambda c, v: setattr(c.campaign_criterion, 'Id',
                                              int(v) if v else None)),
        _SimpleBulkMapping(
            header=_StringTable.ParentId,
            field_to_csv=lambda c: bulk_str(c.campaign_criterion.CampaignId),
            csv_to_field=lambda c, v: setattr(c.campaign_criterion,
                                              'CampaignId', int(v))),
        _SimpleBulkMapping(
            header=_StringTable.Campaign,
            field_to_csv=lambda c: c.campaign_name,
            csv_to_field=lambda c, v: setattr(c, 'campaign_name', v)),
        _ComplexBulkMapping(
            entity_to_csv=lambda c, v: _ProductConditionHelper.
            add_row_values_from_conditions(
                c.campaign_criterion.Criterion.Conditions.ProductCondition, v),
            csv_to_entity=lambda v, c: _ProductConditionHelper.
            add_conditions_from_row_values(
                v, c.campaign_criterion.Criterion.Conditions.ProductCondition))
    ]

    @property
    def campaign_name(self):
        """ The name of the Campaign

        :rtype: str
        """

        return self._campaign_name

    @campaign_name.setter
    def campaign_name(self, campaign_name):
        self._campaign_name = campaign_name

    @property
    def status(self):
        """ The status of the Campaign Criterion

        :rtype: str
        """

        return self._status

    @status.setter
    def status(self, status):
        self._status = status

    @property
    def campaign_criterion(self):
        """ Defines a Campaign Criterion """

        return self._campaign_criterion

    @campaign_criterion.setter
    def campaign_criterion(self, campaign_criterion):
        self._campaign_criterion = campaign_criterion

    def process_mappings_from_row_values(self, row_values):
        self._campaign_criterion = _CAMPAIGN_OBJECT_FACTORY.create(
            'CampaignCriterion')
        self._campaign_criterion.Type = 'CampaignCriterion'
        self._campaign_criterion.Criterion = _CAMPAIGN_OBJECT_FACTORY.create(
            'ProductScope')
        self._campaign_criterion.Criterion.Type = 'ProductScope'

        row_values.convert_to_entity(self, BulkCampaignProductScope._MAPPINGS)

    def process_mappings_to_row_values(self, row_values,
                                       exclude_readonly_data):
        self._validate_property_not_null(self.campaign_criterion,
                                         'campaign_criterion')
        self.convert_to_values(row_values, BulkCampaignProductScope._MAPPINGS)

    def read_additional_data(self, stream_reader):
        super(BulkCampaignProductScope,
              self).read_additional_data(stream_reader)
예제 #2
0
class BulkAdGroupProductTarget(_SingleRecordBulkEntity):
    """ Represents an ad group product target.

    This class exposes the :attr:`biddable_ad_group_criterion` property that can be read and written as fields of the
    Ad Group Product Target record in a bulk file.

    For more information, see Ad Group Product Target at http://go.microsoft.com/fwlink/?LinkID=511550.

    *See also:*

    * :class:`.BulkServiceManager`
    * :class:`.BulkOperation`
    * :class:`.BulkFileReader`
    * :class:`.BulkFileWriter`
    """
    def __init__(
        self,
        biddable_ad_group_criterion=None,
        campaign_name=None,
        ad_group_name=None,
    ):
        super(BulkAdGroupProductTarget, self).__init__()

        self._biddable_ad_group_criterion = biddable_ad_group_criterion
        self._campaign_name = campaign_name
        self._ad_group_name = ad_group_name

    @property
    def biddable_ad_group_criterion(self):
        """ The BiddableAdGroupCriterion Data Object of the Campaign Management Service.

        A subset of BiddableAdGroupCriterion properties are available in the Ad Group Product Target record.
        For more information, see Ad Group Product Target at http://go.microsoft.com/fwlink/?LinkID=511550.

        see BiddableAdGroupCriterion at: https://msdn.microsoft.com/en-us/library/bing-ads-campaign-management-biddableadgroupcriterion.aspx
        """

        return self._biddable_ad_group_criterion

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

    @property
    def campaign_name(self):
        """ The name of the campaign that contains the ad group product target.

        Corresponds to the 'Campaign' field in the bulk file.

        :rtype: str
        """

        return self._campaign_name

    @campaign_name.setter
    def campaign_name(self, value):
        self._campaign_name = value

    @property
    def ad_group_name(self):
        """ The name of the ad group that contains the ad group product target.

        Corresponds to the 'Ad Group' field in the bulk file.

        :rtype: str
        """

        return self._ad_group_name

    @ad_group_name.setter
    def ad_group_name(self, value):
        self._ad_group_name = value

    _MAPPINGS = [
        _SimpleBulkMapping(header=_StringTable.Status,
                           field_to_csv=lambda c: bulk_str(
                               c.biddable_ad_group_criterion.Status),
                           csv_to_field=lambda c, v: setattr(
                               c.biddable_ad_group_criterion, 'Status', v
                               if v else None)),
        _SimpleBulkMapping(
            header=_StringTable.Id,
            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.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,
            field_to_csv=lambda c: c.campaign_name,
            csv_to_field=lambda c, v: setattr(c, '_campaign_name', v)),
        _SimpleBulkMapping(
            header=_StringTable.AdGroup,
            field_to_csv=lambda c: c.ad_group_name,
            csv_to_field=lambda c, v: setattr(c, '_ad_group_name', v)),
        _SimpleBulkMapping(
            header=_StringTable.EditorialStatus,
            field_to_csv=lambda c: bulk_str(c.biddable_ad_group_criterion.
                                            EditorialStatus),
            csv_to_field=lambda c, v: setattr(c.biddable_ad_group_criterion,
                                              'EditorialStatus', v
                                              if v else None)),
        _SimpleBulkMapping(header=_StringTable.Bid,
                           field_to_csv=lambda c: ad_group_bid_bulk_str(
                               c.biddable_ad_group_criterion.CriterionBid.Bid),
                           csv_to_field=lambda c, v: setattr(
                               c.biddable_ad_group_criterion.CriterionBid,
                               'Bid', parse_ad_group_bid(v))),
        _SimpleBulkMapping(
            header=_StringTable.DestinationUrl,
            field_to_csv=lambda c: bulk_optional_str(
                c.biddable_ad_group_criterion.DestinationUrl),
            csv_to_field=lambda c, v: setattr(c.biddable_ad_group_criterion,
                                              'DestinationUrl', v
                                              if v else '')),
        _SimpleBulkMapping(header=_StringTable.Param1,
                           field_to_csv=lambda c: bulk_optional_str(
                               c.biddable_ad_group_criterion.Param1),
                           csv_to_field=lambda c, v: setattr(
                               c.biddable_ad_group_criterion, 'Param1', v
                               if v else '')),
        _SimpleBulkMapping(header=_StringTable.Param2,
                           field_to_csv=lambda c: bulk_optional_str(
                               c.biddable_ad_group_criterion.Param2),
                           csv_to_field=lambda c, v: setattr(
                               c.biddable_ad_group_criterion, 'Param2', v
                               if v else '')),
        _SimpleBulkMapping(header=_StringTable.Param3,
                           field_to_csv=lambda c: bulk_optional_str(
                               c.biddable_ad_group_criterion.Param3),
                           csv_to_field=lambda c, v: setattr(
                               c.biddable_ad_group_criterion, 'Param3', v
                               if v else '')),
        _ComplexBulkMapping(
            entity_to_csv=lambda entity, row_values: _ProductConditionHelper.
            add_row_values_from_conditions(
                entity.biddable_ad_group_criterion.Criterion.Conditions.
                ProductCondition, row_values),
            csv_to_entity=lambda row_values, entity: _ProductConditionHelper.
            add_conditions_from_row_values(
                row_values,
                entity.biddable_ad_group_criterion.Criterion.Conditions.
                ProductCondition,
            ))
    ]

    def process_mappings_to_row_values(self, row_values,
                                       exclude_readonly_data):
        self.convert_to_values(row_values, BulkAdGroupProductTarget._MAPPINGS)

    def process_mappings_from_row_values(self, row_values):
        self.biddable_ad_group_criterion = _CAMPAIGN_OBJECT_FACTORY.create(
            'BiddableAdGroupCriterion')
        self.biddable_ad_group_criterion.Type = 'BiddableAdGroupCriterion'
        self.biddable_ad_group_criterion.CriterionBid = _CAMPAIGN_OBJECT_FACTORY.create(
            'FixedBid')
        self.biddable_ad_group_criterion.CriterionBid.Type = 'FixedBid'
        self.biddable_ad_group_criterion.Criterion = _CAMPAIGN_OBJECT_FACTORY.create(
            'Product')
        self.biddable_ad_group_criterion.Criterion.Type = 'Product'

        row_values.convert_to_entity(self, BulkAdGroupProductTarget._MAPPINGS)

    def read_additional_data(self, stream_reader):
        super(BulkAdGroupProductTarget,
              self).read_additional_data(stream_reader)
예제 #3
0
class BulkCampaign(_SingleRecordBulkEntity):
    """ Represents a campaign that can be read or written in a bulk file.

    This class exposes the :attr:`campaign` property that can be read and written as fields of the
    Campaign record in a bulk file.

    For more information, see Campaign at http://go.microsoft.com/fwlink/?LinkID=511521.

    *See also:*

    * :class:`.BulkServiceManager`
    * :class:`.BulkOperation`
    * :class:`.BulkFileReader`
    * :class:`.BulkFileWriter`
    """
    def __init__(self, account_id=None, campaign=None):
        super(BulkCampaign, self).__init__()

        self._account_id = account_id
        self._campaign = campaign
        self._quality_score_data = None
        self._performance_data = None

    @property
    def account_id(self):
        """ The identifier of the account that contains the campaign.

        Corresponds to the 'Parent Id' field in the bulk file.

        :rtype: int
        """

        return self._account_id

    @account_id.setter
    def account_id(self, account_id):
        self._account_id = account_id

    @property
    def campaign(self):
        """ Defines a campaign within an account.

        See Campaign at https://msdn.microsoft.com/en-us/library/bing-ads-campaign-management-campaign.aspx
        """

        return self._campaign

    @campaign.setter
    def campaign(self, campaign):
        self._campaign = campaign

    @property
    def quality_score_data(self):
        """ The quality score data for the campaign.

        :rtype: QualityScoreData
        """

        return self._quality_score_data

    @property
    def performance_data(self):
        """ The historical performance data for the campaign

        :rtype: PerformanceData
        """

        return self._performance_data

    def _get_shopping_setting(self):
        if not self.campaign.Settings.Setting:
            return None
        shopping_settings = [
            setting for setting in self.campaign.Settings.Setting
            if isinstance(setting, _ShoppingSetting)
        ]
        if len(shopping_settings) != 1:
            raise ValueError(
                'Can only have 1 ShoppingSetting in Campaign Settings.')
        return shopping_settings[0]

    @staticmethod
    def _write_campaign_type(c):
        if not c.campaign.CampaignType:
            return None
        if len(c.campaign.CampaignType) != 1:
            raise ValueError("Only 1 CampaignType can be set in Campaign")
        return c.campaign.CampaignType[0]

    @staticmethod
    def _read_campaign_type(c, v):
        if not v:
            return []
        campaign_type = v
        c.campaign.CampaignType = [campaign_type]
        if campaign_type.lower() == 'shopping':
            c.campaign.Settings = _CAMPAIGN_OBJECT_FACTORY.create(
                'ArrayOfSetting')
            shopping_setting = _CAMPAIGN_OBJECT_FACTORY.create(
                'ShoppingSetting')
            shopping_setting.Type = 'ShoppingSetting'
            c.campaign.Settings.Setting = [shopping_setting]

    @staticmethod
    def _write_store_id(c):
        if not c.campaign.CampaignType:
            return None
        if 'shopping' in [
                campaign_type.lower()
                for campaign_type in c.campaign.CampaignType
        ]:
            shopping_setting = c._get_shopping_setting()
            if not shopping_setting:
                return None
            return bulk_str(shopping_setting.StoreId)

    @staticmethod
    def _read_store_id(c, v):
        if not c.campaign.CampaignType:
            return None
        if 'shopping' in [
                campaign_type.lower()
                for campaign_type in c.campaign.CampaignType
        ]:
            shopping_setting = c._get_shopping_setting()
            if not shopping_setting:
                return None
            shopping_setting.StoreId = int(v) if v else None

    @staticmethod
    def _write_priority(c):
        if not c.campaign.CampaignType:
            return None
        if 'shopping' in [
                campaign_type.lower()
                for campaign_type in c.campaign.CampaignType
        ]:
            shopping_setting = c._get_shopping_setting()
            if not shopping_setting:
                return None
            return bulk_str(shopping_setting.Priority)

    @staticmethod
    def _read_priority(c, v):
        if not c.campaign.CampaignType:
            return None
        if 'shopping' in [
                campaign_type.lower()
                for campaign_type in c.campaign.CampaignType
        ]:
            shopping_setting = c._get_shopping_setting()
            if not shopping_setting:
                return None
            shopping_setting.Priority = int(v) if v else None

    @staticmethod
    def _write_sales_country_code(c):
        if not c.campaign.CampaignType:
            return None
        if 'shopping' in [
                campaign_type.lower()
                for campaign_type in c.campaign.CampaignType
        ]:
            shopping_setting = c._get_shopping_setting()
            if not shopping_setting:
                return None
            return shopping_setting.SalesCountryCode

    @staticmethod
    def _read_sales_country_code(c, v):
        if not c.campaign.CampaignType:
            return None
        if 'shopping' in [
                campaign_type.lower()
                for campaign_type in c.campaign.CampaignType
        ]:
            shopping_setting = c._get_shopping_setting()
            if not shopping_setting:
                return None
            shopping_setting.SalesCountryCode = v

    _MAPPINGS = [
        _SimpleBulkMapping(
            header=_StringTable.CampaignType,
            field_to_csv=lambda c: BulkCampaign._write_campaign_type(c),
            csv_to_field=lambda c, v: BulkCampaign._read_campaign_type(c, v)),
        _SimpleBulkMapping(
            header=_StringTable.Status,
            field_to_csv=lambda c: bulk_str(c.campaign.Status),
            csv_to_field=lambda c, v: setattr(c.campaign, 'Status', v
                                              if v else None)),
        _SimpleBulkMapping(
            header=_StringTable.Id,
            field_to_csv=lambda c: bulk_str(c.campaign.Id),
            csv_to_field=lambda c, v: setattr(c.campaign, 'Id',
                                              int(v) if v else None)),
        _SimpleBulkMapping(
            header=_StringTable.ParentId,
            field_to_csv=lambda c: bulk_str(c.account_id),
            csv_to_field=lambda c, v: setattr(c, '_account_id', int(v))),
        _SimpleBulkMapping(
            header=_StringTable.Campaign,
            field_to_csv=lambda c: c.campaign.Name,
            csv_to_field=lambda c, v: setattr(c.campaign, 'Name', v)),
        _SimpleBulkMapping(
            header=_StringTable.TimeZone,
            field_to_csv=lambda c: c.campaign.TimeZone,
            csv_to_field=lambda c, v: setattr(c.campaign, 'TimeZone', v)),
        _SimpleBulkMapping(
            header=_StringTable.BudgetType,
            field_to_csv=lambda c: bulk_str(c.campaign.BudgetType),
            csv_to_field=lambda c, v: setattr(c.campaign, 'BudgetType', v
                                              if v else None)),
        _ComplexBulkMapping(budget_to_csv, csv_to_budget),
        _SimpleBulkMapping(
            header=_StringTable.BidAdjustment,
            field_to_csv=lambda c: bulk_str(c.campaign.NativeBidAdjustment),
            csv_to_field=lambda c, v: setattr(c.campaign,
                                              'NativeBidAdjustment',
                                              int(v) if v else None)),
        _SimpleBulkMapping(
            header=_StringTable.BingMerchantCenterId,
            field_to_csv=lambda c: BulkCampaign._write_store_id(c),
            csv_to_field=lambda c, v: BulkCampaign._read_store_id(c, v)),
        _SimpleBulkMapping(
            header=_StringTable.CampaignPriority,
            field_to_csv=lambda c: BulkCampaign._write_priority(c),
            csv_to_field=lambda c, v: BulkCampaign._read_priority(c, v)),
        _SimpleBulkMapping(
            header=_StringTable.CountryCode,
            field_to_csv=lambda c: BulkCampaign._write_sales_country_code(c),
            csv_to_field=lambda c, v: BulkCampaign._read_sales_country_code(
                c, v))
    ]

    def read_additional_data(self, stream_reader):
        super(BulkCampaign, self).read_additional_data(stream_reader)

    def process_mappings_from_row_values(self, row_values):
        self._campaign = _CAMPAIGN_OBJECT_FACTORY.create('Campaign')
        row_values.convert_to_entity(self, BulkCampaign._MAPPINGS)
        self._quality_score_data = QualityScoreData.read_from_row_values_or_null(
            row_values)
        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.campaign, 'campaign')
        self.convert_to_values(row_values, BulkCampaign._MAPPINGS)
        if not exclude_readonly_data:
            QualityScoreData.write_to_row_values_if_not_null(
                self.quality_score_data, row_values)
            PerformanceData.write_to_row_values_if_not_null(
                self.performance_data, row_values)
class BulkProductConditionCollection(_SingleRecordBulkEntity):
    """ Represents the product condition collection for a product ad extension.

    Each product condition collection can be read or written in a bulk file.

    This class exposes the :attr:`product_condition_collection` property that can be read and written
    as fields of the Product Ad Extension record in a bulk file.

    For more information, see Product Ad Extension at http://go.microsoft.com/fwlink/?LinkID=511516.

    The Product Ad Extension record includes the distinct properties of the :class:`.BulkProductConditionCollection` class, combined with
    the common properties of the :class:`.BulkProductAdExtension` class, for example :attr:`account_id` and :class:`.ProductAdExtension`.

    One :class:`.BulkProductAdExtension` has one or more :class:`.BulkProductConditionCollection`. Each :class:`.BulkProductConditionCollection` instance
    corresponds to one Product Ad Extension record in the bulk file. If you upload a :class:`.BulkProductAdExtension`,
    then you are effectively replacing any existing site links for the product ad extension.

    *See also:*

    * :class:`.BulkServiceManager`
    * :class:`.BulkOperation`
    * :class:`.BulkFileReader`
    * :class:`.BulkFileWriter`
    """
    def __init__(self,
                 account_id=None,
                 ad_extension_id=None,
                 version=None,
                 status=None,
                 store_id=None,
                 name=None,
                 product_condition_collection=None):
        super(BulkProductConditionCollection, self).__init__()
        self._identifier = _BulkProductAdExtensionIdentifier(
            account_id=account_id,
            ad_extension_id=ad_extension_id,
            version=version,
            status=status,
        )

        self._product_condition_collection = product_condition_collection
        self._name = name
        self._store_id = store_id
        self._store_name = None

    @property
    def name(self):
        """ The name of the ad extension.

        :rtype: str
        """
        return self._name

    @name.setter
    def name(self, value):
        self._name = value

    @property
    def store_id(self):
        """ The product ad extension's store identifier.

        :rtype: int
        """

        return self._store_id

    @store_id.setter
    def store_id(self, value):
        self._store_id = value

    @property
    def product_condition_collection(self):
        """ The collection of product conditions for a product ad extension.

        see ProductConditionCollection in https://msdn.microsoft.com/en-US/library/bing-ads-campaign-management-productconditioncollection.aspx
        """

        return self._product_condition_collection

    @product_condition_collection.setter
    def product_condition_collection(self, value):
        self._product_condition_collection = value

    @property
    def ad_extension_id(self):
        """ The identifier of the ad extension.

        Corresponds to the 'Id' field in the bulk file.

        :rtype: int
        """
        return self._identifier.ad_extension_id

    @ad_extension_id.setter
    def ad_extension_id(self, value):
        self._identifier._ad_extension_id = value

    @property
    def account_id(self):
        """ The ad extension's parent account identifier.

        Corresponds to the 'Parent Id' field in the bulk file.

        :rtype: int
        """

        return self._identifier.account_id

    @account_id.setter
    def account_id(self, value):
        self._identifier._account_id = value

    @property
    def status(self):
        """ The status of the ad extension.

        Corresponds to the 'Status' field in the bulk file.

        :rtype: str
        """
        return self._identifier.status

    @status.setter
    def status(self, value):
        self._identifier._status = value

    @property
    def version(self):
        """ The version of the ad extension.

        Corresponds to the 'Version' field in the bulk file.

        :rtype: int
        """

        return self._identifier.version

    @version.setter
    def version(self, value):
        self._identifier._version = value

    @property
    def store_name(self):
        """ The product ad extension's store name.

        Corresponds to the 'Store Name' field in the bulk file.

        :rtype: str
        """

        return self._store_name

    _MAPPINGS = [
        _ComplexBulkMapping(
            lambda entity, row_values: _ProductConditionHelper.
            add_row_values_from_conditions(
                entity.product_condition_collection.Conditions.
                ProductCondition, row_values), lambda row_values, entity:
            _ProductConditionHelper.add_conditions_from_row_values(
                row_values, entity.product_condition_collection.Conditions.
                ProductCondition)),
        _SimpleBulkMapping(header=_StringTable.Name,
                           field_to_csv=lambda c: c.name,
                           csv_to_field=lambda c, v: setattr(c, 'name', v)),
        _SimpleBulkMapping(
            header=_StringTable.BingMerchantCenterId,
            field_to_csv=lambda c: bulk_str(c.store_id),
            csv_to_field=lambda c, v: setattr(c, 'store_id', int(v))),
        _SimpleBulkMapping(
            header=_StringTable.BingMerchantCenterName,
            field_to_csv=lambda c: bulk_str(c.store_name),
            csv_to_field=lambda c, v: setattr(c, '_store_name', v)),
    ]

    def process_mappings_from_row_values(self, row_values):
        self.product_condition_collection = _CAMPAIGN_OBJECT_FACTORY.create(
            'ProductConditionCollection')
        self._identifier.read_from_row_values(row_values)
        row_values.convert_to_entity(self,
                                     BulkProductConditionCollection._MAPPINGS)

    def process_mappings_to_row_values(self, row_values,
                                       exclude_readonly_data):
        self._validate_property_not_null(self.product_condition_collection,
                                         'product_condition_collection')
        self._validate_property_not_null(
            self.product_condition_collection.Conditions,
            'product_condition_collection.Conditions')
        self._identifier.write_to_row_values(row_values, exclude_readonly_data)
        self.convert_to_values(row_values,
                               BulkProductConditionCollection._MAPPINGS)

    def can_enclose_in_multiline_entity(self):
        return True

    def enclose_in_multiline_entity(self):
        return BulkProductAdExtension(product_collection=self)

    def read_additional_data(self, stream_reader):
        super(BulkProductConditionCollection,
              self).read_additional_data(stream_reader)
예제 #5
0
class BulkCampaign(_SingleRecordBulkEntity):
    """ Represents a campaign that can be read or written in a bulk file.

    This class exposes the :attr:`campaign` property that can be read and written as fields of the
    Campaign record in a bulk file.

    For more information, see Campaign at http://go.microsoft.com/fwlink/?LinkID=511521.

    *See also:*

    * :class:`.BulkServiceManager`
    * :class:`.BulkOperation`
    * :class:`.BulkFileReader`
    * :class:`.BulkFileWriter`
    """
    def __init__(self, account_id=None, campaign=None):
        super(BulkCampaign, self).__init__()

        self._account_id = account_id
        self._campaign = campaign
        self._quality_score_data = None
        self._performance_data = None

    @property
    def account_id(self):
        """ The identifier of the account that contains the campaign.

        Corresponds to the 'Parent Id' field in the bulk file.

        :rtype: int
        """

        return self._account_id

    @account_id.setter
    def account_id(self, account_id):
        self._account_id = account_id

    @property
    def campaign(self):
        """ Defines a campaign within an account.

        See Campaign at https://msdn.microsoft.com/en-us/library/bing-ads-campaign-management-campaign.aspx
        """

        return self._campaign

    @campaign.setter
    def campaign(self, campaign):
        self._campaign = campaign

    @property
    def quality_score_data(self):
        """ The quality score data for the campaign.

        :rtype: QualityScoreData
        """

        return self._quality_score_data

    @property
    def performance_data(self):
        """ The historical performance data for the campaign

        :rtype: PerformanceData
        """

        return self._performance_data

    _MAPPINGS = [
        _SimpleBulkMapping(
            header=_StringTable.Status,
            field_to_csv=lambda c: bulk_str(c.campaign.Status),
            csv_to_field=lambda c, v: setattr(c.campaign, 'Status', v
                                              if v else None)),
        _SimpleBulkMapping(
            header=_StringTable.Id,
            field_to_csv=lambda c: bulk_str(c.campaign.Id),
            csv_to_field=lambda c, v: setattr(c.campaign, 'Id',
                                              int(v) if v else None)),
        _SimpleBulkMapping(
            header=_StringTable.ParentId,
            field_to_csv=lambda c: bulk_str(c.account_id),
            csv_to_field=lambda c, v: setattr(c, '_account_id', int(v))),
        _SimpleBulkMapping(
            header=_StringTable.Campaign,
            field_to_csv=lambda c: c.campaign.Name,
            csv_to_field=lambda c, v: setattr(c.campaign, 'Name', v)),
        _SimpleBulkMapping(
            header=_StringTable.TimeZone,
            field_to_csv=lambda c: c.campaign.TimeZone,
            csv_to_field=lambda c, v: setattr(c.campaign, 'TimeZone', v)),
        _SimpleBulkMapping(
            header=_StringTable.BudgetType,
            field_to_csv=lambda c: bulk_str(c.campaign.BudgetType),
            csv_to_field=lambda c, v: setattr(c.campaign, 'BudgetType', v
                                              if v else None)),
        _ComplexBulkMapping(budget_to_csv, csv_to_budget)
    ]

    def read_additional_data(self, stream_reader):
        super(BulkCampaign, self).read_additional_data(stream_reader)

    def process_mappings_from_row_values(self, row_values):
        self._campaign = _CAMPAIGN_OBJECT_FACTORY.create('Campaign')
        row_values.convert_to_entity(self, BulkCampaign._MAPPINGS)
        self._quality_score_data = QualityScoreData.read_from_row_values_or_null(
            row_values)
        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.campaign, 'campaign')
        self.convert_to_values(row_values, BulkCampaign._MAPPINGS)
        if not exclude_readonly_data:
            QualityScoreData.write_to_row_values_if_not_null(
                self.quality_score_data, row_values)
            PerformanceData.write_to_row_values_if_not_null(
                self.performance_data, row_values)