Browse Source

Add purge_embeds management command

- Fixes #1267
- Caching OEmbed markup prevents markup changes for existing embeds so allow an easy way to clear these if needed
4the4ryushin 2 năm trước cách đây
mục cha
commit
35a2f2cb7e

+ 1 - 0
CHANGELOG.txt

@@ -26,6 +26,7 @@ Changelog
  * Remove unnecessary box-sizing: border-box declarations in SCSS (Albina Starykova)
  * Add full support for secondary buttons with icons in the Wagtail design system - `button bicolor button--icon button-secondary` including the `button-small` variant (Seremba Patrick)
  * Migrated `initTooltips` to TypeScript add JSDoc and unit tests (Fatuma Abdullahi)
+ * Add `purge_embeds` management command to delete all the cached embed objects in the database (Aman Pandey)
  * Fix: Make sure workflow timeline icons are visible in high-contrast mode (Loveth Omokaro)
  * Fix: Ensure authentication forms (login, password reset) have a visible border in Windows high-contrast mode (Loveth Omokaro)
  * Fix: Ensure visual consistency between buttons and links as buttons in Windows high-contrast mode (Albina Starykova)

+ 2 - 0
docs/advanced_topics/embeds.md

@@ -405,3 +405,5 @@ repopulate the records that are being used on the site.
 You may want to do this if you've changed from oEmbed to Embedly or vice-versa
 as the embed code they generate may be slightly different and lead to
 inconsistency on your site.
+
+In general whenever you make changes to embed settings you are recommended to clear out Embed objects using [`purge_embeds` command](purge_embeds).

+ 10 - 0
docs/reference/management_commands.md

@@ -57,6 +57,16 @@ This command deletes old page revisions which are not in moderation, live, appro
 revision for a page. If the `days` argument is supplied, only revisions older than the specified number of
 days will be deleted.
 
+(purge_embeds)=
+
+## purge_embeds
+
+```sh
+manage.py purge_embeds
+```
+
+This command deletes all the cached embed objects from the database. It is recommended to run this command after changes are made to any embed settings so that subsequent embed usage does not from the database cache.
+
 (update_index)=
 
 ## update_index

+ 1 - 0
docs/releases/4.2.md

@@ -35,6 +35,7 @@ depth: 1
  * Remove unnecessary box-sizing: border-box declarations in SCSS (Albina Starykova)
  * Add full support for secondary buttons with icons in the Wagtail design system - `button bicolor button--icon button-secondary` including the `button-small` variant (Seremba Patrick)
  * Migrated `initTooltips` to TypeScript add JSDoc and unit tests (Fatuma Abdullahi)
+ * Add [`purge_embeds`](purge_embeds) management command to delete all the cached embed objects in the database (Aman Pandey)
 
 ### Bug fixes
 

+ 21 - 0
wagtail/management/commands/purge_embeds.py

@@ -0,0 +1,21 @@
+from django.core.management.base import BaseCommand
+
+from wagtail.embeds.models import Embed
+
+
+class Command(BaseCommand):
+    help = "Deletes all of the Embed model objects"
+
+    def handle(self, *args, **options):
+
+        embeds = Embed.objects.all()
+
+        deleted_embeds_count = embeds.delete()[0]
+        if deleted_embeds_count:
+            self.stdout.write(
+                self.style.SUCCESS(
+                    f"Successfully deleted {deleted_embeds_count} embeds"
+                )
+            )
+        else:
+            self.stdout.write("Successfully deleted 0 embeds")

+ 35 - 0
wagtail/tests/test_management_commands.py

@@ -9,6 +9,7 @@ from django.db import models
 from django.test import TestCase
 from django.utils import timezone
 
+from wagtail.embeds.models import Embed
 from wagtail.models import Collection, Page, PageLogEntry, Revision
 from wagtail.signals import page_published, page_unpublished, published, unpublished
 from wagtail.test.testapp.models import (
@@ -697,6 +698,40 @@ class TestPurgeRevisionsCommand(TestCase):
         )
 
 
+class TestPurgeEmbedsCommand(TestCase):
+    fixtures = ["test.json"]
+
+    def setUp(self):
+        # create dummy Embed objects
+        for i in range(5):
+            embed = Embed(
+                hash=f"{i}",
+                url="https://www.youtube.com/watch?v=Js8dIRxwSRY",
+                max_width=None,
+                type="video",
+                html="test html",
+                title="test title",
+                author_name="test author name",
+                provider_name="test provider name",
+                thumbnail_url="http://test/thumbnail.url",
+                width=1000,
+                height=1000,
+            )
+            embed.save()
+
+    def test_purge_embeds(self):
+        """
+        fetch all dummy embeds and confirm they are deleted when the management command runs
+
+        """
+
+        self.assertEqual(Embed.objects.count(), 5)
+
+        management.call_command("purge_embeds", stdout=StringIO())
+
+        self.assertEqual(Embed.objects.count(), 0)
+
+
 class TestCreateLogEntriesFromRevisionsCommand(TestCase):
     fixtures = ["test.json"]