def _handle_adzerk(msg): data = json.loads(msg.body) g.log.debug('data: %s' % data) action = data.get('action') if action == 'deactivate_link': link = Link._by_fullname(data['link'], data=True) _deactivate_link(link) elif action == 'deactivate_campaign': link = Link._by_fullname(data['link'], data=True) campaign = PromoCampaign._by_fullname(data['campaign'], data=True) _deactivate_campaign(link, campaign) elif action == 'update_adzerk': link = Link._by_fullname(data['link'], data=True) campaign = PromoCampaign._by_fullname(data['campaign'], data=True) _update_adzerk(link, campaign)
def query(self): if self.sort == "future_promos": return queries.get_all_unapproved_links() elif self.sort == "pending_promos": return queries.get_all_accepted_links() elif self.sort == "unpaid_promos": return queries.get_all_unpaid_links() elif self.sort == "rejected_promos": return queries.get_all_rejected_links() elif self.sort == "live_promos" and self.sr: return self.live_by_subreddit(self.sr) elif self.sort == 'live_promos': return queries.get_all_live_links() elif self.sort == 'underdelivered': q = queries.get_underdelivered_campaigns() campaigns = PromoCampaign._by_fullname(list(q), data=True, return_dict=False) link_ids = [camp.link_id for camp in campaigns] return [Link._fullname_from_id36(to36(id)) for id in link_ids] elif self.sort == 'reported': return queries.get_reported_links(Subreddit.get_promote_srid()) elif self.sort == 'house': return self.get_house_link_names() elif self.sort == 'all': return queries.get_all_promoted_links()
def query(self): if c.user_is_sponsor: if self.sort == "future_promos": return queries.get_all_unapproved_links() elif self.sort == "pending_promos": return queries.get_all_accepted_links() elif self.sort == "unpaid_promos": return queries.get_all_unpaid_links() elif self.sort == "rejected_promos": return queries.get_all_rejected_links() elif self.sort == "live_promos" and self.sr: return self.live_by_subreddit(self.sr) elif self.sort == 'live_promos': return queries.get_all_live_links() elif self.sort == 'underdelivered': q = queries.get_underdelivered_campaigns() campaigns = PromoCampaign._by_fullname(list(q), data=True, return_dict=False) link_ids = [camp.link_id for camp in campaigns] return [Link._fullname_from_id36(to36(id)) for id in link_ids] elif self.sort == 'reported': return queries.get_reported_links(get_promote_srid()) return queries.get_all_promoted_links() else: if self.sort == "future_promos": return queries.get_unapproved_links(c.user._id) elif self.sort == "pending_promos": return queries.get_accepted_links(c.user._id) elif self.sort == "unpaid_promos": return queries.get_unpaid_links(c.user._id) elif self.sort == "rejected_promos": return queries.get_rejected_links(c.user._id) elif self.sort == "live_promos": return queries.get_live_links(c.user._id) return queries.get_promoted_links(c.user._id)
def _handle_adzerk(msg): data = json.loads(msg.body) g.log.debug('data: %s' % data) action = data.get('action') if action == 'deactivate_orphaned_flight': _deactivate_orphaned_flight(data['flight']) return link = Link._by_fullname(data['link'], data=True) if data['campaign']: campaign = PromoCampaign._by_fullname(data['campaign'], data=True) else: campaign = None if action == 'update_adzerk': if 'triggered_by' in data and data['triggered_by'] is not None: triggered_by = Account._by_fullname(data['triggered_by'], data=True) else: triggered_by = None _update_adzerk(link, campaign, triggered_by) elif action == 'deactivate_overdelivered': _deactivate_overdelivered(link, campaign)
def _handle_deactivate(payload): campaign_ids = payload["campaigns"] and payload["campaigns"].split(",") if not campaign_ids: return campaigns = PromoCampaign._by_fullname(campaign_ids, data=True) lineitems_service.deactivate(campaign_ids)
def update_served(items): for item in items: if not item.promoted: continue campaign = PromoCampaign._by_fullname(item.campaign) if not campaign.has_served: campaign.has_served = True campaign._commit()
def _handle_upsert_campaign(payload): link = Link._by_fullname(payload["link"], data=True) campaign = PromoCampaign._by_fullname(payload["campaign"], data=True) owner = Account._byID(campaign.owner_id) lineitem = lineitems_service.upsert_lineitem(owner, campaign) creative = creatives_service.get_creative(link) lineitems_service.associate_with_creative( lineitem=lineitem, creative=creative)
def filter_campaigns(date, fullnames): campaigns = PromoCampaign._by_fullname(fullnames, data=True, return_dict=False) # filter out campaigns that shouldn't be live pc_date = datetime.datetime(date.year, date.month, date.day, 0, 0, tzinfo=g.tz) campaigns = [camp for camp in campaigns if camp.start_date <= pc_date <= camp.end_date] # check for links with targeted campaigns - we can't handle them now has_targeted = [camp.link_id for camp in campaigns if camp.sr_name != ""] return [camp for camp in campaigns if camp.link_id not in has_targeted]
def filter_campaigns(date, fullnames): campaigns = PromoCampaign._by_fullname(fullnames, data=True, return_dict=False) # filter out campaigns that shouldn't be live pc_date = datetime.datetime(date.year, date.month, date.day, 0, 0, tzinfo=g.tz) campaigns = [camp for camp in campaigns if camp.start_date <= pc_date <= camp.end_date] # check for links with targeted campaigns - we can't handle them now has_targeted = [camp.link_id for camp in campaigns if camp.sr_name != ''] return [camp for camp in campaigns if camp.link_id not in has_targeted]
def _handle_adzerk(msg): data = json.loads(msg.body) g.log.debug('data: %s' % data) action = data.get('action') link = Link._by_fullname(data['link'], data=True) if data['campaign']: campaign = PromoCampaign._by_fullname(data['campaign'], data=True) else: campaign = None if action == 'update_adzerk': _update_adzerk(link, campaign) elif action == 'deactivate_overdelivered': _deactivate_overdelivered(link, campaign)
def _handle_upsert_campaign(payload): link = Link._by_fullname(payload["link"], data=True) campaign = PromoCampaign._by_fullname(payload["campaign"], data=True) owner = Account._byID(campaign.owner_id) author = Account._byID(link.author_id) try: lineitem = lineitems_service.upsert_lineitem(owner, campaign) except ValueError as e: g.log.error("unable to upsert lineitem: %s" % e) return creative = creatives_service.upsert_creative(author, link) lineitems_service.associate_with_creative( lineitem=lineitem, creative=creative)
def make_daily_promotions(offset=0, test=False): """ Arguments: offset - number of days after today to get the schedule for test - if True, new schedule will be generated but not launched Raises Exception with list of campaigns that had errors if there were any """ scheduled_adweights, error_campaigns = get_scheduled(offset) current_adweights_byid = get_live_promotions([LiveAdWeights.ALL_ADS]) current_adweights = current_adweights_byid[LiveAdWeights.ALL_ADS] link_names = [aw.link for aw in itertools.chain(scheduled_adweights, current_adweights)] links = Link._by_fullname(link_names, data=True) camp_names = [aw.campaign for aw in itertools.chain(scheduled_adweights, current_adweights)] campaigns = PromoCampaign._by_fullname(camp_names, data=True) srs = Subreddit._by_name([camp.sr_name for camp in campaigns.itervalues() if camp.sr_name]) expired_links = ({aw.link for aw in current_adweights} - {aw.link for aw in scheduled_adweights}) for link_name in expired_links: link = links[link_name] if is_promoted(link): if test: print "unpromote", link_name else: # update the query queue set_promote_status(link, PROMOTE_STATUS.finished) emailer.finished_promo(link) by_srid = defaultdict(list) for adweight in scheduled_adweights: link = links[adweight.link] campaign = campaigns[adweight.campaign] if campaign.sr_name: sr = srs[campaign.sr_name] sr_id = sr._id sr_over_18 = sr.over_18 else: sr_id = '' sr_over_18 = False if sr_over_18: if test: print "over18", link._fullname else: link.over_18 = True link._commit() if is_accepted(link) and not is_promoted(link): if test: print "promote2", link._fullname else: # update the query queue set_promote_status(link, PROMOTE_STATUS.promoted) emailer.live_promo(link) by_srid[sr_id].append(adweight) if not test: set_live_promotions(by_srid) _mark_promos_updated() else: print by_srid finalize_completed_campaigns(daysago=offset+1) hooks.get_hook('promote.make_daily_promotions').call(offset=offset) # after launching as many campaigns as possible, raise an exception to # report any error campaigns. (useful for triggering alerts in irc) if error_campaigns: raise Exception("Some scheduled campaigns could not be added to daily " "promotions: %r" % error_campaigns)
def _handle_deactivate_campaign(payload): campaign = PromoCampaign._by_fullname(payload["campaign"]) lineitem = lineitems_service.get_lineitem(campaign) if lineitem: lineitems_service.deactivate(lineitem)