Cut subreddit stylesheet image reads over to new model.

This commit is contained in:
Neil Williams
2013-08-16 23:53:07 -07:00
parent cc5d31630c
commit 903acd7e21
7 changed files with 32 additions and 44 deletions

View File

@@ -1737,8 +1737,10 @@ class ApiController(RedditController, OAuth2ResourceController):
# this may also fail if a sponsored image was added and the user is not an admin
errors['BAD_CSS_NAME'] = _("bad image name")
if c.site.images and add_image_to_sr:
if c.site.get_num_images() >= g.max_sr_images:
if add_image_to_sr:
image_count = wiki.ImagesByWikiPage.get_image_count(
c.site, "config/stylesheet")
if image_count >= g.max_sr_images:
errors['IMAGE_ERROR'] = _("too many images (you only get %d)") % g.max_sr_images
if any(errors.values()):

View File

@@ -23,6 +23,7 @@
from __future__ import with_statement
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
@@ -177,16 +178,6 @@ class ValidationError(Exception):
obj = str(self.obj) if hasattr(self,'obj') else ''
return "ValidationError%s: %s (%s)" % (line, self.message, obj)
def legacy_s3_url(url, site):
if isinstance(url, int): # legacy url, needs to be generated
bucket = g.s3_old_thumb_bucket
baseurl = "http://%s" % (bucket)
if g.s3_media_direct:
baseurl = "http://%s/%s" % (s3_direct_url, bucket)
url = "%s/%s_%d.png"\
% (baseurl, site._fullname, url)
url = s3_https_if_secure(url)
return url
# local urls should be in the static directory
local_urls = re.compile(r'\A/static/[a-z./-]+\Z')
@@ -219,10 +210,12 @@ def valid_url(prop,value,report):
# custom urls are allowed, but need to be transformed into a real path
elif custom_img_urls.match(url):
name = custom_img_urls.match(url).group(1)
# the label -> image number lookup is stored on the subreddit
if c.site.images.has_key(name):
url = c.site.images[name]
url = legacy_s3_url(url, c.site)
# this relies on localcache to not be doing a lot of lookups
images = ImagesByWikiPage.get_images(c.site, "config/stylesheet")
if name in images:
url = s3_https_if_secure(images[name])
value._setCssText("url(%s)"%url)
else:
# unknown image label -> error

View File

@@ -239,15 +239,20 @@ def safemarkdown(text, nofollow=False, wrap=True, **kwargs):
return SC_OFF + text + SC_ON
def wikimarkdown(text, include_toc=True, target=None):
from r2.lib.cssfilter import legacy_s3_url
from r2.lib.template_helpers import s3_https_if_secure
# this hard codes the stylesheet page for now, but should be parameterized
# in the future to allow per-page images.
from r2.models.wiki import ImagesByWikiPage
page_images = ImagesByWikiPage.get_images(c.site, "config/stylesheet")
def img_swap(tag):
name = tag.get('src')
name = custom_img_url.search(name)
name = name and name.group(1)
if name and c.site.images.has_key(name):
url = c.site.images[name]
url = legacy_s3_url(url, c.site)
if name and name in page_images:
url = page_images[name]
url = s3_https_if_secure(url)
tag['src'] = url
else:
tag.extract()

View File

@@ -32,6 +32,9 @@ import time, pytz
from pylons import c, g
from pylons.i18n import _
from r2.models.wiki import ImagesByWikiPage
def make_typename(typ):
return 't%s' % to36(typ._type_id)
@@ -797,8 +800,9 @@ class StylesheetTemplate(ThingJsonTemplate):
return 'stylesheet'
def images(self):
sr_images = ImagesByWikiPage.get_images(c.site, "config/stylesheet")
images = []
for name, url in c.site.get_images():
for name, url in sr_images.iteritems():
images.append({'name': name,
'link': 'url(%%%%%s%%%%)' % name,
'url': url})

View File

@@ -35,7 +35,7 @@ from r2.models.token import OAuth2Client, OAuth2AccessToken
from r2.models import traffic
from r2.models import ModAction
from r2.models import Thing
from r2.models.wiki import WikiPage
from r2.models.wiki import WikiPage, ImagesByWikiPage
from r2.lib.db import tdb_cassandra
from r2.config import cache
from r2.config.extensions import is_api
@@ -1916,7 +1916,9 @@ class SubredditStylesheet(Templated):
"""form for editing or creating subreddit stylesheets"""
def __init__(self, site = None,
stylesheet_contents = ''):
Templated.__init__(self, site = site,
images = ImagesByWikiPage.get_images(c.site, "config/stylesheet")
Templated.__init__(self, site = site, images=images,
stylesheet_contents = stylesheet_contents)
class SubredditStylesheetSource(Templated):

View File

@@ -831,23 +831,8 @@ class Subreddit(Thing, Printable, BaseSite):
user = c.user if c.user_is_loggedin else None
return self.can_view(user)
def get_images(self):
"""
Iterator over list of (name, url) pairs which have been
uploaded for custom styling of this subreddit.
"""
for name, img in self.images.iteritems():
if name != "/empties/":
yield (name, img)
def get_num_images(self):
if '/empties/' in self.images:
return len(self.images) - 1
else:
return len(self.images)
def add_image(self, name, url, max_num = None):
def add_image(self, name, url):
"""
Adds an image to the subreddit's image list. The resulting
number of the image is returned. Note that image numbers are
@@ -860,9 +845,6 @@ class Subreddit(Thing, Printable, BaseSite):
The Subreddit will be _dirty if a new image has been added to
its images list, and no _commit is called.
"""
if max_num is not None and self.get_num_images() >= max_num:
raise ValueError, "too many images"
# copy and blank out the images list to flag as _dirty
l = self.images
self.images = None

View File

@@ -293,8 +293,8 @@
</li>
</%def>
${make_li(prototype=True)}
%for name, img_num in c.site.get_images():
${make_li(name=name, img=img_num)}
%for name, url in thing.images.iteritems():
${make_li(name=name, img=url)}
%endfor
</ul>