2019-12-31 13:06:08 +00:00
|
|
|
import json
|
|
|
|
|
2020-02-20 09:49:11 +00:00
|
|
|
from django.shortcuts import redirect
|
2020-02-19 19:13:43 +00:00
|
|
|
from django.views.generic import base, detail, edit
|
2020-03-24 00:15:17 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.decorators.cache import never_cache
|
2019-03-27 15:49:14 +00:00
|
|
|
from django.contrib.auth.mixins import UserPassesTestMixin
|
2020-03-21 17:49:41 +00:00
|
|
|
from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseBadRequest
|
2019-04-27 11:06:09 +00:00
|
|
|
|
2020-03-22 11:46:10 +00:00
|
|
|
from . import registry
|
2020-03-13 22:08:21 +00:00
|
|
|
from .forms import PageForm, SectionForm
|
2019-03-27 15:49:14 +00:00
|
|
|
|
2020-03-22 11:46:10 +00:00
|
|
|
class SectionView:
|
|
|
|
'''Generic section view'''
|
|
|
|
template_name = 'cms/sections/section.html'
|
|
|
|
|
|
|
|
def __init__(self, request):
|
|
|
|
self.request = request
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
return kwargs
|
|
|
|
|
2020-03-29 00:54:56 +00:00
|
|
|
class SectionFormView(SectionView):
|
2020-03-22 11:46:10 +00:00
|
|
|
'''Generic section with associated form'''
|
2020-03-29 00:54:56 +00:00
|
|
|
form_class = None
|
|
|
|
success_url = None
|
2020-03-22 11:46:10 +00:00
|
|
|
|
2020-03-29 00:54:56 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
if 'form' not in kwargs:
|
|
|
|
kwargs['form'] = self.get_form()
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.save()
|
|
|
|
return HttpResponseRedirect(self.success_url)
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def get_form(self, method='get'):
|
|
|
|
form_class = self.form_class
|
|
|
|
kwargs = self.get_form_kwargs()
|
|
|
|
if method == 'post':
|
|
|
|
kwargs.update({
|
|
|
|
'data': self.request.POST,
|
|
|
|
'files': self.request.FILES,
|
|
|
|
})
|
|
|
|
return form_class(**kwargs)
|
2019-12-31 12:05:12 +00:00
|
|
|
|
2020-02-19 19:13:43 +00:00
|
|
|
class PageView(detail.DetailView):
|
2020-03-21 17:49:41 +00:00
|
|
|
'''View of a page with heterogeneous sections'''
|
2020-03-22 11:46:10 +00:00
|
|
|
model = registry.page_class
|
2019-03-27 15:49:14 +00:00
|
|
|
template_name = 'cms/page.html'
|
|
|
|
|
2020-01-02 00:56:15 +00:00
|
|
|
def setup(self, *args, slug='', **kwargs):
|
2020-01-02 18:32:15 +00:00
|
|
|
'''Supply a default argument for slug'''
|
2020-01-02 00:56:15 +00:00
|
|
|
super().setup(*args, slug=slug, **kwargs)
|
2019-12-28 02:05:30 +00:00
|
|
|
|
2020-01-02 18:32:15 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2020-03-21 17:49:41 +00:00
|
|
|
'''Instantiate section views and render final response'''
|
2020-02-19 15:35:23 +00:00
|
|
|
try:
|
|
|
|
page = self.object = self.get_object()
|
|
|
|
except Http404:
|
2020-03-22 11:46:10 +00:00
|
|
|
if self.kwargs['slug'] == '':
|
|
|
|
page = registry.page_class(title='Homepage', slug='')
|
|
|
|
page.save()
|
|
|
|
self.object = page
|
|
|
|
else:
|
|
|
|
raise
|
2020-01-02 18:32:15 +00:00
|
|
|
context = self.get_context_data(**kwargs)
|
2019-08-26 15:31:46 +00:00
|
|
|
sections = page.sections.all()
|
|
|
|
context.update({
|
|
|
|
'page': page,
|
|
|
|
'sections': sections,
|
|
|
|
})
|
2020-01-02 18:32:15 +00:00
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def post(self, request, **kwargs):
|
2020-03-21 17:49:41 +00:00
|
|
|
'''Call the post() method of the correct section view'''
|
2020-01-02 18:32:15 +00:00
|
|
|
try:
|
|
|
|
pk = int(self.request.POST.get('section'))
|
|
|
|
except:
|
|
|
|
return HttpResponseBadRequest()
|
|
|
|
|
|
|
|
page = self.object = self.get_object()
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
sections = page.sections.all()
|
|
|
|
for section in sections:
|
|
|
|
if section.pk == pk:
|
2020-03-22 11:46:10 +00:00
|
|
|
view = registry.get_view(section, request)
|
2020-03-29 00:54:56 +00:00
|
|
|
form = view.get_form(method='post')
|
|
|
|
if form.is_valid():
|
|
|
|
return view.form_valid(form)
|
|
|
|
section.invalid_form = form
|
2020-01-05 02:36:23 +00:00
|
|
|
|
2020-01-02 18:32:15 +00:00
|
|
|
context.update({
|
|
|
|
'page': page,
|
|
|
|
'sections': sections,
|
|
|
|
})
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-22 11:46:10 +00:00
|
|
|
pages = registry.page_class.objects.filter(menu=True)
|
2020-01-02 18:32:15 +00:00
|
|
|
context.update({
|
2020-02-19 15:35:23 +00:00
|
|
|
'pages': pages,
|
2020-01-02 18:32:15 +00:00
|
|
|
})
|
2019-08-26 15:31:46 +00:00
|
|
|
return context
|
|
|
|
|
2020-03-24 00:15:17 +00:00
|
|
|
@method_decorator(never_cache, name='dispatch')
|
2020-02-19 19:13:43 +00:00
|
|
|
class EditPage(UserPassesTestMixin, edit.ModelFormMixin, base.TemplateResponseMixin, base.View):
|
2020-03-21 17:49:41 +00:00
|
|
|
'''Base view with nested forms for editing the page and all its sections'''
|
2020-03-22 11:46:10 +00:00
|
|
|
model = registry.page_class
|
2020-02-19 15:35:23 +00:00
|
|
|
form_class = PageForm
|
2020-01-02 00:56:15 +00:00
|
|
|
template_name = 'cms/edit.html'
|
|
|
|
|
2020-02-19 19:13:43 +00:00
|
|
|
def test_func(self):
|
2020-03-21 17:49:41 +00:00
|
|
|
'''Only allow users with the correct permissions'''
|
|
|
|
return self.request.user.has_perm('cms_page_change')
|
2020-02-19 19:13:43 +00:00
|
|
|
|
2020-02-17 10:03:31 +00:00
|
|
|
def get_form_kwargs(self):
|
2020-03-21 17:49:41 +00:00
|
|
|
'''Set the default slug to the current URL for new pages'''
|
2020-02-17 10:03:31 +00:00
|
|
|
kwargs = super().get_form_kwargs()
|
2020-03-13 22:08:21 +00:00
|
|
|
if 'slug' in self.kwargs:
|
|
|
|
kwargs.update({'initial': {'slug': self.kwargs['slug']}})
|
2020-02-17 10:03:31 +00:00
|
|
|
return kwargs
|
|
|
|
|
2020-02-14 16:20:41 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
2020-03-21 17:49:41 +00:00
|
|
|
'''Populate the fields_per_type dict for use in javascript'''
|
2020-02-14 16:20:41 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-22 11:46:10 +00:00
|
|
|
context['fields_per_type'] = json.dumps(registry.get_fields_per_type())
|
2020-02-14 16:20:41 +00:00
|
|
|
return context
|
|
|
|
|
2020-02-19 19:13:43 +00:00
|
|
|
def get_object(self):
|
2020-03-13 08:39:27 +00:00
|
|
|
'''Prevent 404 by serving the new object form'''
|
2020-02-19 19:13:43 +00:00
|
|
|
try:
|
|
|
|
return super().get_object()
|
2020-03-13 08:39:27 +00:00
|
|
|
except Http404:
|
2020-02-19 19:13:43 +00:00
|
|
|
return None
|
|
|
|
|
2020-03-21 17:49:41 +00:00
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
'''Handle GET requests'''
|
2020-02-19 19:13:43 +00:00
|
|
|
self.object = self.get_object()
|
2020-03-21 17:49:41 +00:00
|
|
|
return self.render_to_response(self.get_context_data(**kwargs))
|
2020-02-20 09:49:11 +00:00
|
|
|
|
2020-03-21 17:49:41 +00:00
|
|
|
def post(self, *args, **kwargs):
|
|
|
|
'''Handle POST requests'''
|
2020-02-14 16:20:41 +00:00
|
|
|
self.object = self.get_object()
|
|
|
|
form = self.get_form()
|
2020-02-20 09:49:11 +00:00
|
|
|
|
2020-03-10 22:55:46 +00:00
|
|
|
if form.is_valid():
|
|
|
|
page = form.save()
|
|
|
|
if page:
|
2020-02-20 09:49:11 +00:00
|
|
|
return HttpResponseRedirect(page.get_absolute_url())
|
2020-03-10 22:55:46 +00:00
|
|
|
return HttpResponseRedirect('/')
|
2020-03-21 17:49:41 +00:00
|
|
|
return self.render_to_response(self.get_context_data(form=form, **kwargs))
|
2020-02-14 16:20:41 +00:00
|
|
|
|
2020-02-19 19:13:43 +00:00
|
|
|
class CreatePage(EditPage):
|
2020-03-21 17:49:41 +00:00
|
|
|
'''View for creating new pages'''
|
2020-02-19 19:13:43 +00:00
|
|
|
def get_object(self):
|
2020-03-22 11:46:10 +00:00
|
|
|
return registry.page_class()
|
2019-08-23 15:19:40 +00:00
|
|
|
|
2020-02-19 19:13:43 +00:00
|
|
|
class UpdatePage(EditPage):
|
2020-03-21 17:49:41 +00:00
|
|
|
'''View for editing existing pages'''
|
2020-03-13 22:08:21 +00:00
|
|
|
|
2020-03-24 00:15:17 +00:00
|
|
|
@method_decorator(never_cache, name='dispatch')
|
2020-03-13 22:08:21 +00:00
|
|
|
class EditSection(UserPassesTestMixin, edit.ModelFormMixin, base.TemplateResponseMixin, base.View):
|
2020-03-22 11:46:10 +00:00
|
|
|
model = registry.section_class
|
2020-03-13 22:08:21 +00:00
|
|
|
form_class = SectionForm
|
|
|
|
template_name = 'cms/edit.html'
|
|
|
|
|
|
|
|
def test_func(self):
|
2020-03-21 17:49:41 +00:00
|
|
|
return self.request.user.has_perm('cms_section_change')
|
2020-03-13 22:08:21 +00:00
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super().get_form_kwargs()
|
|
|
|
kwargs.update({
|
|
|
|
'prefix': 'section',
|
|
|
|
})
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-03-22 11:46:10 +00:00
|
|
|
context['fields_per_type'] = json.dumps(registry.get_fields_per_type())
|
2020-03-13 22:08:21 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
try:
|
2020-03-22 11:46:10 +00:00
|
|
|
self.page = registry.page_class.objects.get(slug=self.kwargs['slug'])
|
|
|
|
except registry.page_class.DoesNotExist:
|
2020-03-13 22:08:21 +00:00
|
|
|
raise Http404()
|
|
|
|
return self.get_section()
|
|
|
|
|
|
|
|
def get_section(self):
|
|
|
|
try:
|
|
|
|
section = self.page.sections.get(number=self.kwargs['number'])
|
2020-03-22 11:46:10 +00:00
|
|
|
except self.page.sections.DoesNotExist:
|
2020-03-13 22:08:21 +00:00
|
|
|
raise Http404()
|
|
|
|
return section
|
|
|
|
|
2020-03-21 17:49:41 +00:00
|
|
|
def get(self, *args, **kwargs):
|
2020-03-13 22:08:21 +00:00
|
|
|
self.object = self.get_object()
|
2020-03-21 17:49:41 +00:00
|
|
|
return self.render_to_response(self.get_context_data(**kwargs))
|
2020-03-13 22:08:21 +00:00
|
|
|
|
2020-03-21 17:49:41 +00:00
|
|
|
def post(self, *args, **kwargs):
|
2020-03-13 22:08:21 +00:00
|
|
|
self.object = self.get_object()
|
|
|
|
form = self.get_form()
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
|
|
section = form.save()
|
|
|
|
if section:
|
2020-03-16 18:32:39 +00:00
|
|
|
return HttpResponseRedirect(section.get_absolute_url())
|
2020-03-13 22:08:21 +00:00
|
|
|
elif self.page.sections.exists():
|
|
|
|
return HttpResponseRedirect(self.page.get_absolute_url())
|
|
|
|
else:
|
|
|
|
return HttpResponseRedirect('/')
|
2020-03-21 17:49:41 +00:00
|
|
|
return self.render_to_response(self.get_context_data(form=form, **kwargs))
|
2020-03-13 22:08:21 +00:00
|
|
|
|
|
|
|
class CreateSection(EditSection):
|
|
|
|
def get_section(self):
|
2020-03-22 11:46:10 +00:00
|
|
|
return registry.section_class(page=self.page)
|
2020-03-13 22:08:21 +00:00
|
|
|
|
|
|
|
class UpdateSection(EditSection):
|
|
|
|
pass
|