Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a list of allowed fonts and corresponding style validator #698

Merged
merged 2 commits into from
Jul 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions qgreenland/models/config/layer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path
from typing import Any, Optional, Union
from xml.etree import ElementTree

from pydantic import Field, validator

Expand Down Expand Up @@ -57,6 +58,24 @@ class Layer(QgrBaseModel):

_validate_description = reusable_validator("description", validate_paragraph_text)

@validator("style")
@classmethod
def style_file_only_contains_allowed_fonts(cls, value):
allowed_fonts = ["Open Sans"]
if value:
style_filepath = _style_filepath(value)
tree = ElementTree.parse(style_filepath)
for elem in tree.getroot().iter():
if font_family := elem.attrib.get("fontFamily", False):
if font_family not in allowed_fonts:
raise exc.QgrInvalidConfigError(
f"Style {style_filepath} contains disallowed font:"
f" '{font_family}'."
f" Only the following fonts are allowed: {allowed_fonts}."
)

return value

@validator("style")
@classmethod
def style_file_exists(cls, value):
Expand Down