def get_and_store( request, action = None, val = None ): ''' Triggeres specific API calls to fetch and pupulate Accounts and Campaigns in those accounts Use this to populate initial data :param request: :param action: get_all_account_details, get_all_campaigns :param val: Account ''' result = {'r':'failed'} result['action'] = action ai = adwords_utils.Adwords_interactor() if action == 'get_all_account_details': all_client_details = ai.get_all_account_details() for temp_client in all_client_details: print temp_client['customerId'] import pdb; pdb.set_trace() try: c = Client.objects.get( login = temp_client['login']) if c.client_id != temp_client['customerId']: c.client_id = temp_client['customerId'] c.name = temp_client['name'] c.save() except Client.DoesNotExist: c = Client( client_id = temp_client['customerId'], name = temp_client['name'], login = temp_client['login'], depth = temp_client['depth'] ) c.save() result['d'] = all_client_details result['r'] = 'success' elif action == 'get_all_campaigns': """ get all campaigns data """ try: temp_client = Client.objects.get( id = val ) all_campaigns = ai.get_all_campaigns( temp_client.id ) for temp_campaign in all_campaigns: try: c = Campaign.objects.get( name = temp_campaign['name'] ) if not c.id: c.id = temp_campaign['id'] c.save() except Campaign.DoesNotExist: c = Campaign( client = temp_client, id = temp_campaign['id'], name = temp_campaign['name'], status = temp_campaign['status'], campaign_stats = temp_campaign['campaignStats'], frequency_cap = temp_campaign['frequencyCap'], ) c.save() result['d'] = all_campaigns result['r'] = 'success' except Client.DoesNotExist: result['d'] = "client doesn't exist" elif action == 'gat_': pass else: pass return HttpResponse( dumps( result ) )