Skip to content

Commit

Permalink
PB-302: Add icon size to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJoss committed Aug 28, 2024
1 parent e74422c commit bd363b5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/icon.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from PIL import Image

from flask import url_for

from app.helpers.icons import get_icon_template_url
Expand Down Expand Up @@ -82,6 +84,16 @@ def get_icon_filepath(self):
name_with_extension = f"{name_with_extension}.png"
return os.path.abspath(os.path.join(IMAGE_FOLDER, self.icon_set.name, name_with_extension))

def get_size(self):
"""
Lazily open image of icon to get the size of the icon from the metadata.
Returns:
A list with the size of the specified icon [x,y]
"""
with Image.open(self.get_icon_filepath()) as img:
return img.size

def serialize(self):
"""
As we want to add "url" to the __dict__, we can't really use a json.dumps to generate our
Expand All @@ -97,5 +109,6 @@ def serialize(self):
"anchor": self.anchor,
"icon_set": self.icon_set.name,
"url": self.get_icon_url(),
"template_url": get_icon_template_url(get_base_url())
"template_url": get_icon_template_url(get_base_url()),
"size": self.get_size()
}
13 changes: 13 additions & 0 deletions tests/unit_tests/test_all_icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ def test_all_icon_metadata_endpoint(self):
)
self.assertTrue(fraction > 0, msg='"anchor" fraction should be > 0')

self.assertIn('size', json_response)
self.assertIsInstance(
json_response['size'],
list,
msg='"size" should be a list with x and y dimension'
)
self.assertEqual(
len(json_response['size']), 2, msg='"size" should have two items; x and y'
)
for elem in json_response['size']:
self.assertIsInstance(elem, (int), msg='"size" should be int')
self.assertTrue(elem > 0, msg='"size" should be > 0')

def test_all_icon_basic_image(self):
"""
Checking URLs without scale or color
Expand Down

0 comments on commit bd363b5

Please sign in to comment.