cms basic class based views django
from django.views.generic import TemplateView
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from . import forms as cms_forms
class ProtectedView:
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
class BaseViewMixin:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
model_name = self.model.__name__
page_title = self.page_title if hasattr(
self, 'page_title') else 'view'
name = '{} {}'.format(model_name, page_title)
context.update({
'model': self.model,
'name': name,
'object_list': self.model.objects.all(),
})
return context
def get_success_url(self, **kwargs):
app_name = self.model._meta.app_label
model_name_lower = self.model.__name__.lower()
success_name = self.success_name if hasattr(
self, 'success_name') else 'list'
return reverse_lazy('{}-{}-{}'.format(
app_name, model_name_lower, success_name))
class CmsIndex(ProtectedView, BaseViewMixin, TemplateView):
template_name = 'cms/index.html'
class CmsAppIndex(ProtectedView, BaseViewMixin, TemplateView):
template_name_suffix = '/app_index'
class CmsList(ProtectedView, BaseViewMixin, ListView):
template_name_suffix = '/list'
class CmsDetail(ProtectedView, BaseViewMixin, DetailView):
template_name_suffix = '/detail'
class CmsCreate(ProtectedView, BaseViewMixin,
cms_forms.DynamicForm, CreateView):
template_name_suffix = '/form'
class CmsUpdate(ProtectedView, BaseViewMixin,
cms_forms.DynamicForm, UpdateView):
template_name_suffix = '/form'
class CmsDelete(ProtectedView, BaseViewMixin, DeleteView):
template_name_suffix = '/delete'