|
| 1 | +from cms.utils.urlutils import admin_reverse |
1 | 2 | from django import forms
|
| 3 | +from django.contrib import admin |
2 | 4 | from django.db import transaction
|
3 | 5 | from django.utils.translation import ugettext_lazy as _
|
4 | 6 |
|
5 | 7 | from djangocms_snippet.cms_config import SnippetCMSAppConfig
|
6 |
| -from djangocms_snippet.models import Snippet, SnippetGrouper |
7 |
| - |
| 8 | +from djangocms_snippet.models import Snippet, SnippetGrouper, SnippetPtr |
8 | 9 |
|
9 | 10 | try:
|
10 | 11 | from djangocms_versioning import __version__ # NOQA
|
@@ -64,3 +65,26 @@ def save(self, **kwargs):
|
64 | 65 | if commit:
|
65 | 66 | snippet.save()
|
66 | 67 | return snippet
|
| 68 | + |
| 69 | + |
| 70 | +class SnippetPluginForm(forms.ModelForm): |
| 71 | + |
| 72 | + class Meta: |
| 73 | + model = SnippetPtr |
| 74 | + fields = ("cmsplugin_ptr", "snippet_grouper") |
| 75 | + |
| 76 | + def __init__(self, *args, **kwargs): |
| 77 | + """ |
| 78 | + Initialise the form with the add button enabled to allow adding a new snippet from the plugin form. To enable |
| 79 | + this the get_related_url method on the widget is monkey patched to build a URL for the Snippet admin instead of |
| 80 | + the SnippetGrouper, as this is not enabled in the admin. |
| 81 | + """ |
| 82 | + super(SnippetPluginForm, self).__init__(*args, **kwargs) |
| 83 | + self.fields["snippet_grouper"].widget.can_add_related = True |
| 84 | + self.fields["snippet_grouper"].widget.get_related_url = self.get_related_url_for_snippet |
| 85 | + |
| 86 | + def get_related_url_for_snippet(self, info, action, *args): |
| 87 | + """ |
| 88 | + Build URL to the Snippet admin for the given action |
| 89 | + """ |
| 90 | + return admin_reverse(f"djangocms_snippet_snippet_{action}", current_app=admin.site.name, args=args) |
0 commit comments