Hey all,
One of my minor annoyances with the class based view system in django is
that there is no clear way to model a confirmable action. The concept does
exists in cbv, but only for deleting objects. I however think that there
are more usages for confirming actions other then delete.
Lets look at an example, a news site can CRUD newsaticles. But this news
site has some articles for free, and some other behind a paywall. Also,
editors on this site can publish newsarticles (after which they come in the
pay section), and then promote them to free articles. The views in django
would then look as follows (with some brevity):
# This also goes for Update/Delete/...
class NewsArticleCreateView(CreateView):
model = NewsArticle
# This also goes for Promote
class NewsArticlePublishView(DetailView):
model = NewsArticle
success_url = 'somewhere'
def post(self, *args, **kwargs):
self.object = self.get_object()
success_url = self.get_success_url()
self.object.published = True
self.object.save()
return HttpResponseRedirect(success_url)
def get_success_url(self):
if self.success_url:
return self.success_url.format(**self.object.__dict__)
else:
raise ImproperlyConfigured(
"No URL to redirect to. Provide a success_url.")
The problem, I think, is that we are copying most of a DeleteView for a
fairly standard confirmable action. The get_success_url function is almost
the same, and most of the code found in the post function is that of delete
in DeleteView. I think therefore that we should consider splitting the
DeleteMixin in a more generic ConfirmMixin, make DeleteMixin a subclass of
ConfirmMixin, and introduce the (more) generic BaseConfirmView and
ConfirmView. The above example will then look as follows:
class NewsArticlePublishView(ConfirmView):
model = NewsArticle
success_url = 'somewhere'
def action_confirmed(self):
self.object.published = True
self.object.save()
I think the benefits of this solution are clear, we introduce readability
in these types of views (look at the class declaration, its a confirm view
alright!) at the cost of library size (another 3 classes got added).
Please be gentle, I am new here ;). But, I enjoy a technical discussion!
And I attached a patch that implement this proposal, it however should be
treated as a POC, not a complete implementation with tests and docs and
such.
--Lennart
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/3ad2f8d6-e9e3-4df6-9579-50b29f0b4561%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.