Skip to content

Commit

Permalink
Refactor supporting file info onto PathItem
Browse files Browse the repository at this point in the history
Give a PathItem knowledge of whether it is a supporting
file or not.
  • Loading branch information
rebkwok committed Mar 18, 2024
1 parent 8f42240 commit 5039e21
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 41 deletions.
20 changes: 4 additions & 16 deletions airlock/business_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,10 @@ def all_files_set(self):
}

def is_supporting_file(self, relpath):
return self.get_request_file(relpath).filetype == RequestFileType.SUPPORTING

def output_files_with_group(self):
return [
UrlPath(filegroup.name, request_file.relpath)
for filegroup in self.filegroups.values()
for request_file in filegroup.output_files
]

def supporting_files_with_group(self):
"""Return the relpaths for all files on the request, of any filetype"""
return [
UrlPath(filegroup.name, request_file.relpath)
for filegroup in self.filegroups.values()
for request_file in filegroup.supporting_files
]
try:
return self.get_request_file(relpath).filetype == RequestFileType.SUPPORTING
except BusinessLogicLayer.FileNotFound:
return False

def set_filegroups_from_dict(self, attrs):
self.filegroups = self._filegroups_from_dict(attrs)
Expand Down
23 changes: 6 additions & 17 deletions airlock/file_browser_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class PathNotFound(Exception):
# should this node be expanded in the tree?
expanded: bool = False

# Is this a supporting file?
supporting_file: bool = False

# what to display for this node when rendering the tree. Defaults to name,
# but this allow it to be overridden.
display_text: str = None
Expand Down Expand Up @@ -139,7 +142,8 @@ def html_classes(self):
if self.selected:
classes.append("selected")

classes.extend(self.extra_html_classes)
if self.supporting_file:
classes.append("supporting")

return " ".join(classes)

Expand Down Expand Up @@ -259,17 +263,6 @@ def get_request_tree(release_request, selected_path=ROOT_PATH, selected_only=Fal
expanded=True,
)

relpath_to_html_class = {
**{
path_with_group: ["output"]
for path_with_group in release_request.output_files_with_group()
},
**{
path_with_group: ["supporting"]
for path_with_group in release_request.supporting_files_with_group()
},
}

def _pluralise(input_list):
if len(input_list) != 1:
return "s"
Expand Down Expand Up @@ -313,7 +306,6 @@ def _pluralise(input_list):
parent=group_node,
selected_path=selected_path,
expanded=expanded,
html_classes_dict=relpath_to_html_class,
)
root_node.children.append(group_node)

Expand All @@ -335,12 +327,9 @@ def get_path_tree(
parent,
selected_path=ROOT_PATH,
expanded=False,
html_classes_dict=None,
):
"""Walk a flat list of paths and create a tree from them."""

html_classes_dict = html_classes_dict or {}

def build_path_tree(path_parts, parent):
# group multiple paths into groups by first part of path
grouped = dict()
Expand All @@ -362,7 +351,7 @@ def build_path_tree(path_parts, parent):
relpath=path,
parent=parent,
selected=selected,
extra_html_classes=html_classes_dict.get(path, []),
supporting_file=container.is_supporting_file(path),
)

# If it has decendants, it is a directory. However, an empty
Expand Down
2 changes: 1 addition & 1 deletion airlock/templates/file_browser/contents.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{% else %}
{% fragment as add_button %}
<div class="flex items-center gap-2">
{% if is_supporting_file %}
{% if path_item.supporting_file %}
{% #description_item title="Supporting file" %}This is a supporting file and will not be releaed.{% /description_item%}
{% endif %}
{% if context == "workspace" %}
Expand Down
2 changes: 0 additions & 2 deletions airlock/views/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ def request_view(request, request_id: str, path: str = ""):
"release_request": release_request,
"root": tree,
"path_item": path_item,
"is_supporting_file": path_item.relpath
in release_request.supporting_files_with_group(),
"context": "request",
"title": f"Request for {release_request.workspace} by {release_request.author}",
# TODO file these in from user/models
Expand Down
11 changes: 6 additions & 5 deletions tests/functional/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_e2e_release_files(page, live_server, dev_users, release_files_stubber):
)
factories.write_workspace_file(
workspace,
"subdir/supporting_file.txt",
"subdir/supporting.txt",
"I am the supporting file content",
)

Expand Down Expand Up @@ -182,7 +182,9 @@ def test_e2e_release_files(page, live_server, dev_users, release_files_stubber):
expect(filegroup_link).to_contain_text(re.compile("my-new-group", flags=re.I))

# In the initial request view, the tree is collapsed
file_link = page.get_by_role("link").locator(".output.file:scope")
# Locate the link by its name, because later when we're looking at
# the request, there will be 2 files that match .locator(".file:scope")
file_link = page.get_by_role("link", name="file.txt").locator(".file:scope")
assert file_link.all() == []

# Click to open the filegroup tree
Expand All @@ -198,15 +200,14 @@ def test_e2e_release_files(page, live_server, dev_users, release_files_stubber):
expect(page.locator("iframe")).to_have_attribute(
"src", release_request.get_contents_url("my-new-group/subdir/file.txt")
)
assert "output" in file_link.get_attribute("class")

# Go back to the Workspace view so we can add a supporting file
find_and_click(page.locator("#workspace-home-button"))
expect(page).to_have_url(live_server.url + "/workspaces/view/test-workspace/")

# Expand the tree and click on the supporting file
find_and_click(page.get_by_role("link", name="subdir").first)
find_and_click(page.get_by_role("link", name="supporting_file.txt").first)
find_and_click(page.get_by_role("link", name="supporting.txt").first)

# Add supporting file to request, choosing the group we created previously
# Find the add file button and click on it to open the modal
Expand Down Expand Up @@ -249,7 +250,7 @@ def test_e2e_release_files(page, live_server, dev_users, release_files_stubber):
assert not tree_element_is_selected(page, file_link)
expect(page.locator("iframe")).to_have_attribute(
"src",
release_request.get_contents_url("my-new-group/subdir/supporting_file.txt"),
release_request.get_contents_url("my-new-group/subdir/supporting.txt"),
)

# Click back to the output file link and ensure the selected classes are correctly applied
Expand Down

0 comments on commit 5039e21

Please sign in to comment.