-
Notifications
You must be signed in to change notification settings - Fork 51
Additional Versioning Compatibility Changes #79
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
Changes from 111 commits
6368d7e
75449d6
9bc367c
7c9455c
4954ee4
8defe7f
b9f822f
49e4bb3
dceee9f
7dfd721
56adbdb
48e125f
5e12a63
fdcde12
13547ae
9b73d59
680ce70
8dc7f91
6e937e8
2df658b
11a7d3e
d7c5dbb
8898232
225b9f9
500cf72
2017d90
25e552e
59f82a8
25048ff
649c4af
061ebc4
bc8d60e
4103082
ad25ec8
929d9d6
8e624d9
f7c848e
85f96f2
669d34b
bea0a0a
9808226
530e416
a3cdc9b
ce96a1f
c696c67
3e3b736
1aec683
7050f88
53609d2
eac5cad
8dfb6db
ff5d635
c2f62de
ef314fb
0ad68fe
f3943a3
a08485e
7ff44b1
736b975
9dd2563
4171e2e
96035a3
5b65538
aa62ced
9fba572
aeaa52a
bdf4eaa
aa5a54c
2d36a3b
a8f56f3
b87a0d6
7375e5b
03df8f7
f895bf1
e16c02c
e4cf009
79eebf4
6bf6557
38c211d
0cd495b
247ae36
ee07ad5
9b23fa8
7cbaaa3
7cb6cc8
b59a31d
3153940
3d97dc8
366739b
3853d19
e958a03
d6b2a59
dbfb808
8e599f3
20ed1c8
463fd7c
32117b4
868030e
5966b48
f27255a
1b9ddc7
93c450c
8e4db70
65a3175
c61190c
a500c9a
b8f7367
4ac015d
e251685
426510f
47dfedf
a6919e8
7f65404
ade6c52
7dea453
7570829
6cb31ef
9e5433f
68f537f
c5dcb04
d7e693e
b9fe248
e9e3394
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,12 +1,22 @@ | ||||||
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 .models import Snippet | ||||||
from .views import SnippetPreviewView | ||||||
|
||||||
|
||||||
class SnippetAdmin(admin.ModelAdmin): | ||||||
try: | ||||||
from djangocms_versioning.admin import ExtendedVersionAdminMixin | ||||||
|
||||||
djangocms_versioning_installed = True | ||||||
except ImportError: | ||||||
djangocms_versioning_installed = False | ||||||
|
||||||
|
||||||
class AbstractSnippetAdmin(admin.ModelAdmin): | ||||||
list_display = ('slug', 'name') | ||||||
search_fields = ['slug', 'name'] | ||||||
prepopulated_fields = {'slug': ('name',)} | ||||||
|
@@ -22,5 +32,56 @@ class SnippetAdmin(admin.ModelAdmin): | |||||
models.TextField: {'widget': Textarea(attrs=text_area_attrs)} | ||||||
} | ||||||
|
||||||
class Meta: | ||||||
abstract = True | ||||||
|
||||||
|
||||||
djangocms_versioning_enabled = getattr( | ||||||
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 is normally done by the cms_config, you can then just call the config for the package and see if it's enabled rather than repeat the settings implementation: https://github.com/divio/djangocms-alias/blob/master/djangocms_alias/cms_config.py#L22 |
||||||
settings, 'DJANGOCMS_SNIPPET_VERSIONING_ENABLED', False | ||||||
) | ||||||
|
||||||
snippet_admin_classes = [ | ||||||
AbstractSnippetAdmin, | ||||||
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. I'm confused as to why we have an abstract class that is not reused, could this not be:
Suggested change
|
||||||
] | ||||||
|
||||||
|
||||||
if djangocms_versioning_installed and djangocms_versioning_enabled: | ||||||
snippet_admin_classes = [ExtendedVersionAdminMixin] + snippet_admin_classes | ||||||
|
||||||
|
||||||
class SnippetAdmin(*snippet_admin_classes): | ||||||
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from django import forms | ||
from django.conf import settings | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
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 = getattr( | ||
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. Same as above, this is set in the cms config. If we just had a setting it would live in conf.py but as this is in the cms_config we should use that as the centralised control. |
||
settings, 'DJANGOCMS_SNIPPET_VERSIONING_ENABLED', False | ||
) | ||
|
||
|
||
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") | ||
published_snippet_queryset = Snippet.objects.all() | ||
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. The phrase published_snippet_queryset is misleading because this is not a published queryset if versioning is not enabled. |
||
|
||
if djangocms_versioning_enabled and is_versioning_installed: | ||
if snippet_grouper: | ||
published_snippet_queryset.exclude(snippet_grouper=snippet_grouper) | ||
|
||
for snippet in published_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 | ||
|
||
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() |
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) |
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 %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.conf.urls import url | ||
|
||
from djangocms_snippet.models import Snippet | ||
from djangocms_snippet.views import SnippetPreviewView | ||
|
||
|
||
urlpatterns = [ | ||
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. What is this used for? The url already exists to preview: https://github.com/django-cms/djangocms-snippet/pull/79/files#diff-aabaa98ef870e0884628a1bb3ddce0de62a156e640798d77f6cbbe6cd6c7cbacR79 |
||
url( | ||
r"^(?P<snippet_id>\d+)/preview/$", | ||
SnippetPreviewView.as_view(), | ||
name=f"{Snippet._meta.app_label}_{Snippet._meta.model_name}_preview" | ||
) | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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
|
||
|
||
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 |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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.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(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__, (snippet_admin.AbstractSnippetAdmin,)) | ||
self.assertEqual(list_display, ('slug', 'name')) | ||
|
||
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) | ||
|
||
self.assertEqual( | ||
self.snippet_admin.__class__.__bases__, (ExtendedVersionAdminMixin, snippet_admin.AbstractSnippetAdmin) | ||
) | ||
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__()) |
Uh oh!
There was an error while loading. Please reload this page.