Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: History URL button broken link on the Snippets change form #84

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
53 commits
Select commit Hold shift + click to select a range
6b142d1
Added missing field for form
adam-murray Oct 14, 2021
253cddc
Fixed form
adam-murray Oct 15, 2021
a6f8329
Fixed form save method
adam-murray Oct 15, 2021
291bc94
Added model property for snippet grouper
adam-murray Oct 15, 2021
1fe4fbf
Added fix for bug in demo
adam-murray Oct 15, 2021
b281e45
Update djangocms_snippet/forms.py
adam-murray Oct 15, 2021
3c8fa93
Added test case for verisoning state machine snippet/page
adam-murray Oct 15, 2021
7032daf
added test case for snippet version rendering
adam-murray Oct 15, 2021
6723e57
Fixed issue with form save method and updated tests.
adam-murray Oct 15, 2021
62d1c2f
Removed unused factories
adam-murray Oct 15, 2021
3451165
Updated plugin test cases
adam-murray Oct 15, 2021
bfc3b6e
Remove unused import in factories
adam-murray Oct 15, 2021
c779f3f
Simplified from unecessary helpers
adam-murray Oct 18, 2021
d0b975e
Removed unused helpers
adam-murray Oct 18, 2021
4d926f2
added test case for forms with commit=False
adam-murray Oct 18, 2021
f3609a6
updated test case
adam-murray Oct 18, 2021
5862325
Remove debugger
adam-murray Oct 18, 2021
c5a97cd
Refactored plugin tests
adam-murray Oct 18, 2021
3f1fbb2
Updated snippet fetch method to get return appropriate snipper versio…
adam-murray Oct 19, 2021
c9cb424
Linting
adam-murray Oct 19, 2021
8900700
Updated old comments
adam-murray Oct 19, 2021
3acf07f
Updated to check requests in models
adam-murray Oct 19, 2021
2b0331c
Updated snippet grouper snippet method
adam-murray Oct 19, 2021
333f5cb
remove unused code
adam-murray Oct 19, 2021
928c738
Updated endpoint logic
adam-murray Oct 19, 2021
3bc134b
Linting
adam-murray Oct 19, 2021
77a6080
Updated SnipeptGrouper snippet method, and added comments
adam-murray Oct 19, 2021
d8aad53
Add additional logic to prevent archived content being returned befor…
adam-murray Oct 20, 2021
59f3b6a
Updated Comment
adam-murray Oct 20, 2021
d67d31a
Updated Comment
adam-murray Oct 20, 2021
c31fca7
Comment Update tests/test_plugins.py
adam-murray Oct 20, 2021
6f579ea
Suggestions from code review
adam-murray Oct 20, 2021
7fd5e40
Updated comment in models.
adam-murray Oct 20, 2021
82962b4
Added filtering for correct version states in grouper snippet return …
adam-murray Oct 20, 2021
35456da
Update model so we don't have to pass the request object to the grouper
adam-murray Oct 20, 2021
4e4d840
Updated render method to prevent request object being passed to models.
adam-murray Oct 20, 2021
08c5939
removed unused property, added __str__ method back on grouper
adam-murray Oct 20, 2021
cc60686
Updated test criteria to reflect removal of property on plugin
adam-murray Oct 20, 2021
def0196
Fixed test criteria
adam-murray Oct 20, 2021
b180d62
Added solution and test case for form save method
adam-murray Oct 21, 2021
001c0bc
Merged in updates
adam-murray Oct 21, 2021
4da7894
Removed test which made incorrect assumption about versioning admin
adam-murray Oct 21, 2021
71b129b
Added back test, but with appropriate criteria
adam-murray Oct 21, 2021
41dac36
Linting
adam-murray Oct 21, 2021
01aefd3
Updated test criteria
adam-murray Oct 21, 2021
315292e
fix: form initialisation in read-only mode throwing error
adam-murray Oct 21, 2021
34a33ff
Merge branch 'support/django-cms-4.0.x' into bugfix/form-initialisati…
adam-murray Oct 21, 2021
4d5d133
fix: Updated comments explaining admin form test
adam-murray Oct 21, 2021
1a8e490
Updated urls
adam-murray Oct 25, 2021
743b6a3
Merge branch 'support/django-cms-4.0.x' into bugfix/history-url-missing
adam-murray Oct 25, 2021
6273120
Fix: Admin delete permission checks whether versioning is enabled, an…
adam-murray Oct 26, 2021
595cc6b
Added tests for new delete permission functionality
adam-murray Oct 26, 2021
f3670b1
fix: Added tests to cover accessing delete endpoint
adam-murray Oct 26, 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
36 changes: 15 additions & 21 deletions djangocms_snippet/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.db import models
from django.forms import Textarea

from cms.utils.permissions import get_model_permission_codename

from .cms_config import SnippetCMSAppConfig
from .forms import SnippetForm
from .models import Snippet
Expand Down Expand Up @@ -47,32 +49,24 @@ class Meta:
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),
),
]
] + super().get_urls()

def has_delete_permission(self, request, obj=None):
"""
When versioning is enabled, delete option is not available.
If versioning is disabled, it may be possible to delete, as long as a user also has add permissions, and they
are not in use.
"""
if obj and not djangocms_versioning_enabled:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will need agreement from the community, as they will also be using versioning the issue will still be raised.

A test will be needed to ensure that the delete button no longer shows / works.

Another test will also be needed for the other alternative of when versioning is not enabled.

return request.user.has_perm(
get_model_permission_codename(self.model, 'add'),
)
return False


admin.site.register(Snippet, SnippetAdmin)
66 changes: 66 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

class SnippetAdminTestCase(CMSTestCase):
def setUp(self):
self.superuser = self.get_superuser()
self.snippet = Snippet.objects.create(
name="Test Snippet",
slug="test-snippet",
html="<h1>This is a test</h1>",
snippet_grouper=SnippetGrouper.objects.create(),
)
self.snippet_version = Version.objects.create(content=self.snippet, created_by=self.superuser)
self.snippet_admin = snippet_admin.SnippetAdmin(Snippet, admin)
self.snippet_admin_request = RequestFactory().get("/admin/djangocms_snippet")
self.edit_url = reverse("admin:djangocms_snippet_snippet_change", args=(self.snippet.id,),)
self.delete_url = reverse("admin:djangocms_snippet_snippet_delete", args=(self.snippet.id,),)

@override_settings(DJANGOCMS_SNIPPET_VERSIONING_ENABLED=False)
def test_admin_list_display_without_versioning(self):
Expand Down Expand Up @@ -67,6 +71,68 @@ def test_admin_uses_form(self):
"""
self.assertEqual(self.snippet_admin.form, SnippetForm)

@override_settings(DJANGOCMS_SNIPPET_VERSIONING_ENABLED=True)
def test_admin_delete_button_disabled_versioning_enabled(self):
"""
If versioning is enabled, the delete button should not be rendered on the change form
"""
admin.site.unregister(Snippet)
reload(cms_config)
reload(snippet_admin)

with self.login_user_context(self.superuser):
response = self.client.get(self.edit_url)

self.assertNotContains(
response, '<a href="/en/admin/djangocms_snippet/snippet/1/delete/" class="deletelink">Delete</a>'
)

@override_settings(DJANGOCMS_SNIPPET_VERSIONING_ENABLED=False)
def test_admin_delete_button_available_versioning_disabled(self):
"""
If versioning is disabled, the delete button should be rendered on the change form
"""
admin.site.unregister(Snippet)
reload(cms_config)
reload(snippet_admin)

with self.login_user_context(self.superuser):
response = self.client.get(self.edit_url)

self.assertContains(
response, '<a href="/en/admin/djangocms_snippet/snippet/1/delete/" class="deletelink">Delete</a>'
)

@override_settings(DJANGOCMS_SNIPPET_VERSIONING_ENABLED=True)
def test_admin_delete_endpoint_inaccessible_versioning_enabled(self):
"""
If versioning is enabled, the delete endpoint should not be accessible.
"""
admin.site.unregister(Snippet)
reload(cms_config)
reload(snippet_admin)

with self.login_user_context(self.superuser):
response = self.client.post(self.delete_url)

# The delete endpoint should return a 403 forbidden if we try to access it with versioning enabled
self.assertEqual(response.status_code, 403)

@override_settings(DJANGOCMS_SNIPPET_VERSIONING_ENABLED=False)
def test_admin_delete_endpoint_accessible_versioning_disabled(self):
"""
If versioning is disabled, the delete endpoint should be accessible.
"""
admin.site.unregister(Snippet)
reload(cms_config)
reload(snippet_admin)

with self.login_user_context(self.superuser):
response = self.client.post(self.delete_url)

# The delete endpoint should return a 200 success if we try to access it with versioning disabled
self.assertEqual(response.status_code, 200)


class SnippetAdminFormTestCase(CMSTestCase):
def setUp(self):
Expand Down