Skip to content

Commit bbab784

Browse files
committed
Refactor url routing
Previously, we had two separately named path for workspace and requests views. This gives them the same name, and provides a default value for path, as per recommend way in django docs. This allows us to consolidate our url handling to just one reverse call, that defaults to the root path if none is specified.
1 parent 367aabb commit bbab784

9 files changed

Lines changed: 22 additions & 33 deletions

File tree

airlock/api.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from airlock.users import User
1616

1717

18-
ROOT_PATH = UrlPath()
18+
ROOT_PATH = UrlPath() # empty path
1919

2020

2121
class Status(Enum):
@@ -49,10 +49,7 @@ def root(self) -> Path:
4949
def get_id(self) -> str:
5050
"""Get the human name for this container."""
5151

52-
def get_absolute_url(self) -> str:
53-
"""Get the url for the container object"""
54-
55-
def get_url_for_path(self, path: UrlPath) -> str:
52+
def get_url(self, path: UrlPath = ROOT_PATH) -> str:
5653
"""Get the url for the container object with path"""
5754

5855

@@ -79,10 +76,7 @@ def root(self):
7976
def get_id(self):
8077
return self.name
8178

82-
def get_absolute_url(self):
83-
return reverse("workspace_home", kwargs={"workspace_name": self.name})
84-
85-
def get_url_for_path(self, relpath):
79+
def get_url(self, relpath=ROOT_PATH):
8680
return reverse(
8781
"workspace_view",
8882
kwargs={"workspace_name": self.name, "path": relpath},
@@ -156,15 +150,7 @@ def root(self):
156150
def get_id(self):
157151
return self.id
158152

159-
def get_absolute_url(self):
160-
return reverse(
161-
"request_home",
162-
kwargs={
163-
"request_id": self.id,
164-
},
165-
)
166-
167-
def get_url_for_path(self, relpath):
153+
def get_url(self, relpath=ROOT_PATH):
168154
return reverse(
169155
"request_view",
170156
kwargs={

airlock/file_browser_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ def is_directory(self):
3838
def name(self):
3939
if self.relpath == ROOT_PATH:
4040
return self.container.get_id()
41+
4142
return self.relpath.name
4243

4344
def url(self):
4445
suffix = "/" if self.is_directory() else ""
45-
return self.container.get_url_for_path(f"{self.relpath}{suffix}")
46+
return self.container.get_url(f"{self.relpath}{suffix}")
4647

4748
def parent(self):
4849
if self.relpath.parents:

airlock/templates/file_browser/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
</form>
7272
{% endif %}
7373
{% endif %}
74-
{% #button type="link" href=workspace.get_absolute_url variant="success" %}Workspace Home{% /button %}
74+
{% #button type="link" href=workspace.get_url variant="success" %}Workspace Home{% /button %}
7575
{% endif %}
7676
</div>
7777
{% endfragment %}

airlock/templates/requests.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{% #card title="Requests by "|add:request.user.username %}
66
{% #list_group %}
77
{% for request in authored_requests %}
8-
{% #list_group_item href=request.get_absolute_url %}{{ request.workspace }}: {{ request.status.name }}{% /list_group_item %}
8+
{% #list_group_item href=request.get_url %}{{ request.workspace }}: {{ request.status.name }}{% /list_group_item %}
99
{% endfor %}
1010
{% /list_group %}
1111
{% /card %}
@@ -14,7 +14,7 @@
1414
{% #card title="Outstanding requests awaiting review" %}
1515
{% #list_group %}
1616
{% for request in outstanding_requests %}
17-
{% #list_group_item href=request.get_absolute_url %}{{ request.workspace }} by {{ request.author }}{% /list_group_item %}
17+
{% #list_group_item href=request.get_url %}{{ request.workspace }} by {{ request.author }}{% /list_group_item %}
1818
{% endfor %}
1919
{% /list_group %}
2020
{% /card %}

airlock/templates/workspaces.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{% #card title="Workspaces for "|add:request.user.username %}
55
{% #list_group %}
66
{% for workspace in workspaces %}
7-
{% #list_group_item href=workspace.get_absolute_url %}{{ workspace.name }}{% /list_group_item %}
7+
{% #list_group_item href=workspace.get_url %}{{ workspace.name }}{% /list_group_item %}
88
{% endfor %}
99
{% /list_group %}
1010
{% /card %}

airlock/urls.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
path(
3535
"workspaces/view/<str:workspace_name>/",
3636
airlock.views.workspace_view,
37-
name="workspace_home",
37+
kwargs={"path": ""},
38+
name="workspace_view",
3839
),
3940
path(
4041
"workspaces/view/<str:workspace_name>/<path:path>",
@@ -55,7 +56,8 @@
5556
path(
5657
"requests/view/<str:request_id>/",
5758
airlock.views.request_view,
58-
name="request_home",
59+
name="request_view",
60+
kwargs={"path": ""},
5961
),
6062
path(
6163
"requests/view/<str:request_id>/<path:path>",

airlock/views.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def workspace_add_file_to_request(request, workspace_name):
177177
if not form.is_valid():
178178
for error in form.errors.values():
179179
messages.error(request, error)
180-
return redirect(workspace.get_url_for_path(relpath))
180+
return redirect(workspace.get_url(relpath))
181181

182182
group_name = request.POST.get("new_filegroup") or request.POST.get("filegroup")
183183
try:
@@ -191,7 +191,7 @@ def workspace_add_file_to_request(request, workspace_name):
191191
request, f"File has been added to request (file group '{group_name}')"
192192
)
193193
# redirect to this just added file
194-
return redirect(release_request.get_url_for_path(relpath))
194+
return redirect(release_request.get_url(relpath))
195195

196196

197197
def request_index(request):
@@ -273,7 +273,7 @@ def request_submit(request, request_id):
273273
raise PermissionDenied(str(exc))
274274

275275
messages.success(request, "Request has been submitted")
276-
return redirect(release_request.get_absolute_url())
276+
return redirect(release_request.get_url())
277277

278278

279279
@require_http_methods(["POST"])
@@ -286,7 +286,7 @@ def request_reject(request, request_id):
286286
raise PermissionDenied(str(exc))
287287

288288
messages.error(request, "Request has been rejected")
289-
return redirect(release_request.get_absolute_url())
289+
return redirect(release_request.get_url())
290290

291291

292292
@require_http_methods(["POST"])
@@ -315,4 +315,4 @@ def request_release_files(request, request_id):
315315
raise
316316

317317
messages.success(request, "Files have been released to jobs.opensafely.org")
318-
return redirect(release_request.get_absolute_url())
318+
return redirect(release_request.get_url())

tests/integration/test_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def test_workspace_request_file_invalid_new_filegroup(client_with_user, api):
260260

261261
assert not filegroupmetadata.request_files.exists()
262262
# redirects to the workspace file again, with error messages
263-
assert response.request["PATH_INFO"] == workspace.get_url_for_path("test/path.txt")
263+
assert response.request["PATH_INFO"] == workspace.get_url("test/path.txt")
264264

265265
all_messages = [msg for msg in response.context["messages"]]
266266
assert len(all_messages) == 1

tests/unit/test_file_browser_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def root(self):
1717
def get_id(self):
1818
return "DummyContainer"
1919

20-
def get_url_for_path(self, relpath):
20+
def get_url(self, relpath):
2121
return f"/test/{relpath}"
2222

2323

@@ -156,7 +156,7 @@ def test_from_relative_path_rejects_path_escape(container, path):
156156

157157
def test_breadcrumbs(container):
158158
path = PathItem(container, "foo/bar/baz")
159-
assert [b.name() for b in path.breadcrumbs()] == [
159+
assert [c.name() for c in path.breadcrumbs()] == [
160160
"DummyContainer",
161161
"foo",
162162
"bar",

0 commit comments

Comments
 (0)