diff --git a/r2/r2/lib/rising.py b/r2/r2/lib/rising.py index 3eaf07364..9eadd7a75 100644 --- a/r2/r2/lib/rising.py +++ b/r2/r2/lib/rising.py @@ -20,14 +20,16 @@ # Inc. All Rights Reserved. ############################################################################### -from pylons import g, c -from r2.models.link import Link, Subreddit -from r2.lib import utils -from r2.lib import count - from datetime import datetime -cache = g.cache +from pylons import g + +from r2.lib import count +from r2.models.link import Link + + +CACHE_KEY = "rising" + def calc_rising(): sr_count = count.get_link_counts() @@ -38,7 +40,7 @@ def calc_rising(): counts = link_count.values() counts.sort(reverse=True) maxcount = sum(counts[:10]) / 20 - + #prune the list rising = [(n, link_names[n].sr_id) for n in link_names.keys() if link_count[n] < maxcount] @@ -57,15 +59,11 @@ def calc_rising(): rising.sort(lambda x, y: r(score(y) - score(x))) return rising + def set_rising(): - rising = calc_rising() - cache.set('rising', rising) + g.cache.set(CACHE_KEY, calc_rising()) + def get_rising(sr): - #get the sr_ids - sr_ids = sr.rising_srs() - if sr_ids: - rising = cache.get('rising', []) - return [p[0] for p in filter(lambda pair: pair[1] in sr_ids, rising)] - else: - return [] + rising = g.cache.get(CACHE_KEY) + return [link for link, sr_id in rising if sr.keep_for_rising(sr_id)] diff --git a/r2/r2/lib/utils/_utils.pyx b/r2/r2/lib/utils/_utils.pyx index e00296dfd..b8686f9db 100644 --- a/r2/r2/lib/utils/_utils.pyx +++ b/r2/r2/lib/utils/_utils.pyx @@ -136,7 +136,7 @@ cdef dict timeintervald = dict(second = 1, week = 60 * 60 * 24 * 7, month = 60 * 60 * 24 * 30, year = 60 * 60 * 24 * 365) -cdef timeinterval_fromstr(str interval): +cpdef timeinterval_fromstr(str interval): "Used by timeago and timefromnow to generate timedeltas from friendly text" parts = interval.strip().split(' ') if len(parts) == 1: diff --git a/r2/r2/models/subreddit.py b/r2/r2/models/subreddit.py index 31f4fa8df..066e491ba 100644 --- a/r2/r2/models/subreddit.py +++ b/r2/r2/models/subreddit.py @@ -471,15 +471,9 @@ class Subreddit(Thing, Printable): return subreddits if return_dict else subreddits.values() - #rising uses this to know which subreddits to include, doesn't - #work for all/friends atm - def rising_srs(self): - if c.default_sr or not hasattr(self, '_id'): - user = c.user if c.user_is_loggedin else None - sr_ids = self.user_subreddits(user) - else: - sr_ids = (self._id,) - return sr_ids + def keep_for_rising(self, sr_id): + """Return whether or not to keep a thing in rising for this SR.""" + return sr_id == self._id def get_links(self, sort, time): from r2.lib.db import queries @@ -840,6 +834,9 @@ class FakeSubreddit(Subreddit): self.title = '' self.link_flair_position = 'right' + def keep_for_rising(self, sr_id): + return False + @property def _should_wiki(self): return False @@ -965,6 +962,9 @@ class AllSR(FakeSubreddit): name = 'all' title = 'all subreddits' + def keep_for_rising(self, sr_id): + return True + def get_links(self, sort, time): from r2.models import Link from r2.lib.db import queries @@ -988,9 +988,6 @@ class AllSR(FakeSubreddit): from r2.lib.db import queries return queries.get_all_gilded_comments() - def rising_srs(self): - return None - class AllMinus(AllSR): name = _("%s (filtered)") % "all" @@ -1000,6 +997,9 @@ class AllMinus(AllSR): self.srs = srs self.sr_ids = [sr._id for sr in srs] + def keep_for_rising(self, sr_id): + return sr_id not in self.sr_ids + @property def title(self): return 'all subreddits except ' + ', '.join(sr.name for sr in self.srs) @@ -1024,6 +1024,15 @@ class _DefaultSR(FakeSubreddit): path = '/' header = g.default_header_url + def _get_sr_ids(self): + if not hasattr(self, "_cached_sr_ids"): + user = c.user if c.user_is_loggedin else None + self._cached_sr_ids = Subreddit.user_subreddits(user) + return self._cached_sr_ids + + def keep_for_rising(self, sr_id): + return sr_id in self._get_sr_ids() + def is_moderator(self, user): return False @@ -1048,8 +1057,7 @@ class _DefaultSR(FakeSubreddit): return q def get_links(self, sort, time): - user = c.user if c.user_is_loggedin else None - sr_ids = Subreddit.user_subreddits(user) + sr_ids = self._get_sr_ids() return self.get_links_sr_ids(sr_ids, sort, time) @property @@ -1142,6 +1150,9 @@ class MultiReddit(_DefaultSR): self.banned_sr_ids = [sr._id for sr in srs if sr._spam] self.kept_sr_ids = [sr._id for sr in srs if not sr._spam] + def keep_for_rising(self, sr_id): + return sr_id in self.kept_sr_ids + def is_moderator(self, user): if not user: return False @@ -1163,9 +1174,6 @@ class MultiReddit(_DefaultSR): def get_links(self, sort, time): return self.get_links_sr_ids(self.kept_sr_ids, sort, time) - def rising_srs(self): - return self.kept_sr_ids - def get_all_comments(self): from r2.lib.db.queries import get_sr_comments, merge_results srs = Subreddit._byID(self.kept_sr_ids, return_dict=False)