Another Step in the Quest to clean up the code base.
This commit is contained in:
@@ -3,25 +3,42 @@ import os
|
||||
import django_comments as comments
|
||||
from csp.decorators import csp_update
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.views import generic
|
||||
|
||||
from events.models import Photo
|
||||
from utils.mixins import PermissionRequiredMixin
|
||||
from . import models, forms
|
||||
|
||||
|
||||
class ArticleArchiveMixin(object):
|
||||
class WYSIWYGEditorMixin(PermissionRequiredMixin):
|
||||
"""
|
||||
Mixin View to add common context data to Views of the news article archive.
|
||||
A view to update the Content-Security-Policy for the WYSIWYG editor.
|
||||
Since it is only used in edit forms, it extends the PermissionRequiredMixin.
|
||||
"""
|
||||
|
||||
@csp_update(SCRIPT_SRC="'unsafe-eval'")
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
"""Add "unsafe-eval" to the Content-Secuirty-Policy HTTP Headers for the
|
||||
WYSIWYG Editor.
|
||||
|
||||
:param request: the HTTP Request Object
|
||||
:param args: args for the super dispatch
|
||||
:param kwargs: kwargs for the super dispatch
|
||||
:return: django HTTPResponse object
|
||||
"""
|
||||
return super(WYSIWYGEditorMixin, self).dispatch(
|
||||
request, *args, **kwargs)
|
||||
|
||||
|
||||
class ArticleArchiveMixin():
|
||||
"""Mixin to add common context data to Views of the news archive."""
|
||||
category = None
|
||||
|
||||
def get_category(self, queryset):
|
||||
"""
|
||||
Filter the queryset by category if one has been specified in the URL
|
||||
|
||||
|
||||
:param queryset: an model.Article.objects Queryset
|
||||
:return: an model.Article.objects Queryset filterd by category
|
||||
"""
|
||||
@@ -29,7 +46,7 @@ class ArticleArchiveMixin(object):
|
||||
category_slug = self.kwargs.get('category')
|
||||
if not category_slug:
|
||||
self.category = None
|
||||
return self.queryset
|
||||
return queryset
|
||||
try:
|
||||
self.category = models.Category.objects.get(slug=category_slug)
|
||||
except models.Category.DoesNotExist:
|
||||
@@ -39,8 +56,8 @@ class ArticleArchiveMixin(object):
|
||||
def get_context_data(self, **kwargs):
|
||||
"""
|
||||
Adds the categories and the active category to the template context.
|
||||
|
||||
:return: an django.template.context
|
||||
|
||||
:return: an django.template.context
|
||||
"""
|
||||
|
||||
context = super(ArticleArchiveMixin, self).get_context_data(**kwargs)
|
||||
@@ -62,7 +79,7 @@ class ArticleArchiveIndex(ArticleArchiveMixin, generic.ArchiveIndexView):
|
||||
|
||||
def get_queryset(self):
|
||||
"""
|
||||
Filter the Queryset by category.
|
||||
Filter the Queryset by category.
|
||||
|
||||
:return: models.Article.objects queryset
|
||||
"""
|
||||
@@ -85,7 +102,7 @@ class ArticleYearArchive(ArticleArchiveMixin, generic.YearArchiveView):
|
||||
|
||||
def get_queryset(self):
|
||||
"""
|
||||
Filter the Queryset by category.
|
||||
Filter the Queryset by category.
|
||||
|
||||
:return: models.Article.objects queryset
|
||||
"""
|
||||
@@ -108,7 +125,7 @@ class ArticleMonthArchive(ArticleArchiveMixin, generic.MonthArchiveView):
|
||||
|
||||
def get_queryset(self):
|
||||
"""
|
||||
Filter the Queryset by category.
|
||||
Filter the Queryset by category.
|
||||
|
||||
:return: models.Article.objects queryset
|
||||
"""
|
||||
@@ -125,7 +142,7 @@ class ArticleDetail(generic.DetailView):
|
||||
queryset = models.Article.objects.filter(status=models.STATUS_PUBLISHED)
|
||||
|
||||
|
||||
class ArticleForm(PermissionRequiredMixin, generic.UpdateView):
|
||||
class ArticleForm(WYSIWYGEditorMixin, generic.UpdateView):
|
||||
"""
|
||||
View to add or edit an Article
|
||||
"""
|
||||
@@ -134,30 +151,34 @@ class ArticleForm(PermissionRequiredMixin, generic.UpdateView):
|
||||
permission_required = 'content.change_article'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""Set the title variable to create/or edit article."""
|
||||
context = super(ArticleForm, self).get_context_data(**kwargs)
|
||||
if self.kwargs.get('pk'):
|
||||
context['title'] = _("Edit Article")
|
||||
else:
|
||||
context['title'] = _("Create Article")
|
||||
context['title'] = _("Edit Article") if self.kwargs.get('pk') else \
|
||||
_("Create Article")
|
||||
return context
|
||||
|
||||
def get_object(self, **kwargs):
|
||||
if self.kwargs.get('pk', None):
|
||||
return models.Article.objects.get(pk=self.kwargs['pk'])
|
||||
else:
|
||||
return models.Article(author=self.request.user)
|
||||
def get_object(self, queryset=None):
|
||||
"""Get the Article or create a new one if no id has been provided.
|
||||
|
||||
:param queryset: Get the single item from this filtered queryset.
|
||||
:return:
|
||||
"""
|
||||
queryset = queryset or self.get_queryset()
|
||||
if self.kwargs.get('pk'):
|
||||
return queryset.get(pk=self.kwargs['pk'])
|
||||
return models.Article(author=self.request.user)
|
||||
|
||||
|
||||
class PageAddForm(PermissionRequiredMixin, generic.CreateView):
|
||||
"""
|
||||
Renders an Form to create a new page for users with conforming permissions.
|
||||
"""
|
||||
class PageAddForm(WYSIWYGEditorMixin, generic.CreateView):
|
||||
""" Renders an Form to create a new page for users with conforming
|
||||
permissions."""
|
||||
|
||||
form_class = forms.PageForm
|
||||
template_name = 'content/page_form.html'
|
||||
permission_required = 'content.add_page'
|
||||
|
||||
def get_initial(self):
|
||||
"""Try to get the path of the parent page as initial data."""
|
||||
path = os.path.splitext(self.kwargs['path'])[0]
|
||||
if path.startswith('/'):
|
||||
path = path[1:]
|
||||
@@ -166,53 +187,76 @@ class PageAddForm(PermissionRequiredMixin, generic.CreateView):
|
||||
parent = models.Page.objects.get(path=path)
|
||||
return {'parent': parent}
|
||||
|
||||
@method_decorator(csp_update(SCRIPT_SRC="'unsafe-eval'"))
|
||||
def dispatch(self, *args, **kwargs):
|
||||
return super(PageAddForm, self).dispatch(*args, **kwargs)
|
||||
|
||||
|
||||
class PageEditForm(PermissionRequiredMixin, generic.UpdateView):
|
||||
"""
|
||||
Renders an Form to edit a page for users with conforming permissions.
|
||||
"""
|
||||
class PageEditForm(WYSIWYGEditorMixin, generic.UpdateView):
|
||||
"""Renders an Form to edit a page for users with conforming permissions."""
|
||||
|
||||
form_class = forms.PageForm
|
||||
model = models.Page
|
||||
permission_required = 'content.change_page'
|
||||
|
||||
def get_object(self, queryset=None):
|
||||
""" Get the path from the URL and fetch the corresponding page.
|
||||
|
||||
First get the path wihout fileextentsion leading or trailing slashes,
|
||||
then search in the database if such a page exists.
|
||||
|
||||
:param queryset: Get the single item from this filtered queryset.
|
||||
:return:
|
||||
"""
|
||||
path = os.path.splitext(self.kwargs['path'])[0]
|
||||
queryset = queryset or self.get_queryset()
|
||||
if path.startswith('/'):
|
||||
path = path[1:]
|
||||
if path.endswith('/'):
|
||||
path = path[:-1]
|
||||
return models.Page.objects.get(path=path)
|
||||
|
||||
@method_decorator(csp_update(SCRIPT_SRC="'unsafe-eval'"))
|
||||
def dispatch(self, *args, **kwargs):
|
||||
return super(PageEditForm, self).dispatch(*args, **kwargs)
|
||||
return queryset.get(path=path)
|
||||
|
||||
|
||||
class PageHtml(generic.DetailView):
|
||||
"""Display static HTML content from the database."""
|
||||
|
||||
model = models.Page
|
||||
|
||||
def get_object(self, queryset=None):
|
||||
"""Get the page content from the db that equals the given URL.
|
||||
|
||||
:param queryset: Get the single item from this filtered queryset.
|
||||
:return:
|
||||
"""
|
||||
queryset = queryset or self.get_queryset()
|
||||
try:
|
||||
return models.Page.objects.get(path=self.kwargs['path'],
|
||||
content_type=1)
|
||||
return queryset.get(path=self.kwargs['path'],
|
||||
content_type=models.HTML_PAGE)
|
||||
except models.Page.DoesNotExist:
|
||||
raise Http404(
|
||||
_("No Page found matching the Path %s") % self.request.path
|
||||
)
|
||||
|
||||
def get_template_names(self):
|
||||
"""Each static page can define its own template, so return the template
|
||||
name that has been stored in the dataset.
|
||||
|
||||
:return: filename of the template to render.
|
||||
"""
|
||||
return self.object.template
|
||||
|
||||
|
||||
class PagePdf(generic.DeleteView):
|
||||
"""Deliver an static PDF File under this given URL."""
|
||||
|
||||
model = models.Page
|
||||
|
||||
def get_object(self, queryset=None):
|
||||
"""Get the PDF page from the db that equals the given URL.
|
||||
|
||||
:param queryset: Get the single item from this filtered queryset.
|
||||
:return: models.Page object or raise a 404 if not found.
|
||||
"""
|
||||
queryset = queryset or self.get_queryset()
|
||||
try:
|
||||
return models.Page.objects.get(path=self.kwargs['path'],
|
||||
content_type=2)
|
||||
return queryset.get(path=self.kwargs['path'],
|
||||
content_type=models.PDF_PAGE)
|
||||
except models.Page.DoesNotExist:
|
||||
raise Http404(
|
||||
_("No PDF Document found matching the Path %s") %
|
||||
@@ -220,22 +264,34 @@ class PagePdf(generic.DeleteView):
|
||||
)
|
||||
|
||||
def render_to_response(self, context, **response_kwargs):
|
||||
"""Stream the PDF File to the client and set the right content headers.
|
||||
|
||||
:param context: useless only for compatility
|
||||
:param response_kwargs: will be added to the HttpResponse kwargs.
|
||||
:return: an HTTPResponse with PDF Content or an Http404 exception
|
||||
"""
|
||||
try:
|
||||
pdf_file = open(self.object.pdf_file.path, 'rb')
|
||||
response = HttpResponse(pdf_file.read(),
|
||||
content_type='application/pdf')
|
||||
pdf_file.close()
|
||||
return response
|
||||
with open(self.object.pdf_file.path, 'rb') as pdf_file:
|
||||
response = HttpResponse(
|
||||
content=pdf_file.read(),
|
||||
content_type='application/pdf',
|
||||
**response_kwargs
|
||||
)
|
||||
return response
|
||||
except:
|
||||
raise Http404('File not Found %s.pdf' % self.kwargs['path'])
|
||||
|
||||
|
||||
class StartPage(generic.TemplateView):
|
||||
"""The Frontpage, a page with the latest infos and some static content."""
|
||||
template_name = 'index.html'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
def get_context_data(self):
|
||||
""" Adds recent ariticles and recent comments to the context.
|
||||
|
||||
:return: array() with the context data
|
||||
"""
|
||||
page = models.Page.objects.get(slug='index')
|
||||
random_photo = Photo.objects.get_random(startpage=True)
|
||||
recent_comment_list = comments.get_model().objects.filter(
|
||||
site__pk=settings.SITE_ID,
|
||||
is_public=True,
|
||||
@@ -245,7 +301,6 @@ class StartPage(generic.TemplateView):
|
||||
context = {
|
||||
'title': page.title,
|
||||
'content': page.content,
|
||||
'random_photo': random_photo,
|
||||
'recent_article_list': models.Article.objects.published()[:3],
|
||||
'recent_comment_list': recent_comment_list,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user