diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a1056c19..76a464e8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,7 @@ Changelog Unreleased ========== +* feat: Added sites support for Snippets * add support for python 3.10 * add support for django 4.2 * drop support for django < 3.2 diff --git a/djangocms_snippet/admin.py b/djangocms_snippet/admin.py index de55dc41..c6e13e53 100644 --- a/djangocms_snippet/admin.py +++ b/djangocms_snippet/admin.py @@ -9,6 +9,7 @@ from django.urls import path from django.utils.translation import gettext as _ +from cms.utils import get_current_site from cms.utils.permissions import get_model_permission_codename from .cms_config import SnippetCMSAppConfig @@ -50,6 +51,13 @@ class SnippetAdmin(*snippet_admin_classes): class Meta: model = Snippet + def get_queryset(self, request): + site = get_current_site() + queryset = super().get_queryset(request) + # Filter queryset with current site and no site + queryset = queryset.filter(models.Q(site=site) | models.Q(site=None)) + return queryset + def get_list_display(self, request): list_display = super().get_list_display(request) list_display = list(list_display) diff --git a/djangocms_snippet/forms.py b/djangocms_snippet/forms.py index 8a0d97b6..a616f265 100644 --- a/djangocms_snippet/forms.py +++ b/djangocms_snippet/forms.py @@ -27,6 +27,7 @@ class Meta: "slug", "snippet_grouper", "template", + "site", ) def __init__(self, *args, **kwargs): diff --git a/djangocms_snippet/migrations/0013_snippet_site.py b/djangocms_snippet/migrations/0013_snippet_site.py new file mode 100644 index 00000000..a7948851 --- /dev/null +++ b/djangocms_snippet/migrations/0013_snippet_site.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2.11 on 2024-05-07 03:43 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sites', '0002_alter_domain_unique'), + ('djangocms_snippet', '0012_auto_20210915_0721'), + ] + + operations = [ + migrations.AddField( + model_name='snippet', + name='site', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='sites.site'), + ), + ] diff --git a/djangocms_snippet/models.py b/djangocms_snippet/models.py index b7baea5f..20a1578d 100644 --- a/djangocms_snippet/models.py +++ b/djangocms_snippet/models.py @@ -1,4 +1,5 @@ from django.conf import settings +from django.contrib.sites.models import Site from django.db import models from django.shortcuts import reverse from django.utils.translation import gettext_lazy as _ @@ -70,6 +71,7 @@ class Snippet(models.Model): default='', max_length=255, ) + site = models.ForeignKey(Site, on_delete=models.CASCADE, null=True, blank=True) def __str__(self): return self.name diff --git a/tests/requirements/dj42_cms40.txt b/tests/requirements/dj42_cms40.txt index 96c85a01..75e2b129 100644 --- a/tests/requirements/dj42_cms40.txt +++ b/tests/requirements/dj42_cms40.txt @@ -4,4 +4,4 @@ Django>=4.2,<5.0 # Unreleased django 4.2 & django-cms 4.0.x compatible packages https://github.com/django-cms/django-cms/tarball/release/4.0.1.x#egg=django-cms -https://github.com/joshyu/djangocms-versioning/tarball/feat/django-42-compatible#egg=djangocms-versioning \ No newline at end of file +https://github.com/django-cms/djangocms-versioning/tarball/support/django-cms-4.0.x#egg=djangocms-versioning diff --git a/tests/test_admin.py b/tests/test_admin.py index d03d2358..8440e3e6 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -1,10 +1,12 @@ from importlib import reload from django.contrib import admin +from django.contrib.sites.models import Site from django.shortcuts import reverse from django.test import RequestFactory, override_settings from cms.test_utils.testcases import CMSTestCase +from cms.utils import get_current_site from djangocms_versioning.models import Version @@ -23,12 +25,52 @@ def setUp(self): html="

This is a test

", snippet_grouper=SnippetGrouper.objects.create(), ) - self.snippet_version = Version.objects.create(content=self.snippet, created_by=self.superuser) + self.snippet_version = Version.objects.create( + content=self.snippet, + created_by=self.superuser, + state='published' + ) self.snippet_admin = snippet_admin.SnippetAdmin(Snippet, admin) self.snippet_admin_request = RequestFactory().get("/admin/djangocms_snippet") self.edit_url = reverse("admin:djangocms_snippet_snippet_change", args=(self.snippet.id,),) self.delete_url = reverse("admin:djangocms_snippet_snippet_delete", args=(self.snippet.id,),) + def test_get_queryset(self): + current_site = get_current_site() + another_site = Site.objects.create(domain='http://www.django-cms.org', name='Django CMS', pk=3) + current_site_snippet = Snippet.objects.create( + name="Test Snippet 1", + slug="test-snippet-one", + html="

This is a test snippet one

", + snippet_grouper=SnippetGrouper.objects.create(), + site=current_site + ) + another_site_snippet = Snippet.objects.create( + name="Test Snippet 2", + slug="test-snippet-two", + html="

This is a test snippet two

", + snippet_grouper=SnippetGrouper.objects.create(), + site=another_site + ) + # Create versions of snippets + Version.objects.create( + content=current_site_snippet, + created_by=self.superuser, + state='published' + ) + Version.objects.create( + content=another_site_snippet, + created_by=self.superuser, + state='published' + ) + queryset = self.snippet_admin.get_queryset(self.snippet_admin_request) + # Test for snippet of current site + self.assertIn(current_site_snippet, queryset) + # Test for snippet with no site + self.assertIn(self.snippet, queryset) + # Test for snippet with another site + self.assertNotIn(another_site_snippet, queryset) + @override_settings(DJANGOCMS_SNIPPET_VERSIONING_ENABLED=False) def test_admin_list_display_without_versioning(self): """