def get(self, feed_item, required=False): """Retrieves an item. Items could be retrieved from a in memory cache in case it has already been retrieved within the current execution. Also, this method is capable of translating 'ext' placeholder IDs with concrete CM ids. Args: feed_item: Feed item from the Bulkdozer feed representing the item to retrieve. Returns: The CM object that represents the identified entity. """ result = None keys = [] id_value = feed_item.get(self._id_field, None) if not id_value and self._search_field and feed_item.get( self._search_field, None): store_key = feed_item[self._search_field] if self._parent_filter_name: if feed_item.get(self._parent_filter_field_name, None): store_key = str( feed_item.get(self._parent_filter_field_name, None)) + store_key result = store.get(self._entity, store_key) if not result: result, key = self._get_by_name(feed_item) keys.append(key) if not result and required: raise Exception('ERROR: Could not find %s with name %s' % (self._entity, feed_item[self._search_field])) elif id_value: if type(id_value) in (str, unicode) and id_value.startswith('ext'): keys.append(id_value) id_value = store.translate(self._entity, id_value) if id_value: feed_item[self._id_field] = id_value if id_value: keys.append(id_value) result = store.get(self._entity, id_value) if not result: result = self._get(feed_item) if not result and required: raise Exception('ERROR: Could not find %s with id %s' % (self._entity, id_value)) store.set(self._entity, keys, result) return result
def get_identifier(self, association, feed): asset_ids = (association.get(FieldMap.CREATIVE_ASSET_ID, None), store.translate(self._entity, association[FieldMap.CREATIVE_ASSET_ID])) for creative_asset in feed.feed: if creative_asset[FieldMap.CREATIVE_ASSET_ID] in asset_ids or str(creative_asset[FieldMap.CREATIVE_ASSET_ID]) in asset_ids: return { 'name': creative_asset.get(FieldMap.CREATIVE_ASSET_NAME, None), 'type': creative_asset.get(FieldMap.CREATIVE_TYPE, None) } return None
def process(self, feed_item): """Processes a Bulkdozer feed item. This method identifies if the dyanmic targeting key already exists in CM, if it doesn't it creates it associated with the advertiser, and then inserts an association with the identified object. Args: feed_item: Bulkdozer feed item to process. Returns: Newly created or updated CM object. """ if feed_item.get(FieldMap.ADVERTISER_ID, None) and feed_item.get( FieldMap.DYNAMIC_TARGETING_KEY_NAME, None): if not self._key_exists( feed_item.get(FieldMap.ADVERTISER_ID, None), feed_item.get(FieldMap.DYNAMIC_TARGETING_KEY_NAME, None)): self._create_key( feed_item.get(FieldMap.DYNAMIC_TARGETING_KEY_NAME, None), 'OBJECT_ADVERTISER', feed_item.get(FieldMap.ADVERTISER_ID, None)) object_type = feed_item.get( FieldMap.DYNAMIC_TARGETING_KEY_OBJECT_TYPE, None) entity_id = feed_item.get(FieldMap.DYNAMIC_TARGETING_KEY_OBJECT_ID, None) if object_type and len(object_type) > 7: entity = object_type[7:] translated_id = store.translate(entity, entity_id) entity_id = translated_id or entity_id self._create_key( feed_item.get(FieldMap.DYNAMIC_TARGETING_KEY_NAME, None), object_type, entity_id) feed_item[FieldMap.DYNAMIC_TARGETING_KEY_OBJECT_ID] = entity_id else: raise Exception( 'Dynamic targeting key, %s and %s are required' % (FieldMap.ADVERTISER_ID, FieldMap.DYNAMIC_TARGETING_KEY_NAME))