Skip to content

Commit

Permalink
Use pydantic models cleaned data for build context
Browse files Browse the repository at this point in the history
  • Loading branch information
neoformit committed Dec 10, 2024
1 parent 9806526 commit efa4973
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/labs/lab_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from bs4 import BeautifulSoup, MarkupResemblesLocatorWarning
from django.conf import settings
from markdown2 import Markdown
from pydantic import ValidationError
from pydantic import BaseModel, ValidationError

from types import SimpleNamespace
from utils.exceptions import LabBuildError
Expand Down Expand Up @@ -75,15 +75,19 @@ def validate(self):

def _validate_sections(self):
"""Validate sections against Pydantic schema."""
validated_sections = []
for section in self['sections']:
try:
LabSectionSchema(**section)
validated_sections.append(
LabSectionSchema(**section).model_dump()
)
except ValidationError as e:
raise LabBuildError(
e,
section_id=section["id"],
source='YAML',
)
self['sections'] = validated_sections

def _get(
self,
Expand Down Expand Up @@ -167,7 +171,7 @@ def _fetch_yaml_context(self):
context = self._fetch_yaml_content(self.content_root, extend=False)

if not self.content_root.endswith('base.yml'):
# Attempt to extend base.yml with self.content_root
# Attempt to extend base.yml with the given content_root
base_content_url = (self.parent_url + 'base.yml')
base_context = self._fetch_yaml_content(
base_content_url, ignore_404=True, extend=False)
Expand All @@ -176,7 +180,7 @@ def _fetch_yaml_context(self):
context = base_context

try:
LabSchema(**context)
context = LabSchema(**context).model_dump()
except ValidationError as exc:
raise LabBuildError(exc, url=self.content_root, source='YAML')

Expand Down Expand Up @@ -216,6 +220,11 @@ def is_excluded_item(item):
for item in data
if not is_excluded_item(item)
]
elif isinstance(data, BaseModel):
data = {
k: filter_excluded_items(v)
for k, v in data.model_dump().items()
}
return data

if self.get('root_domain'):
Expand Down

0 comments on commit efa4973

Please sign in to comment.