Dimitris Kougioumtzis Blog About technology

Cms basic class based views

Category Django

Posted on Dec. 9, 2017



cms basic class based views django


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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'

About

My name is Dimitris Kougioumtzis and i work as a Web developer at Rapidbounce Company

Elsewhere