Skip to content

feat: Snippet CMS 4.0 and djangocms-versioning Data migration #74

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
6368d7e
Added cms_config versionable item and grouper
adam-murray Aug 9, 2021
75449d6
Added admin, requirements for testing and requirements for running
adam-murray Aug 9, 2021
9bc367c
Update
adam-murray Aug 11, 2021
7c9455c
Fixed issue in setup.py and added basic first migration
adam-murray Aug 11, 2021
4954ee4
Added migration to cms4
adam-murray Aug 12, 2021
8defe7f
Added migration to cms4
adam-murray Aug 12, 2021
b9f822f
Began to implement versioning switch, added migration as part of mana…
adam-murray Aug 16, 2021
49e4bb3
Moved data migration to management command, added util for versioning…
adam-murray Aug 16, 2021
dceee9f
Added tests for config as well as util for versioning check
adam-murray Aug 16, 2021
7dfd721
Merge branch 'feature/versioned-snippets' into feature/snippet-data-m…
adam-murray Aug 16, 2021
56adbdb
Updated cms_config
adam-murray Aug 16, 2021
48e125f
Updated management command
adam-murray Aug 16, 2021
5e12a63
Added form, updated model and forms to improve versioned and unversio…
adam-murray Aug 16, 2021
fdcde12
Updated migration, removed unecessary plugin
adam-murray Aug 31, 2021
13547ae
Merged
adam-murray Aug 31, 2021
9b73d59
Removed additional copies of files
adam-murray Aug 31, 2021
680ce70
Split migration command into migration and command
adam-murray Sep 1, 2021
8dc7f91
Updates to migration
adam-murray Sep 2, 2021
6e937e8
Updates to migration
adam-murray Sep 2, 2021
2df658b
Update migration for review
adam-murray Sep 3, 2021
11a7d3e
Remove pdb
adam-murray Sep 3, 2021
d7c5dbb
Migration upgrade
adam-murray Sep 3, 2021
8898232
Remove plugin from migration 10, added version creation
adam-murray Sep 3, 2021
225b9f9
Implemented proper plugin migration
adam-murray Sep 6, 2021
500cf72
Removed management command
adam-murray Sep 6, 2021
2017d90
Migration 10 now handles lack of versioning gracefully.
adam-murray Sep 7, 2021
25e552e
Add snippet property for backwards compatibility
adam-murray Sep 7, 2021
25048ff
Cleaned up migration and models
adam-murray Sep 7, 2021
649c4af
Removed unecessary logging
adam-murray Sep 7, 2021
bc8d60e
Remove redundent comment
adam-murray Sep 7, 2021
4103082
Update djangocms_snippet/migrations/0010_cms4_grouper_version_data_mi…
adam-murray Sep 7, 2021
ad25ec8
Update djangocms_snippet/migrations/0010_cms4_grouper_version_data_mi…
adam-murray Sep 7, 2021
929d9d6
Update djangocms_snippet/migrations/0011_cms4_plugin_data_migration.py
adam-murray Sep 7, 2021
8e624d9
Update djangocms_snippet/conf.py
adam-murray Sep 7, 2021
f7c848e
Update djangocms_snippet/migrations/0011_cms4_plugin_data_migration.py
adam-murray Sep 7, 2021
85f96f2
Fixed variable name spelling mistake
adam-murray Sep 7, 2021
669d34b
Merge branch 'feature/snippet-data-migration' of github.com:adam-murr…
adam-murray Sep 7, 2021
bea0a0a
Use util
adam-murray Sep 7, 2021
9808226
Updated linting in migrations, and versioning enabled check
adam-murray Sep 7, 2021
530e416
Added instructions for DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID…
adam-murray Sep 7, 2021
a3cdc9b
Added instructions for DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID…
adam-murray Sep 7, 2021
ce96a1f
Added instructions for DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID…
adam-murray Sep 7, 2021
c696c67
isort
adam-murray Sep 7, 2021
3e3b736
Removed unecessary plugin
adam-murray Sep 7, 2021
1aec683
Changed to version4 python version compatibility in test suite
adam-murray Sep 7, 2021
7050f88
merge
adam-murray Sep 7, 2021
53609d2
Updated to cms4 based test suite
adam-murray Sep 8, 2021
eac5cad
Merged
adam-murray Sep 8, 2021
8dfb6db
Removed redundent abstract model
adam-murray Sep 8, 2021
ff5d635
Fixed app config issue
adam-murray Sep 8, 2021
c2f62de
Removed unused management command
adam-murray Sep 8, 2021
ef314fb
Removed management command
adam-murray Sep 8, 2021
0ad68fe
Updates snippet tests for cms4
adam-murray Sep 8, 2021
f3943a3
linting
adam-murray Sep 8, 2021
a08485e
Removed unecessary published flag on page creation
adam-murray Sep 8, 2021
7ff44b1
Merged
adam-murray Sep 9, 2021
736b975
Push for testing
adam-murray Sep 9, 2021
9dd2563
Updates to tests
adam-murray Sep 9, 2021
4171e2e
Merged with versioned snippets
adam-murray Sep 9, 2021
96035a3
Added factories, fixed model tests and templatetag tests
adam-murray Sep 10, 2021
5b65538
Updated plugin tests
adam-murray Sep 10, 2021
aa62ced
Plugin tests now use factories
adam-murray Sep 10, 2021
9fba572
Linting
adam-murray Sep 10, 2021
aeaa52a
Linting
adam-murray Sep 10, 2021
bdf4eaa
Fixed old issue with migrations
adam-murray Sep 13, 2021
aa5a54c
Fixed old issue with migrations
adam-murray Sep 13, 2021
2d36a3b
Fixed spelling mistake
adam-murray Sep 13, 2021
a8f56f3
Updated test to no longer use factoris
adam-murray Sep 13, 2021
b87a0d6
Fixed rendering
adam-murray Sep 13, 2021
7375e5b
Fixed migration error
adam-murray Sep 13, 2021
03df8f7
Updated comments
adam-murray Sep 13, 2021
f895bf1
Migration linting
adam-murray Sep 14, 2021
79eebf4
Remove unique=True from snipept slug field
adam-murray Sep 14, 2021
38c211d
Removed unique=True on name
adam-murray Sep 14, 2021
ee07ad5
Update djangocms_snippet/models.py
adam-murray Sep 15, 2021
9b23fa8
Refactrogin
adam-murray Sep 15, 2021
7cbaaa3
Merged
adam-murray Sep 15, 2021
7cb6cc8
Linting
adam-murray Sep 15, 2021
b59a31d
Refactored test_plugin to closer to original
adam-murray Sep 15, 2021
3153940
Refactoring tests
adam-murray Sep 15, 2021
3d97dc8
Updated tests and migration to make grouper mandatory
adam-murray Sep 15, 2021
366739b
Updated models to even with migrations
adam-murray Sep 15, 2021
3853d19
Refactor
adam-murray Sep 15, 2021
e958a03
Linting
adam-murray Sep 15, 2021
c5c236b
Removed unused util
adam-murray Sep 15, 2021
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
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ please set ``DJANGOCMS_SNIPPET_CACHE`` to ``False`` in your settings::

DJANGOCMS_SNIPPET_CACHE = False # default value is True

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.

DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID = 2 # Will use user with id: 2

Template tag
------------

Expand Down
6 changes: 6 additions & 0 deletions djangocms_snippet/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.conf import settings


DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID = getattr(
settings, "DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID", 1
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 2.2.24 on 2021-08-11 09:42
# Generated by Django 2.2.24 on 2021-09-15 04:45

import django.db.models.deletion
from django.db import migrations, models
Expand All @@ -20,6 +20,11 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='snippet',
name='snippet_grouper',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='djangocms_snippet.SnippetGrouper'),
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='djangocms_snippet.SnippetGrouper'),
),
migrations.AddField(
model_name='snippetptr',
name='snippet_grouper',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='djangocms_snippet.SnippetGrouper'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from django.apps import apps as global_apps
from django.contrib.contenttypes.management import create_contenttypes
from django.db import migrations

from djangocms_snippet.cms_config import SnippetCMSAppConfig
from djangocms_snippet.conf import (
DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID,
)


try:
from djangocms_versioning.constants import DRAFT

djangocms_versioning_installed = True
except ImportError:
djangocms_versioning_installed = False


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

djangocms_versioning_config_enabled = SnippetCMSAppConfig.djangocms_versioning_enabled

ContentType = apps.get_model('contenttypes', 'ContentType')
Snippet = apps.get_model('djangocms_snippet', 'Snippet')
SnippetGrouper = apps.get_model('djangocms_snippet', 'SnippetGrouper')
User = apps.get_model('auth', 'User')

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

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

# Get a migration user.
migration_user = User.objects.get(id=DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID)

# Create initial Snippet Versions if versioning is enabled and installed.
if djangocms_versioning_config_enabled and djangocms_versioning_installed:
Version = apps.get_model('djangocms_versioning', 'Version')
Version.objects.create(
created_by=migration_user,
state=DRAFT,
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)
]
22 changes: 22 additions & 0 deletions djangocms_snippet/migrations/0011_cms4_plugin_data_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.2.24 on 2021-08-31 10:45
from django.db import migrations


def cms4_migration(apps, schema_editor):
SnippetPtr = apps.get_model('djangocms_snippet', 'SnippetPtr')

for snippet_plugin in SnippetPtr.objects.all():
snippet = snippet_plugin.snippet
snippet_plugin.snippet_grouper = snippet.snippet_grouper
snippet_plugin.save()


class Migration(migrations.Migration):

dependencies = [
('djangocms_snippet', '0010_cms4_grouper_version_data_migration'),
]

operations = [
migrations.RunPython(cms4_migration)
]
28 changes: 28 additions & 0 deletions djangocms_snippet/migrations/0012_auto_20210915_0721.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 2.2.24 on 2021-09-15 07:21

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('djangocms_snippet', '0011_cms4_plugin_data_migration'),
]

operations = [
migrations.RemoveField(
model_name='snippetptr',
name='snippet',
),
migrations.AlterField(
model_name='snippet',
name='snippet_grouper',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='djangocms_snippet.SnippetGrouper'),
),
migrations.AlterField(
model_name='snippetptr',
name='snippet_grouper',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='djangocms_snippet.SnippetGrouper'),
),
]
14 changes: 10 additions & 4 deletions djangocms_snippet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ class Snippet(models.Model):
"""
name = models.CharField(
verbose_name=_('Name'),
unique=True,
max_length=255,
unique=True,
)
snippet_grouper = models.ForeignKey(
SnippetGrouper,
on_delete=models.PROTECT,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't match the migrations that you ran, did you change the protection bit later i.e. after creating the migrations?

https://github.com/django-cms/djangocms-snippet/pull/74/files#diff-4ca21242eb21cfa24bd2e3f9e4848d967856c53f97e1cd1838f5dc194efcd14fR21

If you ran make migrations this will create a new migration file. The migrations files will need to be updated from Cascade.

null=True,
)
html = models.TextField(
verbose_name=_('HTML'),
Expand All @@ -44,10 +43,10 @@ class Snippet(models.Model):
)
slug = models.SlugField(
verbose_name=_('Slug'),
unique=True,
blank=False,
default='',
max_length=255,
unique=True,
)

def __str__(self):
Expand All @@ -71,14 +70,21 @@ class SnippetPtr(CMSPlugin):
parent_link=True,
on_delete=models.CASCADE,
)
snippet = models.ForeignKey(Snippet, on_delete=models.CASCADE)
snippet_grouper = models.ForeignKey(
SnippetGrouper,
on_delete=models.CASCADE,
)

search_fields = ['snippet__html'] if SEARCH_ENABLED else []

class Meta:
verbose_name = _('Snippet Ptr')
verbose_name_plural = _('Snippet Ptrs')

@property
def snippet(self):
return self.snippet_grouper.snippet_set.first()

def __str__(self):
# Return the referenced snippet's name rather than the default (ID #)
return self.snippet.name
1 change: 1 addition & 0 deletions tests/requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ tox
coverage
isort
flake8
factory-boy
3 changes: 3 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
HELPER_SETTINGS = {
'INSTALLED_APPS': [
'tests.utils',
'djangocms_versioning',
'djangocms_snippet',
],
'CMS_LANGUAGES': {
1: [{
Expand All @@ -11,6 +13,7 @@
},
'LANGUAGE_CODE': 'en',
'ALLOWED_HOSTS': ['localhost'],
'DJANGOCMS_SNIPPET_VERSIONING_ENABLED': True,
}


Expand Down
37 changes: 18 additions & 19 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
from django.test import TestCase
from cms.test_utils.testcases import CMSTestCase

from djangocms_snippet.models import SEARCH_ENABLED, Snippet, SnippetPtr

from .utils.factories import SnippetPluginFactory, SnippetWithVersionFactory

class SnippetModelTestCase(TestCase):

def setUp(self):
pass
class SnippetModelTestCase(CMSTestCase):

def tearDown(self):
pass
def setUp(self):
self.snippet = SnippetWithVersionFactory(
name="test snippet",
html="<p>hello world</p>",
slug="test_snippet",
)
self.snippet.versions.last().publish(user=self.get_superuser())
self.snippet_grouper = self.snippet.snippet_grouper
SnippetPluginFactory(snippet_grouper=self.snippet_grouper, language=["en"])

def test_settings(self):
self.assertEqual(SEARCH_ENABLED, False)

def test_snippet_instance(self):
Snippet.objects.create(
name="test snippet",
html="<p>hello world</p>",
slug="test_snippet",
)
instance = Snippet.objects.all()

self.assertEqual(instance.count(), 1)

instance = Snippet.objects.first()

self.assertEqual(instance.name, "test snippet")
self.assertEqual(instance.html, "<p>hello world</p>")
self.assertEqual(instance.slug, "test_snippet")
# test strings
self.assertEqual(str(instance), "test snippet")

def test_snippet_ptr_instance(self):
snippet = Snippet.objects.create(
name="test snippet",
html="<p>hello world</p>",
slug="test_snippet",
)
SnippetPtr.objects.create(
snippet=snippet,
)
instance = SnippetPtr.objects.all()

self.assertEqual(instance.count(), 1)

instance = SnippetPtr.objects.first()

# test strings
self.assertEqual(str(instance), "test snippet")
Loading