'Combine campaigns with the same target.'

def combine_campaigns(self, campaigns):

    campaigns_by_target = defaultdict(list)
    for campaign in campaigns:
        target_names_tuple = tuple(sorted(campaign.target_names))
        campaigns_by_target[target_names_tuple].append(campaign)
    combined_campaigns = []
    changed = False
    for (target_names_tuple, campaigns) in campaigns_by_target.iteritems():
        if (len(campaigns) > 1):
            changed = True
            name = ','.join((camp.name for camp in campaigns))
            target_names = list(target_names_tuple)
            impressions = sum((camp.impressions for camp in campaigns))
            combined = SimpleCampaign(name, target_names, impressions)
            combined_campaigns.append(combined)
        else:
            combined_campaigns.extend(campaigns)
    return (changed, combined_campaigns)
