Skip to content

Commit c9c4d25

Browse files
feat: Enable add button on the SnippetPluginForm (#127)
1 parent 5f7ff3e commit c9c4d25

File tree

7 files changed

+54
-4
lines changed

7 files changed

+54
-4
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Changelog
44

55
Unreleased
66
==========
7-
7+
* feat: Enable add button to crate a snippet when adding a SnippetPlugin
88

99
4.0.1.dev1 (2022-05-10)
1010
=======================

djangocms_snippet/apps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
class SnippetConfig(AppConfig):
66
name = 'djangocms_snippet'
77
verbose_name = _('Snippets')
8+
default_auto_field = 'django.db.models.AutoField'

djangocms_snippet/cms_plugins.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from cms.plugin_base import CMSPluginBase
88
from cms.plugin_pool import plugin_pool
99

10+
from .forms import SnippetPluginForm
1011
from .models import SnippetPtr
1112
from .utils import show_draft_content
1213

@@ -21,6 +22,7 @@ class SnippetPlugin(CMSPluginBase):
2122
text_enabled = True
2223
text_editor_preview = False
2324
cache = CACHE_ENABLED
25+
form = SnippetPluginForm
2426

2527
def render(self, context, instance, placeholder):
2628
snippet = instance.snippet_grouper.snippet(show_editable=show_draft_content(context["request"]))

djangocms_snippet/forms.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from django import forms
2+
from django.contrib import admin
23
from django.db import transaction
34
from django.utils.translation import ugettext_lazy as _
45

6+
from cms.utils.urlutils import admin_reverse
7+
58
from djangocms_snippet.cms_config import SnippetCMSAppConfig
6-
from djangocms_snippet.models import Snippet, SnippetGrouper
9+
from djangocms_snippet.models import Snippet, SnippetGrouper, SnippetPtr
710

811

912
try:
@@ -64,3 +67,26 @@ def save(self, **kwargs):
6467
if commit:
6568
snippet.save()
6669
return snippet
70+
71+
72+
class SnippetPluginForm(forms.ModelForm):
73+
74+
class Meta:
75+
model = SnippetPtr
76+
fields = ("cmsplugin_ptr", "snippet_grouper")
77+
78+
def __init__(self, *args, **kwargs):
79+
"""
80+
Initialise the form with the add button enabled to allow adding a new snippet from the plugin form. To enable
81+
this the get_related_url method on the widget is overridden to build a URL for the Snippet admin instead of
82+
the SnippetGrouper, as this is not enabled in the admin.
83+
"""
84+
super().__init__(*args, **kwargs)
85+
self.fields["snippet_grouper"].widget.can_add_related = True
86+
self.fields["snippet_grouper"].widget.get_related_url = self.get_related_url_for_snippet
87+
88+
def get_related_url_for_snippet(self, info, action, *args):
89+
"""
90+
Build URL to the Snippet admin for the given action
91+
"""
92+
return admin_reverse(f"djangocms_snippet_snippet_{action}", current_app=admin.site.name, args=args)

tests/requirements/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ isort
66
tox
77

88
# Unreleased django-cms 4.0 compatible packages
9-
https://github.com/django-cms/django-cms/tarball/develop-4#egg=django-cms
9+
http://github.com/django-cms/django-cms/tarball/release/4.0.1.x#egg=django-cms
1010
https://github.com/django-cms/djangocms-versioning/tarball/master#egg=djangocms-versioning

tests/test_forms.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from cms.test_utils.testcases import CMSTestCase
66

77
from djangocms_snippet import cms_config, forms
8+
from djangocms_snippet.forms import SnippetPluginForm
89
from djangocms_snippet.models import Snippet, SnippetGrouper
910

1011
from .utils.factories import SnippetWithVersionFactory
@@ -188,3 +189,23 @@ def test_snippet_form_validation_multiple_version_states_in_grouper(self):
188189
form = forms.SnippetForm(form_data)
189190

190191
self.assertTrue(form.is_valid())
192+
193+
194+
class SnippetPluginFormTestCase(CMSTestCase):
195+
196+
def setUp(self):
197+
self.form = SnippetPluginForm()
198+
199+
def test_get_related_url_for_snippet(self):
200+
"""
201+
Check that the url to add a snippet in the admin is returned
202+
"""
203+
self.assertEqual(self.form.get_related_url_for_snippet("", "add"), "/en/admin/djangocms_snippet/snippet/add/")
204+
205+
def test_get_related_url_for_snippet_used(self):
206+
"""
207+
Checks that the get_related_url widget is overridden
208+
"""
209+
snippet_grouper_widget = self.form.fields["snippet_grouper"].widget
210+
self.assertEqual(snippet_grouper_widget.get_related_url, self.form.get_related_url_for_snippet)
211+
self.assertTrue(snippet_grouper_widget.can_add_related)

tests/test_migrations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_for_missing_migrations(self):
1919
}
2020

2121
try:
22-
call_command('makemigrations', **options)
22+
call_command('makemigrations', 'djangocms_snippet', **options)
2323
except SystemExit as e:
2424
status_code = str(e)
2525
else:

0 commit comments

Comments
 (0)