News for hackers. Stuff that matters.
Re:Our custom Django mixins by rlander on Thursday March 01, 2012 @04:02PM (#3654027)
Very nice snippets, though they're meant to work only with Django's default class views.

A few months ago I started using a small class-based views library called aino-utkik[1] and haven't looked back.

Instead of this:

    class ArtistLogin(FormView):
        form_class = ArtistLoginForm
        template_name = 'artists/artist_login.html'

        def get(self, request, *args, **kwargs):
            request.session.set_test_cookie()
            return super(ArtistLogin, self).get(request, *args, **kwargs)

        def form_valid(self, form):
            login(self.request, form.artist)
            return HttpResponseRedirect(reverse('artist_mypage'))

        def get_form_kwargs(self):
            kwargs = super(ArtistLogin, self).get_form_kwargs()
            kwargs['request'] = self.request
            return kwargs
I can write this:

    class ArtistLogin(View):
        def setup(self):
            self.c.form = ArtistLoginForm(
                request=self.request, data=self.request.POST or None)

        def get(self):
            self.request.session.set_test_cookie()

        def post(self):
            if self.c.form.is_valid():
                login(self.request, self.c.form.artist)
                return HttpResponseRedirect(reverse('artist_mypage'))

which I find way more elegant.

[1] https://github.com/aino/aino-utkik

Re:Our custom Django mixins by kennethlove on Thursday March 01, 2012 @04:06PM (#3654041)
That is a really nice syntax. I kind of like the specificity of ``form_valid`` (for example), though.

And, yes, our mixins are Django-only (hence the title!)

Also, why the ``set_test_cookie`` on every GET?

Re:Our custom Django mixins by rlander on Friday March 02, 2012 @01:26PM (#3657666)
Those snippets were taken from utkik's docs and are probably part of a larger code; but I agree they probably belong somewhere else, maybe in a middleware.

BTW, thanks for the nice post. Is the code available on github or bitbucket?

Re:Our custom Django mixins by kennethlove on Friday March 02, 2012 @04:34PM (#3658480)
Yeah, it is actually.

https://github.com/brack3t/django-braces is the Github repo, or you can ``pip install django-braces`` into your project. All the mixins are then imported from the ``braces`` package.

Re:Our custom Django mixins by chrisjones on Thursday March 01, 2012 @04:19PM (#3654102)
I did not know this existed, I'll have to give it a look. The setup method makes sense, not sure I like having to check is_valid() though.
Re:Our custom Django mixins by andrewingram on Thursday March 01, 2012 @05:36PM (#3654428)
Here are my extensions to Django's class-based views:

https://github.com/AndrewIngram/django-extra-views

Re:Our custom Django mixins by jarcoal on Thursday March 01, 2012 @05:30PM (#3654402)
Django's documentation is indeed terrible for class based views, but I encourage any developers that have had trouble reading through it to just take a look at the source. I found it to be much easier to learn that way.
Re:Our custom Django mixins by kennethlove on Thursday March 01, 2012 @05:37PM (#3654439)
No one should have to read the source to understand such a large and useful area of a framework.

That's similar to expecting people to read the source code to understand ActiveRecord or something.

Re:Our custom Django mixins by pydanny on Thursday March 01, 2012 @05:48PM (#3654481)
I agree. Reading the source is so 2000s. Part of why Django has enjoyed it's community growth is because of it's culture of documentation. And for Django to have released something that isn't well documented is untypical.

This isn't a complaint, except at me. I've promised to update those docs for coming up on a whole year. :P

Re:Our custom Django mixins by jarcoal on Thursday March 01, 2012 @09:30PM (#3655086)
I definitely think the docs should be better, but it's not really that much code to read through. The entire thing is maybe 1000 lines of code.

Here it is, although the code is somewhat mixed with the old, function based, generic views: https://github.com/django/django/tree/master/django/views/ge...

Re:Our custom Django mixins by Daishiman on Thursday March 01, 2012 @06:43PM (#3654659)
Agreed. The docs should be more comprehensive, and there should be at least one contrib package with useful CBV.s
Re:Our custom Django mixins by tkaemming on Thursday March 01, 2012 @05:11PM (#3654315)
I've found this view_class_decorator and MultipleFormView particularly helpful with class-based views: https://gist.github.com/1953579
Re:Our custom Django mixins by kennethlove on Thursday March 01, 2012 @05:18PM (#3654346)
I don't care for the decorator at all, but the MultipleFormView mixin looks very handy. Thanks.
Re:Our custom Django mixins by andrewingram on Friday March 02, 2012 @05:17AM (#3656089)
The decorator actually raises an interesting issue. Are permissions and security part of the core application logic, or are they wrappers.

The mixin approach is nice and OO, but it's potentially tying two different layers of functionality together. Whereas the decorator is closer to aspect-oriented programming which attempts to isolate core business logic from such modifiers.

I'm actually quite interested in the challenge of applying design patterns for large systems (inversion-of-control, AOP etc) to Django, which has some stubborn behaviours that get in the way.

Re:Our custom Django mixins by kennethlove on Friday March 02, 2012 @11:16AM (#3657097)
True, that does bring up an interesting point.

I'll have to give it another look later, maybe I'll change my mind.