Skip to content
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

Additional Versioning Compatibility Changes #79

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
123 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
59f82a8
Remove old foreign key from plugin, and rename new one
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
061ebc4
Merged with target changes
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
e16c02c
Merge branch 'feature/snippet-data-migration' into feature/moderation…
adam-murray Sep 14, 2021
e4cf009
Made copy function test and started copy function
adam-murray Sep 14, 2021
79eebf4
Remove unique=True from snipept slug field
adam-murray Sep 14, 2021
6bf6557
Merge branch 'feature/snippet-data-migration' into feature/moderation…
adam-murray Sep 14, 2021
38c211d
Removed unique=True on name
adam-murray Sep 14, 2021
0cd495b
Enabled moderation, updates to cms_config
adam-murray Sep 14, 2021
247ae36
Updated config test
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
d6b2a59
Merged with migration code
adam-murray Sep 15, 2021
dbfb808
Added migration and change for removing unique=True, allowing version…
adam-murray Sep 15, 2021
8e599f3
Added checks for whether moderation is enabled
adam-murray Sep 15, 2021
20ed1c8
Linting
adam-murray Sep 15, 2021
463fd7c
Linting
adam-murray Sep 15, 2021
32117b4
Updated test and versioning admin class generation
adam-murray Sep 16, 2021
868030e
Implemented proper form and tests
adam-murray Sep 17, 2021
5966b48
Remove commented out code
adam-murray Sep 17, 2021
f27255a
Fixed tests
adam-murray Sep 17, 2021
1b9ddc7
Merged with release 4 branch
adam-murray Sep 17, 2021
93c450c
Remoce Unique=True on slug and name
adam-murray Sep 17, 2021
8e4db70
Merged with updated moderation integration
adam-murray Sep 17, 2021
65a3175
Linting
adam-murray Sep 17, 2021
c61190c
iSort
adam-murray Sep 17, 2021
a500c9a
Added test criteria and comments for admin
adam-murray Sep 17, 2021
b8f7367
Fixed form save method issue found in manual testing
adam-murray Sep 17, 2021
4ac015d
Push or placeholder problems
adam-murray Sep 22, 2021
e251685
Preview view and testcase
adam-murray Oct 8, 2021
426510f
Linting
adam-murray Oct 8, 2021
47dfedf
Linting and additional test case
adam-murray Oct 8, 2021
a6919e8
Update djangocms_snippet/views.py
adam-murray Oct 12, 2021
7f65404
PR updates
adam-murray Oct 12, 2021
ade6c52
Merge branch 'feature/versionining-extra-code-tasks' of github.com:ad…
adam-murray Oct 12, 2021
7dea453
Linting
adam-murray Oct 12, 2021
7570829
Clean up admin class extension
adam-murray Oct 13, 2021
6cb31ef
PR updates
adam-murray Oct 13, 2021
9e5433f
Linitng
adam-murray Oct 13, 2021
68f537f
Added comment to explain why cms_toolbar_models are required
adam-murray Oct 14, 2021
c5dcb04
Update djangocms_snippet/models.py
adam-murray Oct 14, 2021
d7e693e
Added commments to make end-to-end test clearer
adam-murray Oct 14, 2021
b9fe248
Merged
adam-murray Oct 14, 2021
e9e3394
Comment consistency
adam-murray Oct 14, 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
51 changes: 50 additions & 1 deletion djangocms_snippet/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
from django.conf import settings
from django.conf.urls import url
from django.contrib import admin
from django.db import models
from django.forms import Textarea

from .cms_config import SnippetCMSAppConfig
from .models import Snippet
from .views import SnippetPreviewView


class SnippetAdmin(admin.ModelAdmin):
# Use the version mixin if djangocms-versioning is installed and enabled
snippet_admin_classes = [admin.ModelAdmin]
djangocms_versioning_enabled = SnippetCMSAppConfig.djangocms_versioning_enabled

try:
adam-murray marked this conversation as resolved.
Show resolved Hide resolved
from djangocms_versioning.admin import ExtendedVersionAdminMixin

if djangocms_versioning_enabled:
snippet_admin_classes.insert(0, ExtendedVersionAdminMixin)
except ImportError:
pass


class SnippetAdmin(*snippet_admin_classes):
list_display = ('slug', 'name')
search_fields = ['slug', 'name']
prepopulated_fields = {'slug': ('name',)}
Expand All @@ -22,5 +38,38 @@ class SnippetAdmin(admin.ModelAdmin):
models.TextField: {'widget': Textarea(attrs=text_area_attrs)}
}

class Meta:
model = Snippet

def get_urls(self):
info = self.model._meta.app_label, self.model._meta.model_name
return [
url(
r"^$",
self.admin_site.admin_view(self.changelist_view),
name="{}_{}_changelist".format(*info),
),
url(
r"^(?P<snippet_id>\d+)/$",
self.admin_site.admin_view(self.changelist_view),
name="{}_{}_list".format(*info),
),
url(
r"^add/$",
self.admin_site.admin_view(self.add_view),
name="{}_{}_add".format(*info),
),
url(
r"^(?P<object_id>\d+)/change/$",
self.admin_site.admin_view(self.change_view),
name="{}_{}_change".format(*info),
),
url(
r"^(?P<snippet_id>\d+)/preview/$",
self.admin_site.admin_view(SnippetPreviewView.as_view()),
name="{}_{}_preview".format(*info),
),
]


admin.site.register(Snippet, SnippetAdmin)
9 changes: 7 additions & 2 deletions djangocms_snippet/cms_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from cms.app_base import CMSAppConfig

from djangocms_snippet.models import Snippet
from djangocms_snippet.rendering import render_snippet


try:
Expand All @@ -15,12 +16,16 @@

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

cms_enabled = True
# cms toolbar enabled to allow for versioning compare view
cms_toolbar_enabled_models = [(Snippet, render_snippet), ]

if djangocms_moderation_enabled and djangocms_moderation_installed:
moderated_models = [Snippet]

Expand Down
61 changes: 61 additions & 0 deletions djangocms_snippet/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from django import forms
from django.db import transaction
from django.utils.translation import ugettext_lazy as _

from djangocms_snippet.cms_config import SnippetCMSAppConfig
from djangocms_snippet.models import Snippet, SnippetGrouper


try:
from djangocms_versioning import __version__ # NOQA
is_versioning_installed = True
except ImportError:
is_versioning_installed = False

djangocms_versioning_enabled = SnippetCMSAppConfig.djangocms_versioning_enabled


class SnippetForm(forms.ModelForm):
class Meta:
model = Snippet
fields = (
"name",
"html",
"slug",
"snippet_grouper",
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["snippet_grouper"].required = False

def clean(self):
data = super().clean()
name = data.get("name")
slug = data.get("slug")
snippet_grouper = data.get("snippet_grouper")
snippet_queryset = Snippet.objects.all()

if djangocms_versioning_enabled and is_versioning_installed:
if snippet_grouper:
snippet_queryset = snippet_queryset.exclude(snippet_grouper=snippet_grouper)

for snippet in snippet_queryset:
if snippet.name == name:
self.add_error(
"name", _("A Snippet with this name already exists")
)
elif snippet.slug == slug:
self.add_error(
"slug", _("A Snippet with this slug already exists")
)

return data

@transaction.atomic
def save(self, **kwargs):
if not self.cleaned_data.get("snippet_grouper"):
adam-murray marked this conversation as resolved.
Show resolved Hide resolved
super().save(commit=False)
self.save_m2m()
self.instance.snippet_grouper = SnippetGrouper.objects.create()
return super().save()
23 changes: 18 additions & 5 deletions djangocms_snippet/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.conf import settings
from django.db import models
from django.shortcuts import reverse
from django.utils.translation import gettext_lazy as _

from cms.models import CMSPlugin
Expand All @@ -10,7 +11,15 @@


class SnippetGrouper(models.Model):
pass
@property
def name(self):
snippet_qs = Snippet._base_manager.filter(
snippet_grouper=self
)
return snippet_qs.first().name or super().__str__

def __str__(self):
return self.name


# Stores the actual data
Expand Down Expand Up @@ -50,6 +59,14 @@ class Snippet(models.Model):
def __str__(self):
return self.name

def get_preview_url(self):
return reverse(
"admin:{app}_{model}_preview".format(
app=self._meta.app_label, model=self._meta.model_name,
),
args=[self.id],
)

class Meta:
ordering = ['name']
verbose_name = _('Snippet')
Expand Down Expand Up @@ -82,7 +99,3 @@ class Meta:
@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
7 changes: 7 additions & 0 deletions djangocms_snippet/rendering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.template.response import TemplateResponse


def render_snippet(request, snippet):
template = 'djangocms_snippet/admin/preview.html'
context = {'snippet': snippet}
return TemplateResponse(request, template, context)
18 changes: 18 additions & 0 deletions djangocms_snippet/templates/djangocms_snippet/admin/preview.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "admin/base_site.html" %}
{% load static %}
{{ snippet.html|safe|escape }}

{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/changelists.css" %}">
{% endblock %}

{% block coltype %}flex{% endblock %}

{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-list{% endblock %}

{% block content %}
{% autoescape off %}
{{ snippet.html }}
{% endautoescape %}
{% endblock %}
26 changes: 26 additions & 0 deletions djangocms_snippet/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.http import Http404
from django.views.generic import TemplateView

from djangocms_snippet.models import Snippet


class SnippetPreviewView(TemplateView):
template_name = "djangocms_snippet/admin/preview.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
adam-murray marked this conversation as resolved.
Show resolved Hide resolved
snippet_id = kwargs.get("snippet_id", None)

if not snippet_id:
Http404("snippet_id must be provided.")

try:
snippet = Snippet._base_manager.get(pk=self.kwargs.get("snippet_id"))
except Snippet.DoesNotExist:
raise Http404

context.update({
"snippet": snippet,
"opts": Snippet._meta
})
return context
4 changes: 0 additions & 4 deletions tests/requirements/dj22_cms37.txt

This file was deleted.

4 changes: 0 additions & 4 deletions tests/requirements/dj22_cms38.txt

This file was deleted.

4 changes: 0 additions & 4 deletions tests/requirements/dj30_cms37.txt

This file was deleted.

4 changes: 0 additions & 4 deletions tests/requirements/dj30_cms38.txt

This file was deleted.

4 changes: 0 additions & 4 deletions tests/requirements/dj31_cms38.txt

This file was deleted.

3 changes: 3 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
'ALLOWED_HOSTS': ['localhost'],
'DJANGOCMS_SNIPPET_VERSIONING_ENABLED': True,
'DJANGOCMS_SNIPPET_MODERATION_ENABLED': True,
'CMS_TEMPLATES': (
("page.html", "Normal page"),
),
}


Expand Down
53 changes: 53 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from importlib import reload

from django.contrib import admin
from django.test import RequestFactory, override_settings

from cms.test_utils.testcases import CMSTestCase

from djangocms_snippet import admin as snippet_admin
from djangocms_snippet import cms_config
from djangocms_snippet.models import Snippet

from .utils.factories import SnippetWithVersionFactory


class SnippetAdminTestCase(CMSTestCase):
def setUp(self):
self.snippet = SnippetWithVersionFactory()
self.snippet_admin = snippet_admin.SnippetAdmin(Snippet, admin)
self.snippet_admin_request = RequestFactory().get("/admin/djangocms_snippet")

@override_settings(DJANGOCMS_SNIPPET_VERSIONING_ENABLED=False)
adam-murray marked this conversation as resolved.
Show resolved Hide resolved
def test_admin_list_display_without_versioning(self):
"""
Without versioning enabled, list_display should not be extended with version related items
"""
admin.site.unregister(Snippet)
reload(cms_config)
reload(snippet_admin)
self.snippet_admin = snippet_admin.SnippetAdmin(Snippet, admin)

list_display = self.snippet_admin.get_list_display(self.snippet_admin_request)

self.assertEqual(self.snippet_admin.__class__.__bases__, (admin.ModelAdmin, ))
self.assertEqual(list_display, ('slug', 'name'))

@override_settings(DJANGOCMS_SNIPPET_VERSIONING_ENABLED=True)
def test_admin_list_display_with_versioning(self):
adam-murray marked this conversation as resolved.
Show resolved Hide resolved
"""
With versioning enabled, list_display should be populated with both versioning related items, and the
list actions items
"""
from djangocms_versioning.admin import ExtendedVersionAdminMixin
list_display = self.snippet_admin.get_list_display(self.snippet_admin_request)

# Mixins should always come first in the class bases
self.assertEqual(
self.snippet_admin.__class__.__bases__, (ExtendedVersionAdminMixin, admin.ModelAdmin)
)
self.assertEqual(
list_display[:-1], ('slug', 'name', 'get_author', 'get_modified_date', 'get_versioning_state')
)
self.assertEqual(list_display[-1].short_description, 'actions')
self.assertIn("function ExtendedVersionAdminMixin._list_actions", list_display[-1].__str__())
Loading