def handle(self, *args, **options): client = adwords.AdWordsClient.LoadFromStorage() client.partial_failure = True # request a service object from the client object service = Campaign.serviceobj(client) mutatestring_dellst = [] # get a list of the campaigns campaigns = Campaign.listcampaigns(client) # create a list of delete dictionaries for campaign in campaigns: mutatestring_dellst.append(Campaign.deldict(campaign['id'])) # if there are no campaigns in aw with status != remove, return if (len(mutatestring_dellst) == 0): print('Nothing to delete.') return # call mutate for the list of deletes rslts = service.mutate(mutatestring_dellst) # removes partial failure errors from rslts['value'] rsltsvaluelst = [] for campaignsuccess in rslts['value']: if campaignsuccess != "": rsltsvaluelst.append(campaignsuccess) rslts['value'] = rsltsvaluelst # prints the results of the clear operation print('Campaign clear complete. Removed %s campaigns.' % len(rslts['value'])) print('Removed: ') # prints each campaign that was successfully removed for campaignsuccess in rslts['value']: if campaignsuccess != "": print('%s %s Status: %s' % (campaignsuccess['id'], campaignsuccess['name'], campaignsuccess['status'])) if 'partialFailureErrors' in rslts: # prints each campaign that threw an error print('Failed to remove: ') for campaignerror in rslts['partialFailureErrors']: print( '%s %s Reason: %s' % (campaigns[int( campaignerror['fieldPath'][11:12])]['campaignId'], campaigns[int(campaignerror['fieldPath'][11:12])]['name'], campaignerror['errorString']))
def receiver_pre_delete(**kwargs): client = adwords.AdWordsClient.LoadFromStorage() service = Campaign.serviceobj(client) instance = kwargs['instance'] # this variable is set in the overridden delete method, if it is set to # false save is probably being called from Campaign.addcampaign # or Campaign.removecampaign, or specified by kwargs if (instance.sync_aw): mutatestring = Campaign.deldict(instance.campaignid) rslts = service.mutate([mutatestring]) if (rslts['value'][0]['status'] != 'REMOVED'): raise IOError( 'Django Adwords did not successfully set the item to "REMOVED"' )