def fetch_stats(self):
     '''
     Retrieve and save statistic for group
     '''
     response = api_call('%s/stats' % self.adgroup_id)
     instance = AdStatistic.remote.get_or_create_from_resource(response)
     return instance
    def fetch_comments(self,
                       limit=100,
                       filter='stream',
                       summary=True,
                       **kwargs):
        '''
        Retrieve and save all comments
        '''
        extra_fields = {
            'owner_content_type_id':
            ContentType.objects.get_for_model(self).pk,
            'owner_id': self.pk
        }
        ids = []
        response = api_call('%s/comments' % self.graph_id,
                            limit=limit,
                            filter=filter,
                            summary=int(summary),
                            **kwargs)
        if response:
            log.debug('response objects count=%s, limit=%s, after=%s' %
                      (len(response['data']), limit, kwargs.get('after')))
            for resource in response['data']:
                instance = Comment.remote.get_or_create_from_resource(
                    resource, extra_fields)
                ids += [instance.pk]

        return Comment.objects.filter(pk__in=ids), response
    def fetch_groups(self):
        '''
        Retrieve and save all groups for campaign
        '''
        response = api_call('%s/adgroups' % self.campaign_id)

        instances = []
        for resource in response.data:
            instance = AdGroup.remote.get_or_create_from_resource(resource)
            instances += [instance]

        return instances
    def fetch_images(self):
        '''
        Retrieve and save images for account
        '''
        response = api_call('act_%s/adimages' % self.account_id)

        instances = []
        for resource in response.data.values():
            instance = AdImage.remote.get_or_create_from_resource(resource)
            instances += [instance]

        return instances
    def fetch_stats_groups(self):
        '''
        Retrieve and save statistic for groups of accout
        TODO: act_AccountID/adgroupstats?adgroup_ids=[JSON-encoded array of ad group IDs]
        '''
        response = api_call('act_%s/adgroupstats' % self.account_id)

        instances = []
        for resource in response.data:
            instance = AdStatistic.remote.get_or_create_from_resource(resource)
            instances += [instance]

        return instances
    def fetch_campaigns(self):
        '''
        Retrieve and save all campaigns for account
        '''
        if not self.id:
            log.error('It is neccesary to save account before saving campaigns')

        response = api_call('act_%s/adcampaigns' % self.account_id)

        instances = []
        for resource in response.data:
            instance = AdCampaign.remote.get_or_create_from_resource(resource)
            instances += [instance]

        return instances