From 9b30313390d8d8776aef238c5debd3e9287de5be Mon Sep 17 00:00:00 2001 From: Bernard Van Der Vyver Date: Wed, 6 Apr 2022 16:08:30 +0100 Subject: [PATCH] feat: Added site field to plugin (#99) --- CHANGELOG.rst | 1 + djangocms_alias/forms.py | 15 +++++++++++++++ tests/test_cms_plugins.py | 16 ++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d25b5af6..ba5f13dd 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,7 @@ Changelog Unreleased ========== +* feat: Site field added to plugin 1.0.2 (2022-04-01) ================== diff --git a/djangocms_alias/forms.py b/djangocms_alias/forms.py index 088e11e9..2f36b011 100644 --- a/djangocms_alias/forms.py +++ b/djangocms_alias/forms.py @@ -4,10 +4,12 @@ AdminTextInputWidget, RelatedFieldWidgetWrapper, ) +from django.contrib.sites.models import Site from django.db import transaction from django.utils.translation import gettext_lazy as _ from cms.models import CMSPlugin, Placeholder +from cms.utils import get_current_site from cms.utils.permissions import ( get_model_permission_codename, has_plugin_permission, @@ -261,6 +263,10 @@ class Media: ) +class SiteSelectWidget(Select2Mixin, forms.Select): + pass + + class CategorySelectWidget(Select2Mixin, forms.Select): pass @@ -277,6 +283,13 @@ def build_attrs(self, *args, **kwargs): class AliasPluginForm(forms.ModelForm): + site = forms.ModelChoiceField( + label=_("Site"), + queryset=Site.objects.all(), + widget=SiteSelectWidget(attrs={"data-placeholder": _("Select site")}), + required=False, + ) + category = forms.ModelChoiceField( label=_('Category'), queryset=Category.objects.all(), @@ -302,10 +315,12 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if self.instance and self.instance.pk: self.fields['category'].initial = self.instance.alias.category_id + self.fields['site'].initial = get_current_site() class Meta: model = AliasPlugin fields = ( + 'site', 'category', 'alias', 'template', diff --git a/tests/test_cms_plugins.py b/tests/test_cms_plugins.py index e3d3f8a9..b8c92400 100644 --- a/tests/test_cms_plugins.py +++ b/tests/test_cms_plugins.py @@ -3,6 +3,7 @@ from urllib.parse import urlparse from cms.api import add_plugin, create_title +from cms.utils import get_current_site from cms.utils.plugins import downcast_plugins from cms.utils.urlutils import admin_reverse @@ -318,3 +319,18 @@ def test_replace_placeholder_content_with_alias(self): alias_content.placeholder.get_plugins()[1].get_bound_plugin().body, 'test 2', ) + + def test_create_alias_plugin_form_initial_site(self): + current_site = get_current_site() + alias = self._create_alias( + self.placeholder.get_plugins(), + ) + alias_plugin = add_plugin( + self.placeholder, + Alias, + language=self.language, + alias=alias, + ) + form = AliasPluginForm(instance=alias_plugin) + + self.assertEqual(form.fields['site'].initial, current_site)