From b83e0733b03de61866b67cf31728121d595cecfd Mon Sep 17 00:00:00 2001 From: spez Date: Thu, 2 Oct 2008 11:32:26 -0700 Subject: [PATCH] Added support for multiple reddit names in the url. For example, http://www.reddit.com/r/programming+politics Removed all the query_rules functions and references to them. --- r2/r2/config/middleware.py | 2 +- r2/r2/controllers/front.py | 28 +++++++++++++-------- r2/r2/controllers/reddit_base.py | 15 ++++++++--- r2/r2/models/subreddit.py | 43 +++++++++++--------------------- 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/r2/r2/config/middleware.py b/r2/r2/config/middleware.py index 0a9ba5f6e..ffbbc79fd 100644 --- a/r2/r2/config/middleware.py +++ b/r2/r2/config/middleware.py @@ -241,7 +241,7 @@ class DomainMiddleware(object): class SubredditMiddleware(object): - sr_pattern = re.compile(r'^/r/([^/]{3,20})') + sr_pattern = re.compile(r'^/r/([^/]{3,})') def __init__(self, app): self.app = app diff --git a/r2/r2/controllers/front.py b/r2/r2/controllers/front.py index a2c680c7e..0936d88d2 100644 --- a/r2/r2/controllers/front.py +++ b/r2/r2/controllers/front.py @@ -77,13 +77,19 @@ class FrontController(RedditController): def GET_random(self): """The Serendipity button""" n = rand.randint(0, 9) - links = Link._query(*c.site.query_rules()) - links._sort = desc('_date') if n > 5 else desc('_hot') - links._limit = 50 - links = list(links) - l = links[rand.randint(0, len(links)-1)] - l._load() - return self.redirect(l.url) + sort = 'new' if n > 5 else 'hot' + links = c.site.get_links(sort, 'all') + if isinstance(links, thing.Query): + links._limit = 25 + links = [x._fullname for x in links] + else: + links = links[:25] + if links: + name = links[rand.randint(0, min(24, len(links)-1))] + link = Link._by_fullname(name, data = True) + return self.redirect(link.url) + else: + return self.redirect('/') def GET_password(self): """The 'what is my password' page""" @@ -253,9 +259,9 @@ class FrontController(RedditController): comments = Comment._query(Comment.c.reported != 0, Comment.c._spam == False) query = thing.Merge((links, comments), + Link.c.sr_id == c.site._id, sort = desc('_date'), - data = True, - *c.site.query_rules()) + data = True) builder = QueryBuilder(query, num = num, after = after, count = count, reverse = reverse, @@ -267,9 +273,9 @@ class FrontController(RedditController): links = Link._query(Link.c._spam == True) comments = Comment._query(Comment.c._spam == True) query = thing.Merge((links, comments), + Link.c.sr_id == c.site._id, sort = desc('_date'), - data = True, - *c.site.query_rules()) + data = True) builder = QueryBuilder(query, num = num, after = after, count = count, reverse = reverse, diff --git a/r2/r2/controllers/reddit_base.py b/r2/r2/controllers/reddit_base.py index 246cc93b1..fa4b79f04 100644 --- a/r2/r2/controllers/reddit_base.py +++ b/r2/r2/controllers/reddit_base.py @@ -217,9 +217,7 @@ def set_subreddit(): sr_name = request.environ.get("subreddit", request.POST.get('r')) domain = request.environ.get("domain") - #we can get rid of the sr_name == Default.name constraint if - #you're reading this. it was there to fix a stale html issue. - if not sr_name or sr_name == Default.name: + if not sr_name: #check for cnames sub_domain = request.environ.get('sub_domain') sr = Subreddit._by_domain(sub_domain) if sub_domain else None @@ -229,7 +227,16 @@ def set_subreddit(): c.site = Sub else: try: - c.site = Subreddit._by_name(sr_name) + if '+' in sr_name: + srs = set() + sr_names = sr_name.split('+') + real_path = sr_name + for sr_name in sr_names: + srs.add(Subreddit._by_name(sr_name)) + sr_ids = [sr._id for sr in srs] + c.site = MultiReddit(sr_ids, real_path) + else: + c.site = Subreddit._by_name(sr_name) except NotFound: c.site = Default redirect_to("/reddits/create?name=%s" % sr_name) diff --git a/r2/r2/models/subreddit.py b/r2/r2/models/subreddit.py index 8a9b730c0..da77225bb 100644 --- a/r2/r2/models/subreddit.py +++ b/r2/r2/models/subreddit.py @@ -225,10 +225,6 @@ class Subreddit(Thing, Printable): sr_ids = (self._id,) return sr_ids - def query_rules(self): - #really we mean Link.c.sr_id, but rules are type agnostic - return (self.c.sr_id == self._id,) - def get_links(self, sort, time): from r2.lib.db import queries return queries.get_links(self, sort, time) @@ -442,12 +438,6 @@ class FriendsSR(FakeSubreddit): name = 'friends' title = 'friends' - def query_rules(self): - if c.user_is_loggedin: - return (self.c.author_id == c.user.friends,) - else: - return (self.c.sr_id == self.default_srs(c.content_langs, ids = True),) - def get_links(self, sort, time): from r2.lib.db import queries from r2.models import Link @@ -466,12 +456,6 @@ class AllSR(FakeSubreddit): name = 'all' title = 'all' - def query_rules(self): - if c.content_langs != 'all': - return (self.c.lang == c.content_langs,) - else: - return () - def get_links(self, sort, time): from r2.models import Link from r2.lib.db import queries @@ -487,11 +471,6 @@ class DefaultSR(FakeSubreddit): path = '/' header = 'http://static.reddit.com/reddit.com.header.png' - def query_rules(self): - user = c.user if c.user_is_loggedin else None - subreddits = Subreddit.user_subreddits(user) - return (self.c.sr_id == subreddits,) - def get_links_sr_ids(self, sr_ids, sort, time): from r2.lib.db import queries from r2.models import Link @@ -524,7 +503,6 @@ class DefaultSR(FakeSubreddit): def title(self): return _("reddit.com: what's new online!") -#TODO: I'm not sure this is the best way to do this class MaskedSR(DefaultSR): def set_mask(self, mask): self.show_sr = [] @@ -541,13 +519,6 @@ class MaskedSR(DefaultSR): return_dict = False) self.hide_sr = [s._id for s in self.hide_sr] - def query_rules(self): - user = c.user if c.user_is_loggedin else None - subreddits = Subreddit.user_subreddits(user) - subreddits = [s for s in subreddits if s not in self.hide_sr] - subreddits.extend(self.show_sr) - return (self.c.sr_id == subreddits,) - def get_links(self, sort, time): user = c.user if c.user_is_loggedin else None sr_ids = Subreddit.user_subreddits(user) @@ -555,6 +526,20 @@ class MaskedSR(DefaultSR): sr_ids.extend(self.show_sr) return self.get_links_sr_ids(sr_ids, sort, time) +class MultiReddit(DefaultSR): + name = 'multi' + + def __init__(self, sr_ids, path): + DefaultSR.__init__(self) + self.real_path = path + self.sr_ids = sr_ids + + @property + def path(self): + return '/r/' + self.real_path + + def get_links(self, sort, time): + return self.get_links_sr_ids(self.sr_ids, sort, time) class SubSR(FakeSubreddit): stylesheet = 'subreddit.css'