Skip to content

Commit

Permalink
feat: Added site field to plugin (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardvdv authored Apr 6, 2022
1 parent cfd945e commit 9b30313
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog

Unreleased
==========
* feat: Site field added to plugin

1.0.2 (2022-04-01)
==================
Expand Down
15 changes: 15 additions & 0 deletions djangocms_alias/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -261,6 +263,10 @@ class Media:
)


class SiteSelectWidget(Select2Mixin, forms.Select):
pass


class CategorySelectWidget(Select2Mixin, forms.Select):
pass

Expand All @@ -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(),
Expand 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',
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

0 comments on commit 9b30313

Please sign in to comment.