2020-01-02 18:32:15 +00:00
|
|
|
'''CMS Views'''
|
|
|
|
|
2019-12-31 13:06:08 +00:00
|
|
|
import json
|
2020-01-02 18:32:15 +00:00
|
|
|
import swapper
|
2019-12-31 13:06:08 +00:00
|
|
|
|
|
|
|
from django.views import generic
|
2019-04-27 11:06:09 +00:00
|
|
|
from django.shortcuts import redirect
|
2020-01-02 18:32:15 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2019-12-31 13:06:08 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2019-03-27 15:49:14 +00:00
|
|
|
from django.contrib.auth.mixins import UserPassesTestMixin
|
2020-01-02 18:32:15 +00:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponseBadRequest
|
2019-04-27 11:06:09 +00:00
|
|
|
|
2020-01-02 18:32:15 +00:00
|
|
|
from .decorators import register_view
|
2019-12-31 12:05:12 +00:00
|
|
|
from .forms import PageForm, SectionForm
|
2019-04-27 11:06:09 +00:00
|
|
|
from .utils import get_config
|
2019-03-27 15:49:14 +00:00
|
|
|
|
2020-01-02 00:56:15 +00:00
|
|
|
Page = swapper.load_model('cms', 'Page')
|
2019-12-31 12:05:12 +00:00
|
|
|
Section = swapper.load_model('cms', 'Section')
|
|
|
|
|
2020-01-02 18:32:15 +00:00
|
|
|
@register_view(Section)
|
|
|
|
class SectionView:
|
|
|
|
'''Generic section view'''
|
|
|
|
def get(self, request, section):
|
|
|
|
'''Override this to add custom attributes to a section'''
|
|
|
|
return section
|
|
|
|
|
|
|
|
class SectionWithFormView(SectionView):
|
|
|
|
'''Generic section with associated form'''
|
|
|
|
form_class = None
|
|
|
|
success_url = None
|
|
|
|
|
|
|
|
def get_form_class(self):
|
|
|
|
'''Return the form class to use in this view.'''
|
|
|
|
if self.form_class:
|
|
|
|
return self.form_class
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Either specify formclass attribute or override get_form_class()')
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
'''Return the URL to redirect to after processing a valid form.'''
|
|
|
|
if self.success_url:
|
|
|
|
return self.success_url
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Either specify success_url attribute or override get_success_url()')
|
|
|
|
|
|
|
|
def get(self, request, section):
|
|
|
|
'''Add form to section'''
|
|
|
|
form = self.get_form_class()()
|
|
|
|
section.form = form
|
|
|
|
return section
|
|
|
|
|
|
|
|
def post(self, request, section):
|
|
|
|
'''Process form'''
|
|
|
|
form = self.get_form_class()(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save(request)
|
|
|
|
return redirect(self.get_success_url())
|
|
|
|
section.form = form
|
|
|
|
return section
|
2019-03-27 15:49:14 +00:00
|
|
|
|
2020-01-02 00:56:15 +00:00
|
|
|
class MenuMixin:
|
2020-01-02 18:32:15 +00:00
|
|
|
'''Add pages and footer to template context'''
|
2019-03-27 15:49:14 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
pages = Page.objects.filter(menu=True)
|
|
|
|
footer = get_config(10)
|
|
|
|
context.update({
|
|
|
|
'pages': pages,
|
|
|
|
'footer': footer,
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
2020-01-02 00:56:15 +00:00
|
|
|
class MemoryMixin:
|
2020-01-02 18:32:15 +00:00
|
|
|
'''Remember the previous page in session'''
|
2019-08-25 21:36:58 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
request.session['previous_url'] = request.path
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
2020-01-02 00:56:15 +00:00
|
|
|
class PageView(MenuMixin, MemoryMixin, generic.DetailView):
|
2020-01-02 18:32:15 +00:00
|
|
|
'''View of a page with heterogeneous (polymorphic) sections'''
|
2019-03-27 15:49:14 +00:00
|
|
|
model = Page
|
|
|
|
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):
|
|
|
|
'''Call each sections's get() view before rendering final response'''
|
|
|
|
page = self.object = self.get_object()
|
|
|
|
context = self.get_context_data(**kwargs)
|
2019-08-26 15:31:46 +00:00
|
|
|
sections = page.sections.all()
|
2020-01-02 18:32:15 +00:00
|
|
|
for section in sections:
|
|
|
|
view = section.__class__.view_class()
|
|
|
|
view.get(request, section)
|
2019-08-26 15:31:46 +00:00
|
|
|
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):
|
|
|
|
'''Call the post() function of the correct section view'''
|
|
|
|
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:
|
|
|
|
view = section.__class__.view_class()
|
|
|
|
if section.pk == pk:
|
|
|
|
result = view.post(request, section)
|
|
|
|
if isinstance(result, HttpResponseRedirect):
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
view.get(request, section)
|
|
|
|
context.update({
|
|
|
|
'page': page,
|
|
|
|
'sections': sections,
|
|
|
|
})
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
# The following views all require a logged-in staff member
|
|
|
|
|
|
|
|
class StaffRequiredMixin(UserPassesTestMixin):
|
|
|
|
def test_func(self):
|
|
|
|
return self.request.user.is_staff
|
|
|
|
|
|
|
|
class TypeMixin(MenuMixin):
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
fields_per_type = {}
|
|
|
|
for model, _ in Section.TYPES:
|
|
|
|
ctype = ContentType.objects.get(
|
|
|
|
app_label=Section._meta.app_label,
|
|
|
|
model=model.lower(),
|
|
|
|
)
|
|
|
|
fields_per_type[ctype.model] = ctype.model_class().fields
|
|
|
|
|
|
|
|
context.update({
|
|
|
|
'fields_per_type': json.dumps(fields_per_type),
|
|
|
|
})
|
2019-08-26 15:31:46 +00:00
|
|
|
return context
|
|
|
|
|
2020-01-02 00:56:15 +00:00
|
|
|
class BaseUpdateView(generic.UpdateView):
|
|
|
|
template_name = 'cms/edit.html'
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.save()
|
|
|
|
return redirect(self.request.session.get('previous_url'))
|
|
|
|
|
|
|
|
class UpdatePage(StaffRequiredMixin, MenuMixin, BaseUpdateView):
|
|
|
|
model = Page
|
|
|
|
form_class = PageForm
|
|
|
|
|
|
|
|
class UpdateSection(StaffRequiredMixin, TypeMixin, BaseUpdateView):
|
|
|
|
model = Section
|
|
|
|
form_class = SectionForm
|
2019-12-28 02:05:30 +00:00
|
|
|
|
2019-12-31 13:06:08 +00:00
|
|
|
class CreatePage(StaffRequiredMixin, MenuMixin, generic.CreateView):
|
2019-04-27 11:06:09 +00:00
|
|
|
model = Page
|
|
|
|
form_class = PageForm
|
|
|
|
template_name = 'cms/new.html'
|
|
|
|
|
2020-01-02 00:56:15 +00:00
|
|
|
class CreateSection(StaffRequiredMixin, TypeMixin, generic.CreateView):
|
2019-08-23 15:19:40 +00:00
|
|
|
model = Section
|
|
|
|
form_class = SectionForm
|
|
|
|
template_name = 'cms/new.html'
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.page = Page.objects.get(pk=self.kwargs.get('pk'))
|
|
|
|
form.save()
|
2019-08-25 21:36:58 +00:00
|
|
|
return redirect(self.request.session.get('previous_url'))
|