Skip to content

Commit

Permalink
feat: Add configuration to manage redirect on publish (#358)
Browse files Browse the repository at this point in the history
* Remove remove_published_where

* Fix language issue with preview links

* Add setuptools to requirements for

* feat: Add configurable redirect on publish

* fix lint errors

* Fix tests

* Test redirects

* Extend tests to django-cms@develop-4

* Update tests

* Update tests.yml

* Use Py3.11 for latest cms branch

* Redirects for unpublish

* Update docs

* Update toolbar test

* Fix toolbar tests prt 2
  • Loading branch information
fsbraun authored Dec 28, 2023
1 parent 0d5b461 commit b099c9a
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 26 deletions.
40 changes: 37 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ 3.9, "3.10", "3.11", "3.12" ] # latest release minus two
python-version: [ 3.9, "3.10", "3.11", ] # latest release minus two
requirements-file: [
dj32_cms41.txt,
dj40_cms41.txt,
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ 3.9, "3.10", "3.11", "3.12" ] # latest release minus two
python-version: [ 3.9, "3.10", "3.11", ] # latest release minus two
requirements-file: [
dj32_cms41.txt,
dj40_cms41.txt,
Expand Down Expand Up @@ -90,7 +90,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ 3.9, "3.10", "3.11", "3.12" ] # latest release minus two
python-version: [ 3.9, "3.10", "3.11", ] # latest release minus two
requirements-file: [
dj32_cms41.txt,
dj40_cms41.txt,
Expand Down Expand Up @@ -128,3 +128,37 @@ jobs:

- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v2

cms-develop-sqlite:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version: ['3.11']
requirements-file: ['dj42_cms41.txt']
cms-version: [
'https://github.com/django-cms/django-cms/archive/develop-4.tar.gz'
]
os: [
ubuntu-20.04,
]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}

uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r tests/requirements/${{ matrix.requirements-file }}
pip install ${{ matrix.cms-version }}
python setup.py install
- name: Run coverage
run: coverage run setup.py test

- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v2
34 changes: 25 additions & 9 deletions djangocms_versioning/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,21 +947,33 @@ def publish_view(self, request, object_id):
request, self.model._meta, object_id
)

if conf.ON_PUBLISH_REDIRECT in ("preview", "published"):
redirect_url=get_preview_url(version.content)
else:
redirect_url=version_list_url(version.content)

if not version.can_be_published():
self.message_user(request, _("Version cannot be published"), messages.ERROR)
return redirect(version_list_url(version.content))
return redirect(redirect_url)
try:
version.check_publish(request.user)
except ConditionFailed as e:
self.message_user(request, force_str(e), messages.ERROR)
return redirect(version_list_url(version.content))
return redirect(redirect_url)

# Publish the version
version.publish(request.user)

# Display message
self.message_user(request, _("Version published"))
# Redirect
return redirect(version_list_url(version.content))

# Redirect to published?
if conf.ON_PUBLISH_REDIRECT == "published":
redirect_url = None
if hasattr(version.content, "get_absolute_url"):
redirect_url = version.content.get_absolute_url() or redirect_url

return redirect(redirect_url)

def unpublish_view(self, request, object_id):
"""Unpublishes the specified version and redirects back to the
Expand All @@ -974,16 +986,21 @@ def unpublish_view(self, request, object_id):
request, self.model._meta, object_id
)

if conf.ON_PUBLISH_REDIRECT in ("preview", "published"):
redirect_url=get_preview_url(version.content)
else:
redirect_url=version_list_url(version.content)

if not version.can_be_unpublished():
self.message_user(
request, _("Version cannot be unpublished"), messages.ERROR
)
return redirect(version_list_url(version.content))
return redirect(redirect_url)
try:
version.check_unpublish(request.user)
except ConditionFailed as e:
self.message_user(request, force_str(e), messages.ERROR)
return redirect(version_list_url(version.content))
return redirect(redirect_url)

if request.method != "POST":
context = {
Expand Down Expand Up @@ -1016,7 +1033,7 @@ def unpublish_view(self, request, object_id):
# Display message
self.message_user(request, _("Version unpublished"))
# Redirect
return redirect(version_list_url(version.content))
return redirect(redirect_url)

def _get_edit_redirect_version(self, request, version):
"""Helper method to get the latest draft or create one if one does not exist."""
Expand Down Expand Up @@ -1202,11 +1219,10 @@ def compare_view(self, request, object_id):
)
else:
v2_preview_url = get_preview_url(v2.content)
v2_preview_url = add_url_parameters(v2_preview_url, **persist_params)
context.update(
{
"v2": v2,
"v2_preview_url": v2_preview_url,
"v2_preview_url": add_url_parameters(v2_preview_url, **persist_params),
}
)
return TemplateResponse(
Expand Down
2 changes: 1 addition & 1 deletion djangocms_versioning/cms_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_nodes(self, request):

if page not in visible_pages_for_user:
# The page is restricted for the user.
# Therefore we avoid adding it to the menu.
# Therefore, we avoid adding it to the menu.
continue

version = page_content.versions.all()[0]
Expand Down
2 changes: 1 addition & 1 deletion djangocms_versioning/cms_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def get_page_content(self, language=None):
return get_latest_admin_viewable_content(self.page, language=language)

def populate(self):
self.page = self.request.current_page or getattr(self.toolbar.obj, "page", None)
self.page = self.request.current_page
self.title = self.get_page_content() if self.page else None
self.permissions_activated = get_cms_setting("PERMISSION")

Expand Down
5 changes: 5 additions & 0 deletions djangocms_versioning/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@
EMAIL_NOTIFICATIONS_FAIL_SILENTLY = getattr(
settings, "EMAIL_NOTIFICATIONS_FAIL_SILENTLY", False
)

ON_PUBLISH_REDIRECT = getattr(
settings, "DJANGOCMS_VERISONING_ON_PUBLISH_REDIRECT", "published"
)
# Allowed values: "versions", "published", "preview"
4 changes: 4 additions & 0 deletions djangocms_versioning/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.db import models
from django.template.loader import render_to_string
from django.utils.encoding import force_str
from django.utils.translation import get_language

from . import versionables
from .conf import EMAIL_NOTIFICATIONS_FAIL_SILENTLY
Expand Down Expand Up @@ -272,6 +273,9 @@ def get_preview_url(content_obj: models.Model, language: typing.Union[str, None]
if versionable.preview_url:
return versionable.preview_url(content_obj)
if is_editable_model(content_obj.__class__):
if not language:
# Use language field is content object has one to determine the language
language = getattr(content_obj, "language", get_language())
url = get_object_preview_url(content_obj, language=language)
else:
# Or else, the standard change view should be used
Expand Down
25 changes: 25 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,28 @@ Settings for djangocms Versioning
will fail.


.. py:attribute:: DJANGOCMS_VERSIONING_ON_PUBLISH_REDIRECT
Defaults to ``"published"``

.. versionadded:: 2.0

Before version 2.0 the behavior was always ``"versions"``.

This setting determines what happens after publication/unpublication of a
content object. Three options exist:

* ``"versions"``: The user will be redirected to a version overview of
the current object. This is particularly useful for advanced users who
need to keep a regular overview on the existing versions.

* ``"published"``: The user will be redirected to the content object on
the site. Its URL is determined by calling ``.get_absolute_url()`` on
the content object. If does not have an absolute url or the object was
unpublished the user is redirected to the object's preview endpoint.
This is particularly useful if users only want to interact with versions
if necessary.

* ``"preview"``: The user will be redirected to the content object's
preview endpoint.

2 changes: 2 additions & 0 deletions tests/requirements/dj32_cms41.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-r requirements_base.txt

django-cms>=4.1.0rc2

Django>=3.2,<4.0
django-classy-tags
django-fsm>=2.6
Expand Down
2 changes: 2 additions & 0 deletions tests/requirements/dj40_cms41.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-r requirements_base.txt

django-cms>=4.1.0rc2

Django>=4.0,<4.1
django-classy-tags
django-fsm>=2.6
Expand Down
2 changes: 2 additions & 0 deletions tests/requirements/dj41_cms41.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-r requirements_base.txt

django-cms>=4.1.0rc2

Django>=4.1,<4.2
django-classy-tags
django-fsm>=2.6
Expand Down
2 changes: 2 additions & 0 deletions tests/requirements/dj42_cms41.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-r requirements_base.txt

django-cms>=4.1.0rc2

Django>=4.2,<5
django-classy-tags
django-fsm>=2.6
Expand Down
3 changes: 1 addition & 2 deletions tests/requirements/requirements_base.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
setuptools
beautifulsoup4
coverage
django-app-helper
Expand All @@ -14,5 +15,3 @@ psycopg2
setuptools

djangocms-text-ckeditor>=5.1.2
# Unreleased django-cms 4.0 compatible packages
django-cms>=4.1.0rc2
Loading

0 comments on commit b099c9a

Please sign in to comment.