-
Notifications
You must be signed in to change notification settings - Fork 51
feat: Preview icon renders content in read only #102
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 15 commits into
django-cms:support/django-cms-4.0.x
from
Bernardvdv:feature_preview_icon_render_read_only_content
Feb 3, 2022
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
339281e
Preview icon renders content in read only mode
Bernardvdv f5f0a55
Updated method comment
Bernardvdv 59dcb78
Updated permissions check to show snippet in edit mode if owner is lo…
Bernardvdv a0ff1fd
Merge branch 'support/django-cms-4.0.x' into feature_preview_icon_ren…
Bernardvdv 833a0d1
Removed unused list actions method
Bernardvdv 01841fc
Merge remote-tracking branch 'origin/feature_preview_icon_render_read…
Bernardvdv fa9dbe4
Added get paremeter to know when user selected preview
Bernardvdv a9a3d19
Added custom preview endpoint to render form in a read only view
Bernardvdv 1621568
isort and flake errors
Bernardvdv 837b19a
isort spacing
Bernardvdv dbc49a6
Added reference to django for the solution
Bernardvdv 1377bb9
Added a test to ensure preview endpoint fields are rendered read only
Bernardvdv d12521e
Removed unused imports
Bernardvdv 8decb3a
Removed extra blank lines
Bernardvdv 0c47c80
Update djangocms_snippet/admin.py
Bernardvdv 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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
from django.conf import settings | ||
from django.conf.urls import url | ||
from django.contrib import admin | ||
from django.contrib.admin import helpers | ||
from django.contrib.admin.exceptions import DisallowedModelAdminToField | ||
from django.contrib.admin.utils import flatten_fieldsets, unquote | ||
from django.db import models | ||
from django.forms import Textarea | ||
from django.utils.translation import gettext as _ | ||
|
||
from cms.utils.permissions import get_model_permission_codename | ||
|
||
from .cms_config import SnippetCMSAppConfig | ||
from .forms import SnippetForm | ||
from .models import Snippet | ||
from .views import SnippetPreviewView | ||
|
||
|
||
# Use the version mixin if djangocms-versioning is installed and enabled | ||
|
@@ -18,11 +21,15 @@ | |
|
||
try: | ||
from djangocms_versioning.admin import ExtendedVersionAdminMixin | ||
|
||
if djangocms_versioning_enabled: | ||
snippet_admin_classes.insert(0, ExtendedVersionAdminMixin) | ||
except ImportError: | ||
djangocms_versioning_enabled = False | ||
|
||
TO_FIELD_VAR = '_to_field' | ||
IS_POPUP_VAR = '_popup' | ||
|
||
|
||
class SnippetAdmin(*snippet_admin_classes): | ||
list_display = ('name',) | ||
|
@@ -73,15 +80,76 @@ def get_list_display_links(self, request, list_display): | |
self.list_display_links = (None,) | ||
return self.list_display_links | ||
|
||
def preview_view(self, request, snippet_id=None, form_url='', extra_context=None): | ||
""" | ||
Custom preview endpoint to display a change form in read only mode | ||
Solution based on django changeform view implementation | ||
https://github.com/django/django/blob/main/django/contrib/admin/options.py#L1553 | ||
Bernardvdv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
to_field = request.POST.get(TO_FIELD_VAR, request.GET.get(TO_FIELD_VAR)) | ||
|
||
if to_field and not self.to_field_allowed(request, to_field): | ||
raise DisallowedModelAdminToField("The field %s cannot be referenced." % to_field) | ||
|
||
model = self.model | ||
opts = model._meta | ||
|
||
obj = self.get_object(request, unquote(snippet_id), to_field) | ||
|
||
if obj is None: | ||
return self._get_obj_does_not_exist_redirect(request, opts, snippet_id) | ||
|
||
fieldsets = self.get_fieldsets(request, obj) | ||
ModelForm = self.get_form( | ||
request, obj, change=False, fields=flatten_fieldsets(fieldsets) | ||
) | ||
form = ModelForm(instance=obj) | ||
formsets, inline_instances = self._create_formsets(request, obj, change=True) | ||
|
||
readonly_fields = flatten_fieldsets(fieldsets) | ||
|
||
adminForm = helpers.AdminForm( | ||
form, | ||
list(fieldsets), | ||
# Clear prepopulated fields on a view-only form to avoid a crash. | ||
{}, | ||
readonly_fields, | ||
model_admin=self) | ||
media = self.media + adminForm.media | ||
|
||
inline_formsets = self.get_inline_formsets(request, formsets, inline_instances, obj) | ||
for inline_formset in inline_formsets: | ||
media = media + inline_formset.media | ||
|
||
title = _('View %s') | ||
context = { | ||
**self.admin_site.each_context(request), | ||
'title': title % opts.verbose_name, | ||
'subtitle': str(obj) if obj else None, | ||
'adminform': adminForm, | ||
'object_id': snippet_id, | ||
'original': obj, | ||
'is_popup': IS_POPUP_VAR in request.POST or IS_POPUP_VAR in request.GET, | ||
'to_field': to_field, | ||
'media': media, | ||
'inline_admin_formsets': inline_formsets, | ||
'errors': [], | ||
'preserved_filters': self.get_preserved_filters(request), | ||
} | ||
|
||
context.update(extra_context or {}) | ||
|
||
return self.render_change_form(request, context, add=False, change=False, obj=obj, form_url=form_url) | ||
|
||
def get_urls(self): | ||
info = self.model._meta.app_label, self.model._meta.model_name | ||
return [ | ||
url( | ||
r"^(?P<snippet_id>\d+)/preview/$", | ||
self.admin_site.admin_view(SnippetPreviewView.as_view()), | ||
name="{}_{}_preview".format(*info), | ||
), | ||
] + super().get_urls() | ||
url( | ||
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 re-formatting is strange, how it was before was correct. |
||
r"^(?P<snippet_id>\d+)/preview/$", | ||
self.admin_site.admin_view(self.preview_view), | ||
name="{}_{}_preview".format(*info), | ||
), | ||
] + super().get_urls() | ||
|
||
def has_delete_permission(self, request, obj=None): | ||
""" | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are defined by django, we should reuse that definition:
from django.contrib.admin.options import TO_FIELD_VAR