-
Notifications
You must be signed in to change notification settings - Fork 51
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
Aiky30
merged 85 commits into
django-cms:support/django-cms-4.0.x
from
adam-murray:feature/snippet-data-migration
Sep 15, 2021
Merged
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 75449d6
Added admin, requirements for testing and requirements for running
adam-murray 9bc367c
Update
adam-murray 7c9455c
Fixed issue in setup.py and added basic first migration
adam-murray 4954ee4
Added migration to cms4
adam-murray 8defe7f
Added migration to cms4
adam-murray b9f822f
Began to implement versioning switch, added migration as part of mana…
adam-murray 49e4bb3
Moved data migration to management command, added util for versioning…
adam-murray dceee9f
Added tests for config as well as util for versioning check
adam-murray 7dfd721
Merge branch 'feature/versioned-snippets' into feature/snippet-data-m…
adam-murray 56adbdb
Updated cms_config
adam-murray 48e125f
Updated management command
adam-murray 5e12a63
Added form, updated model and forms to improve versioned and unversio…
adam-murray fdcde12
Updated migration, removed unecessary plugin
adam-murray 13547ae
Merged
adam-murray 9b73d59
Removed additional copies of files
adam-murray 680ce70
Split migration command into migration and command
adam-murray 8dc7f91
Updates to migration
adam-murray 6e937e8
Updates to migration
adam-murray 2df658b
Update migration for review
adam-murray 11a7d3e
Remove pdb
adam-murray d7c5dbb
Migration upgrade
adam-murray 8898232
Remove plugin from migration 10, added version creation
adam-murray 225b9f9
Implemented proper plugin migration
adam-murray 500cf72
Removed management command
adam-murray 2017d90
Migration 10 now handles lack of versioning gracefully.
adam-murray 25e552e
Add snippet property for backwards compatibility
adam-murray 25048ff
Cleaned up migration and models
adam-murray 649c4af
Removed unecessary logging
adam-murray bc8d60e
Remove redundent comment
adam-murray 4103082
Update djangocms_snippet/migrations/0010_cms4_grouper_version_data_mi…
adam-murray ad25ec8
Update djangocms_snippet/migrations/0010_cms4_grouper_version_data_mi…
adam-murray 929d9d6
Update djangocms_snippet/migrations/0011_cms4_plugin_data_migration.py
adam-murray 8e624d9
Update djangocms_snippet/conf.py
adam-murray f7c848e
Update djangocms_snippet/migrations/0011_cms4_plugin_data_migration.py
adam-murray 85f96f2
Fixed variable name spelling mistake
adam-murray 669d34b
Merge branch 'feature/snippet-data-migration' of github.com:adam-murr…
adam-murray bea0a0a
Use util
adam-murray 9808226
Updated linting in migrations, and versioning enabled check
adam-murray 530e416
Added instructions for DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID…
adam-murray a3cdc9b
Added instructions for DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID…
adam-murray ce96a1f
Added instructions for DJANGOCMS_SNIPPET_VERSIONING_MIGRATION_USER_ID…
adam-murray c696c67
isort
adam-murray 3e3b736
Removed unecessary plugin
adam-murray 1aec683
Changed to version4 python version compatibility in test suite
adam-murray 7050f88
merge
adam-murray 53609d2
Updated to cms4 based test suite
adam-murray eac5cad
Merged
adam-murray 8dfb6db
Removed redundent abstract model
adam-murray ff5d635
Fixed app config issue
adam-murray c2f62de
Removed unused management command
adam-murray ef314fb
Removed management command
adam-murray 0ad68fe
Updates snippet tests for cms4
adam-murray f3943a3
linting
adam-murray a08485e
Removed unecessary published flag on page creation
adam-murray 7ff44b1
Merged
adam-murray 736b975
Push for testing
adam-murray 9dd2563
Updates to tests
adam-murray 4171e2e
Merged with versioned snippets
adam-murray 96035a3
Added factories, fixed model tests and templatetag tests
adam-murray 5b65538
Updated plugin tests
adam-murray aa62ced
Plugin tests now use factories
adam-murray 9fba572
Linting
adam-murray aeaa52a
Linting
adam-murray bdf4eaa
Fixed old issue with migrations
adam-murray aa5a54c
Fixed old issue with migrations
adam-murray 2d36a3b
Fixed spelling mistake
adam-murray a8f56f3
Updated test to no longer use factoris
adam-murray b87a0d6
Fixed rendering
adam-murray 7375e5b
Fixed migration error
adam-murray 03df8f7
Updated comments
adam-murray f895bf1
Migration linting
adam-murray 79eebf4
Remove unique=True from snipept slug field
adam-murray 38c211d
Removed unique=True on name
adam-murray ee07ad5
Update djangocms_snippet/models.py
adam-murray 9b23fa8
Refactrogin
adam-murray 7cbaaa3
Merged
adam-murray 7cb6cc8
Linting
adam-murray b59a31d
Refactored test_plugin to closer to original
adam-murray 3153940
Refactoring tests
adam-murray 3d97dc8
Updated tests and migration to make grouper mandatory
adam-murray 366739b
Updated models to even with migrations
adam-murray 3853d19
Refactor
adam-murray e958a03
Linting
adam-murray c5c236b
Removed unused util
adam-murray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
djangocms_snippet/migrations/0010_cms4_grouper_version_data_migration.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
djangocms_snippet/migrations/0011_cms4_plugin_data_migration.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
), | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? 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'), | ||
|
@@ -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): | ||
|
@@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ tox | |
coverage | ||
isort | ||
flake8 | ||
factory-boy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.