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,
        })
Esempio n. 3
0
    def test_datetime_to_dfp_datetime_w_date(self):
        year = 2015
        month = 1
        day = 1

        dfp_datetime = utils.datetime_to_dfp_datetime(date(year, month, day))

        self.assertIsInstance(dfp_datetime, dict)
        self.assertEqual(dfp_datetime["date"]["year"], year)
        self.assertEqual(dfp_datetime["date"]["month"], month)
        self.assertEqual(dfp_datetime["date"]["day"], day)
Esempio n. 4
0
    def test_datetime_to_dfp_datetime_w_date(self):
        year = 2015
        month = 1
        day = 1

        dfp_datetime = utils.datetime_to_dfp_datetime(date(year, month, day))

        self.assertIsInstance(dfp_datetime, dict)
        self.assertEqual(dfp_datetime["date"]["year"], year)
        self.assertEqual(dfp_datetime["date"]["month"], month)
        self.assertEqual(dfp_datetime["date"]["day"], day)
Esempio n. 5
0
    def test_datetime_to_dfp_datetime_w_datetime(self):
        year = 2015
        month = 1
        day = 1
        hour = 11
        minute = 0
        second = 0

        dfp_datetime = utils.datetime_to_dfp_datetime(datetime(year, month, day, hour, minute, second))

        self.assertIsInstance(dfp_datetime, dict)
        self.assertEqual(dfp_datetime["date"]["year"], year)
        self.assertEqual(dfp_datetime["date"]["month"], month)
        self.assertEqual(dfp_datetime["date"]["day"], day)
        self.assertEqual(dfp_datetime["hour"], hour)
        self.assertEqual(dfp_datetime["minute"], minute)
        self.assertEqual(dfp_datetime["second"], second)
Esempio n. 6
0
    def test_datetime_to_dfp_datetime_w_datetime(self):
        year = 2015
        month = 1
        day = 1
        hour = 11
        minute = 0
        second = 0

        dfp_datetime = utils.datetime_to_dfp_datetime(
            datetime(year, month, day, hour, minute, second))

        self.assertIsInstance(dfp_datetime, dict)
        self.assertEqual(dfp_datetime["date"]["year"], year)
        self.assertEqual(dfp_datetime["date"]["month"], month)
        self.assertEqual(dfp_datetime["date"]["day"], day)
        self.assertEqual(dfp_datetime["hour"], hour)
        self.assertEqual(dfp_datetime["minute"], minute)
        self.assertEqual(dfp_datetime["second"], second)