From 2c4553fd24ccb053dcf07d53b2ecb402b3996975 Mon Sep 17 00:00:00 2001 From: Max Goodman Date: Fri, 22 Mar 2013 00:35:31 -0700 Subject: [PATCH] Add utility for sending wrapped things in js_preload. --- r2/r2/lib/pages/things.py | 11 +++++++++++ r2/r2/lib/template_helpers.py | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/r2/r2/lib/pages/things.py b/r2/r2/lib/pages/things.py index 2f9a67b12..bf347802b 100644 --- a/r2/r2/lib/pages/things.py +++ b/r2/r2/lib/pages/things.py @@ -232,3 +232,14 @@ def wrap_links(links, wrapper = default_thing_wrapper(), return l.listing() +def wrap_things(*things): + """Instantiate Wrapped for each thing, calling add_props if available.""" + if not things: + return [] + + wrapped = [Wrapped(thing) for thing in things] + if hasattr(things[0], 'add_props'): + # assume all things are of the same type and use the first thing's + # add_props to process the list. + things[0].add_props(c.user, wrapped) + return wrapped diff --git a/r2/r2/lib/template_helpers.py b/r2/r2/lib/template_helpers.py index 65c0acafe..f2c8d44ef 100755 --- a/r2/r2/lib/template_helpers.py +++ b/r2/r2/lib/template_helpers.py @@ -168,6 +168,12 @@ class JSPreload(js.DataSource): def set(self, url, data): self.data[url] = data + def set_wrapped(self, url, wrapped): + from r2.lib.pages.things import wrap_things + if not isinstance(wrapped, Wrapped): + wrapped = wrap_things(wrapped)[0] + self.data[url] = wrapped.render_nocache('', style='api').finalize() + def use(self): hooks.get_hook("js_preload.use").call(js_preload=self)