Esempio n. 1
0
def set_multireddit():
    routes_dict = request.environ["pylons.routes_dict"]
    if "multipath" in routes_dict:
        multipath = routes_dict["multipath"].lower()
        multi_id = None

        if c.user_is_loggedin and routes_dict.get("my_multi"):
            multi_id = "/user/%s/m/%s" % (c.user.name.lower(), multipath)
        elif "username" in routes_dict:
            username = routes_dict["username"].lower()

            if c.user_is_loggedin:
                # redirect /user/foo/m/... to /me/m/... for user foo.
                if username == c.user.name.lower():
                    # trim off multi id
                    url_parts = request.path_qs.split("/")[5:]
                    url_parts.insert(0, "/me/m/%s" % multipath)
                    path = "/".join(url_parts)
                    abort(302, location=BaseController.format_output_url(path))

            multi_id = "/user/%s/m/%s" % (username, multipath)

        if multi_id:
            try:
                c.site = LabeledMulti._byID(multi_id)
            except tdb_cassandra.NotFound:
                abort(404)
    def pre(self):
        BaseController.pre(self)

        action = request.environ["pylons.routes_dict"].get("action")
        if action:
            if not self._get_action_handler():
                action = 'invalid'
            controller = request.environ["pylons.routes_dict"]["controller"]
            timer_name = "service_time.web.{}.{}".format(controller, action)
            c.request_timer = g.stats.get_timer(timer_name)
        else:
            c.request_timer = SimpleSillyStub()

        c.request_timer.start()

        if "Origin" in request.headers:
            oauth_origin = "https://%s" % g.oauth_domain
            response.headers["Access-Control-Allow-Origin"] = oauth_origin
            response.headers["Vary"] = "Origin"
            response.headers["Access-Control-Allow-Methods"] = "GET"
            response.headers["Access-Control-Allow-Credentials"] = "true"
Esempio n. 3
0
 def __init__(self, *args, **kwargs):
     self._pixel_data = None
     BaseController.__init__(self, *args, **kwargs)
 def __init__(self, *args, **kwargs):
     self._pixel_data = None
     BaseController.__init__(self, *args, **kwargs)
Esempio n. 5
0
 def pre(self, *k, **kw):
     BaseController.pre(self, *k, **kw)
     c.extension = request.environ.get('extension')
Esempio n. 6
0
 def pre(self, *k, **kw):
     BaseController.pre(self, *k, **kw)
     c.extension = request.environ.get('extension')
Esempio n. 7
0
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))
            path = "http://%s%s" % (domain, sr.path)
            abort(301, location=BaseController.format_output_url(path))
    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:
            path = "/subreddits/search?q=%s" % sr_name
            abort(302, location=BaseController.format_output_url(path))
    else:
        try:
            c.site = Subreddit._by_name(sr_name, stale=can_stale)
        except NotFound:
            sr_name = chksrname(sr_name)
            if sr_name:
                path = "/subreddits/search?q=%s" % sr_name
                abort(302, location=BaseController.format_output_url(path))
            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:
                path_info = request.environ["PATH_INFO"]
                path = "/domain/%s%s" % (idna, path_info)
                abort(302, location=BaseController.format_output_url(path))
        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
Esempio n. 8
0
 def _redirect(self, red):
     from pylons import c, request
     if c.cname:
         red = BaseController.format_output_url(red, subreddit = c.site,
                                                require_frame = False)
     self.redirect = red