From 76c8a6e7d7328de542c862585fd564e26a4864f6 Mon Sep 17 00:00:00 2001 From: Chad Birch Date: Thu, 12 Sep 2013 11:06:55 -0600 Subject: [PATCH] Automatic gold messages: add "bottlecap" phrases This allows defining a set of phrases on a wiki page, and whenever an automatic gold-related PM is sent, a random phrase is selected and appended to the message. --- r2/example.ini | 3 +++ r2/r2/controllers/ipn.py | 31 +++++++++++++++++++++-------- r2/r2/lib/app_globals.py | 1 + r2/r2/models/admintools.py | 11 ++++++++-- r2/r2/models/builder.py | 3 ++- r2/r2/models/gold.py | 28 ++++++++++++++++++++++++++ r2/r2/public/static/css/reddit.less | 16 +++++++++++++++ r2/r2/templates/message.html | 2 +- 8 files changed, 83 insertions(+), 12 deletions(-) diff --git a/r2/example.ini b/r2/example.ini index 24bbb8db1..1815e74d4 100644 --- a/r2/example.ini +++ b/r2/example.ini @@ -529,6 +529,9 @@ wiki_page_privacy_policy = privacypolicy wiki_page_user_agreement = useragreement wiki_page_registration_info = registration_info +# -- other wiki pages -- +wiki_page_gold_bottlecaps = gold_bottlecaps + # Template names to record render timings for timed_templates = Reddit, Link, Comment, LinkListing, NestedListing, SubredditTopBar diff --git a/r2/r2/controllers/ipn.py b/r2/r2/controllers/ipn.py index 3805e8b9e..c225c7243 100644 --- a/r2/r2/controllers/ipn.py +++ b/r2/r2/controllers/ipn.py @@ -55,6 +55,7 @@ from r2.models import ( account_by_payingid, accountid_from_paypalsubscription, admintools, + append_random_bottlecap_phrase, cancel_subscription, Comment, create_claimed_gold, @@ -251,9 +252,11 @@ def send_gift(buyer, recipient, months, days, signed, giftmessage, comment_id): message += '\n\n' + strings.gold_benefits_msg message += '\n\n' + strings.lounge_msg + message = append_random_bottlecap_phrase(message) try: - send_system_message(recipient, subject, message) + send_system_message(recipient, subject, message, + distinguished='gold-auto') except MessageError: g.log.error('send_gift: could not send system message') @@ -409,8 +412,11 @@ class IpnController(RedditController): 'Please bear with us as Google Wallet ' 'payments can take up to an hour to ' 'complete.') + msg = append_random_bottlecap_phrase(msg) + try: - send_system_message(buyer, subject, msg) + send_system_message(buyer, subject, msg, + distinguished='gold-auto') except MessageError: g.log.error('gcheckout send_system_message failed') elif auth.find("financial-order-state" @@ -608,7 +614,9 @@ class IpnController(RedditController): secret, buyer_id, c.start_time, subscr_id, status=status) - send_system_message(buyer, subject, message) + message = append_random_bottlecap_phrase(message) + + send_system_message(buyer, subject, message, distinguished='gold-auto') payment_blob["status"] = "processed" g.hardcache.set(blob_key, payment_blob, 86400 * 30) @@ -667,13 +675,13 @@ class GoldPaymentController(RedditController): comment = payment_blob.get('comment', None) comment = comment._fullname if comment else None existing = retrieve_gold_transaction(transaction_id) + msg = None if event_type == 'cancelled': subject = _('reddit gold payment cancelled') msg = _('Your reddit gold payment has been cancelled, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} - send_system_message(buyer, subject, msg) if existing: # note that we don't check status on existing, probably # should update gold_table when a cancellation happens @@ -695,7 +703,6 @@ class GoldPaymentController(RedditController): msg = _('Your reddit gold payment has failed, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} - send_system_message(buyer, subject, msg) # probably want to update gold_table here elif event_type == 'refunded': if not (existing and existing.status == 'processed'): @@ -705,9 +712,12 @@ class GoldPaymentController(RedditController): msg = _('Your reddit gold payment has been refunded, contact ' '%(gold_email)s for details') % {'gold_email': g.goldthanks_email} - send_system_message(buyer, subject, msg) reverse_gold_purchase(transaction_id) + if msg: + msg = append_random_bottlecap_phrase(msg) + send_system_message(buyer, subject, msg, distinguished='gold-auto') + class StripeController(GoldPaymentController): name = 'stripe' @@ -817,7 +827,10 @@ class StripeController(GoldPaymentController): subject = _('reddit gold payment') msg = _('Your payment is being processed and reddit gold will be ' 'delivered shortly.') - send_system_message(c.user, subject, msg) + msg = append_random_bottlecap_phrase(msg) + + send_system_message(c.user, subject, msg, + distinguished='gold-auto') class CoinbaseController(GoldPaymentController): @@ -1021,7 +1034,9 @@ def complete_gold_purchase(secret, transaction_id, payer_email, payer_id, g.log.error('gold: got duplicate gold transaction') try: - send_system_message(buyer, subject, message) + message = append_random_bottlecap_phrase(message) + send_system_message(buyer, subject, message, + distinguished='gold-auto') except MessageError: g.log.error('complete_gold_purchase: could not send system message') diff --git a/r2/r2/lib/app_globals.py b/r2/r2/lib/app_globals.py index d222a8e85..24fd1c6b9 100755 --- a/r2/r2/lib/app_globals.py +++ b/r2/r2/lib/app_globals.py @@ -198,6 +198,7 @@ class Globals(object): 'wiki_page_registration_info', 'wiki_page_privacy_policy', 'wiki_page_user_agreement', + 'wiki_page_gold_bottlecaps', 'adserver_click_domain', ], diff --git a/r2/r2/models/admintools.py b/r2/r2/models/admintools.py index f3640f7f4..279a43d57 100644 --- a/r2/r2/models/admintools.py +++ b/r2/r2/models/admintools.py @@ -26,6 +26,7 @@ from r2.lib.filters import websafe from r2.lib.log import log_text from r2.models import Account, Message, Report, Subreddit from r2.models.award import Award +from r2.models.gold import append_random_bottlecap_phrase from r2.models.token import AwardClaimToken from _pylibmc import MemcachedError @@ -274,7 +275,10 @@ def update_gold_users(verbose=False): subject = _("Your reddit gold subscription has expired.") message = _("Your subscription to reddit gold has expired.") message += "\n\n" + renew_msg - send_system_message(account, subject, message) + message = append_random_bottlecap_phrase(message) + + send_system_message(account, subject, message, + distinguished='gold-auto') continue count += 1 @@ -304,7 +308,10 @@ def update_gold_users(verbose=False): message = _("Your subscription to reddit gold will be " "expiring soon.") message += "\n\n" + renew_msg - send_system_message(account, subject, message) + message = append_random_bottlecap_phrase(message) + + send_system_message(account, subject, message, + distinguished='gold-auto') if verbose: for exp_date in sorted(expiration_dates.keys()): diff --git a/r2/r2/models/builder.py b/r2/r2/models/builder.py index ad25d9dea..80cdba83f 100755 --- a/r2/r2/models/builder.py +++ b/r2/r2/models/builder.py @@ -137,7 +137,8 @@ class Builder(object): if hasattr(item, "distinguished"): if item.distinguished == 'yes': w.distinguished = 'moderator' - elif item.distinguished in ('admin', 'special', 'gold'): + elif item.distinguished in ('admin', 'special', + 'gold', 'gold-auto'): w.distinguished = item.distinguished try: diff --git a/r2/r2/models/gold.py b/r2/r2/models/gold.py index fdfbd2e5c..3c6b8f4fe 100644 --- a/r2/r2/models/gold.py +++ b/r2/r2/models/gold.py @@ -38,11 +38,17 @@ from sqlalchemy.types import DateTime, Integer, String from xml.dom.minidom import Document from r2.lib.utils import tup, randstr from httplib import HTTPSConnection +import re +from random import choice from urlparse import urlparse from time import time import socket, base64 from BeautifulSoup import BeautifulStoneSoup +from r2.lib.db.tdb_cassandra import NotFound +from r2.models.subreddit import Frontpage +from r2.models.wiki import WikiPage + gold_bonus_cutoff = datetime(2010,7,27,0,0,0,0,g.tz) ENGINE_NAME = 'authorize' @@ -454,3 +460,25 @@ def retrieve_gold_transaction(transaction_id): def update_gold_transaction(transaction_id, status): rp = gold_table.update(gold_table.c.trans_id == str(transaction_id), values={gold_table.c.status: status}).execute() + +def append_random_bottlecap_phrase(message): + """Appends a random "bottlecap" phrase from the wiki page. + + The wiki page should be an unordered list with each item a separate + bottlecap. + """ + + bottlecap = None + try: + wp = WikiPage.get(Frontpage, g.wiki_page_gold_bottlecaps) + + split_list = re.split('^[*-] ', wp.content, flags=re.MULTILINE) + choices = [item.strip() for item in split_list if item.strip()] + if len(choices): + bottlecap = choice(choices) + except NotFound: + pass + + if bottlecap: + message += '\n\n> ' + bottlecap + return message diff --git a/r2/r2/public/static/css/reddit.less b/r2/r2/public/static/css/reddit.less index 73c4741eb..156446ea1 100755 --- a/r2/r2/public/static/css/reddit.less +++ b/r2/r2/public/static/css/reddit.less @@ -1921,6 +1921,22 @@ textarea.gray { color: gray; } } } +.message.gold-auto { + blockquote { + background-color: #fafafa; + border: 0; + padding: 4px; + margin-left: 0; + margin-top: 1em; + font-style: italic; + font-size: 0.8em; + color: #808080; + + p { margin: 2px; } + strong { font-style: inherit; } + } +} + .clippy img { float: left; } diff --git a/r2/r2/templates/message.html b/r2/r2/templates/message.html index 3cc2369da..f4fdcd2ce 100644 --- a/r2/r2/templates/message.html +++ b/r2/r2/templates/message.html @@ -39,7 +39,7 @@ <%def name="thing_css_class(what)" buffered="True"> -${parent.thing_css_class(what)} ${"new" if thing.new else ""} ${"was-comment" if thing.was_comment else ""} ${"recipient" if thing.recipient else ""} ${"message-reply" if getattr(thing, "is_child", False) else ""} ${"message-parent" if getattr(thing, "is_parent", False) else ""} ${"focal" if getattr(thing, "focal", False) else ""} ${"gold" if getattr(thing, "distinguished", "") == "gold" else ""} +${parent.thing_css_class(what)} ${"new" if thing.new else ""} ${"was-comment" if thing.was_comment else ""} ${"recipient" if thing.recipient else ""} ${"message-reply" if getattr(thing, "is_child", False) else ""} ${"message-parent" if getattr(thing, "is_parent", False) else ""} ${"focal" if getattr(thing, "focal", False) else ""} ${"gold" if getattr(thing, "distinguished", "") == "gold" else ""} ${"gold-auto" if getattr(thing, "distinguished", "") == "gold-auto" else ""} <%def name="tagline(collapse=False)">