mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-04-27 03:00:12 -04:00
CSS Filter: move preview link/comment generation to r2.lib.pages.
These functions, while used in the CSS filter UI, are view code and should be separate from the filter itself.
This commit is contained in:
@@ -55,6 +55,7 @@ from r2.lib.pages import (
|
||||
FriendTableItem,
|
||||
InvitedModTableItem,
|
||||
ModTableItem,
|
||||
SubredditStylesheet,
|
||||
WikiBannedTableItem,
|
||||
WikiMayContributeTableItem,
|
||||
)
|
||||
@@ -76,7 +77,7 @@ from r2.lib import media
|
||||
from r2.lib.db import tdb_cassandra
|
||||
from r2.lib import promote
|
||||
from r2.lib.comment_tree import delete_comment
|
||||
from r2.lib import tracking, cssfilter, emailer
|
||||
from r2.lib import tracking, emailer
|
||||
from r2.lib.subreddit_search import search_reddits
|
||||
from r2.lib.log import log_text
|
||||
from r2.lib.filters import safemarkdown
|
||||
@@ -1773,36 +1774,37 @@ class ApiController(RedditController):
|
||||
if op == 'preview':
|
||||
# try to find a link to use, otherwise give up and
|
||||
# return
|
||||
links = cssfilter.find_preview_links(c.site)
|
||||
links = SubredditStylesheet.find_preview_links(c.site)
|
||||
if links:
|
||||
|
||||
jquery('#preview-table').show()
|
||||
|
||||
# do a regular link
|
||||
jquery('#preview_link_normal').html(
|
||||
cssfilter.rendered_link(links, media = 'off',
|
||||
compress=False))
|
||||
SubredditStylesheet.rendered_link(
|
||||
links, media='off', compress=False))
|
||||
# now do one with media
|
||||
jquery('#preview_link_media').html(
|
||||
cssfilter.rendered_link(links, media = 'on',
|
||||
compress=False))
|
||||
SubredditStylesheet.rendered_link(
|
||||
links, media='on', compress=False))
|
||||
# do a compressed link
|
||||
jquery('#preview_link_compressed').html(
|
||||
cssfilter.rendered_link(links, media = 'off',
|
||||
compress=True))
|
||||
SubredditStylesheet.rendered_link(
|
||||
links, media='off', compress=True))
|
||||
# do a stickied link
|
||||
jquery('#preview_link_stickied').html(
|
||||
cssfilter.rendered_link(links, media='off',
|
||||
compress=False, stickied=True))
|
||||
SubredditStylesheet.rendered_link(
|
||||
links, media='off', compress=False, stickied=True))
|
||||
|
||||
# and do a comment
|
||||
comments = cssfilter.find_preview_comments(c.site)
|
||||
comments = SubredditStylesheet.find_preview_comments(c.site)
|
||||
if comments:
|
||||
jquery('#preview_comment').html(
|
||||
cssfilter.rendered_comment(comments))
|
||||
SubredditStylesheet.rendered_comment(comments))
|
||||
|
||||
jquery('#preview_comment_gilded').html(
|
||||
cssfilter.rendered_comment(comments, gilded=True))
|
||||
SubredditStylesheet.rendered_comment(
|
||||
comments, gilded=True))
|
||||
|
||||
@require_oauth2_scope("modconfig")
|
||||
@validatedForm(VSrModerator(perms='config'),
|
||||
|
||||
@@ -26,7 +26,6 @@ from r2.models import *
|
||||
from r2.models.wiki import ImagesByWikiPage
|
||||
from r2.lib.utils import sanitize_url, strip_www, randstr
|
||||
from r2.lib.strings import string_dict
|
||||
from r2.lib.pages.things import wrap_links
|
||||
|
||||
from pylons import g, c
|
||||
from pylons.i18n import _
|
||||
@@ -344,46 +343,3 @@ def validate_css(string, generate_https_urls):
|
||||
rule))
|
||||
|
||||
return parsed.cssText if parsed else "", report
|
||||
|
||||
def find_preview_comments(sr):
|
||||
from r2.lib.db.queries import get_sr_comments, get_all_comments
|
||||
|
||||
comments = get_sr_comments(sr)
|
||||
comments = list(comments)
|
||||
if not comments:
|
||||
comments = get_all_comments()
|
||||
comments = list(comments)
|
||||
|
||||
return Thing._by_fullname(comments[:25], data=True, return_dict=False)
|
||||
|
||||
def find_preview_links(sr):
|
||||
from r2.lib.normalized_hot import normalized_hot
|
||||
|
||||
# try to find a link to use, otherwise give up and return
|
||||
links = normalized_hot([sr._id])
|
||||
if not links:
|
||||
links = normalized_hot(Subreddit.default_subreddits())
|
||||
|
||||
if links:
|
||||
links = links[:25]
|
||||
links = Link._by_fullname(links, data=True, return_dict=False)
|
||||
|
||||
return links
|
||||
|
||||
def rendered_link(links, media, compress, stickied=False):
|
||||
with c.user.safe_set_attr:
|
||||
c.user.pref_compress = compress
|
||||
c.user.pref_media = media
|
||||
links = wrap_links(links, show_nums = True, num = 1)
|
||||
for wrapped in links:
|
||||
wrapped.stickied = stickied
|
||||
delattr(c.user, 'pref_compress')
|
||||
delattr(c.user, 'pref_media')
|
||||
return links.render(style = "html")
|
||||
|
||||
def rendered_comment(comments, gilded=False):
|
||||
wrapped = wrap_links(comments, num=1)
|
||||
if gilded:
|
||||
for w in wrapped:
|
||||
w.gilded_message = "this comment was fake-gilded"
|
||||
return wrapped.render(style="html")
|
||||
|
||||
@@ -51,7 +51,7 @@ from r2.models import traffic
|
||||
from r2.models import ModAction
|
||||
from r2.models import Thing
|
||||
from r2.models.wiki import WikiPage, ImagesByWikiPage
|
||||
from r2.lib.db import tdb_cassandra
|
||||
from r2.lib.db import tdb_cassandra, queries
|
||||
from r2.config.extensions import is_api
|
||||
from r2.lib.menus import CommentSortMenu
|
||||
from pylons.i18n import _, ungettext
|
||||
@@ -75,6 +75,7 @@ from r2.lib.filters import (
|
||||
from r2.lib.menus import NavButton, NamedButton, NavMenu, PageNameNav, JsButton
|
||||
from r2.lib.menus import SubredditButton, SubredditMenu, ModeratorMailButton
|
||||
from r2.lib.menus import OffsiteButton, menu, JsNavMenu
|
||||
from r2.lib.normalized_hot import normalized_hot
|
||||
from r2.lib.strings import plurals, rand_strings, strings, Score
|
||||
from r2.lib.utils import title_to_url, query_string, UrlParser, vote_hash
|
||||
from r2.lib.utils import url_links_builder, make_offset_date, median, to36
|
||||
@@ -2170,6 +2171,49 @@ class SubredditStylesheet(Templated):
|
||||
Templated.__init__(self, site = site, images=images,
|
||||
stylesheet_contents = stylesheet_contents)
|
||||
|
||||
@staticmethod
|
||||
def find_preview_comments(sr):
|
||||
comments = queries.get_sr_comments(sr)
|
||||
comments = list(comments)
|
||||
if not comments:
|
||||
comments = queries.get_all_comments()
|
||||
comments = list(comments)
|
||||
|
||||
return Thing._by_fullname(comments[:25], data=True, return_dict=False)
|
||||
|
||||
@staticmethod
|
||||
def find_preview_links(sr):
|
||||
# try to find a link to use, otherwise give up and return
|
||||
links = normalized_hot([sr._id])
|
||||
if not links:
|
||||
links = normalized_hot(Subreddit.default_subreddits())
|
||||
|
||||
if links:
|
||||
links = links[:25]
|
||||
links = Link._by_fullname(links, data=True, return_dict=False)
|
||||
|
||||
return links
|
||||
|
||||
@staticmethod
|
||||
def rendered_link(links, media, compress, stickied=False):
|
||||
with c.user.safe_set_attr:
|
||||
c.user.pref_compress = compress
|
||||
c.user.pref_media = media
|
||||
links = wrap_links(links, show_nums=True, num=1)
|
||||
for wrapped in links:
|
||||
wrapped.stickied = stickied
|
||||
delattr(c.user, "pref_compress")
|
||||
delattr(c.user, "pref_media")
|
||||
return links.render(style="html")
|
||||
|
||||
@staticmethod
|
||||
def rendered_comment(comments, gilded=False):
|
||||
wrapped = wrap_links(comments, num=1)
|
||||
if gilded:
|
||||
for w in wrapped:
|
||||
w.gilded_message = "this comment was fake-gilded"
|
||||
return wrapped.render(style="html")
|
||||
|
||||
class SubredditStylesheetSource(Templated):
|
||||
"""A view of the unminified source of a subreddit's stylesheet."""
|
||||
def __init__(self, stylesheet_contents):
|
||||
|
||||
Reference in New Issue
Block a user