Skip to content

fix: Allow for later adding of django CMS versioning #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
Changelog
=========

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


5.0.0
5.0.0 (2025-01-07)
==================
* feat: Universal support for django CMS 3.11 and 4.x


4.1.0 (2024-05-16)
==================

Expand Down
16 changes: 11 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ please set ``DJANGOCMS_SNIPPET_CACHE`` to ``False`` in your settings::

DJANGOCMS_SNIPPET_CACHE = False # default value is False

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

DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID = 2 # Will use user with id: 2
If you use djangocms-versioning or djangocms-moderation, you can have snippets versioned and moderated by
adding the following to your settings::

DJANGOCMS_SNIPPET_VERSIONING = True # Set to version with djangocms-versioning
DJANGOCMS_SNIPPET_MODERATION = True # Set to moderate with djangocms-moderation

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.

Template tag
------------
Expand All @@ -119,7 +124,8 @@ Optionally provide a fallback if there is no matching id/slug/instance::
Known Issues
------------

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.
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.


Running Tests
Expand Down
2 changes: 1 addition & 1 deletion src/djangocms_snippet/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "5.0.0"
__version__ = "5.0.1"
Original file line number Diff line number Diff line change
@@ -1,65 +1,25 @@
from django.apps import apps as global_apps
from django.conf import settings
from django.contrib.contenttypes.management import create_contenttypes
from django.db import migrations


try:
from djangocms_versioning.constants import DRAFT, PUBLISHED

djangocms_versioning_installed = True
except ImportError:
djangocms_versioning_installed = False

djangocms_versioning_config_enabled = getattr(
settings, 'DJANGOCMS_SNIPPET_VERSIONING_ENABLED', True
)


def cms4_grouper_version_migration(apps, schema_editor):
create_contenttypes(global_apps.get_app_config("djangocms_snippet"))

db_alias = schema_editor.connection.alias


ContentType = apps.get_model('contenttypes', 'ContentType')
Snippet = apps.get_model('djangocms_snippet', 'Snippet')
SnippetGrouper = apps.get_model('djangocms_snippet', 'SnippetGrouper')
User = apps.get_model(*settings.AUTH_USER_MODEL.split('.'))

snippet_contenttype = ContentType.objects.get(app_label='djangocms_snippet', model='snippet')
snippet_queryset = Snippet.objects.all()

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

for snippet in snippet_queryset:
grouper = SnippetGrouper.objects.create()
grouper = SnippetGrouper.objects.using(db_alias).create()
snippet.snippet_grouper = grouper
snippet.save()

# Create initial Snippet Versions if versioning is enabled and installed.
# Publish the snippet because all snippets were assumed published before
if djangocms_versioning_config_enabled and djangocms_versioning_installed:
Version.objects.create(
created_by=migration_user,
state=PUBLISHED,
number=1,
object_id=snippet.pk,
content_type=snippet_contenttype,
)


class Migration(migrations.Migration):
dependencies = [
# ('cms', '0034_remove_pagecontent_placeholders'), # Run after the CMS4 migrations
('djangocms_snippet', '0009_auto_20210915_0445'),
]

if djangocms_versioning_installed:
dependencies += [('djangocms_versioning', '0015_version_modified'), ]

operations = [
migrations.RunPython(cms4_grouper_version_migration)
]