From a34476432c79708c4f344bf6a7e9702fdbf7f951 Mon Sep 17 00:00:00 2001 From: Chad Birch Date: Tue, 19 Aug 2014 16:58:54 -0600 Subject: [PATCH] Gold feature: Stylesheets everywhere This allows users to pick a stylesheet from a subreddit of their choosing and have it be applied to (most) pages on reddit where there isn't already a custom stylesheet. --- r2/r2/controllers/reddit_base.py | 5 ++++- r2/r2/lib/pages/pages.py | 12 ++++++++++++ r2/r2/lib/validator/preferences.py | 17 ++++++++++++++++- r2/r2/models/account.py | 1 + r2/r2/public/static/css/reddit.less | 9 +++++++++ r2/r2/templates/prefoptions.html | 21 +++++++++++++++++---- 6 files changed, 59 insertions(+), 6 deletions(-) diff --git a/r2/r2/controllers/reddit_base.py b/r2/r2/controllers/reddit_base.py index 804c41fb57..b2d09e445c 100644 --- a/r2/r2/controllers/reddit_base.py +++ b/r2/r2/controllers/reddit_base.py @@ -1692,7 +1692,10 @@ def pre(self): c.allow_styles = True c.can_apply_styles = self.allow_stylesheets #if the preference is set and we're not at a cname - if not c.user.pref_show_stylesheets and not c.cname: + has_style_override = c.user.gold and c.user.pref_stylesheet_override + if (not c.user.pref_show_stylesheets and + not has_style_override and + not c.cname): c.can_apply_styles = False #if the site has a cname, but we're not using it elif c.site.domain and c.site.css_on_cname and not c.cname: diff --git a/r2/r2/lib/pages/pages.py b/r2/r2/lib/pages/pages.py index f449e3aa76..c653670d24 100644 --- a/r2/r2/lib/pages/pages.py +++ b/r2/r2/lib/pages/pages.py @@ -365,6 +365,18 @@ def __init__(self, space_compress=None, nav_menus=None, loginbox=True, self.toolbars = self.build_toolbars() self.subreddit_stylesheet_url = self.get_subreddit_stylesheet_url(c.site) + # use override stylesheet if they have custom styles disabled or + # this subreddit has no custom stylesheet (or is the front page) + has_override_enabled = c.user.gold and c.user.pref_stylesheet_override + no_sr_styles = (isinstance(c.site, DefaultSR) or + not c.user.pref_show_stylesheets or + not self.subreddit_stylesheet_url) + if has_override_enabled and no_sr_styles: + sr = Subreddit._by_name(c.user.pref_stylesheet_override) + # make sure they can still view their override subreddit + if sr.can_view(c.user): + self.subreddit_stylesheet_url = self.get_subreddit_stylesheet_url(sr) + @staticmethod def get_subreddit_stylesheet_url(sr): if not g.css_killswitch and c.can_apply_styles and c.allow_styles: diff --git a/r2/r2/lib/validator/preferences.py b/r2/r2/lib/validator/preferences.py index 82a5382ec4..0efb5142ec 100644 --- a/r2/r2/lib/validator/preferences.py +++ b/r2/r2/lib/validator/preferences.py @@ -21,13 +21,14 @@ ############################################################################### from copy import copy -from pylons import g +from pylons import c,g from r2.lib.menus import CommentSortMenu from r2.lib.validator.validator import ( VBoolean, VInt, VLang, VOneOf, + VSRByName, ) # Validators that map directly to Account._preference_attrs @@ -58,6 +59,7 @@ 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'), @@ -123,3 +125,16 @@ def filter_prefs(prefs, user): if not (user.gold or user.is_moderator_somewhere): 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 + else: + # don't update if they can't view the chosen subreddit + del prefs['pref_stylesheet_override'] + else: + # if it was blank, unset the error from VSRByName + c.errors.remove(('BAD_SR_NAME', 'stylesheet_override')) diff --git a/r2/r2/models/account.py b/r2/r2/models/account.py index de85e70ba9..dc66b2424b 100644 --- a/r2/r2/models/account.py +++ b/r2/r2/models/account.py @@ -92,6 +92,7 @@ class Account(Thing): pref_no_profanity = True, pref_label_nsfw = True, pref_show_stylesheets = True, + pref_stylesheet_override=None, pref_show_flair = True, pref_show_link_flair = True, pref_mark_messages_read = True, diff --git a/r2/r2/public/static/css/reddit.less b/r2/r2/public/static/css/reddit.less index 483e93a3f2..87b239f0ce 100644 --- a/r2/r2/public/static/css/reddit.less +++ b/r2/r2/public/static/css/reddit.less @@ -4146,6 +4146,15 @@ form input[type=radio] {margin: 2px .5em 0 0; } box-shadow: inset 0px 1px 1px hsla(0,0%,0%,.3), 0px 1px 0px hsla(0,0%,100%,.6); } +.pretty-form.short-text input[type=text].number { + margin: 0 0.5em; +} + +.pretty-form.short-text input[type=text].text { + margin: 0 0.5em 0 0; + width: 10em; +} + .pretty-form .infobar { width: 285px; margin: 5px; diff --git a/r2/r2/templates/prefoptions.html b/r2/r2/templates/prefoptions.html index f929c41e44..df3311bfbc 100644 --- a/r2/r2/templates/prefoptions.html +++ b/r2/r2/templates/prefoptions.html @@ -77,10 +77,13 @@ <%def name="num_input(s, name)"> - + + + +<%def name="text_input(s, name)"> + %if c.user_is_loggedin and thing.done: @@ -345,6 +348,16 @@

<% creddit_link = unsafe(' creddit ') %> ${checkbox(_wsf("use a %(creddit_link)s to automatically renew my gold if it expires", creddit_link=creddit_link), "creddit_autorenew")} +

+

+ <% + input = capture(text_input, c.user.pref_stylesheet_override, 'stylesheet_override') + s = c.user.pref_stylesheet_override + %> + ${unsafe(_("use stylesheet from /r/%(sr)s by default") % dict(sr=input))} + ${_("(blank for standard reddit style)")} +
+ ${_("(used anywhere there isn't another custom style - front page, multireddits, or everywhere if you have custom styles disabled)")} %elif c.user.is_moderator_somewhere: