def get_subreddit(self): """checks if the current url refers to a subreddit and returns that subreddit object. The cases here are: * the hostname is unset or is g.domain, in which case it looks for /r/XXXX or /reddits. The default in this case is Default. * the hostname is a cname to a known subreddit. On failure to find a subreddit, returns None. """ from pylons import g from r2.models import Subreddit, Sub, NotFound, DefaultSR try: if not self.hostname or self.hostname.startswith(g.domain): if self.path.startswith('/r/'): return Subreddit._by_name(self.path.split('/')[2]) elif self.path.startswith('/reddits/'): return Sub else: return DefaultSR() elif self.hostname: return Subreddit._by_domain(self.hostname) except NotFound: pass return None
def get_subreddit(self): """checks if the current url refers to a subreddit and returns that subreddit object. The cases here are: * the hostname is unset or is g.domain, in which case it looks for /r/XXXX or /subreddits. The default in this case is Default. * the hostname is a cname to a known subreddit. On failure to find a subreddit, returns None. """ from pylons import g from r2.models import Subreddit, Sub, NotFound, DefaultSR try: if (not self.hostname or is_subdomain(self.hostname, g.domain) or self.hostname.startswith(g.domain)): if self.path.startswith('/r/'): return Subreddit._by_name(self.path.split('/')[2]) elif self.path.startswith(('/subreddits/', '/reddits/')): return Sub else: return DefaultSR() elif self.hostname: return Subreddit._by_domain(self.hostname) except NotFound: pass return None
def set_subreddit(): #the r parameter gets added by javascript for POST requests so we #can reference c.site in api.py sr_name = request.environ.get("subreddit", request.POST.get('r')) domain = request.environ.get("domain") can_stale = request.method.upper() in ('GET', 'HEAD') c.site = Frontpage if not sr_name: #check for cnames cname = request.environ.get('legacy-cname') if cname: sr = Subreddit._by_domain(cname) or Frontpage domain = g.domain if g.domain_prefix: domain = ".".join((g.domain_prefix, domain)) redirect_to('http://%s%s' % (domain, sr.path), _code=301) elif sr_name == 'r': #reddits c.site = Sub elif '+' in sr_name: sr_names = sr_name.split('+') srs = set(Subreddit._by_name(sr_names, stale=can_stale).values()) if All in srs: c.site = All elif Friends in srs: c.site = Friends else: srs = [sr for sr in srs if not isinstance(sr, FakeSubreddit)] if len(srs) == 0: c.site = MultiReddit([], sr_name) elif len(srs) == 1: c.site = srs.pop() else: sr_ids = [sr._id for sr in srs] c.site = MultiReddit(sr_ids, sr_name) else: try: c.site = Subreddit._by_name(sr_name, stale=can_stale) except NotFound: sr_name = chksrname(sr_name) if sr_name: redirect_to("/reddits/search?q=%s" % sr_name) elif not c.error_page and not request.path.startswith("/api/login/") : abort(404) #if we didn't find a subreddit, check for a domain listing if not sr_name and isinstance(c.site, DefaultSR) and domain: c.site = DomainSR(domain) if isinstance(c.site, FakeSubreddit): c.default_sr = True
def set_subreddit(): #the r parameter gets added by javascript for POST requests so we #can reference c.site in api.py sr_name = request.environ.get("subreddit", request.POST.get('r')) domain = request.environ.get("domain") can_stale = request.method.upper() in ('GET', 'HEAD') c.site = Frontpage if not sr_name: #check for cnames cname = request.environ.get('legacy-cname') if cname: sr = Subreddit._by_domain(cname) or Frontpage domain = g.domain if g.domain_prefix: domain = ".".join((g.domain_prefix, domain)) redirect_to('http://%s%s' % (domain, sr.path), _code=301) elif sr_name == 'r': #reddits c.site = Sub elif '+' in sr_name: sr_names = sr_name.split('+') srs = Subreddit._by_name(sr_names, stale=can_stale).values() if All in srs: c.site = All elif Friends in srs: c.site = Friends else: srs = [sr for sr in srs if not isinstance(sr, FakeSubreddit)] multi_path = '/r/' + sr_name if not srs: c.site = MultiReddit(multi_path, []) elif len(srs) == 1: c.site = srs[0] else: c.site = MultiReddit(multi_path, srs) elif '-' in sr_name: sr_names = sr_name.split('-') if not sr_names[0].lower() == All.name.lower(): redirect_to("/subreddits/search?q=%s" % sr_name) srs = Subreddit._by_name(sr_names[1:], stale=can_stale).values() srs = [sr for sr in srs if not isinstance(sr, FakeSubreddit)] if not srs: c.site = All else: c.site = AllMinus(srs) else: try: c.site = Subreddit._by_name(sr_name, stale=can_stale) except NotFound: sr_name = chksrname(sr_name) if sr_name: redirect_to("/subreddits/search?q=%s" % sr_name) elif not c.error_page and not request.path.startswith( "/api/login/"): abort(404) #if we didn't find a subreddit, check for a domain listing if not sr_name and isinstance(c.site, DefaultSR) and domain: c.site = DomainSR(domain) if isinstance(c.site, FakeSubreddit): c.default_sr = True
def set_subreddit(): #the r parameter gets added by javascript for POST requests so we #can reference c.site in api.py sr_name = request.environ.get("subreddit", request.POST.get('r')) domain = request.environ.get("domain") can_stale = request.method.upper() in ('GET', 'HEAD') c.site = Frontpage if not sr_name: #check for cnames cname = request.environ.get('legacy-cname') if cname: sr = Subreddit._by_domain(cname) or Frontpage domain = g.domain if g.domain_prefix: domain = ".".join((g.domain_prefix, domain)) redirect_to('http://%s%s' % (domain, sr.path), _code=301) elif sr_name == 'r': #reddits c.site = Sub elif '+' in sr_name: sr_names = sr_name.split('+') srs = Subreddit._by_name(sr_names, stale=can_stale).values() if All in srs: c.site = All elif Friends in srs: c.site = Friends else: srs = [sr for sr in srs if not isinstance(sr, FakeSubreddit)] multi_path = '/r/' + sr_name if not srs: c.site = MultiReddit(multi_path, []) elif len(srs) == 1: c.site = srs[0] else: c.site = MultiReddit(multi_path, srs) elif '-' in sr_name: sr_names = sr_name.split('-') base_sr_name, exclude_sr_names = sr_names[0], sr_names[1:] srs = Subreddit._by_name(sr_names, stale=can_stale) base_sr = srs.pop(base_sr_name, None) exclude_srs = [sr for sr in srs.itervalues() if not isinstance(sr, FakeSubreddit)] if base_sr == All: if exclude_srs: c.site = AllMinus(exclude_srs) else: c.site = All elif base_sr == Mod: if exclude_srs: c.site = ModMinus(exclude_srs) else: c.site = Mod else: redirect_to("/subreddits/search?q=%s" % sr_name) else: try: c.site = Subreddit._by_name(sr_name, stale=can_stale) except NotFound: sr_name = chksrname(sr_name) if sr_name: redirect_to("/subreddits/search?q=%s" % sr_name) elif not c.error_page and not request.path.startswith("/api/login/") : abort(404) #if we didn't find a subreddit, check for a domain listing if not sr_name and isinstance(c.site, DefaultSR) and domain: # Redirect IDN to their IDNA name if necessary try: idna = _force_unicode(domain).encode("idna") if idna != domain: redirect_to("/domain/%s%s" % (idna, request.environ["PATH_INFO"])) except UnicodeError: domain = '' # Ensure valid_ascii_domain fails if not c.error_page and not valid_ascii_domain.match(domain): abort(404) c.site = DomainSR(domain) if isinstance(c.site, FakeSubreddit): c.default_sr = True
def set_subreddit(): # the r parameter gets added by javascript for POST requests so we # can reference c.site in api.py sr_name = request.environ.get("subreddit", request.POST.get("r")) domain = request.environ.get("domain") can_stale = request.method.upper() in ("GET", "HEAD") c.site = Frontpage if not sr_name: # check for cnames cname = request.environ.get("legacy-cname") if cname: sr = Subreddit._by_domain(cname) or Frontpage domain = g.domain if g.domain_prefix: domain = ".".join((g.domain_prefix, domain)) redirect_to("http://%s%s" % (domain, sr.path), _code=301) elif sr_name == "r": # reddits c.site = Sub elif "+" in sr_name: sr_names = sr_name.split("+") srs = Subreddit._by_name(sr_names, stale=can_stale).values() if All in srs: c.site = All elif Friends in srs: c.site = Friends else: srs = [sr for sr in srs if not isinstance(sr, FakeSubreddit)] multi_path = "/r/" + sr_name if not srs: c.site = MultiReddit(multi_path, []) elif len(srs) == 1: c.site = srs[0] else: c.site = MultiReddit(multi_path, srs) elif "-" in sr_name: sr_names = sr_name.split("-") if not sr_names[0].lower() == All.name.lower(): redirect_to("/subreddits/search?q=%s" % sr_name) srs = Subreddit._by_name(sr_names[1:], stale=can_stale).values() srs = [sr for sr in srs if not isinstance(sr, FakeSubreddit)] if not srs: c.site = All else: c.site = AllMinus(srs) else: try: c.site = Subreddit._by_name(sr_name, stale=can_stale) except NotFound: sr_name = chksrname(sr_name) if sr_name: redirect_to("/subreddits/search?q=%s" % sr_name) elif not c.error_page and not request.path.startswith("/api/login/"): abort(404) # if we didn't find a subreddit, check for a domain listing if not sr_name and isinstance(c.site, DefaultSR) and domain: c.site = DomainSR(domain) if isinstance(c.site, FakeSubreddit): c.default_sr = True
def set_subreddit(): #the r parameter gets added by javascript for POST requests so we #can reference c.site in api.py sr_name = request.environ.get("subreddit", request.POST.get('r')) domain = request.environ.get("domain") can_stale = request.method.upper() in ('GET', 'HEAD') c.site = Frontpage if not sr_name: #check for cnames cname = request.environ.get('legacy-cname') if cname: sr = Subreddit._by_domain(cname) or Frontpage domain = g.domain if g.domain_prefix: domain = ".".join((g.domain_prefix, domain)) redirect_to('http://%s%s' % (domain, sr.path), _code=301) elif sr_name == 'r': #reddits c.site = Sub elif '+' in sr_name: sr_names = sr_name.split('+') srs = Subreddit._by_name(sr_names, stale=can_stale).values() if All in srs: c.site = All elif Friends in srs: c.site = Friends else: srs = [sr for sr in srs if not isinstance(sr, FakeSubreddit)] multi_path = '/r/' + sr_name if not srs: c.site = MultiReddit(multi_path, []) elif len(srs) == 1: c.site = srs[0] else: c.site = MultiReddit(multi_path, srs) elif '-' in sr_name: sr_names = sr_name.split('-') base_sr_name, exclude_sr_names = sr_names[0], sr_names[1:] srs = Subreddit._by_name(sr_names, stale=can_stale) base_sr = srs.pop(base_sr_name, None) exclude_srs = [ sr for sr in srs.itervalues() if not isinstance(sr, FakeSubreddit) ] if base_sr == All: if exclude_srs: c.site = AllMinus(exclude_srs) else: c.site = All elif base_sr == Mod: if exclude_srs: c.site = ModMinus(exclude_srs) else: c.site = Mod else: redirect_to("/subreddits/search?q=%s" % sr_name) else: try: c.site = Subreddit._by_name(sr_name, stale=can_stale) except NotFound: sr_name = chksrname(sr_name) if sr_name: redirect_to("/subreddits/search?q=%s" % sr_name) elif not c.error_page and not request.path.startswith( "/api/login/"): abort(404) #if we didn't find a subreddit, check for a domain listing if not sr_name and isinstance(c.site, DefaultSR) and domain: # Redirect IDN to their IDNA name if necessary try: idna = _force_unicode(domain).encode("idna") if idna != domain: redirect_to("/domain/%s%s" % (idna, request.environ["PATH_INFO"])) except UnicodeError: domain = '' # Ensure valid_ascii_domain fails if not c.error_page and not valid_ascii_domain.match(domain): abort(404) c.site = DomainSR(domain) if isinstance(c.site, FakeSubreddit): c.default_sr = True