Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Gold feature: Stylesheets everywhere
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Deimos authored and MelissaCole committed Apr 21, 2015
1 parent 36a31de commit a344764
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
5 changes: 4 additions & 1 deletion r2/r2/controllers/reddit_base.py
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions r2/r2/lib/pages/pages.py
Expand Up @@ -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:
Expand Down
17 changes: 16 additions & 1 deletion r2/r2/lib/validator/preferences.py
Expand Up @@ -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
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'))
1 change: 1 addition & 0 deletions r2/r2/models/account.py
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions r2/r2/public/static/css/reddit.less
Expand Up @@ -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;
Expand Down
21 changes: 17 additions & 4 deletions r2/r2/templates/prefoptions.html
Expand Up @@ -77,10 +77,13 @@
</%def>

<%def name="num_input(s, name)">
<input size="4" maxlength="4"
name="${name}" style="margin: 0 .5em 0 .5em"
type="number"
value="${s if s is not None else ''}" />
<input type="text" class="number" size="4" maxlength="4"
name="${name}" value="${s if s is not None else ''}">
</%def>

<%def name="text_input(s, name)">
<input type="text" class="text" size="20" maxlength="20"
name="${name}" value="${s if s is not None else ''}">
</%def>

%if c.user_is_loggedin and thing.done:
Expand Down Expand Up @@ -345,6 +348,16 @@
<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")}
<br><br>
<p>
<%
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))}
&#32;<span class="little gray">${_("(blank for standard reddit style)")}</span>
<br>
<span class="little gray">${_("(used anywhere there isn't another custom style - front page, multireddits, or everywhere if you have custom styles disabled)")}</span>
</td>
</tr>
%elif c.user.is_moderator_somewhere:
Expand Down

0 comments on commit a344764

Please sign in to comment.