Skip to content

Commit 01bac9c

Browse files
committed
Merge branch 'feature/exclude-from-sitemap' into develop
2 parents be789d7 + 5641df3 commit 01bac9c

File tree

7 files changed

+73
-6
lines changed

7 files changed

+73
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
## [Unreleased]
44
### Added
55
- Add support for Wagtail 7.0 and 7.1 (@marteinn)
6+
- Make it possible to exclude pages in trash from sitemap using SkipSitemapIfInTrashMixin (@marteinn)
7+
- Exclude TrashCanPage from sitemap (@marteinn)
8+
- Add support for Django 5.2 (@marteinn)
69

710
### Changed
811
### Fixed
912
- Bump postgres version to 15 in local dev environment (@marteinn)
1013
- Use python 3.13 when running linting/publish in CI (@marteinn)
14+
- Update usage examples (@marteinn)
1115

1216
### Removed
1317
- Drop support for Wagtail 5.2 (@marteinn)
1418
- Drop support for Wagtail 6.2 (@marteinn)
19+
- Drop support for Django 5.0 (@marteinn)
1520

1621
## [3.1.0] - 2025-02-01
1722
### Added

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,31 @@ From the trash can in Wagtail admin it's then possible to permanently delete the
3333
If the parent of the deleted page is either in the trash can or permanently deleted it's still possible to restore the pages by supplying an alternate parent.
3434

3535

36-
## Caveats
36+
## Usage
3737

38+
### App order
3839
Since Wagtail Trash uses the hook `before_delete_page` it might interfere with your applications `before_delete_page` if you have defined one that returns a status code. Make sure wagtail trash is the last hook that runs otherwise or your custom `before_delete_page` might not run since Wagtail Trash doesn't call it.
3940

41+
```python
42+
# Example
43+
INSTALLED_APPS = [
44+
"wagtail.sites",
45+
"wagtail",
46+
"wagtail.contrib.forms",
47+
"wagtail.contrib.redirects",
48+
"wagtail_modeladmin",
49+
"wagtail.contrib.routable_page",
50+
"wagtail.contrib.settings",
51+
"wagtail_trash", # Import here
52+
...
53+
]
54+
```
55+
56+
### Page manager
4057
Also, Wagtail Trash "deletes" pages by unpublishing them, so if you use a queryset that doesn't filter out unpublished pages, pages in trash can might show up. There is a manager that will fix this for you included, example:
4158

4259
```python
43-
from wagtail.core.models import Page, PageManager
60+
from wagtail.models import Page, PageManager
4461
from wagtail_trash.managers import TrashManager
4562

4663
class SomePage(Page):
@@ -51,7 +68,19 @@ class SomePage(Page):
5168
SomePage.objects_excluding_trash.all()
5269
```
5370

54-
Permissions: If you remove a page under a restricted area, this page will be moved and therefore get new permissions. A user might go from not being allowed to see pages under e.g. "Secret Page", but when a page under this area is moved to trash can, the permissions from "Secret Page" are gone so now the user will see it in the trash can.
71+
### Exclude from sitemap
72+
Add SkipSitemapIfInTrashMixin if you want trashed pages to be excluded from sitemap.
73+
74+
```python
75+
from wagtail.models import Page
76+
from wagtail_trash.mixins import SkipSitemapIfInTrashMixin
77+
78+
class SomePage(SkipSitemapIfInTrashMixin, Page):
79+
...
80+
```
81+
82+
### Permissions
83+
If you remove a page under a restricted area, this page will be moved and therefore get new permissions. A user might go from not being allowed to see pages under e.g. "Secret Page", but when a page under this area is moved to trash can, the permissions from "Secret Page" are gone so now the user will see it in the trash can.
5584
This is a solvable issue and will be fixed in a later version.
5685

5786

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
"Programming Language :: Python :: 3.13",
4545
"Framework :: Django",
4646
"Framework :: Django :: 4.2",
47-
"Framework :: Django :: 5.0",
4847
"Framework :: Django :: 5.1",
48+
"Framework :: Django :: 5.2",
4949
"Framework :: Wagtail",
5050
"Framework :: Wagtail :: 6",
5151
"Framework :: Wagtail :: 7",

tests/app/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from wagtail.models import Page, PageManager
22

33
from wagtail_trash.managers import TrashManager
4+
from wagtail_trash.mixins import SkipSitemapIfInTrashMixin
45

56

6-
class TestPage(Page):
7+
class TestPage(SkipSitemapIfInTrashMixin, Page):
78
objects = PageManager()
89
objects_excluding_bins = TrashManager()
910

1011

11-
class OtherPage(Page):
12+
class OtherPage(SkipSitemapIfInTrashMixin, Page):
1213
objects = PageManager()
1314
objects_excluding_bins = TrashManager()

tests/test_admin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from wagtail.models import Page
44
from wagtail.test.utils import WagtailTestUtils
55

6+
from tests.app.models import TestPage
67
from wagtail_trash.models import TrashCan, TrashCanPage
78
from wagtail_trash.views import trash_bulk_delete, trash_delete
89
from wagtail_trash.wagtail_hooks import TrashCanModelAdmin
@@ -82,6 +83,22 @@ def test_removing_page_sends_it_to_trash_can(self):
8283
assert new_page.child_of(TrashCanPage.objects.first())
8384
assert TrashCan.objects.count() == 1
8485

86+
def test_trash_can_and_children_are_not_indexed_in_sitemap(self):
87+
root_page = Page.objects.get(url_path="/")
88+
89+
new_page = TestPage(title="new page")
90+
root_page.add_child(instance=new_page)
91+
92+
assert len(new_page.get_sitemap_urls()) != 0
93+
94+
with self.register_hook("before_delete_page", trash_delete):
95+
delete_url = reverse("wagtailadmin_pages:delete", args=(new_page.id,))
96+
self.client.post(delete_url)
97+
98+
trash_can_page = TrashCanPage.objects.first()
99+
assert len(trash_can_page.get_sitemap_urls()) == 0
100+
assert len(new_page.get_sitemap_urls()) == 0
101+
85102
def test_bulk_delete_sets_and_unsets_slug(self):
86103
from wagtail_trash.wagtail_hooks import urlconf_time
87104

wagtail_trash/mixins.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from wagtail_trash.models import TrashCan
2+
3+
4+
class SkipSitemapIfInTrashMixin:
5+
def get_sitemap_urls(self, request=None):
6+
if self.in_trash_can():
7+
return []
8+
9+
return super().get_sitemap_urls(request=request)
10+
11+
def in_trash_can(self) -> bool:
12+
return TrashCan.objects.filter(page=self).exists()

wagtail_trash/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ class Meta:
3232
class TrashCanPage(Page):
3333
parent_page_types = []
3434
subpage_types = []
35+
36+
def get_sitemap_urls(self, request=None):
37+
return []

0 commit comments

Comments
 (0)