Skip to content

Commit

Permalink
Support multiple placeholder url tokens (#43)
Browse files Browse the repository at this point in the history
* Support multiple placeholder url tokens

* Update dockerfile

* update requirements
  • Loading branch information
stlava authored Nov 10, 2023
1 parent 364cc3e commit 7a03cf4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.6.5-stretch
FROM python:3.7-bullseye

# Env var to force update of the image. Increment for each time this is needed
ENV CACHE_BUSTER_VAR=1
Expand All @@ -17,6 +17,7 @@ RUN mkdir -p /app/ssl && cd /app/ssl && \

# Set up gogo.
ADD resources/requirements.txt /app/resources/requirements.txt
RUN pip install setuptools==45
RUN pip install -r /app/resources/requirements.txt && pip freeze

ADD resources /app/resources/
Expand Down
2 changes: 1 addition & 1 deletion resources/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ s3transfer==0.1.11
six==1.10.0
SQLAlchemy==1.1.14
uritemplate==3.0.0
urllib3==1.26.5
urllib3==1.24.3
Werkzeug==0.15.3
20 changes: 17 additions & 3 deletions src/gogo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
HTTPS_REDIRECT_URL = os.getenv("HTTPS_REDIRECT_URL", "https://localhost")


def _replace_placeholders(input_string, token_string):
placeholder_count = input_string.count("%s")
token_count = token_string.count("/") + 1
if placeholder_count > token_count:
return None

tokens = token_string.split("/", placeholder_count-1)
replaced_string = input_string.replace("%s", "{}").format(*tokens)
return replaced_string


class DashboardView(BaseListView):
template = "dashboard.html"

Expand Down Expand Up @@ -159,15 +170,18 @@ def get(self, name):
and shortcut.secondary_url
and "%s" in shortcut.secondary_url
):
formatted_url = _replace_placeholders(str(shortcut.secondary_url), secondary_arg)
if not formatted_url:
flask.abort(400)

response = flask.make_response(
flask.redirect(
str(shortcut.secondary_url).replace("%s", secondary_arg)
)
flask.redirect(formatted_url)
)
else:
response = flask.make_response(
flask.redirect(str(shortcut.url), code=301)
)

response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
return response

Expand Down

0 comments on commit 7a03cf4

Please sign in to comment.