if the user is logged in, dont hit the server for recently clicked

This commit is contained in:
spez
2009-06-03 12:34:07 -07:00
parent 7ee5b70c7f
commit 9c1b159614
7 changed files with 59 additions and 26 deletions

View File

@@ -36,7 +36,8 @@ from r2.lib.utils import get_title, sanitize_url, timeuntil, set_last_modified
from r2.lib.utils import query_string, to36, timefromnow, link_from_url
from r2.lib.wrapped import Wrapped
from r2.lib.pages import FriendList, ContributorList, ModList, \
BannedList, BoringPage, FormPage, NewLink, CssError, UploadedImage
BannedList, BoringPage, FormPage, NewLink, CssError, UploadedImage, \
ClickGadget
from r2.lib.menus import CommentSortMenu
from r2.lib.normalized_hot import expire_hot
@@ -1369,19 +1370,10 @@ class ApiController(RedditController):
if not links:
return
def wrapper(link):
link.embed_voting_style = 'votable'
return Wrapped(link)
content = ClickGadget(links).make_content()
#this will disable the hardcoded widget styles
request.get.style = "off"
c.render_style = 'htmllite'
builder = IDBuilder([ link._fullname for link in links ],
wrap = wrapper)
listing = LinkListing(builder, nextprev=False, show_nums=False).listing()
jquery('.gadget').show().find('.click-gadget').html(
spaceCompress(listing.render()))
spaceCompress(content))
@noresponse()
def POST_tb_commentspanel_show(self):

View File

@@ -141,19 +141,25 @@ def set_user_cookie(name, val):
c.cookies[uname + '_' + name] = Cookie(value = val)
valid_click_cookie = re.compile(r'(:?t[0-9]+_[a-zA-Z0-9]+)+').match
def read_click_cookie():
# not used at the moment, if you start using this, you should also
# test it
def set_recent_clicks():
c.recent_clicks = []
if not c.user_is_loggedin:
return
click_cookie = read_user_cookie('recentclicks')
if click_cookie:
if valid_click_cookie(click_cookie):
fullnames = [ x for x in UniqueIterator(click_cookie.split(':')) if x ]
names = [ x for x in UniqueIterator(click_cookie.split(':')) if x ]
if len(click_cookie) > 1000:
fullnames = fullnames[:20]
set_user_cookie('recentclicks', ':'.join(fullnames))
return fullnames
names = names[:20]
set_user_cookie('recentclicks', ':'.join(names))
#eventually this will look at the user preference
names = names[:5]
c.recent_clicks = Link._by_fullname(names, data = True,
return_dict = False)
else:
#if the cookie wasn't valid, clear it
set_user_cookie('recentclicks', '')
def read_mod_cookie():
@@ -497,6 +503,7 @@ class RedditController(BaseController):
set_content_lang()
set_colors()
set_recent_reddits()
set_recent_clicks()
# set some environmental variables in case we hit an abort
if not isinstance(c.site, FakeSubreddit):

View File

@@ -308,7 +308,36 @@ class Reddit(Wrapped):
"""returns a Wrapped (or renderable) item for the main content div."""
return self.content_stack(self.infobar, self.nav_menu, self._content)
class ClickGadget(Wrapped): pass
class ClickGadget(Wrapped):
def __init__(self, links, *a, **kw):
self.links = links
self.content = ''
if c.user_is_loggedin and self.links:
self.content = self.make_content()
Wrapped.__init__(self, *a, **kw)
def make_content(self):
def wrapper(link):
link.embed_voting_style = 'votable'
return Wrapped(link)
#temporarily change the render style
orig_render_style = c.render_style
c.render_style = 'htmllite'
#this will disable the hardcoded widget styles
request.get.style = "off"
builder = IDBuilder([link._fullname for link in self.links],
wrap = wrapper)
listing = LinkListing(builder, nextprev=False, show_nums=False).listing()
content = listing.render()
#restore render style
c.render_style = orig_render_style
return content
class RedditMin(Reddit):
"""a version of Reddit that has no sidebar, toolbar, footer,

View File

@@ -790,8 +790,12 @@ $(function() {
.filter(".gray").removeClass("gray").attr("value", "")
});
/* set cookies to be from this user if there is one */
if(reddit.logged)
if (reddit.logged) {
$.cookie_name_prefix(reddit.logged);
}
else {
populate_click_gadget();
}
/* set up the cookie domain */
$.default_cookie_domain(reddit.cur_domain.split(':')[0]);
@@ -802,6 +806,6 @@ $(function() {
/* visually mark the last-clicked entry */
last_click();
populate_click_gadget();
});

View File

@@ -22,11 +22,11 @@
<%namespace file="printable.html" import="simple_button" />
%if c.user.pref_clickgadget:
<div class="gadget" style="display: none;">
<div class="gadget" style="${'display: none' if not thing.content else ''}">
<h2>${_("Recently viewed links")}</h2>
<div class="click-gadget">
<!-- populated by the populate_click_gadget function in reddit.js -->
${thing.content}
</div>
<div class="right">

View File

@@ -135,7 +135,7 @@
%endif
##cheating... we should move ads into a template of its own
${ClickGadget().render()}
${ClickGadget(c.recent_clicks).render()}
</%def>

View File

@@ -356,7 +356,8 @@ ${unsafe(txt)}
/* the user's voting hash */
modhash: ${"'%s'" % c.modhash or "false"},
/* current domain */
cur_domain: "${get_domain(cname = c.frameless_cname, subreddit = False)}",
cur_domain: "${get_domain(cname = c.frameless_cname, subreddit = False,
no_www = True)}",
/* where do ajax request go? */
ajax_domain: "${get_domain(cname=c.authorized_cname, subreddit = False)}",
vl: {},