From 3fa0e7c7bc6b7f008c283831317d6be6b97bdfcd Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Sat, 14 Nov 2020 19:33:12 +0100 Subject: [PATCH] Replace url function with path (#647) --- README.rst | 4 ++++ changes/645.bugfix | 1 + djangocms_blog/admin.py | 7 +++---- djangocms_blog/models.py | 14 ++++++++------ djangocms_blog/settings.py | 8 ++++---- djangocms_blog/taggit_urls.py | 8 +++++--- djangocms_blog/urls.py | 22 +++++++++++----------- docs/features/home.rst | 10 +++++++--- docs/features/permalinks.rst | 12 ++++++++---- tests/test_utils/blog_urls.py | 22 +++++++++++----------- 10 files changed, 62 insertions(+), 46 deletions(-) create mode 100644 changes/645.bugfix diff --git a/README.rst b/README.rst index 15b21fd0..98bf8562 100644 --- a/README.rst +++ b/README.rst @@ -16,6 +16,9 @@ Supported django CMS versions: .. warning:: For Django<2.2, django CMS<3.7 versions support, use djangocms-blog 1.1x. +.. warning:: Version 1.2 introduce a breaking change for customized ``BLOG_PERMALINK_URLS``. + Check the `permalinks`_ documentation for update information. + ************ Installation ************ @@ -53,6 +56,7 @@ See DjangoPackages for an updated list https://www.djangopackages.com/packages/p .. _features documentation: http://djangocms-blog.readthedocs.io/en/latest/features/ .. _installation documentation: http://djangocms-blog.readthedocs.io/en/latest/installation.html +.. _permalinks: http://djangocms-blog.readthedocs.io/en/latest/features/permalinks.html .. _cmsplugin-filer migration documentation: http://djangocms-blog.readthedocs.io/en/latest/cmsplugin_filer.html diff --git a/changes/645.bugfix b/changes/645.bugfix new file mode 100644 index 00000000..bb75356b --- /dev/null +++ b/changes/645.bugfix @@ -0,0 +1 @@ +BREAKING CHANGE: Replace url() function with path() diff --git a/djangocms_blog/admin.py b/djangocms_blog/admin.py index ac61e517..f10bf69e 100644 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -5,14 +5,13 @@ from cms.models import CMSPlugin, ValidationError from django.apps import apps from django.conf import settings -from django.conf.urls import url from django.contrib import admin, messages from django.contrib.admin.options import InlineModelAdmin from django.contrib.sites.models import Site from django.db import models from django.db.models import signals from django.http import HttpResponseRedirect -from django.urls import reverse +from django.urls import path, reverse from django.utils import timezone from django.utils.translation import get_language_from_request, gettext_lazy as _, ngettext as __ from parler.admin import TranslatableAdmin @@ -283,8 +282,8 @@ def get_urls(self): Customize the modeladmin urls """ urls = [ - url( - r"^publish/([0-9]+)/$", + path( + "publish//$", self.admin_site.admin_view(self.publish_post), name="djangocms_blog_publish_article", ), diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 2ea5d920..eb19d598 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -362,19 +362,21 @@ def get_absolute_url(self, lang=None): else: current_date = self.date_created urlconf = get_setting("PERMALINK_URLS")[self.app_config.url_patterns] - if "" in urlconf: + if "" in urlconf: kwargs["year"] = current_date.year - if "" in urlconf: + if "" in urlconf: kwargs["month"] = "%02d" % current_date.month - if "" in urlconf: + if "" in urlconf: kwargs["day"] = "%02d" % current_date.day - if "" in urlconf: + if "" in urlconf: kwargs["slug"] = self.safe_translation_getter("slug", language_code=lang, any_language=True) # NOQA - if "" in urlconf: + if "" in urlconf: kwargs["category"] = category.safe_translation_getter( "slug", language_code=lang, any_language=True ) # NOQA - return reverse("%s:post-detail" % self.app_config.namespace, kwargs=kwargs) + return reverse( + "%s:post-detail" % self.app_config.namespace, kwargs=kwargs, current_app=self.app_config.namespace + ) def get_title(self): title = self.safe_translation_getter("meta_title", any_language=True) diff --git a/djangocms_blog/settings.py b/djangocms_blog/settings.py index 0c314ffb..25bc79c4 100644 --- a/djangocms_blog/settings.py +++ b/djangocms_blog/settings.py @@ -35,10 +35,10 @@ """ PERMALINKS_URLS = { # noqa - PERMALINK_TYPE_FULL_DATE: r"^(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/(?P\w[-\w]*)/$", - PERMALINK_TYPE_SHORT_DATE: r"^(?P\d{4})/(?P\d{1,2})/(?P\w[-\w]*)/$", - PERMALINK_TYPE_CATEGORY: r"^(?P\w[-\w]*)/(?P\w[-\w]*)/$", - PERMALINK_TYPE_SLUG: r"^(?P\w[-\w]*)/$", + PERMALINK_TYPE_FULL_DATE: "////", + PERMALINK_TYPE_SHORT_DATE: "///", + PERMALINK_TYPE_CATEGORY: "//", + PERMALINK_TYPE_SLUG: "/", } """ .. _PERMALINKS_URLS: diff --git a/djangocms_blog/taggit_urls.py b/djangocms_blog/taggit_urls.py index 4984f0da..77e93a07 100644 --- a/djangocms_blog/taggit_urls.py +++ b/djangocms_blog/taggit_urls.py @@ -1,5 +1,7 @@ -from django.conf.urls import include, url +# This file is used on divio cloud only during automatic setup +from django.conf.urls import include # pragma: no cover +from django.urls import path # pragma: no cover -urlpatterns = [ - url(r"^taggit_autosuggest/", include("taggit_autosuggest.urls")), +urlpatterns = [ # pragma: no cover + path("taggit_autosuggest/", include("taggit_autosuggest.urls")), ] diff --git a/djangocms_blog/urls.py b/djangocms_blog/urls.py index 235e7ee6..09d05358 100644 --- a/djangocms_blog/urls.py +++ b/djangocms_blog/urls.py @@ -1,4 +1,4 @@ -from django.conf.urls import url +from django.urls import path from .feeds import FBInstantArticles, LatestEntriesFeed, TagFeed from .settings import get_setting @@ -17,7 +17,7 @@ def get_urls(): details = [] for urlconf in urls.values(): details.append( - url(urlconf, PostDetailView.as_view(), name="post-detail"), + path(urlconf, PostDetailView.as_view(), name="post-detail"), ) return details @@ -27,13 +27,13 @@ def get_urls(): # module-level app_name attribute as per django 1.9+ app_name = "djangocms_blog" urlpatterns = [ - url(r"^$", PostListView.as_view(), name="posts-latest"), - url(r"^feed/$", LatestEntriesFeed(), name="posts-latest-feed"), - url(r"^feed/fb/$", FBInstantArticles(), name="posts-latest-feed-fb"), - url(r"^(?P\d{4})/$", PostArchiveView.as_view(), name="posts-archive"), - url(r"^(?P\d{4})/(?P\d{1,2})/$", PostArchiveView.as_view(), name="posts-archive"), - url(r"^author/(?P[\w\.@+-]+)/$", AuthorEntriesView.as_view(), name="posts-author"), - url(r"^category/(?P[\w\.@+-]+)/$", CategoryEntriesView.as_view(), name="posts-category"), - url(r"^tag/(?P[-\w]+)/$", TaggedListView.as_view(), name="posts-tagged"), - url(r"^tag/(?P[-\w]+)/feed/$", TagFeed(), name="posts-tagged-feed"), + path("", PostListView.as_view(), name="posts-latest"), + path("feed/", LatestEntriesFeed(), name="posts-latest-feed"), + path("feed/fb/", FBInstantArticles(), name="posts-latest-feed-fb"), + path("/", PostArchiveView.as_view(), name="posts-archive"), + path("//", PostArchiveView.as_view(), name="posts-archive"), + path("author//", AuthorEntriesView.as_view(), name="posts-author"), + path("category//", CategoryEntriesView.as_view(), name="posts-category"), + path("tag//", TaggedListView.as_view(), name="posts-tagged"), + path("tag//feed/", TagFeed(), name="posts-tagged-feed"), ] + detail_urls diff --git a/docs/features/home.rst b/docs/features/home.rst index 9f89a267..46b04001 100644 --- a/docs/features/home.rst +++ b/docs/features/home.rst @@ -31,12 +31,16 @@ To avoid this add the following settings to you project: ('category', _('Category')), ) BLOG_PERMALINK_URLS = { - 'full_date': r'^(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/(?P\w[-\w]*)/$', - 'short_date': r'^(?P\d{4})/(?P\d{1,2})/(?P\w[-\w]*)/$', - 'category': r'^(?P\w[-\w]*)/(?P\w[-\w]*)/$', + "full_date": "////", + "short_date: "///", + "category": "//", } Notice that the last permalink type is no longer present. Then, pick any of the three remaining permalink types in the layout section of the apphooks config linked ot the home page (at http://yoursite.com/admin/djangocms_blog/blogconfig/).' + +.. warning:: Version 1.2 introduce a breaking change as it drops ``url`` function in favour of ``path``. + If you have customized the urls as documented above you **must** update the custom urlconf to path-based + patterns. diff --git a/docs/features/permalinks.rst b/docs/features/permalinks.rst index 803af643..dc6a1179 100644 --- a/docs/features/permalinks.rst +++ b/docs/features/permalinks.rst @@ -20,10 +20,14 @@ like the following in the project settings: .. code-block:: python BLOG_PERMALINK_URLS = { - 'full_date': r'^(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/(?P\w[-\w]*)/$', - 'short_date': r'^(?P\d{4})/(?P\d{1,2})/(?P\w[-\w]*)/$', - 'category': r'^post/(?P\w[-\w]*)/(?P\w[-\w]*)/$', - 'slug': r'^post/(?P\w[-\w]*)/$', + "full_date": "////", + "short_date: "///", + "category": "//", + "slug": "/", } And change ``post/`` with the desired prefix. + +.. warning:: Version 1.2 introduce a breaking change as it drops ``url`` function in favour of ``path``. + If you have customized the urls as documented above you **must** update the custom urlconf to path-based + patterns. diff --git a/tests/test_utils/blog_urls.py b/tests/test_utils/blog_urls.py index 818b9d0a..e6370503 100644 --- a/tests/test_utils/blog_urls.py +++ b/tests/test_utils/blog_urls.py @@ -1,4 +1,4 @@ -from django.conf.urls import url +from django.urls import path from djangocms_blog.feeds import FBInstantArticles, LatestEntriesFeed, TagFeed from djangocms_blog.settings import get_setting @@ -17,7 +17,7 @@ def get_urls(): details = [] for urlconf in urls.values(): details.append( - url(urlconf, PostDetailView.as_view(), name="post-detail"), + path(urlconf, PostDetailView.as_view(), name="post-detail"), ) return details @@ -25,13 +25,13 @@ def get_urls(): detail_urls = get_urls() urlpatterns = [ - url(r"^latests/$", PostListView.as_view(), name="posts-latest"), - url(r"^feed/$", LatestEntriesFeed(), name="posts-latest-feed"), - url(r"^feed/fb/$", FBInstantArticles(), name="posts-latest-feed-fb"), - url(r"^(?P\d{4})/$", PostArchiveView.as_view(), name="posts-archive"), - url(r"^(?P\d{4})/(?P\d{1,2})/$", PostArchiveView.as_view(), name="posts-archive"), - url(r"^author/(?P[\w\.@+-]+)/$", AuthorEntriesView.as_view(), name="posts-author"), - url(r"^category/(?P[\w\.@+-]+)/$", CategoryEntriesView.as_view(), name="posts-category"), - url(r"^tag/(?P[-\w]+)/$", TaggedListView.as_view(), name="posts-tagged"), - url(r"^tag/(?P[-\w]+)/feed/$", TagFeed(), name="posts-tagged-feed"), + path("latests/", PostListView.as_view(), name="posts-latest"), + path("feed/", LatestEntriesFeed(), name="posts-latest-feed"), + path("feed/fb/", FBInstantArticles(), name="posts-latest-feed-fb"), + path("/", PostArchiveView.as_view(), name="posts-archive"), + path("//", PostArchiveView.as_view(), name="posts-archive"), + path("author//", AuthorEntriesView.as_view(), name="posts-author"), + path("category//", CategoryEntriesView.as_view(), name="posts-category"), + path("tag//", TaggedListView.as_view(), name="posts-tagged"), + path("tag//feed/", TagFeed(), name="posts-tagged-feed"), ] + detail_urls