def finalize_completed_campaigns(daysago=1): # PromoCampaign.end_date is utc datetime with year, month, day only now = datetime.datetime.now(g.tz) date = now - datetime.timedelta(days=daysago) date = date.replace(hour=0, minute=0, second=0, microsecond=0) q = PromoCampaign._query( PromoCampaign.c.end_date == date, # exclude no transaction and freebies PromoCampaign.c.trans_id > 0, data=True) campaigns = list(q) # check that traffic is up to date earliest_campaign = min(campaigns, key=lambda camp: camp.start_date) start, end = promote.get_total_run(earliest_campaign) missing_traffic = get_missing_traffic(start.replace(tzinfo=None), date.replace(tzinfo=None)) if missing_traffic: raise ValueError("Can't finalize campaigns finished on %s." "Missing traffic from %s" % (date, missing_traffic)) links = Link._byID([camp.link_id for link in links], data=True) for camp in campaigns: if hasattr(camp, 'refund_amount'): continue link = links[camp.link_id] billable_impressions = promote.get_billable_impressions(camp) billable_amount = promote.get_billable_amount(camp, billable_impressions) if billable_amount >= camp.bid: text = ('%s completed with $%s billable (%s impressions @ $%s).' % (camp, billable_amount, billable_impressions, camp.cpm)) PromotionLog.add(link, text) refund_amount = 0. else: refund_amount = camp.bid - billable_amount user = Account._byID(link.author_id, data=True) try: success = authorize.refund_transaction(user, camp.trans_id, camp._id, refund_amount) except authorize.AuthorizeNetException as e: text = ('%s $%s refund failed' % (camp, refund_amount)) PromotionLog.add(link, text) g.log.debug(text + ' (response: %s)' % e) continue text = ('%s completed with $%s billable (%s impressions @ $%s).' ' %s refunded.' % (camp, billable_amount, billable_impressions, camp.cpm, refund_amount)) PromotionLog.add(link, text) camp.refund_amount = refund_amount camp._commit()
def POST_refund_campaign(self, form, jquery, link, campaign): billable_impressions = promote.get_billable_impressions(campaign) billable_amount = promote.get_billable_amount(campaign, billable_impressions) refund_amount = campaign.bid - billable_amount if refund_amount > 0: promote.refund_campaign(link, campaign, billable_amount) form.set_html(".status", _("refund succeeded")) else: form.set_html(".status", _("refund not needed"))
def finalize_completed_campaigns(daysago=1): # PromoCampaign.end_date is utc datetime with year, month, day only now = datetime.datetime.now(g.tz) date = now - datetime.timedelta(days=daysago) date = date.replace(hour=0, minute=0, second=0, microsecond=0) q = PromoCampaign._query(PromoCampaign.c.end_date == date, # exclude no transaction and freebies PromoCampaign.c.trans_id > 0, data=True) campaigns = list(q) # check that traffic is up to date earliest_campaign = min(campaigns, key=lambda camp: camp.start_date) start, end = promote.get_total_run(earliest_campaign) missing_traffic = get_missing_traffic(start.replace(tzinfo=None), date.replace(tzinfo=None)) if missing_traffic: raise ValueError("Can't finalize campaigns finished on %s." "Missing traffic from %s" % (date, missing_traffic)) links = Link._byID([camp.link_id for link in links], data=True) for camp in campaigns: if hasattr(camp, 'refund_amount'): continue link = links[camp.link_id] billable_impressions = promote.get_billable_impressions(camp) billable_amount = promote.get_billable_amount(camp, billable_impressions) if billable_amount >= camp.bid: text = ('%s completed with $%s billable (%s impressions @ $%s).' % (camp, billable_amount, billable_impressions, camp.cpm)) PromotionLog.add(link, text) refund_amount = 0. else: refund_amount = camp.bid - billable_amount user = Account._byID(link.author_id, data=True) try: success = authorize.refund_transaction(user, camp.trans_id, camp._id, refund_amount) except authorize.AuthorizeNetException as e: text = ('%s $%s refund failed' % (camp, refund_amount)) PromotionLog.add(link, text) g.log.debug(text + ' (response: %s)' % e) continue text = ('%s completed with $%s billable (%s impressions @ $%s).' ' %s refunded.' % (camp, billable_amount, billable_impressions, camp.cpm, refund_amount)) PromotionLog.add(link, text) camp.refund_amount = refund_amount camp._commit()
def POST_refund_campaign(self, form, jquery, link, campaign): billable_impressions = promote.get_billable_impressions(campaign) billable_amount = promote.get_billable_amount(campaign, billable_impressions) refund_amount = campaign.bid - billable_amount if refund_amount > 0: promote.refund_campaign(link, campaign, billable_amount) form.set_html('.status', _('refund succeeded')) else: form.set_html('.status', _('refund not needed'))
def POST_refund_campaign(self, form, jquery, link, campaign): billable_impressions = promote.get_billable_impressions(campaign) billable_amount = promote.get_billable_amount(campaign, billable_impressions) refund_amount = promote.get_refund_amount(campaign, billable_amount) if refund_amount > 0: promote.refund_campaign(link, campaign, billable_amount, billable_impressions) form.set_html('.status', _('refund succeeded')) else: form.set_html('.status', _('refund not needed'))
def POST_refund_campaign(self, form, jquery, link, campaign): if not link or not campaign or link._id != campaign.link_id: return abort(404, "not found") billable_impressions = promote.get_billable_impressions(campaign) billable_amount = promote.get_billable_amount(campaign, billable_impressions) refund_amount = promote.get_refund_amount(campaign, billable_amount) if refund_amount > 0: promote.refund_campaign(link, campaign, billable_amount, billable_impressions) form.set_html(".status", _("refund succeeded")) else: form.set_html(".status", _("refund not needed"))
def POST_refund_campaign(self, form, jquery, link, campaign): if not link or not campaign or link._id != campaign.link_id: return abort(404, 'not found') billable_impressions = promote.get_billable_impressions(campaign) billable_amount = promote.get_billable_amount(campaign, billable_impressions) refund_amount = promote.get_refund_amount(campaign, billable_amount) if refund_amount > 0: promote.refund_campaign(link, campaign, billable_amount, billable_impressions) form.set_text('.status', _('refund succeeded')) else: form.set_text('.status', _('refund not needed'))