Skip to content

feat: enable add button on the SnippetPluginForm #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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