django-simplecms/cms/views.py

104 wiersze
3.1 KiB
Python
Czysty Zwykły widok Historia

import json
2019-08-23 15:19:40 +00:00
from django.urls import reverse
from django.views import generic
from django.shortcuts import redirect
from django.contrib.contenttypes.models import ContentType
2019-03-27 15:49:14 +00:00
from django.contrib.auth.mixins import UserPassesTestMixin
from .forms import PageForm, SectionForm
from .utils import get_config
2019-03-27 15:49:14 +00:00
import swapper
Page = swapper.load_model('cms', 'Page')
Section = swapper.load_model('cms', 'Section')
2019-03-27 15:49:14 +00:00
class StaffRequiredMixin(UserPassesTestMixin):
def test_func(self):
return self.request.user.is_staff
class MenuMixin:
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
class TypeMixin(MenuMixin):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
fields_per_type = {}
for model, desc 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),
})
return context
class MemoryMixin:
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)
class PageView(MenuMixin, MemoryMixin, generic.DetailView):
2019-03-27 15:49:14 +00:00
model = Page
template_name = 'cms/page.html'
# Supplies a default argument for slug
def setup(self, *args, slug='', **kwargs):
super().setup(*args, slug=slug, **kwargs)
#self.request = request
#self.args = args
#self.kwargs = kwargs
#self.kwargs['slug'] = slug
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
page = self.object
sections = page.sections.all()
context.update({
'page': page,
'sections': sections,
})
return context
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
class CreatePage(StaffRequiredMixin, MenuMixin, generic.CreateView):
model = Page
form_class = PageForm
template_name = 'cms/new.html'
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'))