fixed add_page and edit_page URL definitions to work with "/" path.

This commit is contained in:
2019-01-01 18:30:52 +01:00
parent 12930c2ec6
commit 5ed8976074
2 changed files with 25 additions and 21 deletions

View File

@@ -14,7 +14,7 @@ from . import models, forms
class WYSIWYGEditorMixin(PermissionRequiredMixin): class WYSIWYGEditorMixin(PermissionRequiredMixin):
""" """
A view to update the Content-Security-Policy for the WYSIWYG editor. A view to update the Content-Security-Policy for the WYSIWYG editor.
Since it is only used in edit forms, it extends the PermissionRequiredMixin. Since it is only used in edit forms, it extends the PermissionRequiredMixin.
""" """
@csp_update(SCRIPT_SRC="'unsafe-eval'") @csp_update(SCRIPT_SRC="'unsafe-eval'")
@@ -160,8 +160,8 @@ class ArticleForm(WYSIWYGEditorMixin, generic.UpdateView):
def get_object(self, queryset=None): def get_object(self, queryset=None):
"""Get the Article or create a new one if no id has been provided. """Get the Article or create a new one if no id has been provided.
:param queryset: Get the single item from this filtered queryset. :param queryset: Get the single item from this filtered queryset.
:return: :return:
""" """
queryset = queryset or self.get_queryset() queryset = queryset or self.get_queryset()
if self.kwargs.get('pk'): if self.kwargs.get('pk'):
@@ -170,7 +170,7 @@ class ArticleForm(WYSIWYGEditorMixin, generic.UpdateView):
class PageAddForm(WYSIWYGEditorMixin, generic.CreateView): class PageAddForm(WYSIWYGEditorMixin, generic.CreateView):
""" Renders an Form to create a new page for users with conforming """ Renders an Form to create a new page for users with conforming
permissions.""" permissions."""
form_class = forms.PageForm form_class = forms.PageForm
@@ -179,13 +179,16 @@ class PageAddForm(WYSIWYGEditorMixin, generic.CreateView):
def get_initial(self): def get_initial(self):
"""Try to get the path of the parent page as initial data.""" """Try to get the path of the parent page as initial data."""
path = os.path.splitext(self.kwargs['path'])[0] if self.kwargs['path']:
if path.startswith('/'): path = os.path.splitext(self.kwargs['path'])[0]
path = path[1:] if path.startswith('/'):
if path.endswith('/'): path = path[1:]
path = path[:-1] if path.endswith('/'):
parent = models.Page.objects.get(path=path) path = path[:-1]
return {'parent': parent} parent = models.Page.objects.get(path=path)
return {'parent': parent}
else:
return None
class PageEditForm(WYSIWYGEditorMixin, generic.UpdateView): class PageEditForm(WYSIWYGEditorMixin, generic.UpdateView):
@@ -197,14 +200,15 @@ class PageEditForm(WYSIWYGEditorMixin, generic.UpdateView):
def get_object(self, queryset=None): def get_object(self, queryset=None):
""" Get the path from the URL and fetch the corresponding page. """ Get the path from the URL and fetch the corresponding page.
First get the path wihout fileextentsion leading or trailing slashes, First get the path wihout fileextentsion leading or trailing slashes,
then search in the database if such a page exists. then search in the database if such a page exists.
:param queryset: Get the single item from this filtered queryset. :param queryset: Get the single item from this filtered queryset.
:return: :return:
""" """
path = os.path.splitext(self.kwargs['path'])[0] path = self.kwargs['path'] or 'index.html'
path = os.path.splitext(path)[0]
queryset = queryset or self.get_queryset() queryset = queryset or self.get_queryset()
if path.startswith('/'): if path.startswith('/'):
path = path[1:] path = path[1:]
@@ -222,7 +226,7 @@ class PageHtml(generic.DetailView):
"""Get the page content from the db that equals the given URL. """Get the page content from the db that equals the given URL.
:param queryset: Get the single item from this filtered queryset. :param queryset: Get the single item from this filtered queryset.
:return: :return:
""" """
queryset = queryset or self.get_queryset() queryset = queryset or self.get_queryset()
try: try:
@@ -248,10 +252,10 @@ class PagePdf(generic.DeleteView):
model = models.Page model = models.Page
def get_object(self, queryset=None): def get_object(self, queryset=None):
"""Get the PDF page from the db that equals the given URL. """Get the PDF page from the db that equals the given URL.
:param queryset: Get the single item from this filtered queryset. :param queryset: Get the single item from this filtered queryset.
:return: models.Page object or raise a 404 if not found. :return: models.Page object or raise a 404 if not found.
""" """
queryset = queryset or self.get_queryset() queryset = queryset or self.get_queryset()
try: try:
@@ -288,8 +292,8 @@ class StartPage(generic.TemplateView):
def get_context_data(self): def get_context_data(self):
""" Adds recent ariticles and recent comments to the context. """ Adds recent ariticles and recent comments to the context.
:return: array() with the context data :return: array() with the context data
""" """
page = models.Page.objects.get(slug='index') page = models.Page.objects.get(slug='index')
recent_comment_list = comments.get_model().objects.filter( recent_comment_list = comments.get_model().objects.filter(

View File

@@ -29,13 +29,13 @@ sitemaps = {
urlpatterns = [ # Ignore PyLintBear (C0103) urlpatterns = [ # Ignore PyLintBear (C0103)
url(r'^$', views.StartPage.as_view()), url(r'^$', views.StartPage.as_view()),
url(r'^404/$', TemplateView.as_view(template_name='404.html')), url(r'^404/$', TemplateView.as_view(template_name='404.html')),
url(r'^add_page/(?P<path>[\+\.\-\d\w\/]+)/$', url(r'^add_page/(?P<path>[\+\.\-\d\w\/]*)$',
views.PageAddForm.as_view(), name='add-page'), views.PageAddForm.as_view(), name='add-page'),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')), url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls),
url(r'^ckeditor/', include('ckeditor_uploader.urls')), url(r'^ckeditor/', include('ckeditor_uploader.urls')),
url(r'^comments/', include('django_comments.urls')), url(r'^comments/', include('django_comments.urls')),
url(r'^edit_page/(?P<path>[\+\.\-\d\w\/]+)/$', url(r'^edit_page/(?P<path>[\+\.\-\d\w\/]*)$',
views.PageEditForm.as_view(), name='edit-page'), views.PageEditForm.as_view(), name='edit-page'),
url(r'^events/', include('events.urls')), url(r'^events/', include('events.urls')),
url(r'^events.ics$', EventListIcal.as_view(), name='events-ical'), url(r'^events.ics$', EventListIcal.as_view(), name='events-ical'),