Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Oct 8, 2025

Problem

When changing an organization's name in Wagtail CMS (e.g., changing "MIT" to "MIT - Universal AI"), the page slug was being automatically regenerated. This caused several issues:

  • Broke existing links that had been sent to users
  • Required updating redirect URLs in Keycloak
  • Lost SEO/bookmark references

The root cause was in the OrganizationPage.save() method, which unconditionally regenerated the slug on every save:

def save(self, clean=True, user=None, log_action=False, **kwargs):
    self.title = str(self.name)
    self.slug = slugify(f"org-{self.name}")  # Always overwrites!
    Page.save(self, clean=clean, user=user, log_action=log_action, **kwargs)

Solution

Changed the slug generation to only occur when the slug is empty (new object) or not set, following the same pattern used by the Department model in courses/models.py:

def save(self, clean=True, user=None, log_action=False, **kwargs):
    self.title = str(self.name)
    if not self.slug:  # Only generate if empty
        self.slug = slugify(f"org-{self.name}")
    Page.save(self, clean=clean, user=user, log_action=log_action, **kwargs)

Additionally, removed promote_panels = [] to expose the slug field in Wagtail admin, allowing administrators to manually edit slugs when needed.

Changes

  • b2b/models.py: Added conditional check for slug generation and removed promote_panels restriction
  • b2b/models_test.py: Added 3 comprehensive tests covering all scenarios:
    • Slug preservation when name changes
    • Slug generation for new organizations
    • Custom slug preservation

Behavior

Scenario Before After
Rename existing org ❌ Slug regenerated (breaks links) ✅ Slug preserved
Create new org ✅ Slug auto-generated ✅ Slug auto-generated
Set custom slug ❌ Overwritten on save ✅ Preserved
Edit slug in Wagtail ❌ Field hidden ✅ Field visible and editable

Testing

  • ✅ All new tests pass
  • ✅ Linting checks pass (ruff)
  • ✅ Pattern matches existing codebase conventions
  • ✅ Backward compatible - no migration required

Fixes the slug regeneration issue while maintaining all existing functionality for new organizations.

Original prompt

This section details on the original issue you should resolve

<issue_title>B2B/UAI: Don't change the org slug if the title alone changes</issue_title>
<issue_description>The "MIT" organization's name got changed to "MIT - Universal AI" on RC and prod via the Wagtail interface, and that resulted in the org page's slug also being updated. It'd be better if it doesn't change the slug, I think, when saving unless we explicitly want to change it. Changing the slug means at least updating the redirect URL in Keycloak, and also likely adding in some redirects elsewhere so links that have been sent out to people/etc. still work.

Expected Behavior

Changing the name of the org in Wagtail should only change the name of the org in Wagtail.

Current Behavior

It does change the name, but the slug is also regenerated erroneously.

Steps to Reproduce

  1. Set up an org.
  2. Change the name of it in Wagtail.
  3. Observe that the page slug is now different.

Possible Solution

Slug generation should only happen if the field is empty or the object is new. If neither of these conditions are present, we should leave the slug field alone. (This also means if we set it to something specific, it won't get overwritten.) This should be a pretty straightforward fix.

Not sure what the rules are in Wagtail for exposing the slug as an editable field (if any, or if it's already there and I just don't remember off the top of my head) but it'd also probably be good to display this so we can adjust it manually if we want.
</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes mitodl/hq#8849

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Fix org slug alteration on title change in Wagtail Fix OrganizationPage slug regeneration on name changes Oct 8, 2025
@Copilot Copilot AI requested a review from jkachel October 8, 2025 18:41
Copilot finished work on behalf of jkachel October 8, 2025 18:41
@jkachel jkachel marked this pull request as ready for review October 9, 2025 15:31
Copy link
Contributor

@jkachel jkachel left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants