From db829684cbc1bf6581078809338d3dabb137f154 Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Sun, 5 Jan 2020 20:16:54 +0100 Subject: [PATCH] Update the way URLs are constructed, respecting articles w/o URLPath --- src/wiki/models/article.py | 10 ++++--- .../editsection/markdown_extensions.py | 10 +++++-- src/wiki/plugins/editsection/views.py | 9 ++++-- src/wiki/plugins/editsection/wiki_plugin.py | 2 +- tests/plugins/editsection/test_editsection.py | 28 +++++++++---------- 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/wiki/models/article.py b/src/wiki/models/article.py index f6ca503e..fbf55264 100644 --- a/src/wiki/models/article.py +++ b/src/wiki/models/article.py @@ -244,12 +244,14 @@ class Article(models.Model): def clear_cache(self): cache.delete(self.get_cache_key()) - def get_absolute_url(self): + def get_url_kwargs(self): urlpaths = self.urlpath_set.all() if urlpaths.exists(): - return urlpaths[0].get_absolute_url() - else: - return reverse('wiki:get', kwargs={'article_id': self.id}) + return {'path': urlpaths[0].path} + return {'article_id': self.id} + + def get_absolute_url(self): + return reverse('wiki:get', kwargs=self.get_url_kwargs()) class ArticleForObject(models.Model): diff --git a/src/wiki/plugins/editsection/markdown_extensions.py b/src/wiki/plugins/editsection/markdown_extensions.py index 116d6c23..00de569f 100644 --- a/src/wiki/plugins/editsection/markdown_extensions.py +++ b/src/wiki/plugins/editsection/markdown_extensions.py @@ -1,5 +1,6 @@ import re +from django.urls import reverse from markdown import Extension from markdown.treeprocessors import Treeprocessor from markdown.util import etree @@ -99,9 +100,12 @@ class EditSectionProcessor(Treeprocessor): link = etree.SubElement(child, 'a') link.text = settings.LINK_TEXT link.attrib["class"] = "article-edit-title-link" - link.attrib["href"] = self.markdown.article.get_absolute_url() \ - + "_plugin/editsection/" + location \ - + "/header/" + header_id + "/" + + # Build the URL + url_kwargs = self.md.article.get_url_kwargs() + url_kwargs['location'] = location + url_kwargs['header'] = header_id + link.attrib["href"] = reverse('wiki:editsection', kwargs=url_kwargs) def run(self, root): self.level = self.config.get('level')[0] diff --git a/src/wiki/plugins/editsection/views.py b/src/wiki/plugins/editsection/views.py index fe39d2f0..59296344 100644 --- a/src/wiki/plugins/editsection/views.py +++ b/src/wiki/plugins/editsection/views.py @@ -106,6 +106,11 @@ class EditSection(EditView): return e.config['location'] return None + def _redirect_to_article(self): + if self.urlpath: + return redirect('wiki:get', path=self.urlpath.path) + return redirect('wiki:get', article_id=self.article.id) + @method_decorator(get_article(can_write=True, not_locked=True)) def dispatch(self, request, article, *args, **kwargs): self.location = kwargs.pop('location', 0) @@ -127,7 +132,7 @@ class EditSection(EditView): request, " ".format(ERROR_SECTION_CHANGED, ERROR_TRY_AGAIN) ) - return redirect('wiki:get', path=self.urlpath.path) + return self._redirect_to_article() else: kwargs['content'] = request.session.get('editsection_content') self.orig_section = kwargs.get('content') @@ -164,4 +169,4 @@ class EditSection(EditView): " ".format(ERROR_ARTICLE_CHANGED, ERROR_TRY_AGAIN) ) - return redirect('wiki:get', path=self.urlpath.path) + return self._redirect_to_article() diff --git a/src/wiki/plugins/editsection/wiki_plugin.py b/src/wiki/plugins/editsection/wiki_plugin.py index 9df6396c..5cab89cc 100644 --- a/src/wiki/plugins/editsection/wiki_plugin.py +++ b/src/wiki/plugins/editsection/wiki_plugin.py @@ -10,7 +10,7 @@ class EditSectionPlugin(BasePlugin): slug = settings.SLUG urlpatterns = {'article': [ - url('^(?P[0-9-]+)/header/(?P
\w+)/$', + url(r'^(?P[0-9-]+)/header/(?P
\w+)/$', views.EditSection.as_view(), name='editsection'), ]} diff --git a/tests/plugins/editsection/test_editsection.py b/tests/plugins/editsection/test_editsection.py index b3789a31..bbe1e12f 100644 --- a/tests/plugins/editsection/test_editsection.py +++ b/tests/plugins/editsection/test_editsection.py @@ -26,13 +26,13 @@ class EditSectionTests(RequireRootArticleMixin, DjangoClientTestBase): title="TestEdit", content=TEST_CONTENT) output = urlpath.article.render() expected = ( - '(?s)' - 'Title 1\[edit\].*' - 'Title 2\[edit\].*' - 'Title 3\[edit\].*' - 'Title 4\[edit\].*' - 'Title 5\[edit\].*' - 'Title 6\[edit\].*' + r'(?s)' + r'Title 1\[edit\].*' + r'Title 2\[edit\].*' + r'Title 3\[edit\].*' + r'Title 4\[edit\].*' + r'Title 5\[edit\].*' + r'Title 6\[edit\].*' ) self.assertRegex(output, expected) @@ -85,13 +85,13 @@ class EditSectionEditTests(EditSectionEditBase, WebTestBase): }) self.submit('#id_save') expected = ( - '(?s)' - 'Title 1\[edit\].*' - 'Title 2\[edit\].*' - 'Header 1\[edit\].*' - 'Content of the new section.*' - 'Title 5\[edit\].*' - 'Title 6\[edit\].*' + r'(?s)' + r'Title 1\[edit\].*' + r'Title 2\[edit\].*' + r'Header 1\[edit\].*' + r'Content of the new section.*' + r'Title 5\[edit\].*' + r'Title 6\[edit\].*' ) self.assertRegex(self.last_response.content.decode('utf-8'), expected) -- 2.45.2