Permalink
Browse files

Reorganize promote.

Functions charge_pending and get_scheduled used accepted_iter
decorator thing which obscured what they were doing. Removed unused
function get_scheduled_campaign.
  • Loading branch information...
1 parent c215726 commit 7529c509b7e10555c7ba0bacd476bab4ab1b24b9 @bsimpson63 bsimpson63 committed with spladug Jun 20, 2012
Showing with 31 additions and 55 deletions.
  1. +31 −55 r2/r2/lib/promote.py
View
86 r2/r2/lib/promote.py
@@ -521,22 +521,6 @@ def promo_datetime_now(offset = None):
now += timedelta(offset)
return now
-
-
-def get_scheduled_campaign(link, offset = None):
- """
- returns the indices of the campaigns that (datewise) could be active.
- """
- now = promo_datetime_now(offset = offset)
- active = []
- campaigns = getattr(link, "campaigns", {})
- for indx in campaigns:
- sd, ed, bid, sr, trans_id = campaigns[indx]
- if sd <= now and ed >= now:
- active.append(indx)
- return active
-
-
def accept_promotion(link):
"""
Accepting is campaign agnostic. Accepting the ad just means that
@@ -585,54 +569,46 @@ def unapprove_promotion(link):
set_status(link, STATUS.unseen)
links, weghts = get_live_promotions()
-def accepted_iter(func, offset = 0):
- now = promo_datetime_now(offset = offset)
+def accepted_campaigns(offset=0):
+ now = promo_datetime_now(offset=offset)
campaigns = PromotionWeights.get_campaigns(now)
- # load the links that have campaigns coming up
links = Link._by_fullname(set(x.thing_name for x in campaigns),
- data = True,return_dict = True)
+ data=True, return_dict=True)
+
for x in campaigns:
l = links[x.thing_name]
- if is_accepted(l):
- # get the campaign of interest from the link
- camp = getattr(l, "campaigns", {}).get(x.promo_idx)
- # the transaction id is the last of the campaign tuple
- if camp and camp[CAMPAIGN.trans_id]:
- func(l, camp, x.promo_idx, x.weight)
-
+ if not is_accepted(l):
+ continue
+ camp = getattr(l, "campaigns", {}).get(x.promo_idx)
+ if not camp or not camp[CAMPAIGN.trans_id]:
+ continue
+ yield (l, x.promo_idx, camp)
+
+def get_scheduled(offset=0):
+ by_sr = {}
+ for l, index, camp in accepted_campaigns(offset=offset):
+ sd, ed, bid, sr, trans_id = camp
+ if authorize.is_charged_transaction(trans_id, index):
+ by_sr.setdefault(sr, []).append((l, bid))
+ return by_sr
-def charge_pending(offset = 1):
- def _charge(l, camp, indx, weight):
+def charge_pending(offset=1):
+ for l, index, camp in accepted_campaigns(offset=offset):
user = Account._byID(l.author_id)
sd, ed, bid, sr, trans_id = camp
try:
- if (not authorize.is_charged_transaction(trans_id, indx) and
- authorize.charge_transaction(user, trans_id, indx)):
- # update the query queue
- if is_promoted(l):
- emailer.queue_promo(l, bid, trans_id)
- else:
- set_status(l, STATUS.pending,
- onchange = lambda: emailer.queue_promo(l, bid, trans_id) )
- promotion_log(l, "auth charge for campaign %s, trans_id: %d" % (indx, trans_id), True)
- except:
- print "Error on %s, campaign %s" % (l, indx)
- accepted_iter(_charge, offset = offset)
-
-
-def get_scheduled(offset = 0):
- """
- gets a dictionary of sr -> list of (link, weight) for promotions
- that should be live as of the day which is offset days from today.
- """
- by_sr = {}
- def _promote(l, camp, indx, weight):
- sd, ed, bid, sr, trans_id = camp
- if authorize.is_charged_transaction(trans_id, indx):
- by_sr.setdefault(sr, []).append((l, weight))
- accepted_iter(_promote, offset = offset)
- return by_sr
+ if (authorize.is_charged_transaction(trans_id, index) or not
+ authorize.charge_transaction(user, trans_id, index)):
+ continue
+ if is_promoted(l):
+ emailer.queue_promo(l, bid, trans_id)
+ else:
+ set_status(l, STATUS.pending,
+ onchange=lambda: emailer.queue_promo(l, bid, trans_id))
+ promotion_log(l, "auth charge for campaign %s, trans_id: %d" % (index, trans_id), True)
+ except:
+ print "Error on %s, campaign %s" % (l, index)
def get_traffic_weights(srnames):
from r2.lib import traffic

0 comments on commit 7529c50

Please sign in to comment.