Skip to content

Commit 833bd6f

Browse files
fix: Allow for later adding of django CMS versioning (#185)
* fix: Allow for later adding of django CMS versioning * fix typo * More clarity in the readme * Update README.rst Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> * Apply suggestions from code review * chore: Update version and Changelog --------- Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
1 parent 31682b3 commit 833bd6f

File tree

4 files changed

+19
-53
lines changed

4 files changed

+19
-53
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
Changelog
33
=========
44

5-
Unreleased
6-
==========
5+
5.0.1 (2025-05-14)
6+
==================
7+
* fix: Allow for later adding of django CMS versioning
78

89

9-
5.0.0
10+
5.0.0 (2025-01-07)
1011
==================
1112
* feat: Universal support for django CMS 3.11 and 4.x
1213

13-
1414
4.1.0 (2024-05-16)
1515
==================
1616

README.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,16 @@ please set ``DJANGOCMS_SNIPPET_CACHE`` to ``False`` in your settings::
8989

9090
DJANGOCMS_SNIPPET_CACHE = False # default value is False
9191

92-
Migration 0010 requires the use of a user in order to create versions for existing snippets (if djangocms_versioning is installed and enabled),
93-
a user can be chosen with the setting ``DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID``, the default is 1.
94-
This setting is also exposed as an Environment variable for Divio projects using the Divio addon.
92+
django CMS 4 and later
93+
----------------------
9594

96-
DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID = 2 # Will use user with id: 2
95+
If you use djangocms-versioning or djangocms-moderation, you can have snippets versioned and moderated by
96+
adding the following to your settings::
97+
98+
DJANGOCMS_SNIPPET_VERSIONING = True # Set to version with djangocms-versioning
99+
DJANGOCMS_SNIPPET_MODERATION = True # Set to moderate with djangocms-moderation
100+
101+
If you enable versioning (e.g., set `DJANGOCMS_SNIPPET_VERSIONING = True`) for djangocms-snippets in a project that already contains snippets, you will need to create `Version` objects for those existing snippets using the `create_version` management command provided by djangocms-versioning.
97102

98103
Template tag
99104
------------
@@ -119,7 +124,8 @@ Optionally provide a fallback if there is no matching id/slug/instance::
119124
Known Issues
120125
------------
121126

122-
When adding a snippet with the `object` or `embed` tag as root element, the CMS toolbar crashes, making any further editing of the page difficult or impossible. A workaround is to just put those elements inside a `div` tag.
127+
When adding a snippet with the `object` or `embed` tag as root element, the CMS toolbar crashes, making any further
128+
editing of the page difficult or impossible. A workaround is to just put those elements inside a `div` tag.
123129

124130

125131
Running Tests

src/djangocms_snippet/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "5.0.0"
1+
__version__ = "5.0.1"
Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,25 @@
1-
from django.apps import apps as global_apps
2-
from django.conf import settings
3-
from django.contrib.contenttypes.management import create_contenttypes
41
from django.db import migrations
52

63

7-
try:
8-
from djangocms_versioning.constants import DRAFT, PUBLISHED
9-
10-
djangocms_versioning_installed = True
11-
except ImportError:
12-
djangocms_versioning_installed = False
13-
14-
djangocms_versioning_config_enabled = getattr(
15-
settings, 'DJANGOCMS_SNIPPET_VERSIONING_ENABLED', True
16-
)
17-
18-
194
def cms4_grouper_version_migration(apps, schema_editor):
20-
create_contenttypes(global_apps.get_app_config("djangocms_snippet"))
21-
5+
db_alias = schema_editor.connection.alias
226

23-
24-
ContentType = apps.get_model('contenttypes', 'ContentType')
257
Snippet = apps.get_model('djangocms_snippet', 'Snippet')
268
SnippetGrouper = apps.get_model('djangocms_snippet', 'SnippetGrouper')
27-
User = apps.get_model(*settings.AUTH_USER_MODEL.split('.'))
28-
29-
snippet_contenttype = ContentType.objects.get(app_label='djangocms_snippet', model='snippet')
30-
snippet_queryset = Snippet.objects.all()
319

32-
# Get a migration user to create a version.
33-
if djangocms_versioning_config_enabled and djangocms_versioning_installed and len(snippet_queryset):
34-
Version = apps.get_model('djangocms_versioning', 'Version')
35-
migration_user = User.objects.get(id=getattr(settings, "DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID", 1))
10+
snippet_queryset = Snippet.objects.using(db_alias).iterator()
3611

3712
for snippet in snippet_queryset:
38-
grouper = SnippetGrouper.objects.create()
13+
grouper = SnippetGrouper.objects.using(db_alias).create()
3914
snippet.snippet_grouper = grouper
4015
snippet.save()
4116

42-
# Create initial Snippet Versions if versioning is enabled and installed.
43-
# Publish the snippet because all snippets were assumed published before
44-
if djangocms_versioning_config_enabled and djangocms_versioning_installed:
45-
Version.objects.create(
46-
created_by=migration_user,
47-
state=PUBLISHED,
48-
number=1,
49-
object_id=snippet.pk,
50-
content_type=snippet_contenttype,
51-
)
52-
5317

5418
class Migration(migrations.Migration):
5519
dependencies = [
56-
# ('cms', '0034_remove_pagecontent_placeholders'), # Run after the CMS4 migrations
5720
('djangocms_snippet', '0009_auto_20210915_0445'),
5821
]
5922

60-
if djangocms_versioning_installed:
61-
dependencies += [('djangocms_versioning', '0015_version_modified'), ]
62-
6323
operations = [
6424
migrations.RunPython(cms4_grouper_version_migration)
6525
]

0 commit comments

Comments
 (0)