def _process_update(self, item, feed_item): """Updates an ad based on the values from the feed. Args: item: Object representing the ad to be updated, this object is updated directly. feed_item: Feed item representing ad values from the Bulkdozer feed. """ campaign = self._campaign_dao.get(feed_item, required=True) item['active'] = feed_item.get(FieldMap.AD_ACTIVE, True) if item['active']: self._wait_all_creative_activation(feed_item) self._setup_rotation_strategy(item['creativeRotation'], feed_item) if feed_item['creative_assignment']: item['creativeRotation']['creativeAssignments'] = [] item['placementAssignments'] = [] item['eventTagOverrides'] = [] self._process_assignments( feed_item, item['creativeRotation'].get('creativeAssignments', []), item['placementAssignments'], item['eventTagOverrides'], campaign) if 'deliverySchedule' in item: item['deliverySchedule']['priority'] = feed_item.get( FieldMap.AD_PRIORITY, None) if feed_item.get(FieldMap.AD_HARDCUTOFF, None) != None: if not 'deliverySchedule' in item: item['deliverySchedule'] = {} item['deliverySchedule']['hardCutoff'] = feed_item.get( FieldMap.AD_HARDCUTOFF) item['archived'] = feed_item.get(FieldMap.AD_ARCHIVED, False) if 'T' in feed_item.get(FieldMap.AD_END_DATE, None): item['endTime'] = feed_item.get(FieldMap.AD_END_DATE, None) else: item['endTime'] = StringExtensions.convertDateStrToDateTimeStr( feed_item.get(FieldMap.AD_END_DATE, None), '23:59:59') if 'T' in feed_item.get(FieldMap.AD_START_DATE, None): item['startTime'] = feed_item.get(FieldMap.AD_START_DATE, None) else: item['startTime'] = StringExtensions.convertDateStrToDateTimeStr( feed_item.get(FieldMap.AD_START_DATE, None)) item['name'] = feed_item.get(FieldMap.AD_NAME, None) self._process_landing_page(item, feed_item)
def _process_new(self, feed_item): """Creates a new ad DCM object from a feed item representing an ad from the Bulkdozer feed. This function simply creates the object to be inserted later by the BaseDAO object. Args: feed_item: Feed item representing the ad from the Bulkdozer feed. Returns: An ad object ready to be inserted in DCM through the API. """ if feed_item.get(FieldMap.AD_ACTIVE, None): self._wait_all_creative_activation(feed_item) campaign = self._campaign_dao.get(feed_item, required=True) creative_assignments = [] placement_assignments = [] event_tag_assignments = [] self._process_assignments(feed_item, creative_assignments, placement_assignments, event_tag_assignments, campaign) creative_rotation = {'creativeAssignments': creative_assignments} self._setup_rotation_strategy(creative_rotation, feed_item) delivery_schedule = { 'impressionRatio': '1', 'priority': feed_item.get(FieldMap.AD_PRIORITY, None), 'hardCutoff': feed_item.get(FieldMap.AD_HARDCUTOFF, None) } ad = { 'active': feed_item.get(FieldMap.AD_ACTIVE, None), 'archived': feed_item.get(FieldMap.AD_ARCHIVED, None), 'campaignId': campaign['id'], 'creativeRotation': creative_rotation, 'deliverySchedule': delivery_schedule, 'endTime': feed_item.get(FieldMap.AD_END_DATE, None) if 'T' in feed_item.get( FieldMap.AD_END_DATE, None) else StringExtensions.convertDateStrToDateTimeStr( feed_item.get(FieldMap.AD_END_DATE, None), '23:59:59'), 'name': feed_item.get(FieldMap.AD_NAME, None), 'placementAssignments': placement_assignments, 'startTime': feed_item.get(FieldMap.AD_START_DATE, None) if 'T' in feed_item.get( FieldMap.AD_START_DATE, None) else StringExtensions.convertDateStrToDateTimeStr( feed_item.get(FieldMap.AD_START_DATE, None)), 'type': feed_item.get(FieldMap.AD_TYPE, 'AD_SERVING_STANDARD_AD'), 'eventTagOverrides': event_tag_assignments } self._process_landing_page(ad, feed_item) return ad
def _process_assignments(self, feed_item, creative_assignments, placement_assignments, event_tag_assignments, campaign): """Updates the ad by setting the values of child objects based on secondary feeds. Args: feed_item: Feed item representing the ad from the Bulkdozer feed. creative_assignments: Feed items representing creative assignments related with the current ad. placement_assignments: Feed items representing placement assignments related with the current ad. event_tag_assignments: Feed items representing event tag assignments related with the current ad. """ assigned_creatives = [] assigned_placements = [] assigned_event_tags = [] for assignment in feed_item['creative_assignment']: creative = self._creative_dao.get(assignment, required=True) assignment[FieldMap.CREATIVE_ID] = creative['id'] if not creative['id'] in assigned_creatives: assigned_creatives.append(creative['id']) sequence = assignment.get(FieldMap.CREATIVE_ROTATION_SEQUENCE, None) weight = assignment.get(FieldMap.CREATIVE_ROTATION_WEIGHT, None) sequence = sequence if type(sequence) is int else None weight = weight if type(weight) is int else None if assignment.get(FieldMap.AD_CREATIVE_ROTATION_START_TIME, ''): startTime = ( assignment.get(FieldMap.AD_CREATIVE_ROTATION_START_TIME, '') if 'T' in assignment.get( FieldMap.AD_CREATIVE_ROTATION_START_TIME, '') else StringExtensions.convertDateStrToDateTimeStr( feed_item.get(FieldMap.AD_CREATIVE_ROTATION_START_TIME, None))) assignment[FieldMap.AD_CREATIVE_ROTATION_START_TIME] = startTime else: startTime = None if assignment.get(FieldMap.AD_CREATIVE_ROTATION_END_TIME, ''): endTime = ( assignment.get(FieldMap.AD_CREATIVE_ROTATION_END_TIME, '') if 'T' in assignment.get(FieldMap.AD_CREATIVE_ROTATION_END_TIME, '') else StringExtensions.convertDateStrToDateTimeStr( feed_item.get(FieldMap.AD_CREATIVE_ROTATION_END_TIME, None), '23:59:59')) assignment[FieldMap.AD_CREATIVE_ROTATION_END_TIME] = endTime else: endTime = None lp = None if assignment.get(FieldMap.AD_LANDING_PAGE_ID, '') != 'CAMPAIGN_DEFAULT': lp = self._landing_page_dao.get(assignment, required=True) else: lp = self._landing_page_dao.get( {FieldMap.AD_LANDING_PAGE_ID: campaign['defaultLandingPageId']}, required=True) creative_assignment = { 'active': True, 'sequence': sequence, 'weight': weight, 'creativeId': assignment.get(FieldMap.CREATIVE_ID, None), 'startTime': startTime, 'endTime': endTime, 'clickThroughUrl': { 'defaultLandingPage': False if (assignment.get(FieldMap.AD_LANDING_PAGE_ID, '') or assignment.get(FieldMap.CUSTOM_CLICK_THROUGH_URL, '')) and assignment.get(FieldMap.AD_LANDING_PAGE_ID, '') != 'CAMPAIGN_DEFAULT' else True, 'landingPageId': lp.get('id', None) if lp else None, 'customClickThroughUrl': assignment.get(FieldMap.CUSTOM_CLICK_THROUGH_URL, '') } } if creative.get('exitCustomEvents'): creative_assignment['richMediaExitOverrides'] = [] if assignment.get(FieldMap.AD_LANDING_PAGE_ID, '') or assignment.get( FieldMap.CUSTOM_CLICK_THROUGH_URL, ''): for exit_custom_event in creative.get('exitCustomEvents', []): creative_assignment['richMediaExitOverrides'].append({ 'exitId': exit_custom_event['id'], 'enabled': True, 'clickThroughUrl': { 'defaultLandingPage': False if (assignment.get(FieldMap.AD_LANDING_PAGE_ID, '') or assignment.get(FieldMap.CUSTOM_CLICK_THROUGH_URL, '') ) and assignment.get(FieldMap.AD_LANDING_PAGE_ID, '') != 'CAMPAIGN_DEFAULT' else True, 'landingPageId': lp.get('id', None) if lp else None, 'customClickThroughUrl': assignment.get(FieldMap.CUSTOM_CLICK_THROUGH_URL, '') } }) creative_assignments.append(creative_assignment) for assignment in feed_item['placement_assignment']: placement = self._placement_dao.get(assignment, required=True) if placement: assignment[FieldMap.PLACEMENT_ID] = placement['id'] if not placement['id'] in assigned_placements: assigned_placements.append(placement['id']) placement_assignments.append({ 'active': True, 'placementId': assignment.get(FieldMap.PLACEMENT_ID, None), }) event_tags = [{ 'assignment': item, 'event_tag': self._event_tag_dao.get(item, required=True) } for item in feed_item['event_tag_assignment']] event_tags += [{ 'assignment': item, 'event_tag': self._event_tag_dao.get(item, required=True) } for item in feed_item['placement_event_tag_profile']] for item in event_tags: assignment = item['assignment'] event_tag = item['event_tag'] if event_tag: assignment[FieldMap.EVENT_TAG_ID] = event_tag['id'] if not event_tag['id'] in assigned_event_tags: assigned_event_tags.append(event_tag['id']) event_tag_assignments.append({ 'id': event_tag['id'], 'enabled': assignment.get(FieldMap.EVENT_TAG_ENABLED, True) })