Skip to content

Commit 4fd5662

Browse files
authored
feat: Moderation compatibility and version copy method implementation (#77)
1 parent a441d5a commit 4fd5662

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

djangocms_snippet/cms_config.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22

33
from cms.app_base import CMSAppConfig
44

5-
from .models import Snippet
5+
from djangocms_snippet.models import Snippet
6+
7+
8+
try:
9+
from djangocms_moderation import __version__ # NOQA
10+
11+
djangocms_moderation_installed = True
12+
except ImportError:
13+
djangocms_moderation_installed = False
614

715

816
class SnippetCMSAppConfig(CMSAppConfig):
917
djangocms_versioning_enabled = getattr(
1018
settings, 'DJANGOCMS_SNIPPET_VERSIONING_ENABLED', False
1119
)
20+
djangocms_moderation_enabled = getattr(
21+
settings, 'DJANGOCMS_SNIPPET_MODERATION_ENABLED', False
22+
)
23+
24+
if djangocms_moderation_enabled and djangocms_moderation_installed:
25+
moderated_models = [Snippet]
1226

1327
if djangocms_versioning_enabled:
1428
from djangocms_versioning.datastructures import (
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 2.2.24 on 2021-09-15 07:51
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('djangocms_snippet', '0012_auto_20210915_0721'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='snippet',
15+
name='name',
16+
field=models.CharField(max_length=255, verbose_name='Name'),
17+
),
18+
migrations.AlterField(
19+
model_name='snippet',
20+
name='slug',
21+
field=models.SlugField(default='', max_length=255, verbose_name='Slug'),
22+
),
23+
]

djangocms_snippet/models.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class Snippet(models.Model):
2121
name = models.CharField(
2222
verbose_name=_('Name'),
2323
max_length=255,
24-
unique=True,
2524
)
2625
snippet_grouper = models.ForeignKey(
2726
SnippetGrouper,
@@ -46,7 +45,6 @@ class Snippet(models.Model):
4645
blank=False,
4746
default='',
4847
max_length=255,
49-
unique=True,
5048
)
5149

5250
def __str__(self):

tests/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'LANGUAGE_CODE': 'en',
1515
'ALLOWED_HOSTS': ['localhost'],
1616
'DJANGOCMS_SNIPPET_VERSIONING_ENABLED': True,
17+
'DJANGOCMS_SNIPPET_MODERATION_ENABLED': True,
1718
}
1819

1920

tests/test_config.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from django.apps import apps
2+
3+
from cms.test_utils.testcases import CMSTestCase
4+
5+
from djangocms_snippet.models import Snippet, SnippetGrouper
6+
7+
from .utils.factories import SnippetWithVersionFactory
8+
9+
10+
class VersioningConfigTestCase(CMSTestCase):
11+
12+
def test_snippet_copy_method(self):
13+
"""
14+
App should use the default copy method, and return an identical model (apart from PK)
15+
"""
16+
snippet_cms_config = apps.get_app_config("djangocms_snippet").cms_config
17+
old_snippet = SnippetWithVersionFactory(
18+
name="snippet",
19+
html="<p>Hello World</p>",
20+
slug="snippet",
21+
)
22+
23+
new_snippet = snippet_cms_config.versioning[0].copy_function(old_snippet)
24+
25+
self.assertNotEqual(old_snippet, new_snippet)
26+
self.assertEqual(old_snippet.name, new_snippet.name)
27+
self.assertEqual(old_snippet.html, new_snippet.html)
28+
self.assertEqual(old_snippet.snippet_grouper, new_snippet.snippet_grouper)
29+
self.assertEqual(1, SnippetGrouper.objects.count())
30+
self.assertEqual(2, Snippet._base_manager.count())

0 commit comments

Comments
 (0)