Skip to content

Commit

Permalink
cli: Use redirects to download directly from configured depot
Browse files Browse the repository at this point in the history
  • Loading branch information
jackrosenthal committed Feb 19, 2024
1 parent bd9440c commit cf0c57e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
3 changes: 3 additions & 0 deletions algobowl/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def get_server(self):
def get_url(self, path):
return f"{self.get_server()}{path}"

def get_file_url(self, name):
return self.get_url(f"/files/{name}?redirect=true")

def set_default_server(self, server):
defaults = self.config.get("defaults", {})
defaults["server"] = server
Expand Down
4 changes: 2 additions & 2 deletions algobowl/cli/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def input_upload(cli, file_path):
@click.pass_obj
def input_download(cli, output_file):
group_id = get_group_id(cli)
r = cli.session.get(cli.config.get_url(f"/files/input_group{group_id}.txt"))
r = cli.session.get(cli.config.get_file_url(f"input_group{group_id}.txt"))
auth.check_response(r)
output_file.write(r.text)

Expand Down Expand Up @@ -163,7 +163,7 @@ def output_download(cli, file_path):
if not to_group_id:
to_group_id = infer_group_from_path(file_path)
r = cli.session.get(
cli.config.get_url(f"/files/output_from_{from_group_id}_to_{to_group_id}.txt"),
cli.config.get_file_url(f"output_from_{from_group_id}_to_{to_group_id}.txt"),
)
auth.check_response(r)
file_path.write_text(r.text)
Expand Down
18 changes: 10 additions & 8 deletions algobowl/controllers/root.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tg
from repoze.who.api import get_api
from tg import expose, flash, lurl, predicates, redirect, require, tmpl_context, url
from tg import expose, flash, lurl, predicates, require, tmpl_context, url
from tg.exceptions import HTTPFound
from tgext.admin.controller import AdminController

Expand Down Expand Up @@ -39,7 +39,7 @@ def index(self):
@expose()
def algobowl(self):
"""Redirect for old route to homepage"""
redirect(url("/"))
tg.redirect(url("/"))

@expose("algobowl.templates.privacy")
def privacy(self):
Expand All @@ -50,8 +50,10 @@ def tos(self):
return dict(page="tos")

@expose()
def files(self, filename):
def files(self, filename, redirect=False):
file = file_redirector.get_file(filename)
if redirect:
tg.redirect(file.url)
tg.response.content_type = "application/octet-stream"
return file.file.read()

Expand All @@ -61,15 +63,15 @@ def login(self):
tg.request.environ["repoze.who.challenge"] = "mpapi"
who_api = get_api(tg.request.environ)
return who_api.challenge()
redirect(url("/"))
tg.redirect(url("/"))

@expose()
def glogin(self):
if not tg.request.identity:
tg.request.environ["repoze.who.challenge"] = "glogin"
who_api = get_api(tg.request.environ)
return who_api.challenge()
redirect(url("/"))
tg.redirect(url("/"))

@expose()
@require(predicates.not_anonymous())
Expand All @@ -86,11 +88,11 @@ def post_login(self, came_from=lurl("/")):
u = tg.request.relative_url(str(came_from), to_application=True)
if not u.startswith(tg.request.application_url):
flash("Dangerous redirect prevented", "warning")
redirect("/")
redirect(u)
tg.redirect("/")
tg.redirect(u)
else:
flash("Login failure", "error")
redirect("/")
tg.redirect("/")

@expose()
@require(predicates.has_permission("admin"))
Expand Down

0 comments on commit cf0c57e

Please sign in to comment.