Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion websites/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,11 @@ class WebsiteContentDetailSerializer(
def update(self, instance, validated_data):
"""Update WebsiteContent, handling filename and metadata."""
title = validated_data.get("title", instance.title)
if instance.type == CONTENT_TYPE_PAGE and title:
if (
instance.type == CONTENT_TYPE_PAGE
and title
and instance.filename != "_index"
):
Comment on lines +574 to +578
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The filename index is also a special Hugo filename (used for leaf bundles) and is explicitly listed in CONTENT_FILENAMES_FORBIDDEN. Similar to _index, it should likely be protected from automatic renaming when the title is updated to avoid breaking the page bundle structure.

Suggested change
if (
instance.type == CONTENT_TYPE_PAGE
and title
and instance.filename != "_index"
):
if (
instance.type == CONTENT_TYPE_PAGE
and title
and instance.filename not in ("index", "_index")
):

new_filename = slugify(title)
if instance.filename != new_filename:
cur_filename = WebsiteContent.objects.filter(
Expand Down
33 changes: 33 additions & 0 deletions websites/serializers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,39 @@ def test_update_page_url_on_title_change_parametrized( # noqa: PLR0913
assert page.filename == expected_filename


@pytest.mark.parametrize(
("initial_filename", "expected_filename"),
[
("_index", "_index"),
("original-title", "new-title"),
],
)
def test_update_page_url_on_title_change_legacy_index_behavior(
enable_websitecontent_signal,
initial_filename,
expected_filename,
):
"""Legacy _index pages skip page url updates; normal pages still proceed with them."""
website = WebsiteFactory.create(owner=UserFactory.create())
page = WebsiteContentFactory.create(
website=website,
type=CONTENT_TYPE_PAGE,
dirpath="",
title="Original Title",
filename=initial_filename,
)

serializer = WebsiteContentDetailSerializer(
instance=page, data={"title": "New Title"}, partial=True
)
assert serializer.is_valid(), serializer.errors
serializer.save()

page.refresh_from_db()
assert page.filename == expected_filename
assert page.title == "New Title"


def test_website_content_detail_serializer_syncs_video_relation_files(
mocker, mocked_website_funcs
):
Expand Down
Loading