Skip to content

Commit

Permalink
Added site field in models n site filter in admin (#159)
Browse files Browse the repository at this point in the history
* Added site field in models n site filter in admin

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* updated versioning branch

* Add test cases and updated the implementation

* Update CHANGELOG.rst

Co-authored-by: Mark Walker <[email protected]>

* corrected formatting

* fixed flake8 issues

* removed list_filter

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Mark Walker <[email protected]>
  • Loading branch information
3 people authored May 14, 2024
1 parent aaec619 commit eabef47
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 2 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: Added sites support for Snippets
* add support for python 3.10
* add support for django 4.2
* drop support for django < 3.2
Expand Down
8 changes: 8 additions & 0 deletions djangocms_snippet/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions djangocms_snippet/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Meta:
"slug",
"snippet_grouper",
"template",
"site",
)

def __init__(self, *args, **kwargs):
Expand Down
20 changes: 20 additions & 0 deletions djangocms_snippet/migrations/0013_snippet_site.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
2 changes: 2 additions & 0 deletions djangocms_snippet/models.py
Original file line number Diff line number Diff line change
@@ -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 _
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/requirements/dj42_cms40.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
https://github.com/django-cms/djangocms-versioning/tarball/support/django-cms-4.0.x#egg=djangocms-versioning
44 changes: 43 additions & 1 deletion tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -23,12 +25,52 @@ def setUp(self):
html="<h1>This is a test</h1>",
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="<h1>This is a test snippet one</h1>",
snippet_grouper=SnippetGrouper.objects.create(),
site=current_site
)
another_site_snippet = Snippet.objects.create(
name="Test Snippet 2",
slug="test-snippet-two",
html="<h1>This is a test snippet two</h1>",
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):
"""
Expand Down

0 comments on commit eabef47

Please sign in to comment.