Pull reddit themes in pref from wiki

The featured reddit themes will be shown on the prefs page as a radio button
with an `other` option. The wiki page holds the data for the tagline and image
previews so that it can be updated without a code push.
This commit is contained in:
MelissaCole
2015-04-17 11:38:13 -07:00
parent 0addf6cea6
commit 49912e6030
12 changed files with 147 additions and 48 deletions

View File

@@ -151,6 +151,7 @@ wiki_page_privacy_policy = privacypolicy
wiki_page_user_agreement = useragreement
wiki_page_registration_info = registration_info
wiki_page_gold_bottlecaps = gold_bottlecaps
wiki_page_stylesheets_everywhere =
#### Feature toggles
disable_ads = false

View File

@@ -3823,10 +3823,11 @@ class ApiController(RedditController):
if sr_style_enabled:
sr = c.site
elif (c.user.pref_stylesheet_override and
elif (c.user.pref_default_theme_sr and
feature.is_enabled('stylesheets_everywhere')):
sr = Subreddit._by_name(c.user.pref_stylesheet_override)
if not sr.can_view(c.user):
sr = Subreddit._by_name(c.user.pref_default_theme_sr)
if (not sr.can_view(c.user) or
not c.user.pref_enable_default_themes):
sr = DefaultSR()
sr_stylesheet_url = Reddit.get_subreddit_stylesheet_url(sr)
if not sr_stylesheet_url:

View File

@@ -1695,9 +1695,9 @@ class RedditController(OAuth2ResourceController):
# use override stylesheet if one exists and:
# this page has no custom stylesheet
# or the user disabled the stylesheet for this sr (indiv or global)
has_style_override = (c.user.pref_stylesheet_override and
has_style_override = (c.user.pref_default_theme_sr and
feature.is_enabled('stylesheets_everywhere') and
Subreddit._by_name(c.user.pref_stylesheet_override).can_view(c.user))
Subreddit._by_name(c.user.pref_default_theme_sr).can_view(c.user))
sr_stylesheet_enabled = c.user.use_subreddit_style(c.site)
if (not sr_stylesheet_enabled and

View File

@@ -55,6 +55,7 @@ from r2.models import (
Random,
RandomNSFW,
RandomSubscription,
StylesheetsEverywhere,
Sub,
SubSR,
Subreddit,
@@ -363,10 +364,10 @@ class Reddit(Templated):
)
self.toolbars = self.build_toolbars()
self.subreddit_stylesheet_url = self.get_subreddit_stylesheet_url(c.site)
has_style_override = (c.user.pref_stylesheet_override and
feature.is_enabled('stylesheets_everywhere'))
has_style_override = (c.user.pref_default_theme_sr and
feature.is_enabled('stylesheets_everywhere') and
c.user.pref_enable_default_themes)
# if there is no style or the style is disabled for this subreddit
self.no_sr_styles = (isinstance(c.site, DefaultSR) or
not self.get_subreddit_stylesheet_url(c.site) or
@@ -375,16 +376,19 @@ class Reddit(Templated):
self.default_theme_sr = DefaultSR()
# use override stylesheet if they have custom styles disabled or
# this subreddit has no custom stylesheet (or is the front page)
if self.no_sr_styles:
self.subreddit_stylesheet_url = self.get_subreddit_stylesheet_url(
self.default_theme_sr)
else:
self.subreddit_stylesheet_url = self.get_subreddit_stylesheet_url(c.site)
if has_style_override and self.no_sr_styles:
sr = Subreddit._by_name(c.user.pref_stylesheet_override)
sr = Subreddit._by_name(c.user.pref_default_theme_sr)
# make sure they can still view their override subreddit
if sr.can_view(c.user) and sr.stylesheet_url:
self.subreddit_stylesheet_url = self.get_subreddit_stylesheet_url(sr)
if c.can_apply_styles and c.allow_styles and sr.header:
self.default_theme_sr = sr
else:
self.subreddit_stylesheet_url = self.get_subreddit_stylesheet_url(
self.default_theme_sr)
@staticmethod
@@ -1090,12 +1094,24 @@ class PrefsPage(Reddit):
return [PageNameNav('nomenu', title = _("preferences")),
NavMenu(buttons, base_path = "/prefs", type="tabmenu")]
class PrefOptions(Templated):
"""Preference form for updating language and display options"""
def __init__(self, done=False, error_style_override=None, generic_error=None):
themes = []
use_other_theme = True
if feature.is_enabled('stylesheets_everywhere'):
for theme in StylesheetsEverywhere.get_all():
if theme.is_enabled:
themes.append(theme)
if theme.id == c.user.pref_default_theme_sr:
use_other_theme = False
theme.checked = True
Templated.__init__(self, done=done,
error_style_override=error_style_override,
generic_error=generic_error)
generic_error=generic_error, themes=themes, use_other_theme=use_other_theme)
class PrefFeeds(Templated):
pass

View File

@@ -21,7 +21,8 @@
###############################################################################
from copy import copy
from pylons import c,g
from pylons import c, g
from r2.config import feature
from r2.lib.menus import CommentSortMenu
from r2.lib.validator.validator import (
VBoolean,
@@ -60,7 +61,6 @@ PREFS_VALIDATORS = dict(
pref_default_comment_sort=VOneOf('default_comment_sort',
CommentSortMenu.visible_options()),
pref_show_stylesheets=VBoolean('show_stylesheets'),
pref_stylesheet_override=VSRByName('stylesheet_override'),
pref_show_flair=VBoolean('show_flair'),
pref_show_link_flair=VBoolean('show_link_flair'),
pref_no_profanity=VBoolean('no_profanity'),
@@ -81,6 +81,9 @@ PREFS_VALIDATORS = dict(
pref_hide_locationbar=VBoolean("hide_locationbar"),
pref_use_global_defaults=VBoolean("use_global_defaults"),
pref_creddit_autorenew=VBoolean("creddit_autorenew"),
pref_enable_default_themes=VBoolean("enable_default_themes", False),
pref_default_theme_sr=VSRByName("theme_selector", False),
pref_other_theme=VSRByName("other_theme", False),
)
@@ -98,6 +101,12 @@ def set_prefs(user, prefs):
def filter_prefs(prefs, user):
# replace stylesheet_override with other_theme if it doesn't exist
if feature.is_enabled_for('stylesheets_everywhere', user):
if not prefs["pref_default_theme_sr"]:
if prefs["pref_other_theme"]:
prefs["pref_default_theme_sr"] = prefs["pref_other_theme"]
for pref_key in prefs.keys():
if pref_key not in user._preference_attrs:
del prefs[pref_key]
@@ -128,15 +137,16 @@ def filter_prefs(prefs, user):
prefs['pref_highlight_new_comments'] = True
# check stylesheet override
override_sr = prefs.get('pref_stylesheet_override')
if override_sr:
if override_sr.can_view(user):
# convert back to name
prefs['pref_stylesheet_override'] = override_sr.name
if feature.is_enabled_for('stylesheets_everywhere', user):
override_sr = prefs['pref_default_theme_sr']
if not override_sr:
del prefs['pref_default_theme_sr']
if prefs['pref_enable_default_themes']:
c.errors.add(c.errors.add(errors.SUBREDDIT_REQUIRED, field="stylesheet_override"))
else:
# don't update if they can't view the chosen subreddit
c.errors.add(errors.SUBREDDIT_NO_ACCESS, field='stylesheet_override')
del prefs['pref_stylesheet_override']
else:
# if it was blank, unset the error from VSRByName
c.errors.remove(('BAD_SR_NAME', 'stylesheet_override'))
if override_sr.can_view(user):
prefs['pref_default_theme_sr'] = override_sr.name
else:
# don't update if they can't view the chosen subreddit
c.errors.add(errors.SUBREDDIT_NO_ACCESS, field='stylesheet_override')
del prefs['pref_default_theme_sr']

View File

@@ -715,9 +715,14 @@ class VAvailableSubredditName(VSubredditName):
class VSRByName(Validator):
def __init__(self, sr_name, required=True):
self.required = required
Validator.__init__(self, sr_name)
def run(self, sr_name):
if not sr_name:
self.set_error(errors.BAD_SR_NAME, code=400)
if self.required:
self.set_error(errors.BAD_SR_NAME, code=400)
else:
sr_name = sr_path_rx.sub('\g<name>', sr_name.strip())
try:

View File

@@ -92,7 +92,8 @@ class Account(Thing):
pref_no_profanity = True,
pref_label_nsfw = True,
pref_show_stylesheets = True,
pref_stylesheet_override=None,
pref_enable_default_themes=False,
pref_default_theme_sr=None,
pref_show_flair = True,
pref_show_link_flair = True,
pref_mark_messages_read = True,

View File

@@ -48,7 +48,7 @@ from r2.lib.db import tdb_cassandra
from r2.lib.db.tdb_cassandra import NotFound, view_of
from r2.models import Account
from r2.models.subreddit import Frontpage
from r2.models.wiki import WikiPage
from r2.models.wiki import WikiPage, WikiPageIniItem
from r2.lib.memoize import memoize
import stripe
@@ -620,3 +620,17 @@ def get_current_value_of_month():
now = datetime.now(g.display_tz)
seconds = calculate_server_seconds(price, now)
return seconds
class StylesheetsEverywhere(WikiPageIniItem):
@classmethod
def _get_wiki_config(cls):
return Frontpage, g.wiki_page_stylesheets_everywhere
def __init__(self, id, tagline, thumbnail_url, preview_url, is_enabled=True):
self.id = id
self.tagline = tagline
self.thumbnail_url = thumbnail_url
self.preview_url = preview_url
self.is_enabled = is_enabled
self.checked = False

View File

@@ -1264,6 +1264,19 @@ $(function() {
return post_form($(this).parent(), "set_sr_style_enabled");
});
$(".reddit-themes .theme").click(function() {
$("div.theme.selected").removeClass("selected");
$("input[name='enable_default_themes']").prop("checked", true);
// if other is selected
if ($(this).hasClass("select-custom-theme")) {
$("#other_theme_selector").prop("checked", true);
} else {
$("input[name='theme_selector'][value='" + $(this).attr("id") + "']")
.prop("checked", true);
}
$(this).addClass("selected");
});
/* ajax ynbutton */
function toggleThis() { return toggle(this); }
$("body")

View File

@@ -24,11 +24,11 @@
from r2.config import feature
from r2.lib.errors import error_list
from r2.lib.menus import CommentSortMenu
from r2.lib.template_helpers import add_sr, _wsf, format_html
from r2.lib.template_helpers import add_sr, _wsf, format_html, make_url_protocol_relative
from r2.lib.utils import UrlParser
import random
%>
<%namespace file="utils.html" import="error_field, language_tool, plain_link"/>
<%namespace file="utils.html" import="md, error_field, language_tool, plain_link"/>
<%def name="checkbox(text, name, disabled = False, disabled_text = '', prefix = 'pref_')">
<input name="${name}" id="${name}" type="checkbox"
@@ -360,22 +360,6 @@
<br><br>
<% creddit_link = unsafe('&#32;<a href="/creddits">creddit</a>&#32;') %>
${checkbox(_wsf("use a %(creddit_link)s to automatically renew my gold if it expires", creddit_link=creddit_link), "creddit_autorenew")}
%if feature.is_enabled('stylesheets_everywhere'):
<br><br>
<p>
<%
input = unsafe(capture(text_input, c.user.pref_stylesheet_override, 'stylesheet_override'))
s = c.user.pref_stylesheet_override
%>
${_wsf("use theme from /r/%(sr)s by default", sr=input)}
&#32;<span class="little gray">${_("(blank for standard reddit theme)")}</span>
<br>
<span class="little gray">${_("(used anywhere there isn't another custom themes - front page, multireddits, or everywhere if you have custom themes disabled)")}</span>
%if thing.error_style_override:
<p class="error">${_(error_list[thing.error_style_override])}</p>
%endif
%endif
</td>
</tr>
%elif c.user.is_moderator_somewhere:
@@ -391,6 +375,46 @@
</tr>
%endif
%if feature.is_enabled('stylesheets_everywhere'):
<tr class="gold-accent">
<th>${_("reddit themes")}</th>
<td class="prefright">
${checkbox(_("use subreddit theme instead of the default reddit theme"), "enable_default_themes")}
&#32;<span class="little gray">
(${_("hover to preview")})
</span>
<div class="container reddit-themes">
% if thing.themes:
% for theme in thing.themes:
${theme_item(
thumbnail_url=theme.thumbnail_url,
preview_url=theme.preview_url,
id=theme.id,
tagline=theme.tagline,
checked=theme.checked,
)}
% endfor
% endif
<div class="theme select-custom-theme ${'selected' if thing.use_other_theme else ''}">
<label><input type="radio" name="theme_selector" id="other_theme_selector" value="" ${"checked" if thing.use_other_theme else ""}>use theme from /r/</label>
<%
chosen_theme_sr = c.user.pref_default_theme_sr if thing.use_other_theme and c.user.pref_default_theme_sr else ""
%>
<input type="text" class="text" size="20" maxlength="20" name="other_theme" placeholder="subreddit" value="${chosen_theme_sr}">
<span class="little gray">
(${_("warning: use non-featured themes at your own risk")})
</span>
</div>
</div>
</div>
%if thing.error_style_override:
<p class="error">${_(error_list[thing.error_style_override])}</p>
%endif
</td>
</tr>
%endif
<tr>
<td>
<input type="submit" class="btn" value="${_('save options')}"/>
@@ -399,3 +423,16 @@
</table>
</form>
<%def name="theme_item(thumbnail_url, preview_url, id, tagline, checked)">
<div class="theme ${'selected' if checked else ''}" id="${id}">
<div class="theme-thumbnail">
<img src="${make_url_protocol_relative(thumbnail_url)}"/>
<div class="theme-preview">
<img src="${make_url_protocol_relative(preview_url)}"/>
</div>
</div>
<label><input type="radio" name="theme_selector" value="${id}" ${"checked" if checked else ""}>
${md(tagline)}</label>
</div>
</%def>

View File

@@ -70,7 +70,7 @@
${'checked="checked"' if thing.use_subreddit_style else ""}
>
<label for="sr_style_enabled">
${_("Use this subreddit's theme")}
${_("Show this subreddit's theme")}
</label>
<div id="sr_style_throbber" class="throbber"></div>
</form>

View File

@@ -418,6 +418,7 @@ ${unsafe(txt)}
['_setCustomVar', 2, 'srpath', '${tracking.get_srpath()}', 3],
['_setCustomVar', 3, 'usertype', user_type, 2],
['_setCustomVar', 4, 'uitype', '${uitype}', 3],
['_setCustomVar', 5, 'style_override', '${jssafe(c.user.pref_default_theme_sr)}', 2],
%if g.googleanalytics_sample_rate:
['_setSampleRate', '${g.googleanalytics_sample_rate}'],
%endif