Skip to content

Commit 3136bee

Browse files
Copilotjkachel
andauthored
Fix OrganizationPage slug regeneration on name changes (#3002)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jkachel <[email protected]> Co-authored-by: James Kachel <[email protected]>
1 parent 192d3cf commit 3136bee

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

b2b/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,15 @@ class OrganizationPage(Page):
9898
FieldPanel("sso_organization_id"),
9999
]
100100

101-
promote_panels = []
101+
# Use default promote_panels from Page to allow manual slug editing
102102

103103
def save(self, clean=True, user=None, log_action=False, **kwargs): # noqa: FBT002
104104
"""Save the page, and update the slug and title appropriately."""
105105

106106
self.title = str(self.name)
107107

108-
self.slug = slugify(f"org-{self.name}")
108+
if not self.slug:
109+
self.slug = slugify(f"org-{self.name}")
109110
Page.save(self, clean=clean, user=user, log_action=log_action, **kwargs)
110111

111112
def get_learners(self):

b2b/models_test.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
import faker
44
import pytest
55

6-
from b2b.factories import ContractPageFactory
7-
from courses.factories import (
8-
CourseRunFactory,
9-
ProgramFactory,
10-
)
6+
from b2b.factories import ContractPageFactory, OrganizationPageFactory
7+
from courses.factories import CourseRunFactory, ProgramFactory
118

129
pytestmark = [pytest.mark.django_db]
1310
FAKE = faker.Faker()
@@ -53,3 +50,45 @@ def test_add_program_courses_to_contract(mocker):
5350

5451
assert contract.programs.count() == 1
5552
assert contract.get_course_runs().count() == 4
53+
54+
55+
def test_organization_page_slug_preserved_on_name_change():
56+
"""Test that the slug is not regenerated when only the name changes."""
57+
org = OrganizationPageFactory.create(name="MIT")
58+
original_slug = org.slug
59+
60+
# Change the name
61+
org.name = "MIT - Universal AI"
62+
org.save()
63+
org.refresh_from_db()
64+
65+
# The slug should not have changed
66+
assert org.slug == original_slug
67+
# But the title should reflect the new name
68+
assert org.title == "MIT - Universal AI"
69+
70+
71+
def test_organization_page_slug_generated_on_create():
72+
"""Test that the slug is generated when creating a new organization."""
73+
org = OrganizationPageFactory.create(name="Test Organization", slug="")
74+
75+
# The slug should have been generated
76+
assert org.slug == "org-test-organization"
77+
assert org.title == "Test Organization"
78+
79+
80+
def test_organization_page_slug_not_overwritten_if_set():
81+
"""Test that a manually set slug is not overwritten."""
82+
org = OrganizationPageFactory.create(name="Test Org", slug="custom-slug")
83+
84+
# The slug should be the custom one
85+
assert org.slug == "custom-slug"
86+
87+
# Change the name
88+
org.name = "Test Org Updated"
89+
org.save()
90+
org.refresh_from_db()
91+
92+
# The slug should still be the custom one
93+
assert org.slug == "custom-slug"
94+
assert org.title == "Test Org Updated"

0 commit comments

Comments
 (0)