From c35b09da5e2c99136cbe6d4202515c15d351e8c6 Mon Sep 17 00:00:00 2001 From: Raffaele Salmaso Date: Sat, 24 Feb 2018 12:31:43 +0100 Subject: [PATCH] Use WikiSite for wiki urls --- docs/customization.rst | 31 +++++++++++ docs/installation.rst | 3 +- docs/release_notes.rst | 2 + src/wiki/urls.py | 6 +++ testproject/testproject/urls.py | 3 +- tests/core/test_sites.py | 92 +++++++++++++++++++++++++++++++++ tests/testdata/urls.py | 3 +- 7 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 tests/core/test_sites.py diff --git a/docs/customization.rst b/docs/customization.rst index 64785dba..c4c99ecd 100644 --- a/docs/customization.rst +++ b/docs/customization.rst @@ -41,4 +41,35 @@ wiki. Add the following as ``wiki/base.html`` somewhere in your {% endblock %} +Site +---- +You can override default django-wiki ``wiki.sites.site`` urls/views site implementation +with your own: override by setting the :attr:`~.WikiConfig.default_site` attribute +of a custom ``AppConfig`` to the dotted import path of either a ``WikiSite`` subclass +or a callable that returns a site instance. + +.. code-block:: python + # myproject/sites.py + + from wiki.sites import WikiSite + + class MyWikiSite(admin.WikiSite): + ... + +.. code-block:: python + # myproject/apps.py + + from wiki.apps import WikiConfig + + class MyWikiConfig(WikiConfig): + default_site = 'myproject.sites.MyWikiSite' + +.. code-block:: python + # myproject/settings.py + + INSTALLED_APPS = [ + ... + 'myproject.apps.MyWikiConfig', # replaces 'wiki.apps.WikiConfig' + ... + ] diff --git a/docs/installation.rst b/docs/installation.rst index 8623c13e..043606e4 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -160,10 +160,9 @@ following lines at the end of your project's ``urls.py``. .. code-block:: python - from wiki.urls import get_pattern as get_wiki_pattern urlpatterns += [ url(r'^notifications/', include('django_nyt.urls')), - url(r'', get_wiki_pattern()) + url(r'', include('wiki.urls')) ] diff --git a/docs/release_notes.rst b/docs/release_notes.rst index 703eaaaa..8493537e 100644 --- a/docs/release_notes.rst +++ b/docs/release_notes.rst @@ -19,12 +19,14 @@ Added ~~~~~ * Django 2 support :url-issue:`755` (Raffaele Salmaso & Mads Jensen) + * Added ``wiki.sites.WikiSite`` for easy customization :url-issue:`827` Changed ~~~~~~~ * Use full path names for ``MARKDOWN_KWARGS['extensions']`` as short names support wil be removed in ``Markdown 2.7`` :url-issue:`823` + * Support for ``include('wiki.urls')`` for urls instantiation :url-issue:`827` Fixed ~~~~~ diff --git a/src/wiki/urls.py b/src/wiki/urls.py index de7d55ae..b0ee7b9a 100644 --- a/src/wiki/urls.py +++ b/src/wiki/urls.py @@ -2,9 +2,15 @@ from django.utils.module_loading import import_string from wiki.compat import include, url from wiki.conf import settings from wiki.core.plugins import registry +from wiki import sites from wiki.views import accounts, article, deleted_list +urlpatterns = [ + url(r'^', sites.site.urls), +] + + class WikiURLPatterns: """ diff --git a/testproject/testproject/urls.py b/testproject/testproject/urls.py index 026981cd..a75dedd0 100644 --- a/testproject/testproject/urls.py +++ b/testproject/testproject/urls.py @@ -4,7 +4,6 @@ from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.http.response import HttpResponse from django.views.static import serve as static_serve from wiki.compat import include, url -from wiki.urls import get_pattern as get_wiki_pattern admin.autodiscover() @@ -22,7 +21,7 @@ if settings.DEBUG: urlpatterns += [ url(r'^notify/', include('django_nyt.urls')), - url(r'', get_wiki_pattern()) + url(r'', include('wiki.urls')), ] handler500 = 'testproject.views.server_error' diff --git a/tests/core/test_sites.py b/tests/core/test_sites.py new file mode 100644 index 00000000..3bd41b13 --- /dev/null +++ b/tests/core/test_sites.py @@ -0,0 +1,92 @@ +from importlib import reload + +from django.contrib.sites.models import Site +from django.test.testcases import TestCase +from wiki import sites, urls +from wiki.apps import WikiConfig +from wiki.compat import include, url +from wiki.models import Article, URLPath + +from ..base import wiki_override_settings + + +class WikiCustomSite(sites.WikiSite): + def get_article_urls(self): + urlpatterns = [ + url('^some-prefix/(?P[0-9]+)/$', self.article_view, name='get'), + ] + return urlpatterns + + def get_article_path_urls(self): + urlpatterns = [ + url('^some-other-prefix/(?P.+/|)$', self.article_view, name='get'), + ] + return urlpatterns + + +class WikiCustomConfig(WikiConfig): + default_site = "tests.core.test_sites.WikiCustomSite" + + +urlpatterns = [ + url(r'^notify/', include('django_nyt.urls')), + url(r'', include('wiki.urls')), +] + + +@wiki_override_settings( + INSTALLED_APPS=[ + 'tests.testdata', + 'django.contrib.auth.apps.AuthConfig', + 'django.contrib.contenttypes.apps.ContentTypesConfig', + 'django.contrib.sessions.apps.SessionsConfig', + 'django.contrib.admin.apps.AdminConfig', + 'django.contrib.humanize.apps.HumanizeConfig', + 'django.contrib.sites.apps.SitesConfig', + 'django_nyt.apps.DjangoNytConfig', + 'mptt', + 'sekizai', + 'sorl.thumbnail', + 'tests.core.test_sites.WikiCustomConfig', + 'wiki.plugins.attachments.apps.AttachmentsConfig', + 'wiki.plugins.notifications.apps.NotificationsConfig', + 'wiki.plugins.images.apps.ImagesConfig', + 'wiki.plugins.macros.apps.MacrosConfig', + 'wiki.plugins.globalhistory.apps.GlobalHistoryConfig', + ], + ROOT_URLCONF='tests.core.test_sites', +) +class CustomWikiSiteTest(TestCase): + def setUp(self): + # Reload wiki.urls since it may have already been instantiated by another test app. + self._old_site = sites.site + sites.site = sites.DefaultWikiSite() + reload(urls) + + def tearDown(self): + sites.site = self._old_site + reload(urls) + + def test_use_custom_wiki_site(self): + self.assertEqual(sites.site.__class__.__name__, 'WikiCustomSite') + + def test_get_absolute_url_if_urlpath_set_is_not_exists__no_root_urlconf(self): + a = Article.objects.create() + + self.assertEqual(a.get_absolute_url(), '/some-prefix/1/') + + def test_get_absolute_url_if_urlpath_set_is_exists__no_root_urlconf(self): + a1 = Article.objects.create() + s1 = Site.objects.create(domain="something.com", name="something.com") + u1 = URLPath.objects.create(article=a1, site=s1) + + a2 = Article.objects.create() + s2 = Site.objects.create(domain="somethingelse.com", name="somethingelse.com") + URLPath.objects.create( + article=a2, + site=s2, + parent=u1, + slug='test_slug' + ) + + self.assertEqual(a2.get_absolute_url(), '/some-other-prefix/test_slug/') diff --git a/tests/testdata/urls.py b/tests/testdata/urls.py index eb1c8163..9dc28bc7 100644 --- a/tests/testdata/urls.py +++ b/tests/testdata/urls.py @@ -2,7 +2,6 @@ from django.conf import settings from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns from wiki.compat import include, url -from wiki.urls import get_pattern as get_wiki_pattern urlpatterns = [ @@ -22,5 +21,5 @@ if settings.DEBUG: urlpatterns += [ url(r'^django_functest/', include('django_functest.urls')), url(r'^notify/', include('django_nyt.urls')), - url(r'', get_wiki_pattern()) + url(r'', include('wiki.urls')), ] -- 2.45.2