Пример #1
0
def get_keywords(modeladmin, request, queryset):
    ai = Adwords_interactor()
    for temp_adgroup in queryset:
        adgroup_keywords = ai.get_ad_group_keywords( ad_group_id = temp_adgroup.id )
        for temp_keyword in adgroup_keywords:
            try:
                Keyword.objects.get(id=temp_keyword['criterion']['id'])
            except Keyword.DoesNotExist:
                kw = Keyword(
                                adgroup = temp_adgroup,
                                id = temp_keyword['criterion']['id'],
                                text = temp_keyword['criterion']['text'],
                                type = temp_keyword['criterion']['type'], 
                                match_type = temp_keyword['criterion']['matchType'],
                                stats = temp_keyword.get('stats',0),
                                adgroupcriterion_type = temp_keyword['AdGroupCriterion_Type'],
                                criterion_use = temp_keyword['criterionUse'],
                                criterion_type = temp_keyword['criterion']['type'],
                             )
                kw.save() 
Пример #2
0
    def load_csv_file(self,csv_file):

        client = Client.objects.get( name = 'ATC' )
        
        header = []
        cur_campaign = None
        cur_adgroup = None
        temp_time = datetime.datetime.now()
        csv_reader = csv.reader( self.stream_lines(csv_file),
                                         dialect = 'excel-tab', delimiter = '\t', quotechar = '|' )
        for linenumber, row in enumerate( csv_reader ):
            try:
                if linenumber % 10000 == 0 :
                    #endtime = datetime.datetime.now()
                    #print endtime - sttime
                    print "="*70,"timetaken:",datetime.datetime.now()-temp_time,"lno",linenumber
                    temp_time = datetime.datetime.now()
                    transaction.commit()
            
                if row:
                    if linenumber == 0:
                        # headers store them for post processing
                        header = row
                    else:
                        cur_row_type = self._get_row_type( row, header )
                        print cur_row_type
                        if  cur_row_type in [ self.ROWTYPE_CAMPAIGN, self.ROWTYPE_ADGROUP, self.ROWTYPE_KEYWORD, self.ROWTYPE_NEGATIVEKEYWORD, self.ROWTYPE_AD ] :
                            try:
                                if not cur_campaign or cur_campaign.name != row[header.index( 'campaign' )]:
                                    cur_campaign = Campaign.objects.get( name = row[header.index( 'campaign' )] , client = client )
                                    if cur_campaign.status != row[header.index( 'campaign status' )]:
                                        cur_campaign.status = row[header.index( 'campaign status' )]
                                        cur_campaign.save()
                                
                                
                            except Campaign.DoesNotExist:
                                cur_campaign = Campaign( client = client,
                                                         name = row[header.index( 'campaign' )],
                                                         status = row[header.index( 'campaign status' )] )
                                cur_campaign.save()
                                transaction.commit()
                            if cur_row_type ==  self.ROWTYPE_CAMPAIGN:
                                if cur_campaign.daily_budget != row[header.index('campaign daily budget')] :
                                    # record change
                                    rc = Record_change(what_changed = "campaign::"+cur_campaign.name, changed_from = cur_campaign.daily_budget, changed_to = row[header.index('campaign daily budget')] )
                                    rc.save() 
                                    # now do the  change :)
                                    cur_campaign.daily_budget = row[header.index('campaign daily budget')]
                                    cur_campaign.save()
                        if  cur_row_type in  [ self.ROWTYPE_ADGROUP, self.ROWTYPE_AD, self.ROWTYPE_KEYWORD, self.ROWTYPE_NEGATIVEKEYWORD, ] :
                            try:
                                if not cur_adgroup or cur_adgroup.name != row[header.index( 'ad group' )]:
                                    cur_adgroup = Adgroup.objects.get( campaign = cur_campaign, name = row[header.index( 'ad group' )] )
                                    is_dirty = False
                                    if cur_row_type == self.ROWTYPE_ADGROUP:
                                        if row[header.index( 'max cpc' )] and cur_adgroup.max_cpc != row[header.index( 'max cpc' )]:
                                            cur_adgroup.max_cpc = "0"+row[header.index( 'max cpc' )]
                                            is_dirty = True
                                        if row[header.index( 'cpa bid' )] and cur_adgroup.cpa_bid != row[header.index( 'cpa bid' )]:
                                                cur_adgroup.cpa_bid = "0"+row[header.index( 'cpa bid' )]
                                                is_dirty = True
                                        if cur_row_type == self.ROWTYPE_ADGROUP:
                                            if row[header.index( 'adgroup status' )] and cur_adgroup.status != row[header.index( 'adgroup status' )]:
                                                cur_adgroup.status = row[header.index( 'adgroup status' )]
                                                is_dirty = True
                                                
                                        if is_dirty:
                                            cur_adgroup.save()
                                                
                            except Adgroup.DoesNotExist:
                                cur_adgroup = Adgroup( campaign = cur_campaign,
                                                      name = row[header.index( 'ad group' )],
                                                      max_cpc = "0"+row[header.index( 'max cpc' )],
                                                      )
                                if cur_row_type == self.ROWTYPE_ADGROUP:
                                    cur_adgroup.status =  row[header.index( 'adgroup status' )]
                                cur_adgroup.save()
                                transaction.commit()
                        if  cur_row_type in [ self.ROWTYPE_KEYWORD, self.ROWTYPE_NEGATIVEKEYWORD ]:
    #                        print "\t\t\t\tkeyword", row[header.index( 'max cpc' )], row[header.index( 'Keyword' )]
                            # create keyword
                            try:
                                cur_keyword = Keyword.objects.get( text = row[header.index( 'keyword' )] )
                            except Keyword.DoesNotExist:
                                cur_keyword = Keyword( text = row[header.index( 'keyword' )] )
                                cur_keyword.save()
                            # create/update criterion
    
                            try:
                                cur_criterion = Criterion.objects.get( adgroup = cur_adgroup, keyword = cur_keyword , 
                                                                       type = row[header.index('criterion type')] )
    
    #                            if cur_criterion.firstpage_cpc != row[header.index('first page cpc')] or cur_criterion.topofpage_cpc != row[header.index('top of page cpc')] or cur_criterion.quality_score != row[header.index('quality score')] or cur_criterion.status != row[header.index('status')] or cur_criterion.approval_status != row[header.index('approval status')]:
    #                                if cur_criterion.quality_score != row[header.index('quality score')]:
    #                                    rc = Record_change(what_changed="quality score", 
    #                                                       changed_from = cur_criterion.quality_score, 
    #                                                       changed_to = row[header.index('quality score')])
    #                                    rc.save()
    #                                    
    #                                cur_criterion.firstpage_cpc = "0"+row[header.index('first page cpc')]
    #                                cur_criterion.topofpage_cpc = "0"+row[header.index('top Of page cpc')]
    #                                cur_criterion.quality_score = "0"+row[header.index('quality score')]
    #                                cur_criterion.status = row[header.index('status')]
    #                                cur_criterion.approval_status = row[header.index('approval status')]
    #                                cur_criterion.save()
    
                            except Criterion.DoesNotExist:
                                cur_criterion = Criterion( adgroup = cur_adgroup, keyword = cur_keyword , type = row[header.index('criterion type')] )
                                cur_criterion.firstpage_cpc = "0"+row[header.index('first page cpc')]
                                cur_criterion.topofpage_cpc = "0"+row[header.index('top of page cpc')]
                                cur_criterion.quality_score = "0"+row[header.index('quality score')]
                                cur_criterion.match_type = row[header.index( 'criterion type' )]
                                if cur_row_type == self.ROWTYPE_KEYWORD: 
                                    cur_criterion.status = row[header.index('status')]
                                    cur_criterion.approval_status = row[header.index('approval status')]
                                cur_criterion.save()  
    
                        if cur_row_type == self.ROWTYPE_AD:
                            try: 
                                cur_ad = Ad.objects.get(adgroup = cur_adgroup, 
                                               headline = row[header.index('headline')],
                                               description_line1 = row[header.index('description line 1')], 
                                               description_line2 = row[header.index('description line 2')], 
                                               display_url = row[header.index('display url')] ,
                                               destination_url = row[header.index('destination url')],
                                               )
                                if cur_ad.approval_status != row[header.index('approval status')]:
                                    cur_ad.approval_status = row[header.index('Approval Status')]
                                    cur_ad.save()
    
                            except Ad.DoesNotExist:
                                cur_ad = Ad(adgroup = cur_adgroup, 
                                               headline = row[header.index('headline')],
                                               description_line1 = row[header.index('description line 1')], 
                                               description_line2 = row[header.index('description line 2')], 
                                               display_url = row[header.index('display url')] ,
                                               destination_url = row[header.index('destination url')],
                                               )
                                if cur_row_type == self.ROWTYPE_AD:
                                    cur_ad.approval_status = row[header.index('approval status')]
                                cur_ad.save()
    
                                pass
    
                        if not cur_row_type:
                            if row[header.index('order')] or row[header.index('id')] or row[header.index('company_name')]:
                                continue
                            print datetime.datetime.now(), linenumber, row
            except Exception as e:
                print "error::",e,":header::", header,":row::", row
                
        transaction.commit()
        csv_file.close()