1
+ from functools import cached_property
2
+
1
3
from plain .exceptions import ImproperlyConfigured , ObjectDoesNotExist
2
4
from plain .forms import Form
3
- from plain .http import Http404 , Response
5
+ from plain .http import Http404
4
6
5
7
from .forms import FormView
6
8
from .templates import TemplateView
9
11
class ObjectTemplateViewMixin :
10
12
context_object_name = ""
11
13
12
- def get (self ) -> Response :
13
- self .load_object ()
14
- return self .render_template ()
15
-
16
- def load_object (self ) -> None :
14
+ @cached_property
15
+ def object (self ):
17
16
try :
18
- self . object = self .get_object ()
17
+ obj = self .get_object ()
19
18
except ObjectDoesNotExist :
20
19
raise Http404
21
20
22
- if not self . object :
21
+ if not obj :
23
22
# Also raise 404 if the object is None
24
23
raise Http404
25
24
25
+ return obj
26
+
26
27
def get_object (self ): # Intentionally untyped... subclasses must override this.
27
28
raise NotImplementedError (
28
29
f"get_object() is not implemented on { self .__class__ .__name__ } "
@@ -50,23 +51,11 @@ class DetailView(ObjectTemplateViewMixin, TemplateView):
50
51
pass
51
52
52
53
53
- class CreateView (ObjectTemplateViewMixin , FormView ):
54
+ class CreateView (FormView ):
54
55
"""
55
56
View for creating a new object, with a response rendered by a template.
56
57
"""
57
58
58
- def post (self ) -> Response :
59
- """
60
- Handle POST requests: instantiate a form instance with the passed
61
- POST variables and then check if it's valid.
62
- """
63
- # Context expects self.object to exist
64
- self .load_object ()
65
- return super ().post ()
66
-
67
- def load_object (self ) -> None :
68
- self .object = None
69
-
70
59
# TODO? would rather you have to specify this...
71
60
def get_success_url (self , form ):
72
61
"""Return the URL to redirect to after processing a valid form."""
@@ -91,14 +80,6 @@ def form_valid(self, form):
91
80
class UpdateView (ObjectTemplateViewMixin , FormView ):
92
81
"""View for updating an object, with a response rendered by a template."""
93
82
94
- def post (self ) -> Response :
95
- """
96
- Handle POST requests: instantiate a form instance with the passed
97
- POST variables and then check if it's valid.
98
- """
99
- self .load_object ()
100
- return super ().post ()
101
-
102
83
def get_success_url (self , form ):
103
84
"""Return the URL to redirect to after processing a valid form."""
104
85
if self .success_url :
@@ -115,7 +96,7 @@ def get_success_url(self, form):
115
96
116
97
def form_valid (self , form ):
117
98
"""If the form is valid, save the associated model."""
118
- self . object = form .save ()
99
+ form .save ()
119
100
return super ().form_valid (form )
120
101
121
102
def get_form_kwargs (self ):
@@ -141,14 +122,6 @@ def save(self):
141
122
142
123
form_class = EmptyDeleteForm
143
124
144
- def post (self ) -> Response :
145
- """
146
- Handle POST requests: instantiate a form instance with the passed
147
- POST variables and then check if it's valid.
148
- """
149
- self .load_object ()
150
- return super ().post ()
151
-
152
125
def get_form_kwargs (self ):
153
126
"""Return the keyword arguments for instantiating the form."""
154
127
kwargs = super ().get_form_kwargs ()
@@ -169,9 +142,9 @@ class ListView(TemplateView):
169
142
170
143
context_object_name = ""
171
144
172
- def get ( self ) -> Response :
173
- self . objects = self . get_objects ()
174
- return super (). get ()
145
+ @ cached_property
146
+ def objects ( self ):
147
+ return self . get_objects ()
175
148
176
149
def get_objects (self ):
177
150
raise NotImplementedError (
0 commit comments