Exemplo n.º 1
0
def _campaign_to_lineitem(campaign, order=None, existing=None):
    if not (existing or order):
        raise ValueError("must either pass an order or an existing lineitem.")

    lineitem = {
        "name": _get_campaign_name(campaign),
        "startDateTime": _date_to_dfp_datetime(campaign.start_date),
        "endDateTime": _date_to_dfp_datetime(campaign.end_date),
        "lineItemType": _priority_to_lineitem_type(campaign.priority),
        "costPerUnit": _dollars_to_money(campaign.cpm / 100),
        "costType": _get_cost_type(campaign),
        "targetPlatform": _get_platform(campaign),
        "skipInventoryCheck": campaign.priority.inventory_override,
        "primaryGoal": {
            "units": campaign.impressions,
        },
    }

    if existing:
        return merge_deep(existing, lineitem)
    else:
        return merge_deep(lineitem, LINE_ITEM_DEFAULTS, {
            "orderId": order["id"],
            "externalId": campaign._fullname,
        })
def _link_to_creative(link, advertiser=None, existing=None):
    defaults = {
        "xsi_type": "TemplateCreative",
        "size": NATIVE_SIZE,
        "creativeTemplateId": g.dfp_selfserve_template_id,
    }

    if not (existing or advertiser):
        raise ValueError("must either pass an advertiser or an existing creative.")

    creative = {
        "name": _get_creative_name(link),
        "creativeTemplateVariableValues": [{
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "title",
            "value": link.title
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "url",
            "value": link.url,
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "selftext",
            "value": link.selftext
        }, {
            "xsi_type": "UrlCreativeTemplateVariableValue",
            "uniqueName": "thumbnail_url",
            "value": getattr(link, "thumbnail_url", ""),
        }, {
            "xsi_type": "UrlCreativeTemplateVariableValue",
            "uniqueName": "mobile_ad_url",
            "value": getattr(link, "mobile_ad_url", ""),
        }, {
            "xsi_type": "UrlCreativeTemplateVariableValue",
            "uniqueName": "third_party_tracking",
            "value": getattr(link, "third_party_tracking", ""),
        }, {
            "xsi_type": "UrlCreativeTemplateVariableValue",
            "uniqueName": "third_party_tracking_2",
            "value": getattr(link, "third_party_tracking_2", ""),
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "link_id",
            "value": link._fullname,
        }],
    }

    if existing:
        return merge_deep(existing, creative)
    else:
        return merge_deep(creative, defaults, {
            "advertiserId": advertiser.id,
        })
Exemplo n.º 3
0
def _link_to_creative(link, advertiser=None, existing=None):
    if not (existing or advertiser):
        raise ValueError("must either pass an advertiser or an existing creative.")

    creative = {
        "name": _get_creative_name(link),
        "creativeTemplateVariableValues": [{
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "title",
            "value": link.title
        }, {
            "xsi_type": "UrlCreativeTemplateVariableValue",
            "uniqueName": "url",
            "value": link.url,
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "selftext",
            "value": link.selftext
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "thumbnail_url",
            "value": link.thumbnail_url,
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "mobile_ad_url",
            "value": link.mobile_ad_url,
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "third_party_tracking",
            "value": link.third_party_tracking,
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "third_party_tracking_2",
            "value": link.third_party_tracking_2,
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "link_id",
            "value": link._fullname,
        }],
    }

    if existing:
        return merge_deep(existing, creative)
    else:
        return merge_deep(creative, CREATIVE_DEFAULTS, {
            "advertiserId": advertiser_id,
        })
def _campaign_to_lineitem(campaign, order=None, existing=None):
    if not (existing or order):
        raise ValueError("must either pass an order or an existing lineitem.")

    lineitem = {
        "name": _get_campaign_name(campaign),
        "lineItemType": _priority_to_lineitem_type(campaign.priority),
        "costPerUnit": utils.pennies_to_dfp_money(campaign.cpm),
        "costType": _get_cost_type(campaign),
        "targetPlatform": "ANY",  # other targets are deprecated
        "skipInventoryCheck": campaign.priority.inventory_override,
        "primaryGoal": _get_goal_type(campaign),
        "targeting": {
            "inventoryTargeting": {
                "targetedPlacementIds": _get_placement_ids(campaign),
            },
        },
    }

    if existing is None:
        # TODO: non-global timezone_id
        now = datetime.today()
        now = now.replace(tzinfo=campaign.start_date.tzinfo)
        start_date = campaign.start_date
        end_date = campaign.end_date

        lineitem["startDateTime"] = utils.datetime_to_dfp_datetime(
            start_date, timezone_id=g.dfp_timezone_id)
        lineitem["endDateTime"] = utils.datetime_to_dfp_datetime(
            end_date, timezone_id=g.dfp_timezone_id)

        if start_date < now:
            lineitem["startDateTimeType"] = "IMMEDIATELY"

        if end_date < now:
            raise ValueError(
                "can't creative lineitem that ends in the past. (%s-%s)" %
                (start_date, end_date))

    if existing:
        return merge_deep(existing, lineitem)
    else:
        return merge_deep(lineitem, LINE_ITEM_DEFAULTS, {
            "orderId": order["id"],
            "externalId": campaign._fullname,
        })
def _campaign_to_lineitem(campaign, order=None, existing=None):
    if not (existing or order):
        raise ValueError("must either pass an order or an existing lineitem.")

    lineitem = {
        "name": _get_campaign_name(campaign),
        "lineItemType": _priority_to_lineitem_type(campaign.priority),
        "costPerUnit": utils.pennies_to_dfp_money(campaign.cpm),
        "costType": _get_cost_type(campaign),
        "targetPlatform": "ANY", # other targets are deprecated
        "skipInventoryCheck": campaign.priority.inventory_override,
        "primaryGoal": _get_goal_type(campaign),
        "targeting": {
            "inventoryTargeting": {
                "targetedPlacementIds": _get_placement_ids(campaign),
            },
        },
    }

    if existing is None:
        # TODO: non-global timezone_id
        now = datetime.today()
        now = now.replace(tzinfo=campaign.start_date.tzinfo)
        start_date = campaign.start_date
        end_date = campaign.end_date

        lineitem["startDateTime"] = utils.datetime_to_dfp_datetime(
            start_date, timezone_id=g.dfp_timezone_id)
        lineitem["endDateTime"] = utils.datetime_to_dfp_datetime(
            end_date, timezone_id=g.dfp_timezone_id)

        if start_date < now:
            lineitem["startDateTimeType"] = "IMMEDIATELY"

        if end_date < now:
            raise ValueError("can't creative lineitem that ends in the past. (%s-%s)" % (start_date, end_date))

    if existing:
        return merge_deep(existing, lineitem)
    else:
        return merge_deep(lineitem, LINE_ITEM_DEFAULTS, {
            "orderId": order["id"],
            "externalId": campaign._fullname,
        })
Exemplo n.º 6
0
    def test_merge_deep(self):
        merged = merge.merge_deep({
            "foo": {
                "a": 1,
                "b": 2,
            },
        }, {
            "foo": {
                "c": 3,
            },
        })

        self.assertEqual(merged, {
            "foo": {
                "a": 1,
                "b": 2,
                "c": 3,
            },
        })
def _link_to_creative(link, advertiser=None, existing=None):
    defaults = {
        "xsi_type": "TemplateCreative",
        "size": NATIVE_SIZE,
        "creativeTemplateId": g.dfp_selfserve_template_id,
    }

    if not (existing or advertiser):
        raise ValueError(
            "must either pass an advertiser or an existing creative.")

    creative = {
        "name":
        _get_creative_name(link),
        "creativeTemplateVariableValues": [{
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "title",
            "value": link.title
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "url",
            "value": link.url,
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "selftext",
            "value": link.selftext
        }, {
            "xsi_type":
            "UrlCreativeTemplateVariableValue",
            "uniqueName":
            "thumbnail_url",
            "value":
            getattr(link, "thumbnail_url", ""),
        }, {
            "xsi_type":
            "UrlCreativeTemplateVariableValue",
            "uniqueName":
            "mobile_ad_url",
            "value":
            getattr(link, "mobile_ad_url", ""),
        }, {
            "xsi_type":
            "UrlCreativeTemplateVariableValue",
            "uniqueName":
            "third_party_tracking",
            "value":
            getattr(link, "third_party_tracking", ""),
        }, {
            "xsi_type":
            "UrlCreativeTemplateVariableValue",
            "uniqueName":
            "third_party_tracking_2",
            "value":
            getattr(link, "third_party_tracking_2", ""),
        }, {
            "xsi_type": "StringCreativeTemplateVariableValue",
            "uniqueName": "link_id",
            "value": link._fullname,
        }],
    }

    if existing:
        return merge_deep(existing, creative)
    else:
        return merge_deep(creative, defaults, {
            "advertiserId": advertiser.id,
        })