Skip to content

Commit

Permalink
ensure canonical urls are consistent (with no slash) (#1605)
Browse files Browse the repository at this point in the history
* ensure canonical urls are consistent (with no slash)

* fix migrations

* add a test for top level page sitemap urls

* fix migration history
  • Loading branch information
mwvolo authored Jan 10, 2025
1 parent 7f02640 commit 5483297
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 27 deletions.
17 changes: 17 additions & 0 deletions donations/migrations/0009_alter_thankyounote_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1.1 on 2025-01-08 23:01

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("donations", "0008_thankyounote_source"),
]

operations = [
migrations.AlterModelOptions(
name="thankyounote",
options={"verbose_name": "Thank You Note", "verbose_name_plural": "Thank You Notes"},
),
]
13 changes: 13 additions & 0 deletions donations/migrations/0010_merge_20250110_1518.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 5.1.1 on 2025-01-10 21:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("donations", "0009_alter_thankyounote_options"),
("donations", "0009_thankyounote_salesforce_id"),
]

operations = []
9 changes: 9 additions & 0 deletions donations/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.core.exceptions import ValidationError

COLOR_SCHEME_CHOICES = (
('red', 'Red'),
Expand All @@ -25,6 +26,14 @@ class ThankYouNote(models.Model):
salesforce_id = models.CharField(max_length=255, default="", blank=True, help_text="Not null if uploaded to Salesforce")


def __str__(self):
return f'{self.first_name} {self.last_name}'

class Meta:
verbose_name = 'Thank You Note'
verbose_name_plural = 'Thank You Notes'


class DonationPopup(models.Model):
download_image = models.ImageField(null=True, blank=True)
download_ready = models.TextField(default="", blank=True)
Expand Down
2 changes: 1 addition & 1 deletion news/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def get_url_parts(self, *args, **kwargs):
return None

site_id, site_root_url, page_url_relative_to_site_root = url_parts
return (site_id, site_root_url, '/press/{}'.format(self.slug))
return site_id, site_root_url, '/press/{}'.format(self.slug)

search_fields = Page.search_fields + [
index.SearchField('body'),
Expand Down
1 change: 1 addition & 0 deletions oxmenus/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from wagtail.admin.panels import FieldPanel
from wagtail.api import APIField


class MenuItemBlock(blocks.StructBlock):
label = blocks.CharBlock(max_length=255)
partial_url = blocks.CharBlock(max_length=255)
Expand Down
2 changes: 1 addition & 1 deletion oxmenus/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from .serializers import OXMenusSerializer


class OXMenusViewSet(viewsets.ModelViewSet):
class OXMenusViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Menus.objects.all()
serializer_class = OXMenusSerializer
17 changes: 17 additions & 0 deletions pages/migrations/0153_alter_homepage_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1.1 on 2025-01-08 23:01

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("pages", "0152_alter_rootpage_body_alter_rootpage_layout"),
]

operations = [
migrations.AlterModelOptions(
name="homepage",
options={"verbose_name": "Home Page"},
),
]
33 changes: 8 additions & 25 deletions pages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from wagtail.models import Orderable, Page
from wagtail.api import APIField
from wagtail.models import Site
from rest_framework.fields import Field

from api.models import FeatureFlag
from openstax.functions import build_image_url, build_document_url
Expand Down Expand Up @@ -224,7 +223,7 @@ def get_url_parts(self, *args, **kwargs):
# note that we ignore the slug and hardcode this url to / for the root page
site_id, site_root_url, page_url_relative_to_site_root = url_parts

return (site_id, site_root_url, '/')
return site_id, site_root_url, ''

def serve_preview(self, request, mode_name):
site_id, site_root, relative_page_url = self.get_url_parts(request)
Expand All @@ -250,7 +249,7 @@ def get_url_parts(self, *args, **kwargs):
return None

site_id, site_root_url, page_url_relative_to_site_root = url_parts
return (site_id, site_root_url, '/{}'.format(self.slug))
return site_id, site_root_url, '/{}'.format(self.slug)


#TODO: start removing these pages as we move to the above structure for all pages.
Expand Down Expand Up @@ -345,7 +344,6 @@ def get_where_map(self):
]

template = 'page.html'

parent_page_types = ['pages.HomePage']
max_count = 1

Expand Down Expand Up @@ -716,10 +714,7 @@ def get_url_parts(self, *args, **kwargs):
site_id, root_url, page_path = url_parts

# return '/' in place of the real page path
return (site_id, root_url, '/')

class Meta:
verbose_name = "homepage"
return site_id, root_url, '/'


class K12MainPage(Page):
Expand Down Expand Up @@ -863,8 +858,6 @@ def k12library(self):
APIField('promote_image')
]

max_count = 1

class Meta:
verbose_name = "K12 Main Page"

Expand Down Expand Up @@ -902,15 +895,11 @@ class Meta:
FieldPanel('promote_image')
]

max_count = 1
template = 'page.html'
parent_page_types = ['pages.HomePage']
subpage_types = ['pages.K12Subject']

max_count = 1

class Meta:
verbose_name = "K12 Main Page"


class ContactUs(Page):
tagline = models.CharField(max_length=255)
Expand Down Expand Up @@ -982,7 +971,7 @@ def get_url_parts(self, *args, **kwargs):

# note that we ignore the parents, all general pages are /{slug}
site_id, site_root_url, page_url_relative_to_site_root = url_parts
return (site_id, site_root_url, '/{}'.format(self.slug))
return site_id, site_root_url, '/{}'.format(self.slug)

promote_image = models.ForeignKey(
'wagtailimages.Image',
Expand Down Expand Up @@ -1557,7 +1546,6 @@ class Technology(Page):
]

template = 'page.html'

parent_page_types = ['pages.HomePage']
max_count = 1

Expand Down Expand Up @@ -1610,7 +1598,6 @@ class ErrataList(Page):
]

template = 'page.html'

parent_page_types = ['pages.HomePage']
max_count = 1

Expand Down Expand Up @@ -1852,7 +1839,6 @@ class LearningResearchPage(Page):
]

template = 'page.html'

parent_page_types = ['pages.HomePage']
max_count = 1

Expand Down Expand Up @@ -1892,7 +1878,6 @@ class Careers(Page):
]

template = 'page.html'

parent_page_types = ['pages.HomePage']
max_count = 1

Expand Down Expand Up @@ -2821,7 +2806,7 @@ def get_url_parts(self, *args, **kwargs):

# note that we ignore the slug and hardcode this url to /subjects
site_id, site_root_url, page_url_relative_to_site_root = url_parts
return (site_id, site_root_url, '/subjects')
return site_id, site_root_url, '/subjects'

api_fields = [
APIField('heading'),
Expand Down Expand Up @@ -2943,7 +2928,7 @@ def get_url_parts(self, *args, **kwargs):

# note that we ignore the slug and hardcode this url to /subjects
site_id, site_root_url, page_url_relative_to_site_root = url_parts
return (site_id, site_root_url, '/subjects/{}'.format(self.slug[0:-6]))
return site_id, site_root_url, '/subjects/{}'.format(self.slug[0:-6])

@property
def selected_subject(self):
Expand Down Expand Up @@ -3098,7 +3083,6 @@ class FormHeadings(Page):
]

template = 'page.html'

parent_page_types = ['pages.HomePage']
max_count = 1

Expand Down Expand Up @@ -3264,7 +3248,7 @@ def get_url_parts(self, *args, **kwargs):
return None

site_id, site_root_url, page_url_relative_to_site_root = url_parts
return (site_id, site_root_url, '/k12/{}'.format(self.slug[4:]))
return site_id, site_root_url, '/k12/{}'.format(self.slug[4:])

api_fields = [
APIField('subheader'),
Expand Down Expand Up @@ -3379,7 +3363,6 @@ class AllyLogos(Page):
]

template = 'page.html'

parent_page_types = ['pages.HomePage']


Expand Down
17 changes: 17 additions & 0 deletions pages/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime
import json
import unittest
from unittest.mock import patch

from django.test import TestCase, Client
from wagtail.test.utils import WagtailTestUtils, WagtailPageTestCase
Expand All @@ -9,6 +11,21 @@
from shared.test_utilities import assertPathDoesNotRedirectToTrailingSlash, mock_user_login
from http import cookies

class TestRootPage(unittest.TestCase):

@patch('pages.models.RootPage.get_url_parts')
def get_url_parts_returns_correct_values(self, mock_get_url_parts):
mock_get_url_parts.return_value = (1, 'http://openstax.org', 'some/path')
root_page = page_models.RootPage()
result = root_page.get_url_parts()
self.assertEqual(result, (1, 'http://openstax.org', ''))

@patch('pages.models.RootPage.get_url_parts')
def get_url_parts_returns_none_when_no_url_parts(self, mock_get_url_parts):
mock_get_url_parts.return_value = None
root_page = page_models.RootPage()
result = root_page.get_url_parts()
self.assertIsNone(result)

class HomePageTests(WagtailPageTestCase):

Expand Down
1 change: 1 addition & 0 deletions salesforce/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class ResourceDownloadAdmin(admin.ModelAdmin):
list_display = ('id', 'created', 'last_access', 'resource_name', 'book', 'book_format', 'account_uuid')
list_filter = ('created', 'book')


admin.site.register(SalesforceSettings, SalesforceSettingsAdmin)
admin.site.register(SalesforceForms, SalesforceFormsAdmin)
admin.site.register(School, SchoolAdmin)
Expand Down

0 comments on commit 5483297

Please sign in to comment.