Skip to content

Versioning - Model and cms_config implementation #75

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
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
9 changes: 3 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ 3.7, 3.8, 3.9, ] # latest release minus two
python-version: [ 3.6, 3.7, ]
requirements-file: [
dj22_cms37.txt,
dj22_cms38.txt,
dj30_cms37.txt,
dj30_cms38.txt,
dj31_cms38.txt,
dj11_cms40.txt,
dj22_cms40.txt,
]
os: [
ubuntu-20.04,
Expand Down
24 changes: 24 additions & 0 deletions djangocms_snippet/cms_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.conf import settings

from cms.app_base import CMSAppConfig

from .models import Snippet


class SnippetCMSAppConfig(CMSAppConfig):
djangocms_versioning_enabled = getattr(
settings, 'DJANGOCMS_SNIPPET_VERSIONING_ENABLED', False
)

if djangocms_versioning_enabled:
from djangocms_versioning.datastructures import (
VersionableItem, default_copy,
)

versioning = [
VersionableItem(
content_model=Snippet,
grouper_field_name="snippet_grouper",
copy_function=default_copy,
)
]
25 changes: 25 additions & 0 deletions djangocms_snippet/migrations/0009_auto_20210811_0942.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.2.24 on 2021-08-11 09:42

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


class Migration(migrations.Migration):

dependencies = [
('djangocms_snippet', '0008_auto_change_name'),
]

operations = [
migrations.CreateModel(
name='SnippetGrouper',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.AddField(
model_name='snippet',
name='snippet_grouper',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='djangocms_snippet.SnippetGrouper'),
),
]
12 changes: 10 additions & 2 deletions djangocms_snippet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
SEARCH_ENABLED = getattr(settings, 'DJANGOCMS_SNIPPET_SEARCH', False)


class SnippetGrouper(models.Model):
pass


# Stores the actual data
class Snippet(models.Model):
"""
Expand All @@ -19,6 +23,11 @@ class Snippet(models.Model):
unique=True,
max_length=255,
)
snippet_grouper = models.ForeignKey(
SnippetGrouper,
on_delete=models.PROTECT,
null=True,
)
html = models.TextField(
verbose_name=_('HTML'),
blank=True,
Expand Down Expand Up @@ -62,8 +71,7 @@ class SnippetPtr(CMSPlugin):
parent_link=True,
on_delete=models.CASCADE,
)

snippet = models.ForeignKey(Snippet, on_delete=models.CASCADE,)
snippet = models.ForeignKey(Snippet, on_delete=models.CASCADE)

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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


REQUIREMENTS = [
'django-cms>=3.7',
'django-cms',
'django-treebeard>=4.3,<4.5',
]

Expand Down
4 changes: 4 additions & 0 deletions tests/requirements/dj11_cms40.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-r base.txt

https://github.com/django-cms/django-cms/tarball/release/4.0.x#egg=django-cms
https://github.com/divio/djangocms-versioning/tarball/master/#egg=djangocms-versioning
5 changes: 5 additions & 0 deletions tests/requirements/dj22_cms40.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-r base.txt

Django>=2.2,<3.0
https://github.com/django-cms/django-cms/tarball/release/4.0.x#egg=django-cms
https://github.com/divio/djangocms-versioning/tarball/master/#egg=djangocms-versioning
18 changes: 7 additions & 11 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ def setUp(self):
template="page.html",
language=self.language,
)
self.home.publish(self.language)
self.page = create_page(
title="help",
template="page.html",
language=self.language,
)
self.page.publish(self.language)
self.placeholder = self.page.placeholders.get(slot="content")
self.pagecontent = self.page.pagecontent_set.first()
self.placeholder = self.pagecontent.placeholders.get(slot="content")
self.superuser = self.get_superuser()

def tearDown(self):
Expand All @@ -36,12 +35,12 @@ def test_html_rendering(self):
slug="plugin_snippet",
)
plugin = add_plugin(
self.page.placeholders.get(slot="content"),
self.pagecontent.placeholders.get(slot="content"),
"SnippetPlugin",
self.language,
snippet=snippet,
)
self.page.publish(self.language)

self.assertEqual(plugin.snippet.name, "plugin_snippet")
self.assertEqual(plugin.snippet.html, "<p>Hello World</p>")
self.assertEqual(plugin.snippet.slug, "plugin_snippet")
Expand All @@ -59,12 +58,11 @@ def test_failing_html_rendering(self):
slug="plugin_snippet",
)
add_plugin(
self.page.placeholders.get(slot="content"),
self.pagecontent.placeholders.get(slot="content"),
"SnippetPlugin",
self.language,
snippet=snippet,
)
self.page.publish(self.language)

with self.login_user_context(self.superuser):
response = self.client.get(request_url)
Expand All @@ -82,12 +80,11 @@ def test_template_rendering(self):
)
snippet.save()
plugin = add_plugin(
self.page.placeholders.get(slot="content"),
self.pagecontent.placeholders.get(slot="content"),
"SnippetPlugin",
self.language,
snippet=snippet,
)
self.page.publish(self.language)
self.assertEqual(plugin.snippet.name, "plugin_snippet")
self.assertEqual(plugin.snippet.slug, "plugin_snippet")

Expand All @@ -109,12 +106,11 @@ def test_failing_template_rendering(self):
)
snippet.save()
add_plugin(
self.page.placeholders.get(slot="content"),
self.pagecontent.placeholders.get(slot="content"),
"SnippetPlugin",
self.language,
snippet=snippet,
)
self.page.publish(self.language)

with self.login_user_context(self.superuser):
response = self.client.get(request_url)
Expand Down