From 74e931705389636db0f8dd0266b7e2645c772e9c Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Tue, 16 Nov 2021 20:05:42 -0700 Subject: [PATCH] Override __str__ for endpoint f-strings By default, f-strings use __str__ not __repr__, unless supplied with a "!r" at the end of the str. Rather than going through and making the strings work for the overridden __repr__ for endpoints, it's easier just to convert the method over to __str__. Also cleaned up some broken routes and formatting. --- app/models/endpoint.py | 5 +++-- app/routes.py | 4 ++-- test/test_autocomplete.py | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/models/endpoint.py b/app/models/endpoint.py index 2ad5318..eeddc64 100644 --- a/app/models/endpoint.py +++ b/app/models/endpoint.py @@ -15,8 +15,9 @@ class Endpoint(Enum): element = 'element' window = 'window' - def __repr__(self): + def __str__(self): return self.value def in_path(self, path: str) -> bool: - return path.startswith(self.value) + return path.startswith(self.value) or \ + path.startswith(f'/{self.value}') diff --git a/app/routes.py b/app/routes.py index 5d6fcc9..82db780 100644 --- a/app/routes.py +++ b/app/routes.py @@ -176,11 +176,11 @@ def home(): return redirect(url_for('.index')) -@app.route(f'{Endpoint.session}/', methods=['GET', 'PUT', 'POST']) +@app.route(f'/{Endpoint.session}/', methods=['GET', 'PUT', 'POST']) def session_check(session_id): if 'uuid' in session and session['uuid'] == session_id: session['valid'] = True - return redirect(request.args.get('follow')) + return redirect(request.args.get('follow'), code=307) else: follow_url = request.args.get('follow') req = PreparedRequest() diff --git a/test/test_autocomplete.py b/test/test_autocomplete.py index 74eb7ef..194a5ab 100644 --- a/test/test_autocomplete.py +++ b/test/test_autocomplete.py @@ -9,7 +9,8 @@ def test_autocomplete_get(client): def test_autocomplete_post(client): - rv = client.post(f'/{Endpoint.autocomplete}', data=dict(q='the+cat+in+the')) + rv = client.post(f'/{Endpoint.autocomplete}', + data=dict(q='the+cat+in+the')) assert rv._status_code == 200 assert len(rv.data) >= 1 assert b'the cat in the hat' in rv.data