Split DeleteView into a more generic ConfirmView

177 views
Skip to first unread message

Lennart Buit

unread,
Dec 7, 2015, 3:23:19 PM12/7/15
to Django developers (Contributions to Django itself)
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

Lennart Buit

unread,
Dec 7, 2015, 3:28:35 PM12/7/15
to Django developers (Contributions to Django itself)
I should add patches that I promise to add ;)

--Lennart
confirm_view.patch

Marc Tamlyn

unread,
Dec 8, 2015, 4:38:01 AM12/8/15
to django-d...@googlegroups.com
Hi Lennart,

I certainly like the idea and have written at least two implementations of `ConfirmView` in my projects before. Without getting into specifics, I'm not sure how we feel about adding more generic views like this, even where it does make sense. To me a good initial course of action could be to consider integrating it with django-braces. They have a collection of common mixins for common or fairly common CBV patterns. In fact we have already merged commonly used parts of django-braces into contrib.auth for 1.9.

Marc

--
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 django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
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/16df2f54-fee9-4b5d-9882-bc9954a5c8dd%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Lennart Buit

unread,
Dec 10, 2015, 6:52:24 PM12/10/15
to Django developers (Contributions to Django itself)
Hello Marc,

Thanks for your response, I will change my proposal to target django-braces. Will take me a few days tho, because I am loaded with (school) work right now :).

--Lennart

Marc Tamlyn

unread,
Dec 11, 2015, 8:26:37 AM12/11/15
to django-d...@googlegroups.com
No problem - I mentioned this thread on twitter to one of the braces devs and he also likes your idea. https://twitter.com/kennethlove/status/674237007439073280

Reply all
Reply to author
Forward
0 new messages