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
4 changes: 1 addition & 3 deletions drf_lint_baseline.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[
"content_sync/serializers.py:133:21:ORM001",
"content_sync/serializers.py:54:21:ORM001",
"websites/serializers.py:257:23:ORM001",
"websites/serializers.py:683:16:ORM001",
"websites/serializers.py:700:20:ORM002"
"websites/serializers.py:257:23:ORM001"
]
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ ignore = [
"RUF012",
"UP007"
]
external = ["ORM001", "ORM002"]

[tool.ruff.lint.pydocstyle]
convention = "pep257"
Expand Down
10 changes: 7 additions & 3 deletions 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 not in constants.CONTENT_FILENAMES_FORBIDDEN
):
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 Expand Up @@ -680,7 +684,7 @@ def get_content_context( # noqa: C901, PLR0912
contents = []
for website_id, text_ids in lookup.items():
contents.extend(
WebsiteContent.objects.filter(
WebsiteContent.objects.filter( # noqa: ORM001
(Q(website__url_path=website_id) | Q(website__name=website_id)),
text_id__in=text_ids,
)
Expand All @@ -697,7 +701,7 @@ def to_representation(self, instance):
if file_field:
result[file_field["name"]] = instance.file.url

drivefile = instance.drivefile_set.first()
drivefile = instance.drivefile_set.first() # noqa: ORM002
if drivefile:
result["gdrive_url"] = DRIVE_FILE_VIEW_URL.format(file_id=drivefile.file_id)

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