2019-08-23 15:19:40 +00:00
|
|
|
from django.urls import reverse
|
2019-04-27 11:06:09 +00:00
|
|
|
from django.shortcuts import redirect
|
2019-03-27 15:49:14 +00:00
|
|
|
from django.contrib.auth.mixins import UserPassesTestMixin
|
2019-04-27 11:06:09 +00:00
|
|
|
from django.views.generic import DetailView, UpdateView, CreateView
|
|
|
|
|
2019-08-23 15:19:40 +00:00
|
|
|
from .models import Page, Section, SubSection
|
|
|
|
from .forms import PageForm, SectionFormSet, SectionForm, SubSectionFormSet, SubSectionForm
|
2019-04-27 11:06:09 +00:00
|
|
|
from .utils import get_config
|
2019-03-27 15:49:14 +00:00
|
|
|
|
|
|
|
class StaffRequiredMixin(UserPassesTestMixin):
|
|
|
|
def test_func(self):
|
|
|
|
return self.request.user.is_staff
|
|
|
|
|
|
|
|
class MenuMixin(object):
|
|
|
|
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
|
|
|
|
|
|
|
|
class PageView(MenuMixin, DetailView):
|
|
|
|
model = Page
|
|
|
|
template_name = 'cms/page.html'
|
|
|
|
|
2019-08-23 15:19:40 +00:00
|
|
|
class CreatePage(StaffRequiredMixin, CreateView):
|
2019-04-27 11:06:09 +00:00
|
|
|
model = Page
|
|
|
|
form_class = PageForm
|
|
|
|
template_name = 'cms/new.html'
|
|
|
|
|
2019-08-23 15:19:40 +00:00
|
|
|
class CreateSection(StaffRequiredMixin, CreateView):
|
|
|
|
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()
|
|
|
|
return redirect(reverse('cms:updatepage', args=[form.instance.page.pk]))
|
|
|
|
|
|
|
|
class CreateSubSection(StaffRequiredMixin, CreateView):
|
|
|
|
model = SubSection
|
|
|
|
form_class = SubSectionForm
|
|
|
|
template_name = 'cms/new.html'
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.section = Section.objects.get(pk=self.kwargs.get('pk'))
|
|
|
|
form.save()
|
|
|
|
return redirect(reverse('cms:updatesection', args=[form.instance.section.pk]))
|
|
|
|
|
|
|
|
class BaseUpdateView(StaffRequiredMixin, MenuMixin, UpdateView):
|
2019-03-27 15:49:14 +00:00
|
|
|
template_name = 'cms/edit.html'
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
form = self.get_form()
|
2019-08-23 15:19:40 +00:00
|
|
|
formset = self.formset_class(request.POST, request.FILES, instance=self.object)
|
2019-03-27 15:49:14 +00:00
|
|
|
if form.is_valid() and formset.is_valid():
|
|
|
|
return self.form_valid(form, formset)
|
|
|
|
else:
|
|
|
|
return self.form_invalid(form, formset)
|
|
|
|
|
|
|
|
def form_valid(self, form, formset):
|
|
|
|
form.save()
|
|
|
|
formset.save()
|
2019-08-23 15:19:40 +00:00
|
|
|
return redirect(self.get_absolute_url(form.instance))
|
2019-03-27 15:49:14 +00:00
|
|
|
|
|
|
|
def form_invalid(self, form, formset):
|
|
|
|
return self.render_to_response(self.get_context_data(form=form, formset=formset))
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
if 'formset' not in context:
|
2019-08-23 15:19:40 +00:00
|
|
|
formset = self.formset_class(instance=self.object)
|
2019-03-27 15:49:14 +00:00
|
|
|
context.update({
|
|
|
|
'formset': formset,
|
2019-08-23 15:19:40 +00:00
|
|
|
'formset_form_url': self.get_formset_form_url(self.object),
|
|
|
|
'formset_description': self.formset_class.model._meta.verbose_name.title(),
|
2019-03-27 15:49:14 +00:00
|
|
|
})
|
|
|
|
return context
|
2019-08-23 15:19:40 +00:00
|
|
|
|
|
|
|
class UpdatePage(BaseUpdateView):
|
|
|
|
model = Page
|
|
|
|
form_class = PageForm
|
|
|
|
formset_class = SectionFormSet
|
|
|
|
|
|
|
|
def get_formset_form_url(self, page):
|
|
|
|
return reverse('cms:createsection', args=[page.pk])
|
|
|
|
|
|
|
|
def get_absolute_url(self, instance):
|
|
|
|
return instance.get_absolute_url()
|
|
|
|
|
|
|
|
class UpdateSection(BaseUpdateView):
|
|
|
|
model = Section
|
|
|
|
form_class = SectionForm
|
|
|
|
formset_class = SubSectionFormSet
|
|
|
|
|
|
|
|
def get_formset_form_url(self, page):
|
|
|
|
return reverse('cms:createsubsection', args=[page.pk])
|
|
|
|
|
|
|
|
def get_absolute_url(self, instance):
|
|
|
|
return instance.page.get_absolute_url()
|