def post_if_goal_reached(date): # bail out if this day's already been submitted for link in get_recent_name_submissions(): if link.revenue_date == date: return revenue = gold_revenue_multi([date]).get(date, 0) goal = gold_goal_on(date) percent = revenue / float(goal) bucket = int(percent) if bucket == 0: return buyer_count = len(gold_buyers_on(date)) template_wp = WikiPage.get(SERVERNAME_SR, "templates/selftext") template = random.choice(template_wp._get("content").split("\r\n---\r\n")) boilerplate = WikiPage.get(SERVERNAME_SR, "templates/boilerplate")._get("content") selftext_template = template + "\n\n---\n\n" + boilerplate link = Link._submit( is_self=True, title=date.strftime("%a %Y-%m-%d"), content=selftext_template % { "percent": int(percent * 100), "buyers": buyer_count, }, author=SYSTEM_ACCOUNT, sr=SERVERNAME_SR, ip="127.0.0.1", spam=False, ) link.flair_text = "Name pending..." link.flair_css_class = "goal-bucket-%d-active" % bucket link.revenue_date = date link.revenue_bucket = bucket link.server_names = [] link._commit() UPVOTE = True queries.queue_vote(SYSTEM_ACCOUNT, link, UPVOTE, "127.0.0.1") queries.new_link(link) link.update_search_index() template = WikiPage.get(SERVERNAME_SR, "templates/notification-message")._get("content") subject_template, sep, body_template = template.partition("\r\n") for id in gold_buyers_on(date): recipient = Account._byID(id, data=True) send_system_message( recipient, subject_template, body_template % { "percent": int(percent * 100), "buyers": buyer_count, "user": recipient.name, "link": link.url, }, )
def post_if_goal_reached(date): # bail out if this day's already been submitted for link in get_recent_name_submissions(): if link.revenue_date == date: return revenue = gold_revenue_multi([date]).get(date, 0) goal = gold_goal_on(date) percent = revenue / float(goal) bucket = int(percent) if bucket == 0: return buyer_count = len(gold_buyers_on(date)) template_wp = WikiPage.get(SERVERNAME_SR, "templates/selftext") template = random.choice(template_wp._get("content").split("\r\n---\r\n")) boilerplate = WikiPage.get(SERVERNAME_SR, "templates/boilerplate")._get("content") selftext_template = template + "\n\n---\n\n" + boilerplate link = Link._submit( is_self=True, title=date.strftime("%a %Y-%m-%d"), content=selftext_template % { "percent": int(percent * 100), "buyers": buyer_count, }, author=SYSTEM_ACCOUNT, sr=SERVERNAME_SR, ip="127.0.0.1", spam=False, ) link.flair_text = "Name pending..." link.flair_css_class = "goal-bucket-%d-active" % bucket link.revenue_date = date link.revenue_bucket = bucket link.server_names = [] link._commit() queries.new_link(link) link.update_search_index() template = WikiPage.get(SERVERNAME_SR, "templates/notification-message")._get("content") subject_template, sep, body_template = template.partition("\r\n") for id in gold_buyers_on(date): recipient = Account._byID(id, data=True) send_system_message( recipient, subject_template, body_template % { "percent": int(percent * 100), "buyers": buyer_count, "user": recipient.name, "link": link.url, }, )
def activate_names(link, names): for comment, name in names: # find a slot to assign a name to. we'll prefer nodes that are # currently empty, and failing that find the least-recently-modified # node. ROOT = "/gold/server-names" slot_names = g.zookeeper.get_children(ROOT) slots = [(slot_name, g.zookeeper.get(os.path.join(ROOT, slot_name))) for slot_name in slot_names] slots.sort(key=lambda (path, (data, stat)): (bool(data), stat.mtime)) slot_path = os.path.join(ROOT, slots[0][0]) comment_data = { 'name': str(name), 'permalink': comment.make_permalink_slow() } g.zookeeper.set(slot_path, json.dumps(comment_data)) lock = g.zookeeper.Lock(slot_path) lock_contenders = lock.contenders() old_name = lock_contenders[0] if lock_contenders else "" old_name = old_name or "one of our servers" # reply to the user wp = WikiPage.get(SERVERNAME_SR, "templates/success-reply") template = random.choice(wp._get("content").split("\r\n---\r\n")) comment, inbox_rel = Comment._new( author=SYSTEM_ACCOUNT, link=link, parent=comment, body=template % { "old-name": old_name, "new-name": name, }, ip="127.0.0.1", ) queries.new_comment(comment, inbox_rel) # update the link's text wp = WikiPage.get(SERVERNAME_SR, "templates/goldisms") goldism = random.choice(wp._get("content").split("\r\n---\r\n")) wp = WikiPage.get(SERVERNAME_SR, "templates/selftext-success") template = wp._get("content") link.selftext = template % { "old-name": old_name, "new-name": name, "goldism": goldism, } link._commit()
def activate_names(link, names): for comment, name in names: # find a slot to assign a name to. we'll prefer nodes that are # currently empty, and failing that find the least-recently-modified # node. ROOT = "/gold/server-names" slot_names = g.zookeeper.get_children(ROOT) slots = [(slot_name, g.zookeeper.get(os.path.join(ROOT, slot_name))) for slot_name in slot_names] slots.sort(key=lambda (path, (data, stat)): (bool(data), stat.mtime)) slot_path = os.path.join(ROOT, slots[0][0]) comment_data = {'name': str(name), 'permalink': comment.make_permalink_slow()} g.zookeeper.set(slot_path, json.dumps(comment_data)) lock = g.zookeeper.Lock(slot_path) lock_contenders = lock.contenders() old_name = lock_contenders[0] if lock_contenders else "" old_name = old_name or "one of our servers" # reply to the user wp = WikiPage.get(SERVERNAME_SR, "templates/success-reply") template = random.choice(wp._get("content").split("\r\n---\r\n")) comment, inbox_rel = Comment._new( author=SYSTEM_ACCOUNT, link=link, parent=comment, body=template % { "old-name": old_name, "new-name": name, }, ip="127.0.0.1", ) queries.queue_vote(SYSTEM_ACCOUNT, comment, dir=True, ip="127.0.0.1") queries.new_comment(comment, inbox_rel) # update the link's text wp = WikiPage.get(SERVERNAME_SR, "templates/goldisms") goldism = random.choice(wp._get("content").split("\r\n---\r\n")) wp = WikiPage.get(SERVERNAME_SR, "templates/selftext-success") template = wp._get("content") link.selftext = template % { "old-name": old_name, "new-name": name, "goldism": goldism, } link._commit()
def GET_policy_page(self, page, requested_rev): if c.render_style == 'compact': self.redirect('/wiki/' + page) if page == 'privacypolicy': wiki_name = g.wiki_page_privacy_policy pagename = _('privacy policy') elif page == 'useragreement': wiki_name = g.wiki_page_user_agreement pagename = _('user agreement') elif page == 'contentpolicy': wiki_name = g.wiki_page_content_policy pagename = _('content policy') else: abort(404) wp = WikiPage.get(Frontpage, wiki_name) revs = list(wp.get_revisions()) # collapse minor edits into revisions with reasons rev_info = [] last_edit = None for rev in revs: if rev.is_hidden: continue if not last_edit: last_edit = rev if rev._get('reason'): rev_info.append({ 'id': str(last_edit._id), 'title': rev._get('reason'), }) last_edit = None if requested_rev: try: display_rev = WikiRevision.get(requested_rev, wp._id) except (tdb_cassandra.NotFound, WikiBadRevision): abort(404) else: display_rev = revs[0] doc_html = wikimarkdown(display_rev.content, include_toc=False) soup = BeautifulSoup(doc_html.decode('utf-8')) toc = generate_table_of_contents(soup, prefix='section') self._number_sections(soup) self._linkify_headings(soup) content = PolicyView( body_html=unsafe(soup), toc_html=unsafe(toc), revs=rev_info, display_rev=str(display_rev._id), ) return PolicyPage( pagename=pagename, content=content, ).render()
def change_css(self, content, parsed, prev=None, reason=None, author=None, force=False): from r2.models import ModAction author = author if author else c.user.name if content is None: content = '' try: wiki = WikiPage.get(self, 'config/stylesheet') except tdb_cassandra.NotFound: wiki = WikiPage.create(self, 'config/stylesheet') wr = wiki.revise(content, previous=prev, author=author, reason=reason, force=force) minified = cssmin(parsed) if minified: if g.static_stylesheet_bucket: digest = hashlib.sha1(minified).digest() self.stylesheet_hash = ( base64.urlsafe_b64encode(digest).rstrip("=")) s3cp.send_file( g.static_stylesheet_bucket, self.static_stylesheet_name, minified, content_type="text/css", never_expire=True, replace=False, ) self.stylesheet_contents = "" self.stylesheet_modified = None else: self.stylesheet_hash = hashlib.md5(minified).hexdigest() self.stylesheet_contents = minified self.stylesheet_modified = datetime.datetime.now(g.tz) else: self.stylesheet_contents = "" self.stylesheet_hash = "" self.stylesheet_modified = datetime.datetime.now(g.tz) self.stylesheet_contents_user = "" # reads from wiki; ensure pg clean self._commit() ModAction.create(self, c.user, action='wikirevise', details='Updated subreddit stylesheet') return wr
def wiki_template(template_slug, sr=None): """Pull content from a subreddit's wiki page for internal use.""" if not sr: sr = Subreddit._by_name(g.default_sr) try: wiki = WikiPage.get(sr, "templates/%s" % template_slug) except tdb_cassandra.NotFound: return None return wiki._get("content")
def change_css(self, content, parsed, prev=None, reason=None, author=None, force=False): from r2.models import ModAction author = author if author else c.user.name if content is None: content = '' try: wiki = WikiPage.get(self, 'config/stylesheet') except tdb_cassandra.NotFound: wiki = WikiPage.create(self, 'config/stylesheet') wr = wiki.revise(content, previous=prev, author=author, reason=reason, force=force) self.stylesheet_contents = parsed self.stylesheet_hash = md5(parsed).hexdigest() set_last_modified(self, 'stylesheet_contents') c.site._commit() ModAction.create(self, c.user, action='wikirevise', details='Updated subreddit stylesheet') return wr
def validpage(self, page): try: wp = WikiPage.get(c.site, page) if self.restricted and wp.restricted: if not (c.is_wiki_mod or wp.special): self.set_error('RESTRICTED_PAGE', code=403) raise AbortWikiError if not this_may_view(wp): self.set_error('MAY_NOT_VIEW', code=403) raise AbortWikiError return wp except tdb_cassandra.NotFound: if self.required: self.set_error('PAGE_NOT_FOUND', code=404) raise AbortWikiError return None
def wiki_template(template_slug, sr=None): """Pull content from a subreddit's wiki page for internal use.""" if not sr: try: sr = Subreddit._by_name(g.default_sr) except NotFound: # a freshly-build dev instance with no pre-populated data # will die on account creation (and other user actions) if a # default is not set. Guard against that for testing. if g.debug: return raise try: wiki = WikiPage.get(sr, "templates/%s" % template_slug) except tdb_cassandra.NotFound: return None return wiki._get("content")
def change_css(self, content, parsed, prev=None, reason=None, author=None, force=False): from r2.models import ModAction author = author if author else c.user.name if content is None: content = "" try: wiki = WikiPage.get(self, "config/stylesheet") except tdb_cassandra.NotFound: wiki = WikiPage.create(self, "config/stylesheet") wr = wiki.revise(content, previous=prev, author=author, reason=reason, force=force) minified = cssmin(parsed) if minified: if g.static_stylesheet_bucket: digest = hashlib.sha1(minified).digest() self.stylesheet_hash = base64.urlsafe_b64encode(digest).rstrip("=") s3cp.send_file( g.static_stylesheet_bucket, self.static_stylesheet_name, minified, content_type="text/css", never_expire=True, replace=False, ) self.stylesheet_contents = "" self.stylesheet_modified = None else: self.stylesheet_hash = hashlib.md5(minified).hexdigest() self.stylesheet_contents = minified self.stylesheet_modified = datetime.datetime.now(g.tz) else: self.stylesheet_contents = "" self.stylesheet_hash = "" self.stylesheet_modified = datetime.datetime.now(g.tz) self.stylesheet_contents_user = "" # reads from wiki; ensure pg clean self._commit() ModAction.create(self, c.user, action="wikirevise", details="Updated subreddit stylesheet") return wr
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
def stylesheet_contents_user(self): try: return WikiPage.get(self, 'config/stylesheet')._get('content','') except tdb_cassandra.NotFound: return self._t.get('stylesheet_contents_user')
def prev_stylesheet(self): try: return WikiPage.get(self, "config/stylesheet")._get("revision", "") except tdb_cassandra.NotFound: return ""
def stylesheet_contents_user(self): try: return WikiPage.get(self, "config/stylesheet")._get("content", "") except tdb_cassandra.NotFound: return self._t.get("stylesheet_contents_user")
def public_description(self): try: return WikiPage.get(self, 'config/description')._get('content','') except tdb_cassandra.NotFound: return self._t.get('public_description')
def prev_public_description_id(self): try: return WikiPage.get(self, 'config/description')._get('revision','') except tdb_cassandra.NotFound: return ''
def prev_stylesheet(self): try: return WikiPage.get(self, 'config/stylesheet')._get('revision', '') except tdb_cassandra.NotFound: return ''
def stylesheet_contents_user(self): try: return WikiPage.get(self, 'config/stylesheet')._get('content', '') except tdb_cassandra.NotFound: return self._t.get('stylesheet_contents_user')
def prev_stylesheet(self): try: return WikiPage.get(self, 'config/stylesheet')._get('revision','') except tdb_cassandra.NotFound: return ''
def sr_admin_menu(self): buttons = [] is_single_subreddit = not isinstance(c.site, (ModSR, MultiReddit)) is_admin = c.user_is_loggedin and c.user_is_admin is_moderator_with_perms = lambda *perms: ( is_admin or c.site.is_moderator_with_perms(c.user, *perms)) if is_single_subreddit and is_moderator_with_perms('config'): buttons.append(NavButton(menu.community_settings, css_class="reddit-edit", dest="edit")) buttons.append(NavButton(menu.edit_stylesheet, css_class="edit-stylesheet", dest="stylesheet")) if is_moderator_with_perms('mail'): buttons.append(NamedButton("modmail", dest="message/inbox", css_class="moderator-mail")) if is_single_subreddit: if is_moderator_with_perms('access'): buttons.append(NamedButton("moderators", css_class="reddit-moderators")) if not c.site.hide_contributors: buttons.append(NavButton( menu.contributors, "contributors", css_class="reddit-contributors")) buttons.append(NamedButton("traffic", css_class="reddit-traffic")) if is_moderator_with_perms('posts'): buttons += [NamedButton("modqueue", css_class="reddit-modqueue"), NamedButton("reports", css_class="reddit-reported"), NamedButton("spam", css_class="reddit-spam"), NamedButton("edited", css_class="reddit-edited")] if is_single_subreddit: if is_moderator_with_perms('access'): buttons.append(NamedButton("banned", css_class="reddit-ban")) if is_moderator_with_perms('flair'): buttons.append(NamedButton("flair", css_class="reddit-flair")) if is_single_subreddit and is_moderator_with_perms('wiki'): # append automod button if they have an AutoMod configuration try: WikiPage.get(c.site, "config/automoderator") buttons.append(NamedButton( "automod", dest="../wiki/config/automoderator", css_class="reddit-automod", )) except tdb_cassandra.NotFound: pass buttons.append(NamedButton("log", css_class="reddit-moderationlog")) if is_moderator_with_perms('posts'): buttons.append( NamedButton("unmoderated", css_class="reddit-unmoderated")) return SideContentBox(_('moderation tools'), [NavMenu(buttons, type="flat_vert", base_path="/about/", css_class="icon-menu", separator="")], _id="moderation_tools", collapsible=True)