Skip to content

Commit

Permalink
Record trans_id for freebies in campaign things
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shlurbee committed Jun 19, 2012
1 parent 768a664 commit 62aecb9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
67 changes: 67 additions & 0 deletions r2/r2/lib/migrate/campaigns_to_things.py
Original file line number Diff line number Diff line change
@@ -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


2 changes: 1 addition & 1 deletion r2/r2/lib/promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions r2/r2/models/promo.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def mark_paid(self, trans_id):
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):
Expand Down

0 comments on commit 62aecb9

Please sign in to comment.