Skip to content

Commit ea905c5

Browse files
committed
feat(types): add safe_repo_tags property to DockerImage
1 parent ffd9c49 commit ea905c5

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

app/contrib/tests/test_kvstore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_model():
1313
return DockerImage(
1414
Id='test_id',
1515
Created=datetime.datetime.now(),
16-
RepoTags=['test_repo:tag'],
16+
RepoTags=['test_repo:tag', 'test_repo:latest'],
1717
SharedSize=0,
1818
Size=100,
1919
containers=['test_container'],

app/contrib/tests/test_types.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,39 @@ def test_docker_image():
5353
assert x.created == datetime(2023, 1, 1, 12, 0, tzinfo=timezone.utc)
5454
assert x.parent_id == 'sha256:987654321fedcba'
5555
assert x.repo_tags == ['image:latest', 'image:v1']
56+
assert x.safe_repo_tags == x.repo_tags
5657
assert x.shared_size == 1000
5758
assert x.size == 5000
5859
assert x.short_id == x.id[7:19]
5960
assert isinstance(x.created_delta, str)
6061
assert x.containers == []
6162

63+
# repo_tags = None
64+
x = DockerImage.model_validate({
65+
'Id': 'sha256:123456789abcdef',
66+
'Created': '2023-01-01T12:00:00Z',
67+
'ParentId': 'sha256:987654321fedcba',
68+
'RepoTags': None,
69+
'SharedSize': 1000,
70+
'Size': 5000,
71+
})
72+
73+
assert x.repo_tags is None
74+
assert x.safe_repo_tags == ['<none>:<none>']
75+
76+
# repo_tags = []
77+
x = DockerImage.model_validate({
78+
'Id': 'sha256:123456789abcdef',
79+
'Created': '2023-01-01T12:00:00Z',
80+
'ParentId': 'sha256:987654321fedcba',
81+
'RepoTags': [],
82+
'SharedSize': 1000,
83+
'Size': 5000,
84+
})
85+
86+
assert x.repo_tags == []
87+
assert x.safe_repo_tags == ['<none>:<none>']
88+
6289

6390
def test_docker_image_list():
6491
images = [

app/contrib/types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ def short_id(self) -> str:
3434
def created_delta(self) -> str:
3535
return naturaltime(self.created)
3636

37+
@property
38+
def safe_repo_tags(self) -> list[str]:
39+
if not self.repo_tags: # empty list or None
40+
return ['<none>:<none>']
41+
return self.repo_tags
42+
3743

3844
class DockerImageList(RootModel):
3945
root: list[DockerImage]

app/templates/pages/images.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
<tr>
2020
<td class="uk-text-nowrap mono-font">{{ item.short_id }}</td>
2121
<td class="uk-text-nowrap">
22-
{% if item.repo_tags %}
23-
{{ item.repo_tags|join('<br>')|safe }}
24-
{% else %}
25-
<span class="uk-text-muted">
26-
&lt;none&gt;:&lt;none&gt;
27-
</span>
28-
{% endif %}
22+
{% for repo_tag in item.safe_repo_tags %}
23+
{% if repo_tag == '<none>:<none>' %}
24+
<span class="uk-text-muted">{{ repo_tag }}</span><br>
25+
{% else %}
26+
{{ repo_tag }}<br>
27+
{% endif %}
28+
{% endfor %}
2929
</td>
3030
<td class="width-1">{{ item.size }}</td>
3131
<td class="width-1">{{ item.shared_size }}</td>

0 commit comments

Comments
 (0)