From aa2225349e6588b563ced4e912c23a5406b24edc Mon Sep 17 00:00:00 2001 From: shlurbee Date: Tue, 22 May 2012 16:07:06 -0700 Subject: [PATCH] Creates campaign model and dual-write campaigns to Thing table First step of breaking campaigns out into their own data object. Campaigns are still stored as an attribute on their promoted Link as before, and all reads are still done from the link attribute, but writes (creation of a campaign and edits to a campaign) are also written into a Thing table. NOTE: This change has a configuration dependency in another repository. The following lines must be added to the .ini file: db_table_promocampaign = thing db_servers_promocampaign = [YOUR SERVER NAMES HERE] --- r2/r2/controllers/promotecontroller.py | 3 +- r2/r2/lib/promote.py | 60 +++++++++++++++++---- r2/r2/models/__init__.py | 1 + r2/r2/models/promo.py | 73 ++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 r2/r2/models/promo.py diff --git a/r2/r2/controllers/promotecontroller.py b/r2/r2/controllers/promotecontroller.py index 0810e59000..ea087f7998 100644 --- a/r2/r2/controllers/promotecontroller.py +++ b/r2/r2/controllers/promotecontroller.py @@ -451,8 +451,7 @@ def GET_pay(self, article, indx): if c.user_is_loggedin and c.user._id != article.author_id: return self.abort404() - # make sure this is a valid campaign index - if indx not in getattr(article, "campaigns", {}): + if not promote.is_valid_campaign(article, indx): return self.abort404() if g.authorizenetapi: diff --git a/r2/r2/lib/promote.py b/r2/r2/lib/promote.py index f0579c0f6d..15c7442f2d 100644 --- a/r2/r2/lib/promote.py +++ b/r2/r2/lib/promote.py @@ -93,6 +93,17 @@ def is_rejected(link): def is_promoted(link): return is_promo(link) and link.promote_status == STATUS.promoted +def is_valid_campaign(link, campaign_id): + # check for campaign in link data (old way) + if link and campaign_id in getattr(link, "campaigns", {}): + return True + # check for campaign in Thing data (new way) + try: + PromoCampaign._byID(campaign_id) + return True + except NotFound: + return False + # no references to promote_status below this function, pls def set_status(l, status, onchange = None): # keep this out here. Useful for updating the queue if there is a bug @@ -344,18 +355,19 @@ def get_transactions(link): def new_campaign(link, dates, bid, sr): - indx = None + # empty string for sr_name means target to all + sr_name = sr.name if sr else "" + # dual-write campaigns as data Things + campaign = PromoCampaign._new(link, sr_name, bid, dates[0], dates[1]) + # note indx in link.campaigns is the Thing id now + indx = campaign._id with g.make_lock(campaign_lock(link)): # get a copy of the attr so that it'll be # marked as dirty on the next write. campaigns = getattr(link, "campaigns", {}).copy() - # create a new index - indx = max(campaigns.keys() or [-1]) + 1 # add the campaign - # store the name not the reddit - sr = sr.name if sr else "" - campaigns[indx] = list(dates) + [bid, sr, 0] - PromotionWeights.add(link, indx, sr, dates[0], dates[1], bid) + campaigns[indx] = list(dates) + [bid, sr_name, 0] + PromotionWeights.add(link, indx, sr_name, dates[0], dates[1], bid) link.campaigns = {} link.campaigns = campaigns link._commit() @@ -370,16 +382,16 @@ def free_campaign(link, index, user): auth_campaign(link, index, user, -1) def edit_campaign(link, index, dates, bid, sr): + sr_name = sr.name if sr else "" with g.make_lock(campaign_lock(link)): campaigns = getattr(link, "campaigns", {}).copy() if index in campaigns: trans_id = campaigns[index][CAMPAIGN.trans_id] prev_bid = campaigns[index][CAMPAIGN.bid] # store the name not the reddit - sr = sr.name if sr else "" - campaigns[index] = list(dates) + [bid, sr, trans_id] + campaigns[index] = list(dates) + [bid, sr_name, trans_id] PromotionWeights.reschedule(link, index, - sr, dates[0], dates[1], bid) + sr_name, dates[0], dates[1], bid) link.campaigns = {} link.campaigns = campaigns link._commit() @@ -387,6 +399,16 @@ def edit_campaign(link, index, dates, bid, sr): #TODO cancel any existing charges if the bid has changed if prev_bid != bid: void_campaign(link, index, c.user) + + # dual-write update to campaign Thing if it exists + try: + campaign = PromoCampaign._byID(index) + campaign.set_bid(sr_name, bid, dates[0], dates[1]) + campaign._commit() + except NotFound: + g.log.debug("Skipping update of non-existent PromoCampaign [link:%d, index:%d]" % + (link._id, index)) + author = Account._byID(link.author_id, True) if getattr(author, "complimentary_promos", False): free_campaign(link, index, c.user) @@ -407,6 +429,13 @@ def delete_campaign(link, index): link._commit() #TODO cancel any existing charges void_campaign(link, index, c.user) + # dual-write update to campaign Thing if it exists + try: + campaign = PromoCampaign._byID(index) + campaign.delete() + except NotFound: + g.log.debug("Skipping deletion of non-existent PromoCampaign [link:%d, index:%d]" % + (link._id, index)) def void_campaign(link, index, user): campaigns = getattr(link, "campaigns", {}).copy() @@ -461,6 +490,17 @@ def auth_campaign(link, index, user, pay_id): link.campaigns = campaigns link._commit() + # dual-write update to campaign Thing + campaign = PromoCampaign._byID(index) + if campaign: + if trans_id > 0: + campaign.mark_paid(trans_id) + elif trans_id < 0: + campaign.mark_freebie() + else: + campaign.mark_payment_error(reason) + campaign._commit() + return bool(trans_id), reason return False, "" diff --git a/r2/r2/models/__init__.py b/r2/r2/models/__init__.py index 669b2f899c..59359c3761 100644 --- a/r2/r2/models/__init__.py +++ b/r2/r2/models/__init__.py @@ -37,3 +37,4 @@ from admintools import * from oauth2 import * from modaction import * +from promo import * diff --git a/r2/r2/models/promo.py b/r2/r2/models/promo.py new file mode 100644 index 0000000000..40b36839a2 --- /dev/null +++ b/r2/r2/models/promo.py @@ -0,0 +1,73 @@ +# The contents of this file are subject to the Common Public Attribution +# License Version 1.0. (the "License"); you may not use this file except in +# compliance with the License. You may obtain a copy of the License at +# http://code.reddit.com/LICENSE. The License is based on the Mozilla Public +# License Version 1.1, but Sections 14 and 15 have been added to cover use of +# software over a computer network and provide for limited attribution for the +# Original Developer. In addition, Exhibit A has been modified to be consistent +# with Exhibit B. +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for +# the specific language governing rights and limitations under the License. +# +# The Original Code is reddit. +# +# The Original Developer is the Initial Developer. The Initial Developer of the +# Original Code is reddit. +# +# All portions of the code written by reddit are Copyright (c) 2006-2012 +# reddit, Inc. All Rights Reserved. +################################################################################ +from r2.lib.db.thing import Thing, NotFound +from r2.lib.utils import Enum +from r2.models import Link + +PaymentState = Enum('UNPAID', 'PAID', 'FREEBIE') +TransactionCode = Enum('NEW', 'FREEBIE') + +class PromoCampaign(Thing): + + _defaults = dict(link_id=None, + sr_name='', + owner_id=None, + payment_state=PaymentState.UNPAID, + trans_id=TransactionCode.NEW, + trans_error=None, + bid=None, + start_date=None, + end_date=None) + + @classmethod + def _new(cls, link, sr_name, bid, start_date, end_date): + pc = PromoCampaign(link_id=link._id, + sr_name=sr_name, + bid=bid, + start_date=start_date, + end_date=end_date, + owner_id=link.author_id) + pc._commit() + return pc + + def set_bid(self, sr_name, bid, start_date, end_date): + self.sr_name = sr_name + self.bid = bid + self.start_date = start_date + self.end_date = end_date + + 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 + self.payment_state = PaymentState.FREEBIE + + def mark_payment_error(self, error_msg): + self.trans_id = TransactionCode.ERROR + self.trans_error = error_msg + + def delete(self): + self._deleted = True + self._commit() +