Skip to content

Commit

Permalink
feat: Enable add button on the SnippetPluginForm (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljcollinsuk authored Nov 15, 2022
1 parent 5f7ff3e commit c9c4d25
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
=======================
Expand Down
1 change: 1 addition & 0 deletions djangocms_snippet/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
class SnippetConfig(AppConfig):
name = 'djangocms_snippet'
verbose_name = _('Snippets')
default_auto_field = 'django.db.models.AutoField'
2 changes: 2 additions & 0 deletions djangocms_snippet/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"]))
Expand Down
28 changes: 27 additions & 1 deletion djangocms_snippet/forms.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tests/requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c9c4d25

Please sign in to comment.