def POST_edit(self, form, jquery, title, description, resources, nsfw): """Configure the thread. Requires the `settings` permission for this thread. See also: [/live/*thread*/about.json](#GET_live_{thread}_about.json). """ if not is_event_configuration_valid(form): return changes = {} if title != c.liveupdate_event.title: changes["title"] = title if description != c.liveupdate_event.description: changes["description"] = description changes["description_html"] = safemarkdown(description, nofollow=True) or "" if resources != c.liveupdate_event.resources: changes["resources"] = resources changes["resources_html"] = safemarkdown(resources, nofollow=True) or "" _broadcast(type="settings", payload=changes) c.liveupdate_event.title = title c.liveupdate_event.description = description c.liveupdate_event.resources = resources c.liveupdate_event.nsfw = nsfw c.liveupdate_event._commit() form.set_html(".status", _("saved")) form.refresh()
def thing_attr(self, thing, attr): from r2.lib.media import get_media_embed if attr in ("media_embed", "secure_media_embed"): media_object = getattr(thing, attr.replace("_embed", "_object")) if media_object and not isinstance(media_object, basestring): media_embed = get_media_embed(media_object) if media_embed: return { "scrolling": media_embed.scrolling, "width": media_embed.width, "height": media_embed.height, "content": media_embed.content, } return {} elif attr == "clicked": # this hasn't been used in years. return False elif attr == "editted" and not isinstance(thing.editted, bool): return (time.mktime(thing.editted.astimezone(pytz.UTC).timetuple()) - time.timezone) elif attr == 'subreddit': return thing.subreddit.name elif attr == 'subreddit_id': return thing.subreddit._fullname elif attr == 'selftext': if not thing.expunged: return thing.selftext else: return '' elif attr == 'selftext_html': if not thing.expunged: return safemarkdown(thing.selftext) else: return safemarkdown(_("[removed]")) return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): from r2.lib.scraper import get_media_embed if attr == "media_embed": if (thing.media_object and not isinstance(thing.media_object, basestring)): media_embed = get_media_embed(thing.media_object) if media_embed: return dict(scrolling = media_embed.scrolling, width = media_embed.width, height = media_embed.height, content = media_embed.content) return dict() elif attr == "editted" and not isinstance(thing.editted, bool): return (time.mktime(thing.editted.astimezone(pytz.UTC).timetuple()) - time.timezone) elif attr == 'subreddit': return thing.subreddit.name elif attr == 'subreddit_id': return thing.subreddit._fullname elif attr == 'selftext': if not thing.expunged: return thing.selftext else: return '' elif attr == 'selftext_html': if not thing.expunged: return safemarkdown(thing.selftext) else: return safemarkdown(_("[removed]")) return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): if attr == "_fullname": return "LiveUpdateEvent_" + thing._id elif attr == "viewer_count": if thing.state == "live": return thing.active_visitors else: return None elif attr == "viewer_count_fuzzed": if thing.state == "live": return thing.active_visitors_fuzzed else: return None elif attr == "total_views": # this requires an extra query, so we'll only show it in places # where we're just getting one event. if not hasattr(thing, "total_views"): return None return thing.total_views elif attr == "description_html": return filters.spaceCompress( filters.safemarkdown(thing.description, nofollow=True) or "") elif attr == "resources_html": return filters.spaceCompress( filters.safemarkdown(thing.resources, nofollow=True) or "") elif attr == "websocket_url": if thing.state == "live": return websockets.make_url("/live/" + thing._id, max_age=24 * 60 * 60) else: return None else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): if attr not in self._public_attrs and not thing.can_view(c.user): return None if attr == "_ups" and thing.hide_subscribers: return 0 # Don't return accounts_active counts in /subreddits elif (attr == "accounts_active" and isinstance(c.site, SubSR)): return None elif attr == 'description_html': return safemarkdown(thing.description) elif attr == 'public_description_html': return safemarkdown(thing.public_description) elif attr in ('is_banned', 'is_contributor', 'is_moderator', 'is_subscriber'): if c.user_is_loggedin: check_func = getattr(thing, attr) return bool(check_func(c.user)) return None elif attr == 'submit_text_html': return safemarkdown(thing.submit_text) elif attr == 'community_rules': if thing.community_rules: return thing.community_rules.split('\n') return [] else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): if attr not in self._public_attrs and not thing.can_view(c.user): return None if attr == "_ups" and thing.hide_subscribers: return 0 # Don't return accounts_active counts in /subreddits elif attr == "accounts_active" and isinstance(c.site, SubSR): return None elif attr == "description_html": return safemarkdown(thing.description) elif attr == "public_description_html": return safemarkdown(thing.public_description) elif attr in ("is_banned", "is_contributor", "is_moderator", "is_subscriber"): if c.user_is_loggedin: check_func = getattr(thing, attr) return bool(check_func(c.user)) return None elif attr == "submit_text_html": return safemarkdown(thing.submit_text) elif attr == "community_rules": if thing.community_rules: return thing.community_rules.split("\n") return [] elif attr == "user_sr_style_enabled": if c.user_is_loggedin: return c.user.use_subreddit_style(thing) else: return True else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): if attr == "_fullname": return "LiveUpdateEvent_" + thing._id elif attr == "viewer_count": if thing.state == "live": return thing.active_visitors else: return None elif attr == "viewer_count_fuzzed": if thing.state == "live": return thing.active_visitors_fuzzed else: return None elif attr == "description_html": return filters.spaceCompress( filters.safemarkdown(thing.description, nofollow=True) or "") elif attr == "resources_html": return filters.spaceCompress( filters.safemarkdown(thing.resources, nofollow=True) or "") elif attr == "websocket_url": if thing.state == "live": return websockets.make_url( "/live/" + c.liveupdate_event._id, max_age=24 * 60 * 60) else: return None else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): if attr == "_fullname": return "LiveUpdateEvent_" + thing._id elif attr == "viewer_count": if thing.state == "live": return thing.active_visitors else: return None elif attr == "viewer_count_fuzzed": if thing.state == "live": return thing.active_visitors_fuzzed else: return None elif attr == "description_html": return filters.spaceCompress( filters.safemarkdown(thing.description, nofollow=True) or "") elif attr == "resources_html": return filters.spaceCompress( filters.safemarkdown(thing.resources, nofollow=True) or "") elif attr == "websocket_url": if thing.state == "live": return websockets.make_url( "/live/" + thing._id, max_age=24 * 60 * 60) else: return None else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def POST_edit(self, form, jquery, title, description, resources, nsfw): """Configure the thread. Requires the `settings` permission for this thread. See also: [/live/*thread*/about.json](#GET_live_{thread}_about.json). """ if not is_event_configuration_valid(form): return changes = {} if title != c.liveupdate_event.title: changes["title"] = title if description != c.liveupdate_event.description: changes["description"] = description changes["description_html"] = safemarkdown(description, nofollow=True) or "" if resources != c.liveupdate_event.resources: changes["resources"] = resources changes["resources_html"] = safemarkdown(resources, nofollow=True) or "" if nsfw != c.liveupdate_event.nsfw: changes["nsfw"] = nsfw if changes: _broadcast(type="settings", payload=changes) c.liveupdate_event.title = title c.liveupdate_event.description = description c.liveupdate_event.resources = resources c.liveupdate_event.nsfw = nsfw c.liveupdate_event._commit() form.set_html(".status", _("saved")) form.refresh()
def thing_attr(self, thing, attr): if attr == "_fullname": return "LiveUpdateEvent_" + thing._id elif attr == "viewer_count": if thing.state == "live": return thing.active_visitors else: return None elif attr == "viewer_count_fuzzed": if thing.state == "live": return thing.active_visitors_fuzzed else: return None elif attr == "total_views": # this requires an extra query, so we'll only show it in places # where we're just getting one event. if not hasattr(thing, "total_views"): return None return thing.total_views elif attr == "description_html": return filters.spaceCompress( filters.safemarkdown(thing.description, nofollow=True) or "") elif attr == "resources_html": return filters.spaceCompress( filters.safemarkdown(thing.resources, nofollow=True) or "") elif attr == "websocket_url": if thing.state == "live": return websockets.make_url( "/live/" + thing._id, max_age=24 * 60 * 60) else: return None else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def POST_edit(self, form, jquery, title, description, timezone): if form.has_errors("title", errors.NO_TEXT, errors.TOO_LONG): return if form.has_errors("description", errors.TOO_LONG): return if form.has_errors("timezone", errors.INVALID_TIMEZONE): return changes = {} if title != c.liveupdate_event.title: changes["title"] = title if description != c.liveupdate_event.description: changes["description"] = safemarkdown(description, wrap=False) send_websocket_broadcast(type="settings", payload=changes) c.liveupdate_event.title = title c.liveupdate_event.description = description c.liveupdate_event.timezone = timezone.zone c.liveupdate_event._commit() form.set_html(".status", _("saved")) form.refresh()
def thing_attr(self, thing, attr): if attr == "description_html": # if safemarkdown is passed a falsy string it returns None :/ description_html = safemarkdown(thing.description_md) or '' return description_html else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): from r2.models import Comment, Link, Message if attr == "was_comment": return thing.was_comment elif attr == "context": return ("" if not thing.was_comment else thing.permalink + "?context=3") elif attr == "dest": if thing.to_id: return thing.to.name else: return "#" + thing.subreddit.name elif attr == "subreddit": if thing.sr_id: return thing.subreddit.name return None elif attr == "body_html": return safemarkdown(thing.body) elif attr == "author" and getattr(thing, "hide_author", False): return None elif attr == "parent_id": if thing.was_comment: if getattr(thing, "parent_id", None): return make_fullname(Comment, thing.parent_id) else: return make_fullname(Link, thing.link_id) elif getattr(thing, "parent_id", None): return make_fullname(Message, thing.parent_id) elif attr == "first_message_name": if getattr(thing, "first_message", None): return make_fullname(Message, thing.first_message) return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): from r2.models import Comment, Link, Subreddit if attr == 'link_id': return make_fullname(Link, thing.link_id) elif attr == "controversiality": return 1 if thing.is_controversial else 0 elif attr == "editted" and not isinstance(thing.editted, bool): return ( time.mktime(thing.editted.astimezone(pytz.UTC).timetuple()) - time.timezone) elif attr == 'subreddit': return thing.subreddit.name elif attr == 'subreddit_id': return thing.subreddit._fullname elif attr == "parent_id": if getattr(thing, "parent_id", None): return make_fullname(Comment, thing.parent_id) else: return make_fullname(Link, thing.link_id) elif attr == "body_html": return spaceCompress(safemarkdown(thing.body)) elif attr == "gilded": return thing.gildings return ThingJsonTemplate.thing_attr(self, thing, attr)
def __init__(self, content, edit_by, edit_date, may_revise=False, page=None, diff=None, renderer='wiki', sr_path=True): self.page_content_md = content if renderer == 'wiki': self.page_content = wikimarkdown(content) elif renderer == 'reddit': self.page_content = safemarkdown(content) elif renderer == 'stylesheet': self.page_content = SubredditStylesheetSource(content).render() elif renderer == "automoderator": self.page_content = AutoModeratorConfig(content).render() elif renderer == "rawcode": self.page_content = RawCode(content).render() self.renderer = renderer self.page = page self.diff = diff self.edit_by = edit_by self.may_revise = may_revise self.edit_date = edit_date self.base_url = c.wiki_base_url self.sr_path = sr_path Templated.__init__(self)
def thing_attr(self, thing, attr): from r2.models import Comment, Link, Subreddit if attr == 'link_id': return make_fullname(Link, thing.link_id) elif attr == "controversiality": return 1 if thing.is_controversial else 0 elif attr == "editted" and not isinstance(thing.editted, bool): return (time.mktime(thing.editted.astimezone(pytz.UTC).timetuple()) - time.timezone) elif attr == 'subreddit': return thing.subreddit.name elif attr == 'subreddit_id': return thing.subreddit._fullname elif attr == "parent_id": if getattr(thing, "parent_id", None): return make_fullname(Comment, thing.parent_id) else: return make_fullname(Link, thing.link_id) elif attr == "body_html": return spaceCompress(safemarkdown(thing.body)) elif attr == "gilded": return thing.gildings elif attr == "archived": return not thing.votable return ThingJsonTemplate.thing_attr(self, thing, attr)
def GET_comments(self, link): if not link: self.abort404() if not link.subreddit_slow.can_view(c.user): abort(403, 'forbidden') links = list(wrap_links(link)) if not links: # they aren't allowed to see this link return abort(403, 'forbidden') link = links[0] wrapper = make_wrapper(render_class = StarkComment, target = "_top") b = TopCommentBuilder(link, CommentSortMenu.operator('confidence'), num=10, wrap=wrapper) listing = NestedListing(b, parent_name=link._fullname) raw_bar = strings.comments_panel_text % dict( fd_link=link.permalink) md_bar = safemarkdown(raw_bar, target="_top") res = RedditMin(content=CommentsPanel(link=link, listing=listing.listing(), expanded=auto_expand_panel(link), infobar=md_bar)) return res.render()
def GET_comments(self, link): if not link: self.abort404() if not link.subreddit_slow.can_view(c.user): abort(403, 'forbidden') links = list(wrap_links(link)) if not links: # they aren't allowed to see this link return self.abort(403, 'forbidden') link = links[0] wrapper = make_wrapper(render_class = StarkComment, target = "_top") b = TopCommentBuilder(link, CommentSortMenu.operator('confidence'), wrap = wrapper) listing = NestedListing(b, num = 10, # TODO: add config var parent_name = link._fullname) raw_bar = strings.comments_panel_text % dict( fd_link=link.permalink) md_bar = safemarkdown(raw_bar, target="_top") res = RedditMin(content=CommentsPanel(link=link, listing=listing.listing(), expanded=auto_expand_panel(link), infobar=md_bar)) return res.render()
def GET_document(self): try: c.errors = c.errors or ErrorSet() # clear cookies the old fashioned way c.cookies = Cookies() code = request.GET.get('code', '') try: code = int(code) except ValueError: code = 404 srname = request.GET.get('srname', '') takedown = request.GET.get('takedown', "") # StatusBasedRedirect will override this anyway, but we need this # here for pagecache to see. response.status_int = code if srname: c.site = Subreddit._by_name(srname) if request.GET.has_key('allow_framing'): c.allow_framing = bool(request.GET['allow_framing'] == '1') if code in (204, 304): # NEVER return a content body on 204/304 or downstream # caches may become very confused. if request.GET.has_key('x-sup-id'): x_sup_id = request.GET.get('x-sup-id') if '\r\n' not in x_sup_id: response.headers['x-sup-id'] = x_sup_id return "" elif c.render_style not in self.allowed_render_styles: return str(code) elif c.render_style in extensions.API_TYPES: data = request.environ.get('extra_error_data', {'error': code}) if request.environ.get("WANT_RAW_JSON"): return scriptsafe_dumps(data) return websafe_json(json.dumps(data)) elif takedown and code == 404: link = Link._by_fullname(takedown) return pages.TakedownPage(link).render() elif code == 403: return self.send403() elif code == 429: return self.send429() elif code == 500: randmin = {'admin': random.choice(self.admins)} failien_url = make_failien_url() sad_message = safemarkdown(rand_strings.sadmessages % randmin) return redditbroke % (failien_url, sad_message) elif code == 503: return self.send503() elif c.site: return self.send404() else: return "page not found" except Exception as e: return handle_awful_failure("ErrorController.GET_document: %r" % e)
def to_serializable(self, sr, author, current_user=None): return { 'id': to36(self.id), 'date': self.date.isoformat(), 'author': to_serializable_author(author, sr, current_user, self.is_author_hidden), 'body': safemarkdown(self.body), 'isInternal': self.is_internal }
def thing_attr(self, thing, attr): if attr == "_ups" and thing.hide_subscribers: return 0 # Don't return accounts_active counts in /subreddits elif (attr == "accounts_active" and isinstance(c.site, SubSR)): return None elif attr == 'description_html': return safemarkdown(thing.description) else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): if attr not in self._public_attrs and not thing.can_view(c.user): return None if attr == "_ups" and thing.hide_subscribers: return 0 # Don't return accounts_active counts in /subreddits elif (attr == "accounts_active" and isinstance(c.site, SubSR)): return None elif attr == 'description_html': return safemarkdown(thing.description) elif attr == 'public_description_html': return safemarkdown(thing.public_description) elif attr == "is_moderator": if c.user_is_loggedin: return thing.moderator return None elif attr == "is_contributor": if c.user_is_loggedin: return thing.contributor return None elif attr == "is_subscriber": if c.user_is_loggedin: return thing.subscriber return None elif attr == 'is_banned': if c.user_is_loggedin: return thing.banned return None elif attr == 'submit_text_html': return safemarkdown(thing.submit_text) elif attr == 'community_rules': if thing.community_rules: return thing.community_rules.split('\n') return [] elif attr == 'user_sr_style_enabled': if c.user_is_loggedin: return c.user.use_subreddit_style(thing) else: return True else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): if attr == "was_comment": return thing.was_comment elif attr == "context": return ("" if not thing.was_comment else thing.permalink + "?context=3") elif attr == "dest": return thing.to.name elif attr == "body_html": return safemarkdown(thing.body) return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): if attr in self._private_attrs and not thing.can_view(c.user): return None if attr == "_ups" and thing.hide_subscribers: return 0 # Don't return accounts_active counts in /subreddits elif attr == "accounts_active" and isinstance(c.site, SubSR): return None elif attr == "description_html": return safemarkdown(thing.description) elif attr in ("is_banned", "is_contributor", "is_moderator", "is_subscriber"): if c.user_is_loggedin: check_func = getattr(thing, attr) return bool(check_func(c.user)) return None elif attr == "submit_text_html": return safemarkdown(thing.submit_text) else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): # Don't reveal revenue information via /r/lounge's subscribers if attr == "_ups" and g.lounge_reddit and thing.name == g.lounge_reddit: return 0 # Don't return accounts_active counts in /subreddits elif attr == "accounts_active" and isinstance(c.site, SubSR): return None elif attr == "description_html": return safemarkdown(thing.description) else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): # Don't reveal revenue information via /r/lounge's subscribers if (attr == "_ups" and g.lounge_reddit and thing.name == g.lounge_reddit): return 0 # Don't return accounts_active counts in /subreddits elif (attr == "accounts_active" and isinstance(c.site, SubSR)): return None elif attr == 'description_html': return safemarkdown(thing.description) else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): if attr == "_fullname": return "LiveUpdateEvent_" + thing._id elif attr == "viewer_count": return thing.active_visitors elif attr == "viewer_count_fuzzed": return thing.active_visitors_fuzzed elif attr == "description_html": return filters.spaceCompress( filters.safemarkdown(thing.description) or "") else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def send404(self): c.response.status_code = 404 if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] if c.site._spam and not c.user_is_admin: message = (strings.banned_subreddit % dict(link = '/feedback')) res = pages.RedditError(_('this reddit has been banned'), unsafe(safemarkdown(message))) return res.render() else: return pages.Reddit404().render()
def thing_attr(self, thing, attr): if attr == "_id": return str(thing._id) elif attr == "body_html": return filters.spaceCompress(filters.safemarkdown(thing.body, nofollow=True)) elif attr == "author": if not thing.author.deleted: return thing.author.name else: return None elif attr == "stricken": return bool(thing.stricken) return ThingJsonTemplate.thing_attr(self, thing, attr)
def __init__(self, content, edit_by, edit_date, may_revise=False, page=None, diff=None, renderer="wiki"): self.page_content_md = content if renderer == "wiki": self.page_content = wikimarkdown(content) elif renderer == "reddit": self.page_content = safemarkdown(content) elif renderer == "stylesheet": self.page_content = SubredditStylesheetSource(content).render() self.renderer = renderer self.page = page self.diff = diff self.edit_by = edit_by self.may_revise = may_revise self.edit_date = edit_date self.base_url = c.wiki_base_url Templated.__init__(self)
def send404(self): c.response.status_code = 404 if "usable_error_content" in request.environ: return request.environ["usable_error_content"] if c.site.spammy() and not c.user_is_admin: subject = "the subreddit /r/%s has been incorrectly banned" % c.site.name lnk = "/r/redditrequest/submit?url=%s&title=%s" % ( url_escape("http://%s/r/%s" % (g.domain, c.site.name)), ("the subreddit /r/%s has been incorrectly banned" % c.site.name), ) message = strings.banned_subreddit % dict(link=lnk) res = pages.RedditError(_("this reddit has been banned"), unsafe(safemarkdown(message))) return res.render() else: return pages.Reddit404().render()
def thing_attr(self, thing, attr): if attr == "_ups" and thing.hide_subscribers: return 0 # Don't return accounts_active counts in /subreddits elif (attr == "accounts_active" and isinstance(c.site, SubSR)): return None elif attr == 'description_html': return safemarkdown(thing.description) elif attr in ('is_banned', 'is_contributor', 'is_moderator', 'is_subscriber'): if c.user_is_loggedin: check_func = getattr(thing, attr) return bool(check_func(c.user)) return None else: return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): from r2.models import Comment, Link, Subreddit if attr == 'link_id': return make_fullname(Link, thing.link_id) elif attr == 'sr_id': if hasattr(thing, attr): return make_fullname(Subreddit, thing.sr_id) return None elif attr == "parent_id": try: return make_fullname(Comment, thing.parent_id) except AttributeError: return make_fullname(Link, thing.link_id) elif attr == "body_html": return spaceCompress(safemarkdown(thing.body)) return ThingJsonTemplate.thing_attr(self, thing, attr)
def thing_attr(self, thing, attr): from r2.models import Comment, Link, Subreddit if attr == 'link_id': return make_fullname(Link, thing.link_id) elif attr == 'subreddit': return thing.subreddit.name elif attr == 'subreddit_id': return thing.subreddit._fullname elif attr == "parent_id": if getattr(thing, "parent_id", None): return make_fullname(Comment, thing.parent_id) else: return make_fullname(Link, thing.link_id) elif attr == "body_html": return spaceCompress(safemarkdown(thing.body)) return ThingJsonTemplate.thing_attr(self, thing, attr)
def send404(self): c.response.status_code = 404 if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] if c.site._spam and not c.user_is_admin: subject = ("the subreddit /r/%s has been incorrectly banned" % c.site.name) message = ( strings.banned_subreddit % dict(link='/message/compose?to=%s&subject=%s' % (url_escape(g.admin_message_acct), url_escape(subject)))) res = pages.RedditError(_('this reddit has been banned'), unsafe(safemarkdown(message))) return res.render() else: return pages.Reddit404().render()
def send404(self): c.response.status_code = 404 if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] if c.site._spam and not c.user_is_admin: subject = ("the subreddit /r/%s has been incorrectly banned" % c.site.name) message = (strings.banned_subreddit % dict(link = '/message/compose?to=%s&subject=%s' % (url_escape(g.admin_message_acct), url_escape(subject)))) res = pages.RedditError(_('this reddit has been banned'), unsafe(safemarkdown(message))) return res.render() else: return pages.Reddit404().render()
def __init__(self, content, edit_by, edit_date, may_revise=False, page=None, diff=None, renderer='wiki'): self.page_content_md = content if renderer == 'wiki': self.page_content = wikimarkdown(content) elif renderer == 'reddit': self.page_content = safemarkdown(content) elif renderer == 'stylesheet': self.page_content = SubredditStylesheetSource(content).render() self.renderer = renderer self.page = page self.diff = diff self.edit_by = edit_by self.may_revise = may_revise self.edit_date = edit_date self.base_url = c.wiki_base_url Templated.__init__(self)
def thing_attr(self, thing, attr): if attr == "was_comment": return thing.was_comment elif attr == "context": return ("" if not thing.was_comment else thing.permalink + "?context=3") elif attr == "dest": if thing.to_id: return thing.to.name else: return "#" + thing.subreddit.name elif attr == "subreddit": if thing.sr_id: return thing.subreddit.name return None elif attr == "body_html": return safemarkdown(thing.body) return ThingJsonTemplate.thing_attr(self, thing, attr)
def POST_edit(self, form, jquery, title, description): if not is_event_configuration_valid(form): return changes = {} if title != c.liveupdate_event.title: changes["title"] = title if description != c.liveupdate_event.description: changes["description"] = description changes["description_html"] = safemarkdown(description, wrap=False) or "" _broadcast(type="settings", payload=changes) c.liveupdate_event.title = title c.liveupdate_event.description = description c.liveupdate_event._commit() form.set_html(".status", _("saved")) form.refresh()
def send404(self): c.response.status_code = 404 if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] if c.site.spammy() and not c.user_is_admin: subject = ("the subreddit /r/%s has been incorrectly banned" % c.site.name) lnk = ("/r/redditrequest/submit?url=%s&title=%s" % (url_escape("http://%s/r/%s" % (g.domain, c.site.name)), ("the subreddit /r/%s has been incorrectly banned" % c.site.name))) message = strings.banned_subreddit % dict(link=lnk) res = pages.RedditError(_('this reddit has been banned'), unsafe(safemarkdown(message))) return res.render() else: return pages.Reddit404().render()
def __init__(self, content, edit_by, edit_date, may_revise=False, page=None, diff=None, renderer='wiki'): self.page_content_md = content if renderer == 'wiki': self.page_content = wikimarkdown(content) elif renderer == 'reddit': self.page_content = safemarkdown(content) elif renderer == 'stylesheet': self.page_content = SubredditStylesheetSource(content).render() elif renderer == "automoderator": self.page_content = AutoModeratorConfig(content).render() self.renderer = renderer self.page = page self.diff = diff self.edit_by = edit_by self.may_revise = may_revise self.edit_date = edit_date self.base_url = c.wiki_base_url Templated.__init__(self)
def thing_attr(self, thing, attr): from r2.lib.scraper import get_media_embed if attr == "media_embed": if (thing.media_object and not isinstance(thing.media_object, basestring)): media_embed = get_media_embed(thing.media_object) if media_embed: return dict(scrolling = media_embed.scrolling, width = media_embed.width, height = media_embed.height, content = media_embed.content) return dict() elif attr == 'subreddit': return thing.subreddit.name elif attr == 'subreddit_id': return thing.subreddit._fullname elif attr == 'selftext': return thing.selftext elif attr == 'selftext_html': return safemarkdown(thing.selftext) return ThingJsonTemplate.thing_attr(self, thing, attr)
def pollsandmarkdown(text, thing): ret = renderpolls(safemarkdown(text), thing) return ret
def GET_document(self): try: c.errors = c.errors or ErrorSet() # clear cookies the old fashioned way c.cookies = Cookies() code = request.GET.get('code', '') try: code = int(code) except ValueError: code = 404 srname = request.GET.get('srname', '') takedown = request.GET.get('takedown', '') error_name = request.GET.get('error_name', '') if isinstance(c.user, basestring): # somehow requests are getting here with c.user unset c.user_is_loggedin = False c.user = UnloggedUser(browser_langs=None) if srname: c.site = Subreddit._by_name(srname) if request.GET.has_key('allow_framing'): c.allow_framing = bool(request.GET['allow_framing'] == '1') if (error_name == 'IN_TIMEOUT' and not 'usable_error_content' in request.environ): timeout_days_remaining = c.user.days_remaining_in_timeout errpage = pages.InterstitialPage( _("suspended"), content=pages.InTimeoutInterstitial( timeout_days_remaining=timeout_days_remaining, ), ) request.environ['usable_error_content'] = errpage.render() if code in (204, 304): # NEVER return a content body on 204/304 or downstream # caches may become very confused. return "" elif c.render_style not in self.allowed_render_styles: return str(code) elif c.render_style in extensions.API_TYPES: data = request.environ.get('extra_error_data', {'error': code}) message = request.GET.get('message', '') if message: data['message'] = message if request.environ.get("WANT_RAW_JSON"): return scriptsafe_dumps(data) return websafe_json(json.dumps(data)) elif takedown and code == 404: link = Link._by_fullname(takedown) return pages.TakedownPage(link).render() elif code == 400: return self.send400() elif code == 403: return self.send403() elif code == 429: return self.send429() elif code == 500: failien_url = make_failien_url() sad_message = get_funny_translated_string("500_page") sad_message %= {'admin': random.choice(self.admins)} sad_message = safemarkdown(sad_message) return redditbroke % (failien_url, sad_message) elif code == 503: return self.send503() elif c.site: return self.send404() else: return "page not found" except Exception as e: return handle_awful_failure("ErrorController.GET_document: %r" % e)