Skip to content

Commit 658b22e

Browse files
committed
fix: Allow for later adding of django CMS versioning
1 parent 31682b3 commit 658b22e

File tree

2 files changed

+14
-47
lines changed

2 files changed

+14
-47
lines changed

README.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ 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+
When adding versioning to djangocms-snippets, you will need to create ``Version``objects using
102+
djangocms-versioning's ``create_version`` management command.
97103

98104
Template tag
99105
------------
@@ -119,7 +125,8 @@ Optionally provide a fallback if there is no matching id/slug/instance::
119125
Known Issues
120126
------------
121127

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.
128+
When adding a snippet with the `object` or `embed` tag as root element, the CMS toolbar crashes, making any further
129+
editing of the page difficult or impossible. A workaround is to just put those elements inside a `div` tag.
123130

124131

125132
Running Tests
Lines changed: 2 additions & 42 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).all()
3611

3712
for snippet in snippet_queryset:
3813
grouper = SnippetGrouper.objects.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)