mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-01-24 14:27:58 -05:00
Record trans_id for freebies in campaign things
Initial campiagn Thing writes were recording an enum value for freebie campaigns but it actually is useful to include the transaction id (which is negative for freebies) because it references the freebie's record in the bid table. Note: This commit also includes a one-off script for fixing the trans_id in existing campaign things.
This commit is contained in:
67
r2/r2/lib/migrate/campaigns_to_things.py
Normal file
67
r2/r2/lib/migrate/campaigns_to_things.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
from r2.models import *
|
||||
|
||||
def fix_trans_id():
|
||||
bad_campaigns = list(PromoCampaign._query(PromoCampaign.c.trans_id == 1, data=True))
|
||||
num_bad_campaigns = len(bad_campaigns)
|
||||
|
||||
if not num_bad_campaigns:
|
||||
print "No campaigns with trans_id == 1"
|
||||
return
|
||||
|
||||
# print some info and prompt user to continue
|
||||
print ("Found %d campaigns with trans_id == 1. \n"
|
||||
"Campaigns ids: %s \n"
|
||||
"Press 'c' to fix them or any other key to abort." %
|
||||
(num_bad_campaigns, [pc._id for pc in bad_campaigns]))
|
||||
input_char = sys.stdin.read(1)
|
||||
if input_char != 'c' and input_char != 'C':
|
||||
print "aborting..."
|
||||
return
|
||||
|
||||
# log the ids for reference
|
||||
print ("Fixing %d campaigns with bad freebie trans_id: %s" %
|
||||
(num_bad_campaigns, [pc._id for pc in bad_campaigns]))
|
||||
|
||||
# get corresponding links and copy trans_id from link data to campaign thing
|
||||
link_ids = set([campaign.link_id for campaign in bad_campaigns])
|
||||
print "Fetching associated links: %s" % link_ids
|
||||
try:
|
||||
links = Link._byID(link_ids, data=True, return_dict=False)
|
||||
except NotFound, e:
|
||||
print("Invalid data: Some promocampaigns have invalid link_ids. "
|
||||
"Please delete these campaigns or fix the data before "
|
||||
"continuing. Exception: %s" % e)
|
||||
|
||||
# organize bad campaigns by link_id
|
||||
bad_campaigns_by_link = defaultdict(list)
|
||||
for c in bad_campaigns:
|
||||
bad_campaigns_by_link[c.link_id].append(c)
|
||||
|
||||
# iterate through links and copy trans_id from pickled list on the link to
|
||||
# the campaign thing
|
||||
failed = []
|
||||
for link in links:
|
||||
link_campaigns = getattr(link, "campaigns")
|
||||
thing_campaigns = bad_campaigns_by_link[link._id]
|
||||
for campaign in thing_campaigns:
|
||||
try:
|
||||
sd, ed, bid, sr_name, trans_id = link_campaigns[campaign._id]
|
||||
if trans_id != campaign.trans_id:
|
||||
campaign.trans_id = trans_id
|
||||
campaign._commit()
|
||||
except:
|
||||
failed.append({
|
||||
'link_id': link._id,
|
||||
'campaign_id': campaign._id,
|
||||
'exc type': sys.exc_info()[0],
|
||||
'exc msg': sys.exc_info()[1]
|
||||
})
|
||||
|
||||
# log the actions for future reference
|
||||
msg = ("%d of %d campaigns updated successfully. %d updates failed: %s" %
|
||||
(num_bad_campaigns, num_bad_campaigns - len(failed), len(failed), failed))
|
||||
print msg
|
||||
|
||||
|
||||
@@ -501,7 +501,7 @@ def auth_campaign(link, index, user, pay_id):
|
||||
if trans_id > 0:
|
||||
campaign.mark_paid(trans_id)
|
||||
elif trans_id < 0:
|
||||
campaign.mark_freebie()
|
||||
campaign.mark_freebie(trans_id)
|
||||
else:
|
||||
campaign.mark_payment_error(reason)
|
||||
campaign._commit()
|
||||
|
||||
@@ -59,8 +59,8 @@ class PromoCampaign(Thing):
|
||||
self.trans_id = trans_id
|
||||
self.payment_state = PaymentState.PAID
|
||||
|
||||
def mark_freebie(self):
|
||||
self.trans_id = TransactionCode.FREEBIE
|
||||
def mark_freebie(self, trans_id):
|
||||
self.trans_id = trans_id
|
||||
self.payment_state = PaymentState.FREEBIE
|
||||
|
||||
def mark_payment_error(self, error_msg):
|
||||
|
||||
Reference in New Issue
Block a user