Skip to content

Commit

Permalink
Merge pull request #76 from geoadmin/feat-PB-227_helptext_translations
Browse files Browse the repository at this point in the history
PB-227: Helptext translations
  • Loading branch information
LukasJoss authored Aug 30, 2024
2 parents e74422c + e1dfd45 commit 32d84df
Show file tree
Hide file tree
Showing 13 changed files with 1,981 additions and 2 deletions.
17 changes: 17 additions & 0 deletions app/helpers/check_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from flask import abort

from app.helpers.description import find_descripton_file
from app.icon_set import get_icon_set

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -88,3 +89,19 @@ def get_and_check_icon(icon_set, icon_name):
logger.error("The icon doesn't exist: %s", path)
abort(404, "Icon not found in icon set")
return icon


def check_if_descripton_file_exists(icon_set):
"""
Checks that the icon set has a corresponding dictionary containing description for all available
languages.
if not raises a flask error and abort the current request.
Args:
icon_set: (IconSet) the icon set of which we want to check if it has a description file
"""
# checking that the icon exists in the icon set's folder
path = find_descripton_file(icon_set)
if not os.path.isfile(path):
logger.error("The description dictionary doesn't exist: %s", path)
abort(404, "Description dictionary not found")
55 changes: 55 additions & 0 deletions app/helpers/description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import json
import logging
import os

from flask import abort

from app.settings import DESCRIPTION_FOLDER

logger = logging.getLogger(__name__)


def get_icon_set_description(icon_set=''):
'''
Return json containing the description in all available languages for all icons of the
provided icon set
'''
path = find_descripton_file(icon_set)
if not os.path.isfile(path):
return None

with open(path, encoding='utf-8') as f:
icon_set_descriptions = json.load(f)

return icon_set_descriptions


def get_icon_description(icon_name='', icon_set=''):
'''
Return json containing the description in all available languages for an icon in the specified
icon set
'''
path = find_descripton_file(icon_set)
if not os.path.isfile(path):
return None

with open(path, encoding='utf-8') as f:
df = json.load(f)

try:
icon_description = df[icon_name]
except KeyError:
logger.error("Description for icon not found: %s", icon_name)
abort(404, "Description for icon not found")

return icon_description


def find_descripton_file(icon_set):
'''
Return file path of description file if it exists or False in case it doesn't
'''
path = os.path.abspath(os.path.join(DESCRIPTION_FOLDER, icon_set)) + '-dictionary.json'
if os.path.isfile(path):
return path
return False
4 changes: 3 additions & 1 deletion app/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from flask import url_for

from app.helpers.description import get_icon_description
from app.helpers.icons import get_icon_template_url
from app.helpers.url import get_base_url
from app.settings import DEFAULT_COLOR
Expand Down Expand Up @@ -97,5 +98,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()),
"description": get_icon_description(self.name, self.icon_set.name)
}
31 changes: 30 additions & 1 deletion app/icon_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from flask import url_for

from app.helpers.description import find_descripton_file
from app.helpers.description import get_icon_set_description
from app.helpers.icons import get_icon_set_template_url
from app.helpers.url import get_base_url
from app.icon import Icon
Expand Down Expand Up @@ -76,6 +78,19 @@ def get_icons_url(self):
"""
return url_for('icons_from_icon_set', icon_set_name=self.name, _external=True)

def get_icon_set_description_url(self):
"""
Generate and return the URL that will list the description in all available lanaguages
of all available icons of this icon set.
Returns:
the URL to the description in all available languages of all icons in this icon set if
it exists, otherwise return None
"""
if find_descripton_file(self.name):
return url_for('description_from_icon_set', icon_set_name=self.name, _external=True)
return None

def get_icon(self, icon_name):
"""
Generate and return the URL to access the metadata of one specific icon of this icon set
Expand Down Expand Up @@ -112,6 +127,19 @@ def get_all_icons(self):
icons.append(self.get_icon(name_without_extension))
return icons

def get_description(self):
"""
Generate a dictionary containing the description in all available languages of all icons
belonging to this icon set.
Returns:
A dictionary of all icon description in all available languages from this icon set it
it exists, otherwise return None
"""
if not self.is_valid():
return None
return get_icon_set_description(self.name)

def serialize(self):
"""
As we want to add "icons_url" to the __dict__, we can't really use a json.dumps to generate
Expand All @@ -126,5 +154,6 @@ def serialize(self):
"name": self.name,
"colorable": self.colorable,
"icons_url": self.get_icons_url(),
"template_url": get_icon_set_template_url(get_base_url())
"template_url": get_icon_set_template_url(get_base_url()),
"description_url": self.get_icon_set_description_url()
}
8 changes: 8 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from app import app
from app.helpers.check_functions import check_color_channels
from app.helpers.check_functions import check_if_descripton_file_exists
from app.helpers.check_functions import check_scale
from app.helpers.check_functions import get_and_check_icon
from app.helpers.check_functions import get_and_check_icon_set
Expand Down Expand Up @@ -58,6 +59,13 @@ def icons_from_icon_set(icon_set_name):
return make_api_compliant_response(icon_set.get_all_icons())


@app.route('/sets/<string:icon_set_name>/description', methods=['GET'])
def description_from_icon_set(icon_set_name):
icon_set = get_and_check_icon_set(icon_set_name)
check_if_descripton_file_exists(icon_set_name)
return make_api_compliant_response(icon_set.get_description())


@app.route('/sets/<string:icon_set_name>/icons/<string:icon_name>', methods=['GET'])
def icon_metadata(icon_set_name, icon_name):
icon_set = get_and_check_icon_set(icon_set_name)
Expand Down
3 changes: 3 additions & 0 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
load_dotenv(ENV_FILE, override=True, verbose=True)

IMAGE_FOLDER = os.path.abspath(os.path.join(os.path.dirname(__file__), '../static/images/'))
DESCRIPTION_FOLDER = os.path.abspath(
os.path.join(os.path.dirname(__file__), '../metadata/description/')
)

COLORABLE_ICON_SETS = ['default']
DEFAULT_COLOR = {"r": '255', "g": '0', "b": '0'}
Expand Down
Loading

0 comments on commit 32d84df

Please sign in to comment.