~netlandish/django-wiki

db829684cbc1bf6581078809338d3dabb137f154 — Benjamin Bach 4 years ago d192873
Update the way URLs are constructed, respecting articles w/o URLPath
M src/wiki/models/article.py => src/wiki/models/article.py +6 -4
@@ 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):

M src/wiki/plugins/editsection/markdown_extensions.py => src/wiki/plugins/editsection/markdown_extensions.py +7 -3
@@ 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]

M src/wiki/plugins/editsection/views.py => src/wiki/plugins/editsection/views.py +7 -2
@@ 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()

M src/wiki/plugins/editsection/wiki_plugin.py => src/wiki/plugins/editsection/wiki_plugin.py +1 -1
@@ 10,7 10,7 @@ class EditSectionPlugin(BasePlugin):

    slug = settings.SLUG
    urlpatterns = {'article': [
        url('^(?P<location>[0-9-]+)/header/(?P<header>\w+)/$',
        url(r'^(?P<location>[0-9-]+)/header/(?P<header>\w+)/$',
            views.EditSection.as_view(),
            name='editsection'),
    ]}

M tests/plugins/editsection/test_editsection.py => tests/plugins/editsection/test_editsection.py +14 -14
@@ 26,13 26,13 @@ class EditSectionTests(RequireRootArticleMixin, DjangoClientTestBase):
                                         title="TestEdit", content=TEST_CONTENT)
        output = urlpath.article.render()
        expected = (
            '(?s)'
            'Title 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-0-0/header/T1/">\[edit\]</a>.*'
            'Title 2<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-1-0/header/T2/">\[edit\]</a>.*'
            'Title 3<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-2-0/header/T3/">\[edit\]</a>.*'
            'Title 4<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-2-1/header/T4/">\[edit\]</a>.*'
            'Title 5<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-3-0/header/T5/">\[edit\]</a>.*'
            'Title 6<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-0-0/header/T6/">\[edit\]</a>.*'
            r'(?s)'
            r'Title 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-0-0/header/T1/">\[edit\]</a>.*'
            r'Title 2<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-1-0/header/T2/">\[edit\]</a>.*'
            r'Title 3<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-2-0/header/T3/">\[edit\]</a>.*'
            r'Title 4<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-2-1/header/T4/">\[edit\]</a>.*'
            r'Title 5<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-3-0/header/T5/">\[edit\]</a>.*'
            r'Title 6<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-0-0/header/T6/">\[edit\]</a>.*'
        )
        self.assertRegex(output, expected)



@@ 85,13 85,13 @@ class EditSectionEditTests(EditSectionEditBase, WebTestBase):
        })
        self.submit('#id_save')
        expected = (
            '(?s)'
            'Title 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-0-0/header/T1/">\[edit\]</a>.*'
            'Title 2<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-1-0/header/T2/">\[edit\]</a>.*'
            'Header 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-0-0/header/H1/">\[edit\]</a>.*'
            'Content of the new section.*'
            'Title 5<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-1-0/header/T5/">\[edit\]</a>.*'
            'Title 6<a class="article-edit-title-link" href="/testedit/_plugin/editsection/3-0-0/header/T6/">\[edit\]</a>.*'
            r'(?s)'
            r'Title 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-0-0/header/T1/">\[edit\]</a>.*'
            r'Title 2<a class="article-edit-title-link" href="/testedit/_plugin/editsection/1-1-0/header/T2/">\[edit\]</a>.*'
            r'Header 1<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-0-0/header/H1/">\[edit\]</a>.*'
            r'Content of the new section.*'
            r'Title 5<a class="article-edit-title-link" href="/testedit/_plugin/editsection/2-1-0/header/T5/">\[edit\]</a>.*'
            r'Title 6<a class="article-edit-title-link" href="/testedit/_plugin/editsection/3-0-0/header/T6/">\[edit\]</a>.*'
        )
        self.assertRegex(self.last_response.content.decode('utf-8'), expected)