Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pydantic import BaseModel

from ai_document_plugin_service.ai.common.types import TemplateSection
from ai_document_plugin_service.ai.common.types import TemplateContent, TemplateSection


class CoverPageLabel(BaseModel):
Expand Down Expand Up @@ -55,3 +55,14 @@ class CoverPageDefinition(BaseModel):

def cover_page_assignment_field_labels(definition: CoverPageDefinition) -> dict[str, str]:
return {field.id: field.label for section in definition.sections for field in section.fields}


def stored_cover_section_count(content: TemplateContent) -> int:
"""Count the stored cover prefix, independently of the current YAML definition."""
count = 0
for section in content.sections:
# CoverPageSection metadata survives persistence through TemplateSection extras.
if not all(key in section.model_dump() for key in ('id', 'preview', 'fields')):
break
count += 1
return count
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
TemplateScope,
TemplateUpdateRequest,
)
from ai_document_plugin_service.cover_page.cover_page_definition import CoverPageDefinition
from ai_document_plugin_service.cover_page.cover_page_definition import CoverPageDefinition, stored_cover_section_count
from ai_document_plugin_service.service.errors import (
AccessDeniedError,
ConflictError,
Expand Down Expand Up @@ -140,10 +140,11 @@ def _to_list_item(record: TemplateRecord) -> TemplateListItem:
has_cover_page=record.has_cover_page,
)

def _to_detail(self, record: TemplateRecord) -> TemplateDetail:
@staticmethod
def _to_detail(record: TemplateRecord) -> TemplateDetail:
content = record.content
if record.has_cover_page:
content = content.model_copy(update={'sections': content.sections[len(self._cover_page_sections) :]})
content = content.model_copy(update={'sections': content.sections[stored_cover_section_count(content) :]})

return TemplateDetail(
uuid=record.uuid,
Expand Down
57 changes: 57 additions & 0 deletions service/tests/service/test_template_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,60 @@ async def test_cover_sections_are_stored_but_hidden_when_editing_and_not_duplica
cover_page_version=saved['cover_page_version'],
)
assert content['sections'] == [{'title': 'Projects', 'content': 'Custom body'}]


@pytest.mark.parametrize(('stored_count', 'current_count'), [(2, 3), (3, 2), (0, 3), (3, 0)])
async def test_changed_cover_section_count_preserves_body_on_save(stored_count, current_count):
database = _database()
body = [
{'title': 'Cover 0', 'content': 'User-defined body with the same title as a cover section'},
{'title': 'Second body section', 'content': 'Keep this too'},
]
covers = [
COVER_DEFINITION.sections[0].model_copy(update={'id': f'cover_{i}', 'title': f'Cover {i}'})
for i in range(max(stored_count, current_count))
]
record = TemplateRecord(
uuid=uuid.uuid4(),
title='Existing',
tenant_uuid=TENANT_UUID,
user_uuid=USER_UUID,
cover_page_version='1',
content=TemplateContent.model_validate(
{
'sections': [
*[section.model_dump() for section in covers[:stored_count]],
*body,
]
}
),
)
database.get_template.return_value = record
definition = COVER_DEFINITION.model_copy(update={'sections': covers[:current_count]})
service = TemplateService(database, definition)
detail = await service.get(_user(), record.uuid)
assert detail.content.model_dump(exclude_unset=True)['sections'] == body
await service.update(
_user(),
record.uuid,
TemplateUpdateRequest(
title=detail.title,
content=detail.content,
has_cover_page=True,
),
)
saved = database.update_template.await_args.kwargs['content']
assert len(saved.sections) == current_count + len(body)
assert [section.model_dump(exclude_unset=True) for section in saved.sections[current_count:]] == body
database.get_template.return_value = TemplateRecord(
uuid=record.uuid,
title=record.title,
tenant_uuid=TENANT_UUID,
user_uuid=USER_UUID,
cover_page_version=definition.version,
content=TemplateContent.model_validate(saved.model_dump()),
)
reopened = await service.get(_user(), record.uuid)
assert [(section.title, section.content) for section in reopened.content.sections] == [
(section['title'], section['content']) for section in body
]