Пример #1
0
def login():
    """Store the current URL as the redirect target on success, then redirect
    to the login endpoint for the current app.

    :return: a :func:`~flask.redirect` to the login endpoint for the current
      :class:`~flask.Flask` app.
    """
    session[REDIRECT_KEY] = urlparse(request.url)._replace(netloc=get_host()).geturl()
    return redirect(url_for("login"))
Пример #2
0
def index(path):
    if ".pr." in get_host() and not is_staff("cs61a"):
        return login()

    bucket = get_bucket(
        {
            "cs61a": "website-base",
            "website": "website-base",
            "website-server": "website-base",
        },
        "website-base",
    )
    return serve_path(bucket, "/released/", path, path_404="404/index.html")
Пример #3
0
def get_bucket(app_lookup: Dict[str, str], default_app: str = None):
    if current_app.debug:
        return f"{default_app}.buckets.cs61a.org"

    host = get_host()
    if ".pr." in host:
        pr, app, *_ = host.split(".")
        pr = int(pr)
        if app not in app_lookup:
            abort(404)
        bucket = f"{app_lookup[app]}-pr{pr}.buckets.cs61a.org"
        try:
            storage.Client().get_bucket(bucket)
            return bucket
        except NotFound:
            pass
    else:
        app, *_ = host.split(".")
        if app not in app_lookup:
            abort(404)
    return f"{app_lookup[app]}.buckets.cs61a.org"
Пример #4
0
def serve_path(bucket, root, path):
    filename = safe_join(root, path)[1:]
    client = storage.Client()
    bucket = client.get_bucket(bucket)
    try:
        if not filename:
            raise NotFound(filename)
        blob = bucket.blob(filename)
        with tempfile.NamedTemporaryFile() as temp:
            blob.download_to_filename(temp.name)
            mimetype = None
            if filename.endswith(".scm"):
                mimetype = "text/x-scheme"
            return send_file(temp.name,
                             attachment_filename=filename,
                             mimetype=mimetype)
    except NotFound:
        if filename.endswith("404.html"):
            abort(404)
        elif filename.endswith("index.html"):
            return serve_path(bucket, root, "404.html"), 404
        else:
            if path and not path.endswith("/"):
                if bucket.blob(filename + "/" + "index.html").exists():
                    target = urlunparse((
                        "https" if getenv("ENV") == "prod" else "http",
                        get_host(),
                        "/" + path + "/",
                        urlparse(request.url).params,
                        urlparse(request.url).query,
                        urlparse(request.url).fragment,
                    ))
                    return redirect(target, 301)
                else:
                    return serve_path(bucket, root, "404.html"), 404
            return serve_path(bucket, root, path + "index.html")
Пример #5
0
def get_host_username():
    return get_host().split(".")[0] if is_prod_build() else DEFAULT_USER
Пример #6
0
def is_prod_build():
    return not app.debug and ".pr." not in get_host() and "cs61a" in get_host()
Пример #7
0
def login():
    session[REDIRECT_KEY] = urlparse(
        request.url)._replace(netloc=get_host()).geturl()
    return redirect(url_for("login"))
Пример #8
0
def is_prod_build():
    return ".pr." not in get_host() and "cs61a" in get_host()