diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cc0d8f72..3a5039b2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,7 +4,7 @@ Changelog Unreleased ========== - +* feat: Enable add button to crate a snippet when adding a SnippetPlugin 4.0.1.dev1 (2022-05-10) ======================= diff --git a/djangocms_snippet/apps.py b/djangocms_snippet/apps.py index 898fdf2c..8a6cda20 100644 --- a/djangocms_snippet/apps.py +++ b/djangocms_snippet/apps.py @@ -5,3 +5,4 @@ class SnippetConfig(AppConfig): name = 'djangocms_snippet' verbose_name = _('Snippets') + default_auto_field = 'django.db.models.AutoField' diff --git a/djangocms_snippet/cms_plugins.py b/djangocms_snippet/cms_plugins.py index a6802626..bbaa2692 100644 --- a/djangocms_snippet/cms_plugins.py +++ b/djangocms_snippet/cms_plugins.py @@ -7,6 +7,7 @@ from cms.plugin_base import CMSPluginBase from cms.plugin_pool import plugin_pool +from .forms import SnippetPluginForm from .models import SnippetPtr from .utils import show_draft_content @@ -21,6 +22,7 @@ class SnippetPlugin(CMSPluginBase): text_enabled = True text_editor_preview = False cache = CACHE_ENABLED + form = SnippetPluginForm def render(self, context, instance, placeholder): snippet = instance.snippet_grouper.snippet(show_editable=show_draft_content(context["request"])) diff --git a/djangocms_snippet/forms.py b/djangocms_snippet/forms.py index e503b9d5..0a7d1952 100644 --- a/djangocms_snippet/forms.py +++ b/djangocms_snippet/forms.py @@ -1,9 +1,12 @@ from django import forms +from django.contrib import admin from django.db import transaction from django.utils.translation import ugettext_lazy as _ +from cms.utils.urlutils import admin_reverse + from djangocms_snippet.cms_config import SnippetCMSAppConfig -from djangocms_snippet.models import Snippet, SnippetGrouper +from djangocms_snippet.models import Snippet, SnippetGrouper, SnippetPtr try: @@ -64,3 +67,26 @@ def save(self, **kwargs): if commit: snippet.save() return snippet + + +class SnippetPluginForm(forms.ModelForm): + + class Meta: + model = SnippetPtr + fields = ("cmsplugin_ptr", "snippet_grouper") + + def __init__(self, *args, **kwargs): + """ + Initialise the form with the add button enabled to allow adding a new snippet from the plugin form. To enable + this the get_related_url method on the widget is overridden to build a URL for the Snippet admin instead of + the SnippetGrouper, as this is not enabled in the admin. + """ + super().__init__(*args, **kwargs) + self.fields["snippet_grouper"].widget.can_add_related = True + self.fields["snippet_grouper"].widget.get_related_url = self.get_related_url_for_snippet + + def get_related_url_for_snippet(self, info, action, *args): + """ + Build URL to the Snippet admin for the given action + """ + return admin_reverse(f"djangocms_snippet_snippet_{action}", current_app=admin.site.name, args=args) diff --git a/tests/requirements/base.txt b/tests/requirements/base.txt index 96a6f304..95b3f3f1 100644 --- a/tests/requirements/base.txt +++ b/tests/requirements/base.txt @@ -6,5 +6,5 @@ isort tox # Unreleased django-cms 4.0 compatible packages -https://github.com/django-cms/django-cms/tarball/develop-4#egg=django-cms +http://github.com/django-cms/django-cms/tarball/release/4.0.1.x#egg=django-cms https://github.com/django-cms/djangocms-versioning/tarball/master#egg=djangocms-versioning diff --git a/tests/test_forms.py b/tests/test_forms.py index 399db3d0..83c71a8e 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -5,6 +5,7 @@ from cms.test_utils.testcases import CMSTestCase from djangocms_snippet import cms_config, forms +from djangocms_snippet.forms import SnippetPluginForm from djangocms_snippet.models import Snippet, SnippetGrouper from .utils.factories import SnippetWithVersionFactory @@ -188,3 +189,23 @@ def test_snippet_form_validation_multiple_version_states_in_grouper(self): form = forms.SnippetForm(form_data) self.assertTrue(form.is_valid()) + + +class SnippetPluginFormTestCase(CMSTestCase): + + def setUp(self): + self.form = SnippetPluginForm() + + def test_get_related_url_for_snippet(self): + """ + Check that the url to add a snippet in the admin is returned + """ + self.assertEqual(self.form.get_related_url_for_snippet("", "add"), "/en/admin/djangocms_snippet/snippet/add/") + + def test_get_related_url_for_snippet_used(self): + """ + Checks that the get_related_url widget is overridden + """ + snippet_grouper_widget = self.form.fields["snippet_grouper"].widget + self.assertEqual(snippet_grouper_widget.get_related_url, self.form.get_related_url_for_snippet) + self.assertTrue(snippet_grouper_widget.can_add_related) diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 29c4a94b..d098d35d 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -19,7 +19,7 @@ def test_for_missing_migrations(self): } try: - call_command('makemigrations', **options) + call_command('makemigrations', 'djangocms_snippet', **options) except SystemExit as e: status_code = str(e) else: